working out some kinks in the WSWrap

This commit is contained in:
AJ ONeal 2020-05-21 05:09:49 -06:00
parent b50fb11fb9
commit 39ada8ec7a
5 changed files with 86 additions and 50 deletions

View File

@ -72,7 +72,9 @@ func main() {
//mux.HandleTLS("*", mux.TerminateTLS(mux)) //mux.HandleTLS("*", mux.TerminateTLS(mux))
mux.ForwardTCP("*", "localhost:3000", 120*time.Second) mux.ForwardTCP("*", "localhost:3000", 120*time.Second)
// TODO set failure // TODO set failure
log.Fatal("Closed server: ", packer.ListenAndServe(wsconn, mux))
wsw := packer.NewWSWrap(wsconn)
log.Fatal("Closed server: ", packer.ListenAndServe(wsw, mux))
} }
func getToken(secret string) (token string, err error) { func getToken(secret string) (token string, err error) {

View File

@ -22,13 +22,11 @@ type Listener struct {
} }
// Listen creates a new Listener and sets it up to receive and distribute connections. // Listen creates a new Listener and sets it up to receive and distribute connections.
func Listen(wsconn WSConn) *Listener { func Listen(wsw *WSWrap) *Listener {
ctx := context.TODO() ctx := context.TODO()
// Wrap the websocket and feed it into the Encoder and Decoder // Feed the socket into the Encoder and Decoder
wsw := &WSWrap{wsconn: wsconn, tmpr: nil}
listener := &Listener{ listener := &Listener{
//wsconn: wsconn,
wsw: wsw, wsw: wsw,
incoming: make(chan *Conn, 1), // buffer ever so slightly incoming: make(chan *Conn, 1), // buffer ever so slightly
close: make(chan struct{}), close: make(chan struct{}),
@ -42,7 +40,7 @@ func Listen(wsconn WSConn) *Listener {
go func() { go func() {
err := listener.encoder.Run() err := listener.encoder.Run()
fmt.Printf("encoder stopped entirely: %q", err) fmt.Printf("encoder stopped entirely: %q", err)
wsw.wsconn.Close() wsw.Close()
}() }()
// Decode the stream as it comes in // Decode the stream as it comes in
@ -62,8 +60,8 @@ func Listen(wsconn WSConn) *Listener {
} }
// ListenAndServe listens on a websocket and handles the incomming net.Conn-like connections with a Handler // ListenAndServe listens on a websocket and handles the incomming net.Conn-like connections with a Handler
func ListenAndServe(wsconn WSConn, mux Handler) error { func ListenAndServe(wsw *WSWrap, mux Handler) error {
listener := Listen(wsconn) listener := Listen(wsw)
return Serve(listener, mux) return Serve(listener, mux)
} }

View File

@ -3,61 +3,21 @@ package packer
import ( import (
"errors" "errors"
"io" "io"
"net"
"testing" "testing"
"time"
) )
func TestDialServer(t *testing.T) { func TestDialServer(t *testing.T) {
// TODO replace the websocket connection with a mock server // TODO replace the websocket connection with a mock server
//ctx := context.Background() //ctx := context.Background()
wsconn := &WSTestConn{ wsw := &WSWrap{}
rwt: &RWTest{},
}
mux := NewRouteMux() mux := NewRouteMux()
t.Fatal(ListenAndServe(wsconn, mux)) t.Fatal(ListenAndServe(wsw, mux))
} }
var ErrNoImpl error = errors.New("not implemented") var ErrNoImpl error = errors.New("not implemented")
// WSTestConn is a fake websocket connection
type WSTestConn struct {
closed bool
rwt *RWTest
}
func (wst *WSTestConn) NextReader() (messageType int, r io.Reader, err error) {
return 0, nil, ErrNoImpl
}
func (wst *WSTestConn) NextWriter(messageType int) (io.WriteCloser, error) {
return nil, ErrNoImpl
}
func (wst *WSTestConn) WriteControl(messageType int, data []byte, deadline time.Time) error {
if wst.closed {
return io.EOF
}
return nil
}
func (wst *WSTestConn) WriteMessage(messageType int, data []byte) error {
if wst.closed {
return io.EOF
}
return nil
}
func (wst *WSTestConn) SetReadDeadline(t time.Time) error {
return ErrNoImpl
}
func (wst *WSTestConn) Close() error {
wst.closed = true
return nil
}
func (wst *WSTestConn) RemoteAddr() net.Addr {
addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:8443")
return addr
}
// RWTest is a fake buffer // RWTest is a fake buffer
type RWTest struct { type RWTest struct {
closed bool closed bool

View File

@ -0,0 +1,43 @@
package packer
import (
"io"
"net"
"time"
)
// WSTestConn is a fake websocket connection
type WSTestConn struct {
closed bool
rwt *RWTest
}
func (wst *WSTestConn) NextReader() (messageType int, r io.Reader, err error) {
return 0, nil, ErrNoImpl
}
func (wst *WSTestConn) NextWriter(messageType int) (io.WriteCloser, error) {
return nil, ErrNoImpl
}
func (wst *WSTestConn) WriteControl(messageType int, data []byte, deadline time.Time) error {
if wst.closed {
return io.EOF
}
return nil
}
func (wst *WSTestConn) WriteMessage(messageType int, data []byte) error {
if wst.closed {
return io.EOF
}
return nil
}
func (wst *WSTestConn) SetReadDeadline(t time.Time) error {
return ErrNoImpl
}
func (wst *WSTestConn) Close() error {
wst.closed = true
return nil
}
func (wst *WSTestConn) RemoteAddr() net.Addr {
addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:8443")
return addr
}

View File

@ -31,6 +31,14 @@ type WSConn interface {
// LocalAddr() net.Addr // LocalAddr() net.Addr
} }
// NewWSWrap allocates a new websocket connection wrapper
func NewWSWrap(wsconn WSConn) *WSWrap {
return &WSWrap{
wsconn: wsconn,
tmpr: nil,
}
}
func (wsw *WSWrap) Read(b []byte) (int, error) { func (wsw *WSWrap) Read(b []byte) (int, error) {
if nil == wsw.tmpr { if nil == wsw.tmpr {
_, msgr, err := wsw.wsconn.NextReader() _, msgr, err := wsw.wsconn.NextReader()
@ -86,3 +94,28 @@ func (wsw *WSWrap) Close() error {
_ = wsw.wsconn.Close() _ = wsw.wsconn.Close()
return err return err
} }
// LocalAddr returns the local network address.
func (wsw *WSWrap) LocalAddr() *Addr {
panic("not implemented")
}
// RemoteAddr returns the remote network address.
func (wsw *WSWrap) RemoteAddr() *Addr {
panic("not implemented")
}
// SetDeadline sets the read and write deadlines associated
func (wsw *WSWrap) SetDeadline(t time.Time) error {
panic("not implemented")
}
// SetReadDeadline sets the deadline for future Read calls
func (wsw *WSWrap) SetReadDeadline(t time.Time) error {
panic("not implemented")
}
// SetWriteDeadline sets the deadline for future Write calls
func (wsw *WSWrap) SetWriteDeadline(t time.Time) error {
panic("not implemented")
}