public void ReceiveDataCallback(IAsyncResult ar) { // Retrieve the state object and the handler socket from the asynchronous state object. NetworkPacket netPacket = (NetworkPacket)ar.AsyncState; // Read data from the client socket. int bytesRead = netPacket.WorkSocket.EndReceive(ar); netPacket.Message.AddRange(netPacket.Buffer); int packetSize = BitConverter.ToInt32(netPacket.Message.ToArray(), 0); if (packetSize <= 0) { netPacket.WorkSocket.BeginReceive( netPacket.Buffer, 0, netPacket.BufferSize, 0, new AsyncCallback(ReceiveDataCallback), netPacket); return; } if (bytesRead > 0 && netPacket.Message.Count < packetSize) { netPacket.BufferSize = packetSize - netPacket.Message.Count; netPacket.Buffer = new byte[netPacket.BufferSize]; netPacket.WorkSocket.BeginReceive( netPacket.Buffer, 0, netPacket.BufferSize, 0, new AsyncCallback(ReceiveDataCallback), netPacket); return; } else { switch (RequestBase.GetPacketAction(netPacket)) { /* * case RequestID.RequestResponse: { * RequestResponse response = new RequestResponse(netPacket); * switch (response.ResponsePacketAction) { * case RequestID.LoginRequestAction: { * RequestLogin requestLogin = new RequestLogin(); * requestLogin.Deserialize(netPacket.Message.ToArray()); * break; * } * case RequestID.ChatMessageRequestAction: { * break; * } * default: * break; * } * break; * } * * case RequestID.LoginRequestAction: { * RequestLogin requestLogin = new RequestLogin(); * requestLogin.Deserialize(netPacket.Message.ToArray()); * break; * } */ /* * Handle received chat messages */ case RequestID.ChatMessageRequestAction: { RequestChatMessage requestChatMessage = RequestChatMessage.StaticDeserialize(netPacket.Message.ToArray()); UI.FormChat formChat = chatWindowList.Find(chat => { return((requestChatMessage.Recipient == User.Username && requestChatMessage.Sender == chat.chatName) || requestChatMessage.Recipient == chat.chatName); }); if (formChat != null) { formChat.messageList.Add(requestChatMessage); } else { AddNewChatWindow(this, requestChatMessage.Sender == User.Username ? requestChatMessage.Recipient : requestChatMessage.Sender, requestChatMessage); } break; } /* * Handle received chatUserLists */ case RequestID.RequestGetChatUserList: { form.requestGetChatUserList.Add( RequestGetChatUserList.StaticDeserialize(netPacket.Message.ToArray())); break; } } } IsReceivingData = false; }