Ejemplo n.º 1
0
        /// <inheritdoc />
        protected override bool SynchronizedRead(ISocket socket, byte[] buffer, out EndPointDisconnectReason error)
        {
            var index = 0;
            int toRead;

            while ((toRead = buffer.Length - index) > 0)
            {
                SocketError socketError;
                var         bytesRead = socket.Receive(buffer, index, toRead, SocketFlags.None, out socketError);
                index += bytesRead;

                if (socketError != SocketError.Success ||
                    bytesRead <= 0 ||
                    !socket.Connected)
                {
                    Log.DebugFormat("{0}: Error while reading from socket, {1} out of {2} bytes read, method {3}, IsConnected: {4}",
                                    Name,
                                    bytesRead,
                                    buffer.Length, socketError, socket.Connected);
                    error = ToDisconnectReason(socketError, bytesRead: bytesRead);
                    return(false);
                }
            }

            error = EndPointDisconnectReason.Unknown;
            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Is called when the endpoint reports a failure.
        /// </summary>
        private void EndPointOnOnFailure(EndPointDisconnectReason endPointReason, ConnectionId connectionId)
        {
            lock (_syncRoot)
            {
                // If we're disposing this silo (or have disposed it alrady), then the heartbeat monitor
                // reported a failure that we caused intentionally (by killing the host process) and thus
                // this "failure" musn't be reported.
                if (_isDisposed)
                {
                    return;
                }
            }

            // The socket will have logged all this information already thus we can skip it here
            Log.DebugFormat("SocketEndPoint detected a failure of the connection to the host process: {0}", endPointReason);

            Failure failure;

            switch (endPointReason)
            {
            case EndPointDisconnectReason.ReadFailure:
            case EndPointDisconnectReason.RpcInvalidResponse:
            case EndPointDisconnectReason.WriteFailure:
            case EndPointDisconnectReason.ConnectionAborted:
            case EndPointDisconnectReason.ConnectionReset:
            case EndPointDisconnectReason.ConnectionTimedOut:
                failure = Failure.ConnectionFailure;
                break;

            case EndPointDisconnectReason.RequestedByEndPoint:
            case EndPointDisconnectReason.RequestedByRemotEndPoint:
                failure = Failure.ConnectionClosed;
                break;

            case EndPointDisconnectReason.HeartbeatFailure:
                failure = Failure.HeartbeatFailure;
                break;

            case EndPointDisconnectReason.UnhandledException:
                failure = Failure.UnhandledException;
                break;

            default:
                Log.WarnFormat("Unknown EndPointDisconnectReason: {0}", endPointReason);
                failure = Failure.Unknown;
                break;
            }

            Operation op = Operation.HandleFailure(failure, connectionId);

            _actions.Enqueue(op);
        }
		protected override bool SynchronizedWrite(TTransport socket, byte[] data, int length, out EndPointDisconnectReason error)
		{
			try
			{
				socket.Write(data, 0, length);
				error = EndPointDisconnectReason.Unknown;
				return true;
			}
			catch (Exception)
			{
				error = EndPointDisconnectReason.Unknown;
				return false;
			}
		}
		protected override bool SynchronizedRead(TTransport socket, byte[] buffer, TimeSpan timeout, out EndPointDisconnectReason error)
		{
			int read = socket.Read(buffer, 0, buffer.Length);
			if (read != buffer.Length)
			{
				error = EndPointDisconnectReason.ConnectionAborted;
				return false;
			}

			error = EndPointDisconnectReason.Unknown;
			return true;
		}
		protected override bool SynchronizedRead(TTransport socket, byte[] buffer, out EndPointDisconnectReason error)
		{
			return SynchronizedRead(socket, buffer, TimeSpan.MaxValue, out error);
		}
Ejemplo n.º 6
0
 protected override bool SynchronizedRead(IDisposable socket, byte[] buffer, out EndPointDisconnectReason error)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 7
0
 protected override bool SynchronizedWrite(IDisposable socket, byte[] data, int length, out EndPointDisconnectReason error)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 8
0
        /// <inheritdoc />
        protected override bool SynchronizedRead(ISocket socket, byte[] buffer, TimeSpan timeout, out EndPointDisconnectReason error)
        {
            var start = DateTime.Now;

            while (socket.Available < buffer.Length)
            {
                if (!socket.Connected)
                {
                    error = EndPointDisconnectReason.Unknown;
                    Log.DebugFormat("{0}: Error while reading from socket, {1} out of {2} read, method {3}, IsConnected: {4}",
                                    Name,
                                    0,
                                    buffer.Length, error, socket.Connected);
                    return(false);
                }

                var remaining = timeout - (DateTime.Now - start);
                if (remaining <= TimeSpan.Zero)
                {
                    error = EndPointDisconnectReason.ConnectionTimedOut;
                    Log.DebugFormat("{0}: Error while reading from socket, {1} out of {2} read, method {3}, IsConnected: {4}",
                                    Name,
                                    0,
                                    buffer.Length, error, socket.Connected);
                    return(false);
                }

                var t = (int)(remaining.TotalMilliseconds * 1000);
                if (!socket.Poll(t, SelectMode.SelectRead))
                {
                    error = EndPointDisconnectReason.ConnectionTimedOut;
                    Log.DebugFormat("{0}: Error while reading from socket, {1} out of {2} read, method {3}, IsConnected: {4}",
                                    Name,
                                    0,
                                    buffer.Length, error, socket.Connected);
                    return(false);
                }
            }

            return(SynchronizedRead(socket, buffer, out error));
        }
Ejemplo n.º 9
0
        /// <inheritdoc />
        protected override bool SynchronizedWrite(ISocket socket, byte[] data, int length, out EndPointDisconnectReason error)
        {
            if (!socket.Connected)
            {
                error = EndPointDisconnectReason.Unknown;
                return(false);
            }

            SocketError socketError;
            var         written = socket.Send(data, offset: 0, size: length, socketFlags: SocketFlags.None, errorCode: out socketError);

            if (written != length || socketError != SocketError.Success || !socket.Connected)
            {
                Log.DebugFormat("{0}: Error while writing to socket, {1} out of {2} written, method {3}, IsConnected: {4}",
                                Name,
                                written,
                                data.Length, socketError, socket.Connected);

                error = ToDisconnectReason(socketError);
                return(false);
            }

            error = EndPointDisconnectReason.Unknown;
            return(true);
        }
Ejemplo n.º 10
0
 private void EndPointOnOnFailure(EndPointDisconnectReason endPointDisconnectReason, ConnectionId id)
 {
     OnFailure?.Invoke(endPointDisconnectReason, id);
 }