Exemplo n.º 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);
     }
 }
            internal static dynamic GetP2PDataRequestFailure(P2PDataRequest request, string errorMessage)
            {
                P2PDataRequestFailure p2pdatREqFail = new P2PDataRequestFailure(request, errorMessage);

                return(p2pdatREqFail);
            }
            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)
                    {
                    }
                }
            }
            internal static P2PDataRequestFailure ParseAndGet_P2PDataRequestFailure_FromXMLDataString(string XMLString)
            {
                System.IO.StringReader   sr     = null;
                System.Xml.XmlTextReader m_xmlr = null;

                try
                {
                    sr     = new System.IO.StringReader(XMLString);
                    m_xmlr = new System.Xml.XmlTextReader(sr);
                    m_xmlr.WhitespaceHandling = System.Xml.WhitespaceHandling.None;
                }
                catch (System.Xml.XmlException)
                {
                    string msg;
                    msg = "Error trying to get XML format from  P2PDataRequest Data string [" + XMLString + "]";
                }
                catch (Exception ex)
                {
                    string msg = "";
                    msg = "Error trying to parse P2PDataRequest  XML string : " + ex.Message;
                    throw (new Exception(msg));
                }
                m_xmlr.Read();
                string HeaderIdentifier = m_xmlr.Name;

                if (HeaderIdentifier != "P2P_DATA_REQUEST_FAILURE")
                {
                    throw (new System.Xml.XmlException("Invalid P2PDataRequestFailure header \'" + HeaderIdentifier + "\'. It was expected to be \'P2P_DATA_REQUEST_FAILURE\'"));
                }
                else
                {
                    //**********************************************************
                    //retrieves the data operation type
                    //**********************************************************
                    string P2PDataOperationID = "";
                    P2PDataOperationID = m_xmlr.GetAttribute("P2PDataOperationID");
                    if (P2PDataOperationID == null)
                    {
                        P2PDataOperationID = Guid.NewGuid().ToString();
                    }

                    //**********************************************************
                    //retrieves the data that holds the error message
                    //**********************************************************
                    m_xmlr.Read();
                    string       dataSectionName = m_xmlr.Name;
                    DataVariable variable        = default(DataVariable);
                    if (dataSectionName == "DATA")
                    {
                        string variableXMLstring = m_xmlr.ReadOuterXml();
                        variable = XMLDataFormatting.RetrieveDataVariableFromXMLString(variableXMLstring);
                    }
                    else
                    {
                        throw (new Exception("Invalid data XML section name \'" + dataSectionName + "\'. Is expected to be \'DATA\'"));
                    }
                    string errorMEssage = System.Convert.ToString(variable.Data);

                    //creates the object to return to caller
                    P2PDataRequestFailure reqFailure = new P2PDataRequestFailure(P2PDataOperationID, errorMEssage);
                    return(reqFailure);
                }
            }