func StreamTest(t *testing.T, callback func(*link.Session)) { protocol := New(1024, 1024, 1024) server, err := link.Serve("tcp", "0.0.0.0:0", protocol) unitest.NotError(t, err) addr := server.Listener().Addr().String() go server.Serve(func(session *link.Session) { for { var msg TestMessage if err := session.Receive(&msg); err != nil { break } if err := session.Send(msg); err != nil { break } } }) session, err := link.Connect("tcp", addr, protocol) unitest.NotError(t, err) callback(session) session.Close() server.Stop() MakeSureSessionGoroutineExit(t) }
func main() { var addr string flag.StringVar(&addr, "addr", "127.0.0.1:10010", "echo server address") flag.Parse() session, err := link.Connect("tcp", addr, link.String(link.Uint16BE)) if err != nil { panic(err) } go func() { var msg string for { if err := session.Receive(&msg); err != nil { break } fmt.Printf("%s\n", msg) } }() for { var msg string if _, err := fmt.Scanf("%s\n", &msg); err != nil { break } if err = session.Send(msg); err != nil { break } } session.Close() println("bye") }
func main() { flag.Parse() session, err := link.Connect("tcp", *addr, packet.New( binary.SplitByUint16BE, 1024, 1024, 1024, )) if err != nil { panic(err) } go func() { var msg packet.RAW for { if err := session.Receive(&msg); err != nil { break } fmt.Printf("%s\n", msg) } }() for { var msg string if _, err := fmt.Scanf("%s\n", &msg); err != nil { break } if err = session.Send(packet.RAW(msg)); err != nil { break } } session.Close() println("bye") }
func Test_Simple(t *testing.T) { backend, err := StartEchoBackend() unitest.NotError(t, err) gateway := StartTestGateway(t, backend.Listener().Addr().String()) gatewayAddr := gateway.server.Listener().Addr().String() client, err := link.Connect("tcp://"+gatewayAddr, link.Packet(link.Uint16BE), link.Bytes()) unitest.NotError(t, err) for i := 0; i < 10000; i++ { msg1 := RandBytes(1024) err1 := client.Send(msg1) unitest.NotError(t, err1) var msg2 []byte err2 := client.Receive(&msg2) unitest.NotError(t, err2) if bytes.Equal(msg1, msg2) == false { t.Log(i, msg1, msg2) t.Fail() } unitest.Pass(t, bytes.Equal(msg1, msg2)) } client.Close() gateway.Stop() backend.Stop() time.Sleep(time.Second * 2) MakeSureSessionGoroutineExit(t) }
func PacketTest(t *testing.T, callback func(*link.Session)) { protocol := New(binary.SplitByUvarint, 1024, 1024, 1024) server, err := link.Serve("tcp", "0.0.0.0:0", protocol) unitest.NotError(t, err) go server.Serve(func(session *link.Session) { for { var msg RAW if err := session.Receive(&msg); err != nil { break } if err := session.Send(msg); err != nil { break } } }) session, err := link.Connect("tcp", server.Listener().Addr().String(), protocol) unitest.NotError(t, err) callback(session) session.Close() server.Stop() MakeSureSessionGoroutineExit(t) }
func Test_Gateway_Complex(t *testing.T) { backend := StartTestBackend(t, func(session *link.Session) { var msg packet.RAW for { if err := session.Receive(&msg); err != nil { break } if err := session.Send(msg); err != nil { break } } }) gateway := StartTestGateway(t, backend.Listener().Addr().String()) gatewayAddr := gateway.server.Listener().Addr().String() var wg sync.WaitGroup for i := 0; i < 20; i++ { wg.Add(1) go func() { defer wg.Done() client, err := link.Connect("tcp", gatewayAddr, protocol) unitest.NotError(t, err) for j := 0; j < 500; j++ { msg1 := RandBytes(1024) err1 := client.Send(packet.RAW(msg1)) unitest.NotError(t, err1) var msg2 packet.RAW err2 := client.Receive(&msg2) unitest.NotError(t, err2) if bytes.Equal(msg1, msg2) == false { t.Log(j, msg1, msg2) t.Fail() } unitest.Pass(t, bytes.Equal(msg1, msg2)) } client.Close() }() } wg.Wait() gateway.Stop() backend.Stop() time.Sleep(time.Second * 2) MakeSureSessionGoroutineExit(t) }
// 连接服务器 func Connect(connectServerName string, network string, address string, codecType link.CodecType, dispatch dispatch.DispatchInterface) (*link.Session, error) { session, err := link.Connect(network, address, codecType) if err == nil { session.AddCloseCallback(session, func() { ERR(connectServerName + " Disconnect At " + ServerName) }) if dispatch != nil { go sessionReceive(session, dispatch) } } return session, err }
//初始化 func InitClient(ip string, port string) error { //连接DB服务器 addr := ip + ":" + port client, err := link.Connect("tcp", addr, packet.New(binary.SplitByUint32BE, 1024, 1024, 1024)) if err != nil { return err } session = client go dealReceiveMsgS2C() ConnectDBServer() return nil }
func SessionTest(t *testing.T, codecType link.CodecType, test func(*testing.T, *link.Session)) { server, err := link.Serve("tcp", "0.0.0.0:0", Bytes(Uint16BE)) unitest.NotError(t, err) addr := server.Listener().Addr().String() serverWait := new(sync.WaitGroup) go func() { for { session, err := server.Accept() if err != nil { break } serverWait.Add(1) go func() { io.Copy(session.Conn(), session.Conn()) serverWait.Done() }() } }() clientWait := new(sync.WaitGroup) for i := 0; i < 60; i++ { clientWait.Add(1) go func() { session, err := link.Connect("tcp", addr, codecType) unitest.NotError(t, err) test(t, session) session.Close() clientWait.Done() }() } clientWait.Wait() server.Stop() serverWait.Wait() MakeSureSessionGoroutineExit(t) }
func Test_Broadcast(t *testing.T) { var ( clientNum = 20 channel = link.NewChannel(Broadcast{}) broadcastWait sync.WaitGroup clientWait sync.WaitGroup ) clientWait.Add(clientNum) backend := StartTestBackend(t, func(session *link.Session) { channel.Join(session) clientWait.Done() for { var msg packet.RAW if err := session.Receive(&msg); err != nil { break } broadcastWait.Done() } }) go func() { clientWait.Wait() for i := 0; i < 10000; i++ { msg := RandBytes(10) channel.Broadcast(packet.RAW(msg)) broadcastWait.Add(clientNum) broadcastWait.Wait() } }() gateway := StartTestGateway(t, backend.Listener().Addr().String()) gatewayAddr := gateway.server.Listener().Addr().String() var wg sync.WaitGroup for i := 0; i < clientNum; i++ { wg.Add(1) go func() { defer wg.Done() client, err := link.Connect("tcp", gatewayAddr, protocol) unitest.NotError(t, err) for j := 0; j < 10000; j++ { var msg packet.RAW err := client.Receive(&msg) unitest.NotError(t, err) err = client.Send(msg) unitest.NotError(t, err) } client.Close() }() } wg.Wait() gateway.Stop() backend.Stop() time.Sleep(time.Second * 2) MakeSureSessionGoroutineExit(t) }
func Test_Broadcast(t *testing.T) { var ( clientNum = 20 packetNum = 2000 channel = NewChannel(link.Bytes()) broadcastMsg []byte broadcastWait sync.WaitGroup clientWait sync.WaitGroup ) backend, err := link.Serve("tcp://0.0.0.0:0", NewBackend(), link.Bytes()) unitest.NotError(t, err) go backend.Loop(func(session *link.Session) { channel.Join(session) clientWait.Done() for { var msg []byte if err := session.Receive(&msg); err != nil { break } unitest.Pass(t, bytes.Equal(msg, broadcastMsg)) broadcastWait.Done() } }) clientWait.Add(clientNum) go func() { clientWait.Wait() for i := 0; i < packetNum; i++ { broadcastMsg = RandBytes(1024) channel.Broadcast(broadcastMsg) broadcastWait.Add(clientNum) broadcastWait.Wait() } }() gateway := StartTestGateway(t, backend.Listener().Addr().String()) gatewayAddr := gateway.server.Listener().Addr().String() var wg sync.WaitGroup for i := 0; i < clientNum; i++ { wg.Add(1) go func() { defer wg.Done() client, err := link.Connect("tcp://"+gatewayAddr, link.Packet(link.Uint16BE), link.Bytes()) unitest.NotError(t, err) for j := 0; j < packetNum; j++ { var msg []byte err := client.Receive(&msg) unitest.NotError(t, err) err = client.Send(msg) unitest.NotError(t, err) } client.Close() }() } wg.Wait() gateway.Stop() backend.Stop() time.Sleep(time.Second * 2) MakeSureSessionGoroutineExit(t) }