コード例 #1
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);
     }
 }
コード例 #2
0
            private void ReadSocket(IAsyncResult ar)
            {
                var receivedData = new byte[P2PNetworkingDefinitions.DATABUFFER_SIZE + 1];
                int bytesRead    = 0;

                this._connectionWithRemotePortLost = false;
                this._unexpectedErrorOcurred       = false;
                this._unexpectedErrorMEssage       = "";

                try
                {
                    try
                    {
                        bytesRead = this._socketStream.EndRead(ar);
                    }
                    catch (Exception)
                    {
                    }


                    if (bytesRead > 0)
                    {
                        byte[] ReaDatalBufferReceived = new byte[bytesRead - 1 + 1];

                        System.Array.Copy(this._ReceiveDataBuffer, ReaDatalBufferReceived, bytesRead);
                        System.Array.Clear(this._ReceiveDataBuffer, 0, this._ReceiveDataBuffer.Length);

                        if (!(ReaDatalBufferReceived == null))
                        {
                            try
                            {
                                //*****************************************
                                //tries to deserialize to a P2PDATA
                                //*****************************************
                                this._P2PDataRetrievedFromRemotePort = P2PData.Deserialize(ReaDatalBufferReceived);
                                this._dataRetrievedIsAvailable       = true;
                            }
                            catch (Exception)
                            {
                                try
                                {
                                    //*****************************************
                                    //tries to deserialize to a P2P_DATA_DELIVERY_RESULT
                                    //*****************************************
                                    this._P2PDataDeliveryResultReceivedFromRemotePort = Services.P2PCommunicationsScheme.Data.P2PDataDeliveryResult.Deserialize(ReaDatalBufferReceived);
                                    this._dataDEliveryResultISAvailable = true;
                                }
                                catch (Exception)
                                {
                                    try
                                    {
                                        //*****************************************
                                        //tries to deserialize to a P2P_DATA_REQUEST_FAILURE
                                        //*****************************************
                                        this._P2PDataRequestFailureReceived = P2PDataRequestFailure.Deserialize(ReaDatalBufferReceived);
                                        this._dataRetrievalErrorIsAvailable = true;
                                    }
                                    catch (Exception)
                                    {
                                        this._unexpectedErrorMEssage = "Invalid Data received in P2PPortClient";
                                        this._unexpectedErrorOcurred = true;
                                    }
                                }
                            }
                        }

                        this._socketStream.BeginRead(_ReceiveDataBuffer, 0, _ReceiveDataBuffer.Length, new AsyncCallback(ReadSocket), null);
                    }
                    else
                    {
                        //and error has ocurred and the remote port has broken its
                        //link but is not catched by the environment, but can be determined
                        //becuase the read operation proceeds with no data available
                        if (this._manualDisconnection == false)
                        {
                            this._unexpectedErrorOcurred = true;
                            this._unexpectedErrorMEssage = "An unexpected error ocurred in the connection when client was waiting a response from remote port.";
                            try
                            {
                                this._tcpClient.Client.Close();
                            }
                            catch (Exception)
                            {
                            }
                            try
                            {
                                if (ConnectionLostEvent != null)
                                {
                                    ConnectionLostEvent(this);
                                }
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }
                catch (IOException)
                {
                    try
                    {
                        this._connectionWithRemotePortLost = true;
                        this._tcpClient.Client.Close();
                    }
                    catch (Exception)
                    {
                    }
                    try
                    {
                        if (ConnectionLostEvent != null)
                        {
                            ConnectionLostEvent(this);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
                catch (Exception ex)
                {
                    this._unexpectedErrorMEssage = ex.Message;
                    this._unexpectedErrorOcurred = true;
                    try
                    {
                        if (ConnectionLostEvent != null)
                        {
                            ConnectionLostEvent(this);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }