void OnConnected(Socket connectSocket, SocketAsyncEventArgs context, TaskCompletionSource <ClientTransport> taskCompletionSource)
        {
            try {
                if (connectSocket == null || !connectSocket.Connected)
                {
                    // canceled.
                    taskCompletionSource.SetException(
                        new RpcTransportException(
                            RpcError.ConnectionTimeoutError,
                            "Connect timeout.",
                            string.Format(CultureInfo.CurrentCulture, "Timeout: {0}", Configuration.ConnectTimeout)
                            )
                        );
                    return;
                }

                MsgPackRpcClientProtocolsTrace.TraceEvent(
                    MsgPackRpcClientProtocolsTrace.EndConnect,
                    "Connected. {{ \"Socket\" : 0x{0:X}, \"RemoteEndPoint\" : \"{1}\", \"LocalEndPoint\" : \"{2}\" }}",
                    ClientTransport.GetHandle(connectSocket),
                    ClientTransport.GetRemoteEndPoint(connectSocket, context),
                    ClientTransport.GetLocalEndPoint(connectSocket)
                    );

                taskCompletionSource.SetResult(GetTransport(connectSocket));
            }
            finally {
                context.Dispose();
            }
        }
Example #2
0
        /// <summary>
        ///		Sets the bound <see cref="ClientTransport"/>.
        /// </summary>
        /// <param name="transport">The binding transport.</param>
        internal void SetTransport(ClientTransport transport)
        {
            Contract.Requires(transport != null);

            nextProcess = transport.UnpackResponseHeader;
            base.SetTransport(transport);
        }
Example #3
0
        internal ClientRequestContext GetRequestContext(ClientTransport transport)
        {
            Contract.Requires(transport != null);
            Contract.Ensures(Contract.Result <ClientRequestContext>() != null);

            var result = RequestContextPool.Borrow();

            result.SetTransport(transport);
            result.RenewSessionId();
            return(result);
        }
Example #4
0
        /// <summary>
        ///		Handles socket error.
        /// </summary>
        /// <param name="socket">The <see cref="Socket"/> which might cause socket error.</param>
        /// <param name="context">The <see cref="SocketAsyncEventArgs"/> which holds actual error information.</param>
        /// <returns>
        ///		<see cref="RpcErrorMessage"/> corresponds for the socket error.
        ///		<c>null</c> if the operation result is not socket error.
        /// </returns>
        protected internal RpcErrorMessage?HandleSocketError(Socket socket, SocketAsyncEventArgs context)
        {
            if (context.SocketError.IsError() == false)
            {
                return(null);
            }

            MsgPackRpcClientProtocolsTrace.TraceEvent(
                MsgPackRpcClientProtocolsTrace.SocketError,
                "Socket error. {{ \"Socket\" : 0x{0:X}, \"RemoteEndpoint\" : \"{1}\", \"LocalEndpoint\" : \"{2}\", \"LastOperation\" : \"{3}\", \"SocketError\" : \"{4}\", \"ErrorCode\" : 0x{5:X} }}",
                ClientTransport.GetHandle(socket),
                ClientTransport.GetRemoteEndPoint(socket, context),
                ClientTransport.GetLocalEndPoint(socket),
                context.LastOperation,
                context.SocketError,
                (int)context.SocketError
                );

            return(context.SocketError.ToClientRpcError());
        }
        void OnCompleted(object sender, SocketAsyncEventArgs e)
        {
            var socket               = sender as Socket;
            var userToken            = e.UserToken as Tuple <TaskCompletionSource <ClientTransport>, ConnectTimeoutWatcher>;
            var taskCompletionSource = userToken.Item1;
            var watcher              = userToken.Item2;

            if (watcher != null)
            {
                EndConnectTimeoutWatch(watcher);
            }

            var error = HandleSocketError(e.ConnectSocket ?? socket, e);

            if (error != null)
            {
                taskCompletionSource.SetException(error.Value.ToException());
                return;
            }

            switch (e.LastOperation)
            {
            case SocketAsyncOperation.Connect: {
                OnConnected(e.ConnectSocket, e, taskCompletionSource);
                break;
            }

            default: {
                MsgPackRpcClientProtocolsTrace.TraceEvent(
                    MsgPackRpcClientProtocolsTrace.UnexpectedLastOperation,
                    "Unexpected operation. {{ \"Socket\" : 0x{0:X}, \"RemoteEndPoint\" : \"{1}\", \"LocalEndPoint\" : \"{2}\", \"LastOperation\" : \"{3}\" }}",
                    ClientTransport.GetHandle(socket),
                    ClientTransport.GetRemoteEndPoint(socket, e),
                    ClientTransport.GetLocalEndPoint(socket),
                    e.LastOperation
                    );
                taskCompletionSource.SetException(new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Unknown socket operation : {0}", e.LastOperation)));
                break;
            }
            }
        }
 /// <summary>
 ///		Returns specified <see cref="ClientTransport"/> to the internal pool.
 /// </summary>
 /// <param name="transport">The <see cref="ClientTransport"/> to be returned.</param>
 internal sealed override void ReturnTransport(ClientTransport transport)
 {
     ReturnTransport((TTransport)transport);
 }
Example #7
0
 /// <summary>
 ///		Returns specified <see cref="ClientTransport"/> to the internal pool.
 /// </summary>
 /// <param name="transport">The <see cref="ClientTransport"/> to be returned.</param>
 internal abstract void ReturnTransport(ClientTransport transport);