Inheritance: TcpSocketHandler
        private void ReceiveCallback(object state)
        {
            TcpClientSocketHandler  handler = null;
            IClientChannelSinkStack dataArrivedCallbackState = null;

            try
            {
                handler = (TcpClientSocketHandler)state;
                dataArrivedCallbackState = (IClientChannelSinkStack)handler.DataArrivedCallbackState;
                ITransportHeaders headers        = handler.ReadHeaders();
                Stream            responseStream = handler.GetResponseStream();
                dataArrivedCallbackState.AsyncProcessResponse(headers, responseStream);
            }
            catch (Exception exception)
            {
                try
                {
                    if (dataArrivedCallbackState != null)
                    {
                        dataArrivedCallbackState.DispatchException(exception);
                    }
                }
                catch
                {
                }
            }
        }
Beispiel #2
0
        } // SendRequestWithRetry

        private void ReceiveCallback(Object state)
        {
            TcpClientSocketHandler  clientSocket = null;
            IClientChannelSinkStack sinkStack    = null;

            try
            {
                clientSocket = (TcpClientSocketHandler)state;
                sinkStack    = (IClientChannelSinkStack)clientSocket.DataArrivedCallbackState;

                ITransportHeaders responseHeaders = clientSocket.ReadHeaders();
                Stream            responseStream  = clientSocket.GetResponseStream();

                // call down the sink chain
                sinkStack.AsyncProcessResponse(responseHeaders, responseStream);
            }
            catch (Exception e)
            {
                try
                {
                    if (sinkStack != null)
                    {
                        sinkStack.DispatchException(e);
                    }
                }
                catch (Exception)
                {
                    // Fatal Error.. ignore
                }
            }

            // The client socket will be returned to the cache
            //   when the response stream is closed.
        } // ReceiveCallback
        public void ProcessMessage(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream, out ITransportHeaders responseHeaders, out Stream responseStream)
        {
            Debugger.NotifyOfCrossThreadDependency();
            TcpClientSocketHandler handler = this.SendRequestWithRetry(msg, requestHeaders, requestStream);

            responseHeaders = handler.ReadHeaders();
            responseStream  = handler.GetResponseStream();
        }
        public void AsyncProcessRequest(IClientChannelSinkStack sinkStack, IMessage msg, ITransportHeaders headers, Stream stream)
        {
            TcpClientSocketHandler handler = this.SendRequestWithRetry(msg, headers, stream);

            if (handler.OneWayRequest)
            {
                handler.ReturnToCache();
            }
            else
            {
                handler.DataArrivedCallback      = new WaitCallback(this.ReceiveCallback);
                handler.DataArrivedCallbackState = sinkStack;
                handler.BeginReadMessage();
            }
        }
        } // TcpClientTransportSink

        public void ProcessMessage(IMessage msg,
                                   ITransportHeaders requestHeaders, Stream requestStream,
                                   out ITransportHeaders responseHeaders, out Stream responseStream)
        {
            InternalRemotingServices.RemotingTrace("TcpClientTransportSink::ProcessMessage");

            TcpClientSocketHandler clientSocket =
                SendRequestWithRetry(msg, requestHeaders, requestStream);

            // receive response
            responseHeaders = clientSocket.ReadHeaders();
            responseStream  = clientSocket.GetResponseStream();

            // The client socket will be returned to the cache
            //   when the response stream is closed.
        } // ProcessMessage
        private TcpClientSocketHandler SendRequestWithRetry(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream)
        {
            long position = 0L;
            bool flag     = true;
            bool canSeek  = requestStream.CanSeek;

            if (canSeek)
            {
                position = requestStream.Position;
            }
            TcpClientSocketHandler socket = null;
            string machinePortAndSid      = this._machineAndPort + (this._channel.IsSecured ? ("/" + this.GetSid()) : null);

            if (this.authSet && !this._channel.IsSecured)
            {
                throw new RemotingException(CoreChannel.GetResourceString("Remoting_Tcp_AuthenticationConfigClient"));
            }
            bool openNew = (this._channel.IsSecured && (this._securityUserName != null)) && (this._connectionGroupName == null);

            try
            {
                socket = (TcpClientSocketHandler)this.ClientSocketCache.GetSocket(machinePortAndSid, openNew);
                socket.SendRequest(msg, requestHeaders, requestStream);
            }
            catch (SocketException)
            {
                for (int i = 0; ((i < this._retryCount) && canSeek) && flag; i++)
                {
                    try
                    {
                        requestStream.Position = position;
                        socket = (TcpClientSocketHandler)this.ClientSocketCache.GetSocket(machinePortAndSid, openNew);
                        socket.SendRequest(msg, requestHeaders, requestStream);
                        flag = false;
                    }
                    catch (SocketException)
                    {
                    }
                }
                if (flag)
                {
                    throw;
                }
            }
            requestStream.Close();
            return(socket);
        }
        } // ProcessMessage

        public void AsyncProcessRequest(IClientChannelSinkStack sinkStack, IMessage msg,
                                        ITransportHeaders headers, Stream stream)
        {
            InternalRemotingServices.RemotingTrace("TcpClientTransportSink::AsyncProcessRequest");

            TcpClientSocketHandler clientSocket =
                SendRequestWithRetry(msg, headers, stream);

            if (clientSocket.OneWayRequest)
            {
                clientSocket.ReturnToCache();
            }
            else
            {
                // do an async read on the reply
                clientSocket.DataArrivedCallback      = new WaitCallback(this.ReceiveCallback);
                clientSocket.DataArrivedCallbackState = sinkStack;
                clientSocket.BeginReadMessage();
            }
        } // AsyncProcessRequest
        } // TcpClientTransportSink

        public void ProcessMessage(IMessage msg,
                                   ITransportHeaders requestHeaders, Stream requestStream,
                                   out ITransportHeaders responseHeaders, out Stream responseStream)
        {
            InternalRemotingServices.RemotingTrace("TcpClientTransportSink::ProcessMessage");

            // the call to SendRequest can block a func eval, so we want to notify the debugger that we're
            // about to call a blocking operation.
            System.Diagnostics.Debugger.NotifyOfCrossThreadDependency();

            TcpClientSocketHandler clientSocket =
                SendRequestWithRetry(msg, requestHeaders, requestStream);

            // receive response
            responseHeaders = clientSocket.ReadHeaders();
            responseStream  = clientSocket.GetResponseStream();

            // The client socket will be returned to the cache
            //   when the response stream is closed.
        } // ProcessMessage
