// 接收头消息 void ReceiveHeader(System.IAsyncResult ar) { NetBitStream stream = (NetBitStream)ar.AsyncState; try { int read = _socket.EndReceive(ar); // 服务器断开连接 if (read < 1) { Disconnect(0); PushPacket((ushort)MessageIdentifiers.ID.CONNECTION_LOST, ""); return; } // 获得消息体长度 stream.DecodeHeader(); // 下一个读取 _socket.BeginReceive(stream.BYTES, NetBitStream.header_length, stream.BodyLength, SocketFlags.None, new System.AsyncCallback(ReceiveBody), stream); } catch (System.Exception e) { PushPacket((ushort)MessageIdentifiers.ID.CONNECTION_LOST, e.Message); Disconnect(0); } }
void ReceiveHeader(System.IAsyncResult ar) { NetBitStream stream = (NetBitStream)ar.AsyncState; try { int read = stream._socket.EndReceive(ar); if (read < 1) { PushPacket((ushort)MessageIdentifiers.ID.CONNECTION_LOST, "", stream._socket); return; } stream.DecodeHeader(); stream._socket.BeginReceive(stream.BYTES, NetBitStream.header_length, stream.BodyLength, SocketFlags.None, new System.AsyncCallback(ReceiveBody), stream); } catch (System.Exception e) { PushPacket((ushort)MessageIdentifiers.ID.CONNECTION_LOST, e.Message, stream._socket); } }