Beispiel #1
0
        public void OnHandleClientData(IAsyncResult asyncResult)
        {
            try
            {
                TxRxPacket dataStatus = (TxRxPacket)asyncResult.AsyncState;


                dataStatus.mCurrentSocket.EndReceive(asyncResult);
                dataStatus.StoreCurrentData();


                IMessage rxMessage = mMessageParser.ParseMessage(dataStatus.mStoredBuffer);

                if (rxMessage == null)
                {
                    //receive the rest of the message
                    dataStatus.mCurrentSocket.BeginReceive(dataStatus.mDataBuffer, 0, dataStatus.mDataBuffer.Length,
                                                           SocketFlags.None, new AsyncCallback(OnHandleClientData), dataStatus);
                    return;
                }
                //handle the message (which can either be register or unregister)
                //send response message if needed
                switch (rxMessage.Type)
                {
                case ((int)MessageType.ResgisteredClientsListMessage):
                {
                    Socket workerSocket = dataStatus.mCurrentSocket;
                    //respond with the current group in the message

                    RegisteredClientsListMessage rxClientList = (RegisteredClientsListMessage)rxMessage;
                    if (rxClientList.Clients != null)
                    {
                        for (int i = 0; i < rxClientList.Clients.Count; ++i)
                        {
                            groupClientsDetails.Add(rxClientList.Clients[i]);
                            //register on each of them
                            IClient client = new P2PClient(mListenPort, rxClientList.Clients[i].ClientIPAddress,
                                                           rxClientList.Clients[i].ClientListenPort, group);
                            client.Initialize();
                            client = null;
                        }

                        if (mOnRecieveListOfClients != null && group == ((RegisteredClientsListMessage)rxMessage).Group)
                        {
                            mOnRecieveListOfClients.Invoke(this, new ReceiveListOfClientsEventArgs(((RegisteredClientsListMessage)rxMessage).Clients));
                        }
                    }

                    break;
                }

                case ((int)MessageType.RegisterMessage):
                {
                    if (!(groupClientsDetails.IndexOf(((RegisterMessage)rxMessage).Client) >= 0))
                    {
                        groupClientsDetails.Add(((RegisterMessage)rxMessage).Client);

                        if (mOnRegisterClient != null && group == ((RegisterMessage)rxMessage).Group)
                        {
                            mOnRegisterClient.Invoke(this, new ServerRegisterEventArgs(((RegisterMessage)rxMessage).Client));
                        }
                        if (mOnReceiveMessage != null)
                        {
                            mOnReceiveMessage.Invoke(this, new ReceiveMessageEventArgs(rxMessage));
                        }
                    }

                    break;
                }

                case ((int)MessageType.UnregisterMessage):
                {
                    if ((groupClientsDetails.IndexOf(((UnregisterMessage)rxMessage).Client) >= 0))
                    {
                        groupClientsDetails.Remove(((UnregisterMessage)rxMessage).Client);

                        if (mOnUnRegisterClient != null && group == ((UnregisterMessage)rxMessage).Group)
                        {
                            mOnUnRegisterClient.Invoke(this,
                                                       new ServerRegisterEventArgs(((UnregisterMessage)rxMessage).Client));
                        }
                    }

                    break;
                }

                case ((int)MessageType.CommandMessage):
                {
                    if (mOnReceiveMessage != null)
                    {
                        mOnReceiveMessage.Invoke(this, new ReceiveMessageEventArgs(rxMessage));
                    }
                    break;
                }

                default:
                {
                    if (rxMessage.Type != ((int)MessageType.EmptyMessage))
                    {
                        if (mOnReceiveMessage != null)
                        {
                            mOnReceiveMessage.Invoke(this,
                                                     new ReceiveMessageEventArgs(rxMessage));
                        }
                    }

                    break;
                }
                }
            }
            catch (ObjectDisposedException ex)
            {
                System.Diagnostics.Debug.WriteLine("Socket has been closed : " + ex.Message);
            }
            catch (SocketException ex)
            {
                if (ex.ErrorCode == 10054) // Error code for Connection reset by peer
                {
                    System.Diagnostics.Debug.WriteLine("Connection reset by peer : " + ex.Message);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
            }
        }
Beispiel #2
0
        protected void OnHandleClientData(IAsyncResult asyncResult)
        {
            try
            {
                TxRxPacket dataStatus = (TxRxPacket)asyncResult.AsyncState;

                int countRx = dataStatus.mCurrentSocket.EndReceive(asyncResult);

                /*
                 * String rxMsgText = Encoding.UTF8.GetString(dataStatus.mDataBuffer, 0, countRx);
                 *
                 * //parse the message
                 * IMessage rxMessage = ParseMessage(rxMsgText);
                 */

                IMessage rxMessage = mMessageParser.ParseMessage(dataStatus.mDataBuffer);

                //handle the message (which can either be register or unregister)
                //send response message if needed : workerSocket.Send(byData);
                switch (rxMessage.Type)
                {
                case ((int)MessageType.RegisterMessage):
                {
                    if (!mClientDetailsGroups.ContainsKey(((RegisterMessage)rxMessage).Group))
                    {
                        mClientDetailsGroups[((RegisterMessage)rxMessage).Group] = new Collection <ICollaborativeClientDetails>();
                    }


                    if (mClientDetailsGroups[((RegisterMessage)rxMessage).Group].IndexOf(((RegisterMessage)rxMessage).Client) >= 0)
                    {
                        mClientDetailsGroups[((RegisterMessage)rxMessage).Group].Remove(((RegisterMessage)rxMessage).Client);
                    }

                    //Socket workerSocket = (Socket)dataStatus.mCurrentSocket;
                    //respond with the current group in the message

                    RegisteredClientsListMessage response = new RegisteredClientsListMessage();
                    response.Clients = mClientDetailsGroups[((RegisterMessage)rxMessage).Group];
                    response.Group   = ((RegisterMessage)rxMessage).Group;

                    //create a socket connection to the newly added client listener port
                    Socket     clientSocket   = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    IPAddress  remoteMachine  = IPAddress.Parse(((RegisterMessage)rxMessage).Client.ClientIPAddress);
                    IPEndPoint remoteEndpoint = new IPEndPoint(remoteMachine, ((RegisterMessage)rxMessage).Client.ClientListenPort);
                    clientSocket.Connect(remoteEndpoint);

                    if (clientSocket.Connected)
                    {
                        Console.WriteLine("Client connected " +
                                          ((RegisterMessage)rxMessage).Client.ClientIPAddress + ":" +
                                          ((RegisterMessage)rxMessage).Client.ClientListenPort);
                    }

                    clientSocket.Send(response.GetMessagePacket());
                    clientSocket.Close(1);     //just a minor timeout to be sure the message got there
                    //the socket just lost the purpose of ever existing

                    mClientDetailsGroups[((RegisterMessage)rxMessage).Group].Add(((RegisterMessage)rxMessage).Client);

                    if (mOnRegisterClient != null)
                    {
                        mOnRegisterClient.Invoke(this, new ServerRegisterEventArgs(((RegisterMessage)rxMessage).Client));
                    }
                    break;
                }

                case ((int)MessageType.UnregisterMessage):
                {
                    mClientDetailsGroups[((UnregisterMessage)rxMessage).Group]
                    .Remove(((UnregisterMessage)rxMessage).Client);
                    //do not contact all others in the same group... have the client take care of that
                    if (mOnRegisterClient != null)
                    {
                        mOnRegisterClient.Invoke(this,
                                                 new ServerRegisterEventArgs(((UnregisterMessage)rxMessage).Client));
                    }


                    Console.WriteLine("Client " + ((UnregisterMessage)rxMessage).Client.ClientIPAddress + ((UnregisterMessage)rxMessage).Client.ClientListenPort + " unregistered");
                    break;
                }
                }
            }
            catch (ObjectDisposedException ex)
            {
                System.Diagnostics.Debug.WriteLine("Socket has been closed : " + ex.Message);
            }
            catch (SocketException ex)
            {
                if (ex.ErrorCode == 10054) // Error code for Connection reset by peer
                {
                    System.Diagnostics.Debug.WriteLine("Connection reset by peer : " + ex.Message);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
            }
        }