Esempio n. 1
0
 internal static void PrepareDataToSend(SocketAsyncEventArgs e, P2PDataRequest dataRequest)
 {
     Byte[] SerializedData = dataRequest.Serialize();
     DataPreparationHandler.PrepareAndBufferData(e, SerializedData);
 }
            private P2PData RetrieveData(P2PDataRequest request, int timeOutInSeconds)
            {
                bool dataRetrieved = false;

                byte[]    bufferToBeSend = null;
                Exception exception      = null;

                int OPERATION_TIME_OUT_IN_MS = timeOutInSeconds * 1000;


                bufferToBeSend = request.Serialize();

                lock (this._socketPortBlockingFlag)
                {
                    try
                    {
                        if (!(this._socketStream == null))
                        {
                            //initialization of control flags
                            this._dataRetrievedIsAvailable      = false;
                            this._dataRetrievalErrorIsAvailable = false;
                            this._unexpectedErrorOcurred        = false;

                            //sends the data request using the socket stream
                            try
                            {
                                //*********************************************************************************
                                this._socketStream.Write(bufferToBeSend, 0, bufferToBeSend.Length);

                                //*********************************************************************************
                                //crates a signaled thread thar waits until the controls flags changes because a data or error is received
                                //in the read socket thread
                                WaitDataRequest_AR_Event.Reset();
                                ThreadPool.QueueUserWorkItem(WaitingRequestResponseThredFcn, WaitDataRequest_AR_Event);

                                if (WaitDataRequest_AR_Event.WaitOne(OPERATION_TIME_OUT_IN_MS, true))
                                {
                                    //the working method activated the signal so an event is availabel according with the
                                    //activated control flags
                                    if (this._dataRetrievedIsAvailable)
                                    {
                                        this.Statistics.LogSuccesfulDataRequestEvent(request);
                                        dataRetrieved = true;
                                    }
                                    else if (this._dataRetrievalErrorIsAvailable)
                                    {
                                        this.Statistics.LogFailedDataRequestEvent(request);
                                        exception = new P2PDataRetrievalException(request, this._P2PDataRequestFailureReceived.ErrorMessage);
                                    }
                                    else if (this._connectionWithRemotePortLost)
                                    {
                                        this.Statistics.LogFailedDataRequestEvent(request);
                                        exception = new P2PPortConnectionException();
                                    }
                                    else if (this._unexpectedErrorOcurred)
                                    {
                                        this.Statistics.LogFailedDataRequestEvent(request);
                                        if (!(this._unexpectedErrorMEssage == null))
                                        {
                                            if (this._unexpectedErrorMEssage.Length > 0)
                                            {
                                                exception = new P2PDataRetrievalUnexpectedException(request, this._unexpectedErrorMEssage);
                                            }
                                            else
                                            {
                                                exception = new P2PDataRetrievalUnexpectedException(request);
                                            }
                                        }
                                        else
                                        {
                                            exception = new P2PDataRetrievalUnexpectedException(request);
                                        }
                                    }
                                }
                                else
                                {
                                    //timeout waiting for the response
                                    this.Statistics.LogFailedDataRequestEvent(request);
                                    exception = new P2PDataRequestTimeOutException(request);
                                }
                            }
                            catch (Exception ex)
                            {
                                throw (ex);
                            }
                        }
                        else
                        {
                            exception = new Exception("Socket stream invalid of not available.");
                        }
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                    }
                }

                if (!(exception == null))
                {
                    throw (exception);
                }
                else
                {
                    if (dataRetrieved)
                    {
                        return(this._P2PDataRetrievedFromRemotePort);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }