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.ForwardTCP("*", "localhost:3000", 120*time.Second)
// 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) {

View File

@ -22,13 +22,11 @@ type Listener struct {
}
// 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()
// Wrap the websocket and feed it into the Encoder and Decoder
wsw := &WSWrap{wsconn: wsconn, tmpr: nil}
// Feed the socket into the Encoder and Decoder
listener := &Listener{
//wsconn: wsconn,
wsw: wsw,
incoming: make(chan *Conn, 1), // buffer ever so slightly
close: make(chan struct{}),
@ -42,7 +40,7 @@ func Listen(wsconn WSConn) *Listener {
go func() {
err := listener.encoder.Run()
fmt.Printf("encoder stopped entirely: %q", err)
wsw.wsconn.Close()
wsw.Close()
}()
// 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
func ListenAndServe(wsconn WSConn, mux Handler) error {
listener := Listen(wsconn)
func ListenAndServe(wsw *WSWrap, mux Handler) error {
listener := Listen(wsw)
return Serve(listener, mux)
}

View File

@ -3,61 +3,21 @@ package packer
import (
"errors"
"io"
"net"
"testing"
"time"
)
func TestDialServer(t *testing.T) {
// TODO replace the websocket connection with a mock server
//ctx := context.Background()
wsconn := &WSTestConn{
rwt: &RWTest{},
}
wsw := &WSWrap{}
mux := NewRouteMux()
t.Fatal(ListenAndServe(wsconn, mux))
t.Fatal(ListenAndServe(wsw, mux))
}
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
type RWTest struct {
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
}
// 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) {
if nil == wsw.tmpr {
_, msgr, err := wsw.wsconn.NextReader()
@ -86,3 +94,28 @@ func (wsw *WSWrap) Close() error {
_ = wsw.wsconn.Close()
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")
}