Beispiel #1
0
        public static void ReceiveMessage(IAsyncResult ar)
        {
            var clientSocket = ar.AsyncState as Socket;

            try
            {
                //方法参考:http://msdn.microsoft.com/zh-cn/library/system.net.sockets.socket.endreceive.aspx
                var length = clientSocket.EndReceive(ar);
                if (length > 0)
                {
                    //读取出来消息内容 使用protobuf
                    var data = buffer.Take(length).ToArray();
                    //var message = Person.Parser.ParseFrom(data);
                    //ushort msgId = 0;
                    //Person message = ProtoBufUtil.Uncode<Person>(data, out msgId);

                    //NetMsg.HandleMsg<Person>(data);

                    MemoryStream ms = null;
                    using (ms = new MemoryStream(data))
                    {
                        BinaryReader reader  = new BinaryReader(ms);
                        ushort       msgLen  = reader.ReadUInt16();
                        ushort       protoId = reader.ReadUInt16();

                        Console.WriteLine($"[Server] receive :protoID:{protoId},dataLen:{msgLen}");
                        if (msgLen <= data.Length - 4)
                        {
                            byte[] pbdata = reader.ReadBytes(msgLen);
                            NetMsg.HandleMsg(pbdata, protoId);
                        }
                        else
                        {
                            Console.WriteLine($"[Server] {protoId} 协议长度错误");
                        }
                    }

                    //var message = Encoding.Unicode.GetString(buffer, 0, length);
                    //显示消息
                    //Console.WriteLine(message.Name);

                    //接收下一个消息(因为这是一个递归的调用,所以这样就可以一直接收消息了)
                    clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), clientSocket);
                }
                else
                {
                    Console.WriteLine($"客户端断开连接:{clientSocket.RemoteEndPoint}");
                    OnClientDisconnect(clientSocket);
                }
            }
            catch (SocketException ex)
            {
                // 如果socket有未发送完的数据,断开时会触发10054异常,在此捕捉
                if (ex.SocketErrorCode == SocketError.ConnectionReset)
                {
                    Console.WriteLine($"clientSocket有未发送完的数据 {ex.Message}");
                    OnClientDisconnect(clientSocket);
                }
            }
        }
Beispiel #2
0
    // Update is called once per frame
    void Update()
    {
        if (clientSocket == null)
        {
            return;
        }


        if (Time.time - lastSendPingTime >= 1)
        {
            lastSendPingTime = Time.time;
            //心跳检测
            var elapTime = new TimeSpan(DateTime.Now.Ticks - lastSCPingTime);
            if (elapTime.Seconds > 5)
            {
                Debug.Log($"未收到服务器的心跳信息超过5秒 心跳超时===={DateTime.Now}");
            }

            CSPing csPing = new CSPing()
            {
                Time = 1
            };
            NetMsg.SendMsg(clientSocket, csPing, (ushort)PB.EnmCmdID.CsPing);
        }
        //测试code
        if (Time.time - lastSendTime >= sendTimeIntveral)
        {
            lastSendTime = Time.time;
            //var message = "Message from client : " + DateTime.Now;
            //var outputBuffer = Encoding.Unicode.GetBytes(message);



            //使用protobuf
            Person john = new Person
            {
                Id     = 1234,
                Name   = "Message from client at " + DateTime.Now.ToString(),
                Email  = "*****@*****.**",
                Phones = { new Person.Types.PhoneNumber {
                               Number = "555-4321", Type = PhoneType.Home
                           } }
            };

            //byte[] byteArray = john.ToByteArray();
            //byte[] byteArray = ProtoBufUtil.Encode(john.ToByteArray(), 102);
            if (clientSocket == null)
            {
                return;
            }
            //clientSocket.BeginSend(byteArray, 0, byteArray.Length, SocketFlags.None, null, null);
            NetMsg.SendMsg(clientSocket, john, (ushort)PB.EnmCmdID.CsPerson);
        }


        lock (_lockObj)
        {
            while (receiveQ.Count > 0)
            {
                NetData netData = receiveQ.Dequeue();
                NetMsg.HandleMsg(netData.msg, netData.msgId);
            }
        }
    }
Beispiel #3
0
 private void HandleMsg(byte[] msg)
 {
     NetMsg.HandleMsg(msg);
 }
Beispiel #4
0
 void HandleMessage(IPEndPoint client, byte[] buffer)
 {
     NetMsg.HandleMsg(buffer);
 }