Beispiel #9
0
        } // Next


        private TcpClientSocketHandler SendRequestWithRetry(IMessage msg,
                                                            ITransportHeaders requestHeaders,
                                                            Stream requestStream)
        {
            // If the stream is seekable, we can retry once on a failure to write.
            long initialPosition = 0;
            bool bCanSeek        = requestStream.CanSeek;

            if (bCanSeek)
            {
                initialPosition = requestStream.Position;
            }

            TcpClientSocketHandler clientSocket = null;

            try
            {
                clientSocket = (TcpClientSocketHandler)ClientSocketCache.GetSocket(_machineAndPort);
                clientSocket.SendRequest(msg, requestHeaders, requestStream);
            }
            catch (SocketException)
            {
                // retry sending if possible
                if (bCanSeek)
                {
                    // reset position...
                    requestStream.Position = initialPosition;

                    // ...and try again.
                    clientSocket = (TcpClientSocketHandler)
                                   ClientSocketCache.GetSocket(_machineAndPort);

                    clientSocket.SendRequest(msg, requestHeaders, requestStream);
                }
            }

            requestStream.Close();

            return(clientSocket);
        } // SendRequestWithRetry
        } // Next


        private TcpClientSocketHandler SendRequestWithRetry(IMessage msg,
                                                            ITransportHeaders requestHeaders,
                                                            Stream requestStream)
        {
            // If the stream is seekable, we can retry once on a failure to write.
            long initialPosition = 0;
            bool sendException   = true;
            bool bCanSeek        = requestStream.CanSeek;

            if (bCanSeek)
            {
                initialPosition = requestStream.Position;
            }

            TcpClientSocketHandler clientSocket = null;
            // Add the sid string only if the channel is secure.
            String machinePortAndSid = _machineAndPort + (_channel.IsSecured ? "/" + GetSid() : null);

            // The authentication config entries are only valid if secure is true
            if (authSet && !_channel.IsSecured)
            {
                throw new RemotingException(CoreChannel.GetResourceString(
                                                "Remoting_Tcp_AuthenticationConfigClient"));
            }

            // If explicitUserName is set but connectionGroupName isnt we will need to authenticate on each call
            bool openNewAlways = (_channel.IsSecured) &&
                                 (_securityUserName != null) && (_connectionGroupName == null);

            try
            {
                clientSocket = (TcpClientSocketHandler)ClientSocketCache.GetSocket(machinePortAndSid, openNewAlways);
                clientSocket.SendRequest(msg, requestHeaders, requestStream);
            }
            catch (SocketException)
            {
                // Retry sending if socketexception occured, stream is seekable, retrycount times
                for (int count = 0; (count < _retryCount) && (bCanSeek && sendException); count++)
                {
                    // retry sending if possible
                    try
                    {
                        // reset position...
                        requestStream.Position = initialPosition;

                        // ...and try again.
                        clientSocket = (TcpClientSocketHandler)
                                       ClientSocketCache.GetSocket(machinePortAndSid, openNewAlways);

                        clientSocket.SendRequest(msg, requestHeaders, requestStream);
                        sendException = false;
                    }
                    catch (SocketException)
                    {
                    }
                }
                if (sendException)
                {
                    throw;
                }
            }

            requestStream.Close();

            return(clientSocket);
        } // SendRequestWithRetry