Skip to content

Commit

Permalink
WebSocket support
Browse files Browse the repository at this point in the history
  • Loading branch information
fish-tennis committed Feb 26, 2024
1 parent 0054fd9 commit dcb6ffa
Show file tree
Hide file tree
Showing 18 changed files with 865 additions and 96 deletions.
14 changes: 12 additions & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ type ConnectionConfig struct {
// 发包超时设置(秒)
// net.Conn.SetWriteDeadline
WriteTimeout uint32

Codec Codec

Handler ConnectionHandler

// ws或wss的http路径,如"/ws"或"/wss"
Path string

// "ws"或"wss"
Scheme string
}

type baseConnection struct {
Expand Down Expand Up @@ -169,6 +179,6 @@ func NewConnectionId() uint32 {
return atomic.AddUint32(&_connectionIdCounter, 1)
}

type ConnectionCreator func(config *ConnectionConfig, codec Codec, handler ConnectionHandler) Connection
type ConnectionCreator func(config *ConnectionConfig) Connection

type AcceptConnectionCreator func(conn net.Conn, config *ConnectionConfig, codec Codec, handler ConnectionHandler) Connection
type AcceptConnectionCreator func(conn net.Conn, config *ConnectionConfig) Connection
24 changes: 15 additions & 9 deletions custom_packet_no_ringbuffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func TestCustomPacketNoRingBuffer(t *testing.T) {
defer cancel()

netMgr := GetNetMgr()
serverCodec := &customCodec{}
serverHandler := &echoCustomPacketServerHandler{}
connectionConfig := ConnectionConfig{
SendPacketCacheCap: 16,
MaxPacketSize: 1024 * 1024 * 32, // 支持超出DefaultPacketHeaderSize大小的包
Expand All @@ -38,21 +40,25 @@ func TestCustomPacketNoRingBuffer(t *testing.T) {
}
listenAddress := "127.0.0.1:10002"

serverCodec := &customCodec{}
serverHandler := &echoCustomPacketServerHandler{}
// 自定义TcpConnection
if netMgr.NewListenerCustom(ctx, listenAddress, connectionConfig, serverCodec, serverHandler, nil, func(conn net.Conn, config *ConnectionConfig, codec Codec, handler ConnectionHandler) Connection {
return NewTcpConnectionSimpleAccept(conn, config, codec, handler)
}) == nil {
listenerConfig := &ListenerConfig{
AcceptConfig: connectionConfig,
AcceptConnectionCreator: func(conn net.Conn, config *ConnectionConfig) Connection {
return NewTcpConnectionSimpleAccept(conn, config)
},
}
listenerConfig.AcceptConfig.Codec = serverCodec
listenerConfig.AcceptConfig.Handler = serverHandler
if netMgr.NewListener(ctx, listenAddress, listenerConfig) == nil {
panic("listen failed")
}
time.Sleep(time.Second)

clientCodec := &customCodec{}
clientHandler := &echoCustomPacketClientHandler{}
connectionConfig.Codec = &customCodec{}
connectionConfig.Handler = &echoCustomPacketClientHandler{}
// 自定义TcpConnection
if netMgr.NewConnectorCustom(ctx, listenAddress, &connectionConfig, clientCodec, clientHandler, nil, func(config *ConnectionConfig, codec Codec, handler ConnectionHandler) Connection {
return NewTcpConnectionSimple(config, codec, handler)
if netMgr.NewConnectorCustom(ctx, listenAddress, &connectionConfig, nil, func(config *ConnectionConfig) Connection {
return NewTcpConnectionSimple(config)
}) == nil {
panic("connect failed")
}
Expand Down
13 changes: 10 additions & 3 deletions echo_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestEchoData(t *testing.T) {
defer cancel()

netMgr := GetNetMgr()
codec := NewDefaultCodec()
connectionConfig := ConnectionConfig{
SendPacketCacheCap: 100,
SendBufferSize: 60, // 设置的比较小,便于测试缓存写满的情况
Expand All @@ -32,14 +33,20 @@ func TestEchoData(t *testing.T) {
RecvTimeout: 0,
HeartBeatInterval: 2,
WriteTimeout: 0,
Codec: codec,
}
listenAddress := "127.0.0.1:10002"
//codec := NewXorCodec([]byte{0,1,2,3,4,5,6})
codec := NewDefaultCodec()
netMgr.NewListener(ctx, listenAddress, connectionConfig, codec, &echoServerHandler{}, &echoListenerHandler{})
listenerConfig := &ListenerConfig{
AcceptConfig: connectionConfig,
ListenerHandler: &echoListenerHandler{},
}
listenerConfig.AcceptConfig.Handler = &echoServerHandler{}
netMgr.NewListener(ctx, listenAddress, listenerConfig)
time.Sleep(time.Second)

netMgr.NewConnector(ctx, listenAddress, &connectionConfig, codec, &echoClientHandler{}, nil)
connectionConfig.Handler = &echoClientHandler{}
netMgr.NewConnector(ctx, listenAddress, &connectionConfig, nil)

netMgr.Shutdown(true)
}
Expand Down
22 changes: 15 additions & 7 deletions echo_proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ func TestEchoProto(t *testing.T) {
logger.Warn("%v", packet)
})
serverHandler.GetPacketHandler(PacketCommand(pb.CmdTest_Cmd_TestMessage))
if netMgr.NewListener(ctx, listenAddress, connectionConfig, serverCodec, serverHandler, nil) == nil {

listenerConfig := &ListenerConfig{
AcceptConfig: connectionConfig,
}
listenerConfig.AcceptConfig.Codec = serverCodec
listenerConfig.AcceptConfig.Handler = serverHandler
if netMgr.NewListener(ctx, listenAddress, listenerConfig) == nil {
panic("listen failed")
}
time.Sleep(time.Millisecond)
Expand All @@ -61,14 +67,16 @@ func TestEchoProto(t *testing.T) {
}
// 客户端作为connector,需要设置心跳包
clientHandler.RegisterHeartBeat(func() Packet {
return NewProtoPacket(PacketCommand(pb.CmdTest_Cmd_HeartBeat),&pb.HeartBeatReq{})
return NewProtoPacket(PacketCommand(pb.CmdTest_Cmd_HeartBeat), &pb.HeartBeatReq{})
})
// 注册客户端的消息回调
clientHandler.Register(PacketCommand(pb.CmdTest_Cmd_HeartBeat), clientHandler.onHeartBeatRes, new(pb.HeartBeatRes))
clientHandler.Register(PacketCommand(pb.CmdTest_Cmd_TestMessage), clientHandler.onTestMessage, new(pb.TestMessage))
// 测试没有注册proto.Message的消息
clientHandler.Register(PacketCommand(100), clientHandler.onTestDataMessage, nil)
if netMgr.NewConnector(ctx, listenAddress, &connectionConfig, clientCodec, clientHandler, nil) == nil {
connectionConfig.Codec = clientCodec
connectionConfig.Handler = clientHandler
if netMgr.NewConnector(ctx, listenAddress, &connectionConfig, nil) == nil {
panic("connect failed")
}

Expand Down Expand Up @@ -165,11 +173,11 @@ func (e *echoProtoClientHandler) onTestDataMessage(connection Connection, packet
}

func TestListenerError(t *testing.T) {
config := ConnectionConfig{}
tcpListener := NewTcpListener(config, nil, nil, nil)
config := &ListenerConfig{}
tcpListener := NewTcpListener(config)
tcpListener.Addr()
tcpListener1 := GetNetMgr().NewListener(context.Background(), "127.0.0.1:10001", config, nil, nil, nil)
tcpListener2 := GetNetMgr().NewListener(context.Background(), "127.0.0.1:10001", config, nil, nil, nil)
tcpListener1 := GetNetMgr().NewListener(context.Background(), "127.0.0.1:10001", config)
tcpListener2 := GetNetMgr().NewListener(context.Background(), "127.0.0.1:10001", config)
if tcpListener1 != nil {
tcpListener1.Close()
}
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/fish-tennis/gnet

go 1.16

require google.golang.org/protobuf v1.26.0
require (
github.com/gorilla/websocket v1.5.1
google.golang.org/protobuf v1.32.0
)
46 changes: 44 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,50 @@
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
14 changes: 14 additions & 0 deletions listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,24 @@ type Listener interface {
Close()
}

type ListenerConfig struct {
AcceptConfig ConnectionConfig
AcceptConnectionCreator AcceptConnectionCreator
ListenerHandler ListenerHandler
// ws或wss的http监听路径,如"/ws"或"/wss"
Path string
// 签名cert文件,wss专用
CertFile string
// 签名key文件,wss专用
KeyFile string
}

type baseListener struct {
// unique listener id
listenerId uint32

config *ListenerConfig

handler ListenerHandler
}

Expand Down
55 changes: 37 additions & 18 deletions net_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,35 @@ func (this *NetMgr) init() {
this.wg = sync.WaitGroup{}
}

// create a new TcpListener
func (this *NetMgr) NewListener(ctx context.Context, address string, acceptConnectionConfig ConnectionConfig, acceptConnectionCodec Codec,
acceptConnectionHandler ConnectionHandler, listenerHandler ListenerHandler) Listener {
return this.NewListenerCustom(ctx, address, acceptConnectionConfig, acceptConnectionCodec,
acceptConnectionHandler, listenerHandler, func(_conn net.Conn, _config *ConnectionConfig, _codec Codec, _handler ConnectionHandler) Connection {
return NewTcpConnectionAccept(_conn, _config, _codec, _handler)
})
func (this *NetMgr) NewListener(ctx context.Context, address string, listenerConfig *ListenerConfig) Listener {
if listenerConfig.AcceptConnectionCreator == nil {
listenerConfig.AcceptConnectionCreator = func(conn net.Conn, config *ConnectionConfig) Connection {
return NewTcpConnectionAccept(conn, config)
}
}
newListener := NewTcpListener(listenerConfig)
newListener.netMgrWg = &this.wg
if !newListener.Start(ctx, address) {
logger.Error("NewListener Start Failed")
return nil
}
this.listenerMapLock.Lock()
this.listenerMap[newListener.GetListenerId()] = newListener
this.listenerMapLock.Unlock()

newListener.onClose = func(listener Listener) {
this.listenerMapLock.Lock()
delete(this.listenerMap, listener.GetListenerId())
this.listenerMapLock.Unlock()
}
return newListener
}

// create a new Listener, with custom acceptConnectionCreator
func (this *NetMgr) NewListenerCustom(ctx context.Context, address string, acceptConnectionConfig ConnectionConfig, acceptConnectionCodec Codec,
acceptConnectionHandler ConnectionHandler, listenerHandler ListenerHandler, acceptConnectionCreator AcceptConnectionCreator) Listener {
newListener := NewTcpListener(acceptConnectionConfig, acceptConnectionCodec, acceptConnectionHandler, listenerHandler)
newListener.acceptConnectionCreator = acceptConnectionCreator
func (this *NetMgr) NewWsListener(ctx context.Context, address string, listenerConfig *ListenerConfig) Listener {
newListener := NewWsListener(listenerConfig)
newListener.netMgrWg = &this.wg
if !newListener.Start(ctx, address) {
logger.Debug("NewListener Start Failed")
logger.Error("NewWsListener Start Failed")
return nil
}
this.listenerMapLock.Lock()
Expand All @@ -74,16 +86,23 @@ func (this *NetMgr) NewListenerCustom(ctx context.Context, address string, accep

// create a new TcpConnection
func (this *NetMgr) NewConnector(ctx context.Context, address string, connectionConfig *ConnectionConfig,
codec Codec, handler ConnectionHandler, tag interface{}) Connection {
return this.NewConnectorCustom(ctx, address, connectionConfig, codec, handler, tag, func(_config *ConnectionConfig, _codec Codec, _handler ConnectionHandler) Connection {
return NewTcpConnector(_config, _codec, _handler)
tag interface{}) Connection {
return this.NewConnectorCustom(ctx, address, connectionConfig, tag, func(_config *ConnectionConfig) Connection {
return NewTcpConnector(_config)
})
}

func (this *NetMgr) NewWsConnector(ctx context.Context, address string, connectionConfig *ConnectionConfig,
tag interface{}) Connection {
return this.NewConnectorCustom(ctx, address, connectionConfig, tag, func(_config *ConnectionConfig) Connection {
return NewWsConnection(_config)
})
}

// create a new Connection, with custom connectionCreator
func (this *NetMgr) NewConnectorCustom(ctx context.Context, address string, connectionConfig *ConnectionConfig,
codec Codec, handler ConnectionHandler, tag interface{}, connectionCreator ConnectionCreator) Connection {
newConnector := connectionCreator(connectionConfig, codec, handler)
tag interface{}, connectionCreator ConnectionCreator) Connection {
newConnector := connectionCreator(connectionConfig)
newConnector.SetTag(tag)
if !newConnector.Connect(address) {
newConnector.Close()
Expand Down
23 changes: 15 additions & 8 deletions packet_size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,19 @@ func TestPacketSize(t *testing.T) {
testMessage := packet.Message().(*pb.TestMessage)
logger.Info("recv%v:%s", testMessage.I32, testMessage.Name)
}, new(pb.TestMessage))
if GetNetMgr().NewListener(ctx, listenAddress, connectionConfig, defaultCodec, serverHandler, nil) == nil {
listenerConfig := &ListenerConfig{
AcceptConfig: connectionConfig,
}
listenerConfig.AcceptConfig.Codec = defaultCodec
listenerConfig.AcceptConfig.Handler = serverHandler
if GetNetMgr().NewListener(ctx, listenAddress, listenerConfig) == nil {
panic("listen failed")
}

clientHandler := NewDefaultConnectionHandler(defaultCodec)
clientConnector := GetNetMgr().NewConnector(ctx, listenAddress, &connectionConfig, defaultCodec, clientHandler, nil)
connectionConfig.Codec = defaultCodec
connectionConfig.Handler = clientHandler
clientConnector := GetNetMgr().NewConnector(ctx, listenAddress, &connectionConfig, nil)
if clientConnector == nil {
panic("connect failed")
}
Expand Down Expand Up @@ -71,15 +78,15 @@ func TestPacketSize(t *testing.T) {
}

func TestPacketSizeInit(t *testing.T) {
config := &ConnectionConfig{}
codec := NewProtoCodec(nil)
tcpConnector := NewTcpConnector(config, codec, nil)
tcpConnector.SetCodec(codec)
NewTcpConnectionAccept(nil, config, codec, nil)
config := &ConnectionConfig{
Codec: NewProtoCodec(nil),
}
tcpConnector := NewTcpConnector(config)
NewTcpConnectionAccept(nil, config)
tcpConnector.Send(PacketCommand(123), nil)
tcpConnector.LocalAddr()
tcpConnector.RemoteAddr()
simpleTcpConnector := NewTcpConnectionSimple(config, nil, nil)
simpleTcpConnector := NewTcpConnectionSimple(config)
simpleTcpConnector.Send(PacketCommand(123), nil)
simpleTcpConnector.LocalAddr()
simpleTcpConnector.RemoteAddr()
Expand Down
12 changes: 9 additions & 3 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,19 @@ func TestTestServer(t *testing.T) {

protoMap := make(map[PacketCommand]reflect.Type)
protoMap[PacketCommand(123)] = reflect.TypeOf(new(pb.TestMessage)).Elem()
codec := NewProtoCodec(protoMap)
connectionConfig.Codec = NewProtoCodec(protoMap)

netMgr.NewListener(ctx, listenAddress, connectionConfig, codec, &testServerClientHandler{}, &testServerListenerHandler{})
listenerConfig := &ListenerConfig{
AcceptConfig: connectionConfig,
ListenerHandler: &testServerListenerHandler{},
}
listenerConfig.AcceptConfig.Handler = &testServerClientHandler{}
netMgr.NewListener(ctx, listenAddress, listenerConfig)
time.Sleep(time.Second)

connectionConfig.Handler = &testClientHandler{}
for i := 0; i < clientCount; i++ {
netMgr.NewConnector(ctx, listenAddress, &connectionConfig, codec, &testClientHandler{}, nil)
netMgr.NewConnector(ctx, listenAddress, &connectionConfig, nil)
}

netMgr.Shutdown(true)
Expand Down
Loading

0 comments on commit dcb6ffa

Please sign in to comment.