コード例 #1
0
        /*
         *  Receive data callback
         */
        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.
            Console.WriteLine("ReceiveDataCallback");
            try {
                int bytesRead = netPacket.WorkSocket.EndReceive(ar);
                netPacket.Message.AddRange(netPacket.Buffer);
                int packetSize = BitConverter.ToInt32(netPacket.Message.ToArray(), 0);
                Console.WriteLine("Packet Size " + packetSize + " action " + netPacket.Message.ToArray()[4]);
                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
                {
                    Console.WriteLine("Client action: {0} {1} {2}", RequestBase.GetPacketAction(netPacket), packetSize, netPacket.Message.Count);
                    switch (RequestBase.GetPacketAction(netPacket))
                    {
                    case RequestID.RegisterRequestAction: {
                        HandleUserRegistration(netPacket);
                        break;
                    }

                    case RequestID.LoginRequestAction: {
                        HandleUserLogin(netPacket);
                        break;
                    }

                    case RequestID.ChatMessageRequestAction: {
                        HandleChatMessage(netPacket);
                        break;
                    }

                    case RequestID.RequestGetChatUserList: {
                        GetChatUserList(netPacket);
                        break;
                    }
                    }
                    netPacket.Message.Clear();
                }
            } catch (Exception e) {
            }
        }
コード例 #2
0
        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;
        }