public void Handle(TcpClient client) { DCLog.Log("connect a client ..."); mChannel.Init(client); mChannel.AddListener(OnMsg); mChannel.StartReceive(); }
public void Init(string host, int port) { DCLog.Log("server start ...."); mTcpListener = new TcpListener(IPAddress.Parse(host), port); mTcpListener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); mTcpListener.Server.NoDelay = true; mTcpListener.Start(); }
public async void Start() { DCLog.Log("server start accept ...."); while (true) { if (mDisposed) { return; } var tcpClient = await mTcpListener.AcceptTcpClientAsync(); var clientHandler = new ClientHandler(); clientHandler.SetServer(this); clientHandler.Handle(tcpClient); } }
public void OnMsg(Packet packet) { //首次连接第一个协议必须是user token var protoPacket = ProtoPacket.FromRecvBuf(packet.Bytes, 0, packet.Length); if (protoPacket.ProtoObj is PTestDemoClsReq req) { var f1 = req.F1; if (f1 == null) { DCLog.Log("default is null"); } } //echo to client mChannel.Send( SendBuf.From(Encoding.UTF8.GetBytes("echo ")), SendBuf.From(packet.Bytes, 0, packet.Length)); }