private void OnAcceptted(ListeningContext context)
        {
            if (context.AcceptSocket == null || context.AcceptSocket.RemoteEndPoint == null)
            {
                // Canceled due to shutdown.
                return;
            }

#if !API_SIGNATURE_TEST
            MsgPackRpcServerProtocolsTrace.TraceEvent(
                MsgPackRpcServerProtocolsTrace.EndAccept,
                "Accept. {{ \"Socket\" : 0x{0:X}, \"RemoteEndPoint\" : \"{1}\", \"LocalEndPoint\" : \"{2}\" }}",
                ServerTransport.GetHandle(context.AcceptSocket),
                ServerTransport.GetRemoteEndPoint(context.AcceptSocket, context),
                ServerTransport.GetLocalEndPoint(context.AcceptSocket)
                );
#endif

            Contract.Assert(context.BytesTransferred == 0, context.BytesTransferred.ToString());

            var transport = this.GetTransport(context.AcceptSocket);
            context.AcceptSocket = null;
            this.Accept(context);
            transport.Receive(this.GetRequestContext(transport));
        }
        private void OnCompleted(object sender, SocketAsyncEventArgs e)
        {
            var socket = sender as Socket;

            if (!this.HandleSocketError(socket, e))
            {
                return;
            }

            switch (e.LastOperation)
            {
            case SocketAsyncOperation.Accept:
            {
                var context = e as ListeningContext;
                Contract.Assert(context != null);
                this.OnAcceptted(context);
                break;
            }

            default:
            {
#if !API_SIGNATURE_TEST
                MsgPackRpcServerProtocolsTrace.TraceEvent(
                    MsgPackRpcServerProtocolsTrace.UnexpectedLastOperation,
                    "Unexpected operation. {{ \"Socket\" : 0x{0:X}, \"RemoteEndPoint\" : \"{1}\", \"LocalEndPoint\" : \"{2}\", \"LastOperation\" : \"{3}\" }}",
                    ServerTransport.GetHandle(socket),
                    ServerTransport.GetRemoteEndPoint(socket, e),
                    ServerTransport.GetLocalEndPoint(socket),
                    e.LastOperation
                    );
#endif
                break;
            }
            }
        }
        /// <summary>
        ///		Set bound transport to this context.
        /// </summary>
        /// <param name="transport">The transport to be bound.</param>
        internal void SetTransport(ServerTransport transport)
        {
            Contract.Requires(transport != null);

            this.NextProcess = transport.UnpackRequestHeader;
            base.SetTransport(transport);
        }
        /// <summary>
        ///		Handles the socket error as server error.
        /// </summary>
        /// <param name="socket">The <see cref="Socket"/> caused error.</param>
        /// <param name="context">The <see cref="SocketAsyncEventArgs"/> instance containing the asynchronous operation data.</param>
        /// <returns>
        ///		<c>true</c>, if the error can be ignore, it is in shutdown which is initiated by another thread, for example; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///		<paramref name="socket"/> is <c>null</c>.
        ///		Or <paramref name="context"/> is <c>null</c>.
        /// </exception>
        /// <remarks>
        ///		When this method returns <c>false</c>, <see cref="RpcServer.ServerError"/> event will be also ocurred.
        /// </remarks>
        protected internal bool HandleSocketError(Socket socket, SocketAsyncEventArgs context)
        {
            if (socket == null)
            {
                throw new ArgumentNullException("socket");
            }

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            Contract.EndContractBlock();

            bool?isError = context.SocketError.IsError();

            if (isError == null)
            {
                MsgPackRpcServerProtocolsTrace.TraceEvent(
                    MsgPackRpcServerProtocolsTrace.IgnoreableError,
                    "Ignoreable error. {{ \"Socket\" : 0x{0:X}, \"RemoteEndpoint\" : \"{1}\", \"LocalEndpoint\" : \"{2}\", \"LastOperation\" : \"{3}\", \"SocketError\" : \"{4}\", \"ErrorCode\" : 0x{5:X} }}",
                    ServerTransport.GetHandle(socket),
                    ServerTransport.GetRemoteEndPoint(socket, context),
                    ServerTransport.GetLocalEndPoint(socket),
                    context.LastOperation,
                    context.SocketError,
                    ( int )context.SocketError
                    );
                return(true);
            }
            else if (isError.GetValueOrDefault())
            {
                var errorDetail =
                    String.Format(
                        CultureInfo.CurrentCulture,
                        "Socket error. {{ \"Socket\" : 0x{0:X}, \"RemoteEndpoint\" : \"{1}\", \"LocalEndpoint\" : \"{2}\", \"LastOperation\" : \"{3}\", \"SocketError\" : \"{4}\", \"ErrorCode\" : 0x{5:X} }}",
                        ServerTransport.GetHandle(socket),
                        ServerTransport.GetRemoteEndPoint(socket, context),
                        ServerTransport.GetLocalEndPoint(socket),
                        context.LastOperation,
                        context.SocketError,
                        ( int )context.SocketError
                        );
                MsgPackRpcServerProtocolsTrace.TraceEvent(
                    MsgPackRpcServerProtocolsTrace.SocketError,
                    errorDetail
                    );

                this.RaiseServerError(new RpcTransportException(context.SocketError.ToRpcError(), "Socket error.", errorDetail, new SocketException(( int )context.SocketError)));
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ClientShutdownEventArgs"/> class.
        /// </summary>
        /// <param name="transport">The transport which detects client shutdown.</param>
        /// <param name="clientEndPoint">The client <see cref="EndPoint"/>.</param>
        /// <exception cref="ArgumentNullException">
        ///		<paramref name="transport"/> is <c>null</c>.
        /// </exception>
        public ClientShutdownEventArgs(ServerTransport transport, EndPoint clientEndPoint)
        {
            if (transport == null)
            {
                throw new ArgumentNullException("transport");
            }

            Contract.EndContractBlock();

            this._transport      = transport;
            this._clientEndPoint = clientEndPoint;
        }
Exemplo n.º 6
0
		/// <summary>
		/// Initializes a new instance of the <see cref="ClientShutdownEventArgs"/> class.
		/// </summary>
		/// <param name="transport">The transport which detects client shutdown.</param>
		/// <param name="clientEndPoint">The client <see cref="EndPoint"/>.</param>
		/// <exception cref="ArgumentNullException">
		///		<paramref name="transport"/> is <c>null</c>.
		/// </exception>
		public ClientShutdownEventArgs( ServerTransport transport, EndPoint clientEndPoint )
		{
			if ( transport == null )
			{
				throw new ArgumentNullException( "transport" );
			}

			Contract.EndContractBlock();

			this._transport = transport;
			this._clientEndPoint = clientEndPoint;
		}
Exemplo n.º 7
0
 internal override void ReturnTransport(ServerTransport transport)
 {
 }
Exemplo n.º 8
0
			internal override void ReturnTransport( ServerTransport transport )
			{

			}
 internal override void ReturnTransport(ServerTransport transport)
 {
     Contract.Requires(transport != null);
     Contract.Requires(Object.ReferenceEquals(transport.Manager, this));
 }
 /// <summary>
 ///		Invoked from the <see cref="ServerTransport"/> which was created by this manager,
 ///		returns the transport to this manager.
 /// </summary>
 /// <param name="transport">The <see cref="ServerTransport"/> which was created by this manager.</param>
 internal abstract void ReturnTransport(ServerTransport transport);
Exemplo n.º 11
0
 /// <summary>
 ///		Invoked from the <see cref="ServerTransport"/> which was created by this manager,
 ///		returns the transport to this manager.
 /// </summary>
 /// <param name="transport">The <see cref="ServerTransport"/> which was created by this manager.</param>
 internal sealed override void ReturnTransport(ServerTransport transport)
 {
     this.ReturnTransport(( TTransport )transport);
 }
 /// <summary>
 ///		Serializes the specified response data.
 /// </summary>
 /// <typeparam name="T">The type of return value.</typeparam>
 /// <param name="returnValue">The return value.</param>
 /// <param name="error">The error.</param>
 /// <param name="returnValueSerializer">The serializer for the return value.</param>
 internal void Serialize <T>(T returnValue, RpcErrorMessage error, MessagePackSerializer <T> returnValueSerializer)
 {
     ServerTransport.Serialize(this, returnValue, error, returnValueSerializer);
 }