Exemplo n.º 1
0
            // This method is used to process the accept socket connection
            private void ProcessAccept(SocketAsyncEventArgs AsyncEventArgs)
            {
                // First we get the accept socket from the passed in arguments
                Socket acceptsocket = AsyncEventArgs.AcceptSocket;

                // If the accept socket is connected to a client we will process it
                // otherwise nothing happens
                if (acceptsocket.Connected)
                {
                    try
                    {
                        // Go get a read socket out of the read socket stack
                        SocketAsyncEventArgs readsocket = _socketPool.Pop();

                        // If we get a socket, use it, otherwise all the sockets in the stack are used up
                        // and we can't accept anymore connections until one frees up
                        if (readsocket != null)
                        {
                            P2PPortClientHandler newClientHandler;

                            try
                            {
                                newClientHandler = new P2PPortClientHandler(acceptsocket, readsocket);

                                // Start a receive request and immediately check to see if the receive is already complete
                                // Otherwise OnIOCompleted will get called when the receive is complete
                                bool IOPending = acceptsocket.ReceiveAsync(newClientHandler.readSocket);
                                if (!IOPending)
                                {
                                    ProcessReceive(readsocket);
                                }

                                //raises the event that a clien has conencted
                                this._ConnectedClientsTable.Add(newClientHandler.clientID, newClientHandler);
                                if (NewClientConnectionEvent != null)
                                {
                                    NewClientConnectionEvent(newClientHandler.clientID);
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                        else
                        {
                            acceptsocket.Close();
                            var ex = new Exception("Client connection refused because the maximum number of client connections (" + Convert.ToString(P2PNetworkingDefinitions.DEFAULT_MAX_CONNECTIONS) + ") allowed on the server has been reached.");
                            throw ex;
                        }
                    }
                    catch (Exception ex)
                    {
                        exceptionthrown = true;
                    }

                    // Start the process again to wait for the next connection
                    StartAcceptAsync(AsyncEventArgs);
                }
            }
Exemplo n.º 2
0
            private void _incommingDataProcessingQueue_NewItemDetected(object Item)
            {
                try
                {
                    if (Item != null)
                    {
                        P2PPortClientHandler clientHandler = (P2PPortClientHandler)Item;

                        ProcessReceivedData(clientHandler);
                    }
                }
                catch
                {
                }
            }
Exemplo n.º 3
0
            private void CloseAndFinishAllClientConnectionsOnPortDisposing()
            {
                IEnumerator          enumm = default(IEnumerator);
                P2PPortClientHandler _P2PPortClientHandler      = default(P2PPortClientHandler);
                Hashtable            copyOFClientConnectedTable = default(Hashtable);

                copyOFClientConnectedTable = (Hashtable)(this._ConnectedClientsTable.Clone());

                enumm = copyOFClientConnectedTable.GetEnumerator();
                while (enumm.MoveNext())
                {
                    _P2PPortClientHandler = (P2PPortClientHandler)(((DictionaryEntry)enumm.Current).Value);
                    try
                    {
                        _P2PPortClientHandler.handlerSocket.Close();
                    }
                    catch (Exception)
                    {
                    }
                }
                this._ConnectedClientsTable.Clear();
            }
Exemplo n.º 4
0
            private void CloseAndFinishClientConnection(P2PPortClientHandler clientHandler)
            {
                P2PPortClientHandler currentClientHandler = default(P2PPortClientHandler);

                currentClientHandler = (P2PPortClientHandler)this._ConnectedClientsTable[clientHandler.clientID];
                try
                {
                    if (!(currentClientHandler.handlerSocket == null))
                    {
                        currentClientHandler.handlerSocket.Close();
                    }
                }
                catch (Exception)
                {
                }
                try
                {
                    this._ConnectedClientsTable.Remove(currentClientHandler.clientID);
                }
                catch (Exception ex)
                {
                    CustomEventLog.WriteEntry(ex);
                }
            }
Exemplo n.º 5
0
 // function that mainly process the received data in the server port
 private void ProcessReceivedData(P2PPortClientHandler client)
 {
     try
     {
         if (!(client == null))
         {
             try
             {
                 //*****************************************
                 //tries to deserialize to a P2PDATA
                 //*****************************************
                 P2PData data = P2PData.Deserialize(client.ReceiveDataBuffer);
                 this.Statistics.LogDataReceptionEvent(data);
                 try
                 {
                     //catches if an exception ocurrs to avoid the programm exist from the reading thread
                     //because of a failure in the object that implements the interface.
                     ((IUseP2PCommunicationsScheme)this._portOwnerComponent).ReceiveData(data, this.ListeningPortNumber);
                     Services.P2PCommunicationsScheme.Data.P2PDataDeliveryResult successfulDataDelivery = Services.P2PCommunicationsScheme.Data.P2PDataDeliveryResult.GetSucceedDeliveryResult(data);
                     client.handlerSocket.Send(successfulDataDelivery.Serialize());
                     //***************************************************************************************
                 }
                 catch (Exception ex)
                 {
                     Services.P2PCommunicationsScheme.Data.P2PDataDeliveryResult failureDataDelivery = Services.P2PCommunicationsScheme.Data.P2PDataDeliveryResult.GetFailureDeliveryResult(data, ex.Message);
                     client.handlerSocket.Send(failureDataDelivery.Serialize());
                     //***************************************************************************************
                 }
             }
             catch (Exception ex)
             {
                 //*****************************************
                 //tries to deserialize to a P2P_DATA_DELIVERY_RESULT
                 //*****************************************
                 P2PDataRequest dataRequest   = default(P2PDataRequest);
                 P2PData        dataRequested = default(P2PData);
                 try
                 {
                     dataRequest = P2PDataRequest.Deserialize(client.ReceiveDataBuffer);
                     try
                     {
                         dataRequested = ((IUseP2PCommunicationsScheme)this._portOwnerComponent).RetrieveData(dataRequest, this.ListeningPortNumber);
                         if (!(dataRequested == null))
                         {
                             this.Statistics.LogSuccesfulDataRequestEvent(dataRequest);
                             client.handlerSocket.Send(dataRequested.Serialize());
                             //***************************************************************************************
                         }
                         else
                         {
                             this.Statistics.LogFailedDataRequestEvent(dataRequest);
                             string portOwnerName = ((IUseP2PCommunicationsScheme)this._portOwnerComponent).P2PPortOwnerName;
                             if (portOwnerName == null)
                             {
                                 portOwnerName = "UNKNOWN";
                             }
                             string errorMessage = "The remote object \'" + portOwnerName + "\' can\'t handle the requested data named \'" + dataRequest.RequestedDataName + "\'";
                             P2PDataRequestFailure reqFAilure = P2PDataRequestFailure.GetP2PDataRequestFailure(dataRequest, errorMessage);
                             client.handlerSocket.Send(reqFAilure.Serialize());
                             //***************************************************************************************
                         }
                     }
                     catch (Exception ex2)
                     {
                         this.Statistics.LogFailedDataRequestEvent(dataRequest);
                         string portOwnerName = ((IUseP2PCommunicationsScheme)this._portOwnerComponent).P2PPortOwnerName;
                         if (portOwnerName == null)
                         {
                             portOwnerName = "UNKNOWN";
                         }
                         string errorMessage = ex2.Message;
                         P2PDataRequestFailure reqFAilure = P2PDataRequestFailure.GetP2PDataRequestFailure(dataRequest, errorMessage);
                         client.handlerSocket.Send(reqFAilure.Serialize());
                         //***************************************************************************************
                     }
                 }
                 catch (Exception)
                 {
                     string msg = "";
                     msg = "Error processing incomming data from P2PPort client : " + ex.ToString();
                     CustomEventLog.WriteEntry(EventLogEntryType.Error, msg);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         string msg = "";
         msg = "Error processing incomming data from P2PPort client : " + ex.ToString();
         CustomEventLog.WriteEntry(EventLogEntryType.Error, msg);
     }
 }