Esempio n. 1
0
        // 接收体消息
        void ReceiveBody(System.IAsyncResult ar)
        {
            NetPacket packet = (NetPacket)ar.AsyncState;

            try
            {
                // 返回网络上接收的数据长度
                int read = packet.socket.EndReceive(ar);
                // 已断开连接
                if (read < 1)
                {
                    // 通知丢失连接
                    WriteMsg("OnLost", packet.socket);
                    return;
                }
                packet.readLength += read;

                // 消息体必须读满指定的长度
                if (packet.readLength < packet.bodyLenght)
                {
                    packet.socket.BeginReceive(packet.bytes,
                                               NetPacket.headerLength + packet.readLength,
                                               packet.bodyLenght - packet.readLength,
                                               SocketFlags.None,
                                               new System.AsyncCallback(ReceiveBody),
                                               packet);
                }
                else
                {
                    // 将消息传入到逻辑处理队列
                    NetPacket newpacket = new NetPacket(packet);
                    networkMgr.AddPacket(newpacket);

                    // 下一个读取
                    packet.Reset();
                    packet.socket.BeginReceive(packet.bytes,
                                               0,
                                               NetPacket.headerLength,
                                               SocketFlags.None,
                                               new System.AsyncCallback(ReceiveHeader),
                                               packet);
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("ReceiveBody:" + ex.Message);
            }
        }
Esempio n. 2
0
        void ReceiveBody(System.IAsyncResult ar)
        {
            NetPacket packet = (NetPacket)ar.AsyncState;

            try
            {
                // 返回网络上接收的数据长度
                int read = packet.socket.EndReceive(ar);
                if (read < 1)
                {
                    Console.WriteLine("======172======");
                    // 丢失连接
                    AddInternalPacket("OnLost", packet.socket);
                    return;
                }
                packet.readLength += read;
                // 消息体必须读满指定的长度
                if (packet.readLength < packet.bodyLength)
                {
                    packet.socket.BeginReceive(packet.bytes,
                                               NetPacket.headerLength + packet.readLength,
                                               packet.bodyLength - packet.readLength,
                                               SocketFlags.None,
                                               new System.AsyncCallback(ReceiveBody),
                                               packet);
                }
                else
                {
                    // 将消息传入逻辑处理队列
                    networkMgr.AddPacket(packet);

                    // 下一个读取
                    packet.Reset();
                    packet.socket.BeginReceive(packet.bytes, 0,
                                               NetPacket.headerLength,
                                               SocketFlags.None,
                                               new System.AsyncCallback(ReceiveHeader),
                                               packet);
                }
            }
            catch (System.Exception e)
            {
                Console.WriteLine("ReceiveBody: " + e.Message);
            }
        }