Exemplo n.º 1
0
        public TcpConnection(Socket socket, Action <ITcpConnection, byte[]> messageArrivedHandler, Action <ITcpConnection, SocketError> connectionClosedHandler)
        {
            Ensure.NotNull(socket, "socket");
            Ensure.NotNull(messageArrivedHandler, "messageArrivedHandler");
            Ensure.NotNull(connectionClosedHandler, "connectionClosedHandler");

            _socket                  = socket;
            _localEndPoint           = socket.LocalEndPoint;
            _remotingEndPoint        = socket.RemoteEndPoint;
            _messageArrivedHandler   = messageArrivedHandler;
            _connectionClosedHandler = connectionClosedHandler;

            _sendSocketArgs = new SocketAsyncEventArgs();
            _sendSocketArgs.AcceptSocket = socket;
            _sendSocketArgs.Completed   += OnSendAsyncCompleted;

            _receiveSocketArgs = new SocketAsyncEventArgs();
            _receiveSocketArgs.AcceptSocket = socket;
            _receiveSocketArgs.Completed   += OnReceiveAsyncCompleted;

            _logger = ObjectContainer.Resolve <ILoggerFactory>().Create(GetType().FullName);

            _framer = new LengthPrefixMessageFramer();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            TryReceive();
            TrySend();
        }
Exemplo n.º 2
0
        public TcpConnectionManager(string connectionName,
                                    ITcpDispatcher dispatcher,
                                    IPublisher publisher,
                                    IPEndPoint remoteEndPoint,
                                    TcpClientConnector connector)
        {
            Ensure.NotNull(dispatcher, "dispatcher");
            Ensure.NotNull(publisher, "publisher");
            Ensure.NotNull(remoteEndPoint, "remoteEndPoint");
            Ensure.NotNull(connector, "connector");

            _connectionName = connectionName;

            _tcpEnvelope = new SendOverTcpEnvelope(this);
            _publisher   = publisher;
            _dispatcher  = dispatcher;

            EndPoint = remoteEndPoint;

            _framer = new LengthPrefixMessageFramer();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            _connection = connector.ConnectTo(remoteEndPoint, OnConnectionEstablished, OnConnectionFailed);
            _connection.ConnectionClosed += OnConnectionClosed;
        }
Exemplo n.º 3
0
        public TcpConnection(Socket socket, SocketSetting setting, IBufferPool receiveDataBufferPool, Action <ITcpConnection, byte[]> messageArrivedHandler, Action <ITcpConnection, SocketError> connectionClosedHandler)
        {
            Ensure.NotNull(socket, "socket");
            Ensure.NotNull(setting, "setting");
            Ensure.NotNull(receiveDataBufferPool, "receiveDataBufferPool");
            Ensure.NotNull(messageArrivedHandler, "messageArrivedHandler");
            Ensure.NotNull(connectionClosedHandler, "connectionClosedHandler");

            _id      = Guid.NewGuid();
            _socket  = socket;
            _setting = setting;
            _receiveDataBufferPool   = receiveDataBufferPool;
            _localEndPoint           = socket.LocalEndPoint;
            _remotingEndPoint        = socket.RemoteEndPoint;
            _messageArrivedHandler   = messageArrivedHandler;
            _connectionClosedHandler = connectionClosedHandler;

            _sendSocketArgs = new SocketAsyncEventArgs();
            _sendSocketArgs.AcceptSocket = socket;
            _sendSocketArgs.Completed   += OnSendAsyncCompleted;

            _receiveSocketArgs = new SocketAsyncEventArgs();
            _receiveSocketArgs.AcceptSocket = socket;
            _receiveSocketArgs.Completed   += OnReceiveAsyncCompleted;
            _receiveSocketArgs.UserToken    = new ConcurrentQueue <ReceivedData>();

            _framer = DependencyManage.Resolve <IMessageFramer>();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            TryReceive();
            TrySend();
        }
Exemplo n.º 4
0
        public TcpConnection(Socket socket, SocketSetting setting, IBufferPool receiveDataBufferPool, Action <ITcpConnection, byte[]> messageArrivedHandler, Action <ITcpConnection, SocketError> connectionClosedHandler)
        {
            socket.CheckNotNull("socket");
            setting.CheckNotNull("setting");
            receiveDataBufferPool.CheckNotNull("receiveDataBufferPool");
            messageArrivedHandler.CheckNotNull("messageArrivedHandler");
            connectionClosedHandler.CheckNotNull("connectionClosedHandler");

            _socket  = socket;
            _setting = setting;
            _receiveDataBufferPool   = receiveDataBufferPool;
            _localEndPoint           = socket.LocalEndPoint;
            _remotingEndPoint        = socket.RemoteEndPoint;
            _messageArrivedHandler   = messageArrivedHandler;
            _connectionClosedHandler = connectionClosedHandler;

            _sendSocketArgs = new SocketAsyncEventArgs();
            _sendSocketArgs.AcceptSocket = _socket;
            _sendSocketArgs.Completed   += OnSendAsyncCompleted;

            _receiveSocketArgs = new SocketAsyncEventArgs();
            _receiveSocketArgs.AcceptSocket = socket;
            _receiveSocketArgs.Completed   += OnReceiveAsyncCompleted;

            _logger = IocManager.Instance.Resolve <ILoggerFactory>().Create(GetType().FullName);
            _framer = IocManager.Instance.Resolve <IMessageFramer>();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            TryReceive();
            TrySend();
        }
Exemplo n.º 5
0
        public TcpConnectionManager(string connectionName,
                                    ITcpConnection openedConnection,
                                    IMessageFramer framer,
                                    Action <TcpConnectionManager, byte[]> messageReceived,
                                    Action <TcpConnectionManager, SocketError> onConnectionClosed)
        {
            Ensure.NotNull(openedConnection, nameof(openedConnection));
            Ensure.NotNull(framer, nameof(framer));

            ConnectionName = connectionName;
            ConnectionId   = openedConnection.ConnectionId;

            _framer = framer;
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            _messageReceived  = messageReceived;
            _connectionClosed = onConnectionClosed;

            RemoteEndPoint = openedConnection.RemoteEndPoint;
            _tcpConnection = openedConnection;
            _tcpConnection.ConnectionClosed += OnConnectionClosed;
            if (_tcpConnection.IsClosed)
            {
                OnConnectionClosed(_tcpConnection, SocketError.Success);
                return;
            }
        }
Exemplo n.º 6
0
        public TcpConnectionManager(string connectionName,
                                    Guid connectionId,
                                    ITcpDispatcher dispatcher,
                                    IPublisher publisher,
                                    TcpConnection openedConnection)
        {
            Ensure.NotEmptyGuid(connectionId, "connectionId");
            Ensure.NotNull(dispatcher, "dispatcher");
            Ensure.NotNull(publisher, "publisher");
            Ensure.NotNull(openedConnection, "openedConnnection");

            _connectionName = connectionName;
            _connectionId   = connectionId;

            _tcpEnvelope = new SendOverTcpEnvelope(this);
            _publisher   = publisher;
            _dispatcher  = dispatcher;

            EndPoint = openedConnection.EffectiveEndPoint;

            _framer = new LengthPrefixMessageFramer();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            _connection = openedConnection;
            _connection.ConnectionClosed += OnConnectionClosed;
            ScheduleHeartbeat(0);
        }
Exemplo n.º 7
0
        public TcpConnection(Socket socket, SocketSetting setting, IBufferPool receiveDataBufferPool, Action <ITcpConnection, byte[]> messageArrivedHandler, Action <ITcpConnection, SocketError> connectionClosedHandler)
        {
            Ensure.NotNull(socket, "socket");
            Ensure.NotNull(setting, "setting");
            Ensure.NotNull(receiveDataBufferPool, "receiveDataBufferPool");
            Ensure.NotNull(messageArrivedHandler, "messageArrivedHandler");
            Ensure.NotNull(connectionClosedHandler, "connectionClosedHandler");

            _id      = Guid.NewGuid();
            _socket  = socket;
            _setting = setting;
            _receiveDataBufferPool   = receiveDataBufferPool;
            _localEndPoint           = socket.LocalEndPoint;
            _remotingEndPoint        = socket.RemoteEndPoint;
            _messageArrivedHandler   = messageArrivedHandler;
            _connectionClosedHandler = connectionClosedHandler;

            _sendSocketArgs = new SocketAsyncEventArgs();
            _sendSocketArgs.AcceptSocket = socket;
            _sendSocketArgs.Completed   += OnSendAsyncCompleted;

            _receiveSocketArgs = new SocketAsyncEventArgs();
            _receiveSocketArgs.AcceptSocket = socket;
            _receiveSocketArgs.Completed   += OnReceiveAsyncCompleted;

            _logger = ObjectContainer.Resolve <ILoggerFactory>().Create(GetType().FullName);
            _framer = ObjectContainer.Resolve <IMessageFramer>();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            TryReceive();
            TrySend();
        }
Exemplo n.º 8
0
        public TcpConnectionManager(string connectionName,
                                    Guid connectionId,
                                    ITcpDispatcher dispatcher,
                                    IPublisher publisher,
                                    IPEndPoint remoteEndPoint,
                                    TcpClientConnector connector,
                                    bool useSsl,
                                    string sslTargetHost,
                                    bool sslValidateServer,
                                    IPublisher networkSendQueue,
                                    IAuthenticationProvider authProvider,
                                    TimeSpan heartbeatInterval,
                                    TimeSpan heartbeatTimeout,
                                    Action <TcpConnectionManager> onConnectionEstablished,
                                    Action <TcpConnectionManager, SocketError> onConnectionClosed)
        {
            Ensure.NotEmptyGuid(connectionId, "connectionId");
            Ensure.NotNull(dispatcher, "dispatcher");
            Ensure.NotNull(publisher, "publisher");
            Ensure.NotNull(authProvider, "authProvider");
            Ensure.NotNull(remoteEndPoint, "remoteEndPoint");
            Ensure.NotNull(connector, "connector");
            if (useSsl)
            {
                Ensure.NotNull(sslTargetHost, "sslTargetHost");
            }

            ConnectionId   = connectionId;
            ConnectionName = connectionName;

            _tcpEnvelope  = new SendOverTcpEnvelope(this, networkSendQueue);
            _publisher    = publisher;
            _dispatcher   = dispatcher;
            _authProvider = authProvider;

            _framer = new LengthPrefixMessageFramer();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            _weakThisEnvelope  = new SendToWeakThisEnvelope(this);
            _heartbeatInterval = heartbeatInterval;
            _heartbeatTimeout  = heartbeatTimeout;
            _connectionPendingSendBytesThreshold = ESConsts.UnrestrictedPendingSendBytes;
            _connectionQueueSizeThreshold        = ESConsts.MaxConnectionQueueSize;

            _connectionEstablished = onConnectionEstablished;
            _connectionClosed      = onConnectionClosed;

            RemoteEndPoint = remoteEndPoint;
            _connection    = useSsl
                                ? connector.ConnectSslTo(ConnectionId, remoteEndPoint, ConnectionTimeout,
                                                         sslTargetHost, sslValidateServer, OnConnectionEstablished, OnConnectionFailed)
                                : connector.ConnectTo(ConnectionId, remoteEndPoint, ConnectionTimeout, OnConnectionEstablished,
                                                      OnConnectionFailed);
            _connection.ConnectionClosed += OnConnectionClosed;
            if (_connection.IsClosed)
            {
                OnConnectionClosed(_connection, SocketError.Success);
            }
        }
        public TcpConnectionManager(string connectionName,
                                    Guid connectionId,
                                    ITcpDispatcher dispatcher,
                                    IPublisher publisher,
                                    string targetHost,
                                    EndPoint remoteEndPoint,
                                    TcpClientConnector connector,
                                    bool useSsl,
                                    Func <X509Certificate, X509Chain, SslPolicyErrors, ValueTuple <bool, string> > sslServerCertValidator,
                                    Func <X509CertificateCollection> sslClientCertificatesSelector,
                                    IPublisher networkSendQueue,
                                    IAuthenticationProvider authProvider,
                                    AuthorizationGateway authorization,
                                    TimeSpan heartbeatInterval,
                                    TimeSpan heartbeatTimeout,
                                    Action <TcpConnectionManager> onConnectionEstablished,
                                    Action <TcpConnectionManager, SocketError> onConnectionClosed)
        {
            Ensure.NotEmptyGuid(connectionId, "connectionId");
            Ensure.NotNull(dispatcher, "dispatcher");
            Ensure.NotNull(publisher, "publisher");
            Ensure.NotNull(authProvider, "authProvider");
            Ensure.NotNull(authorization, "authorization");
            Ensure.NotNull(remoteEndPoint, "remoteEndPoint");
            Ensure.NotNull(connector, "connector");

            ConnectionId   = connectionId;
            ConnectionName = connectionName;

            _tcpEnvelope   = new SendOverTcpEnvelope(this, networkSendQueue);
            _publisher     = publisher;
            _dispatcher    = dispatcher;
            _authProvider  = authProvider;
            _authorization = authorization;

            _framer = new LengthPrefixMessageFramer();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            _weakThisEnvelope  = new SendToWeakThisEnvelope(this);
            _heartbeatInterval = heartbeatInterval;
            _heartbeatTimeout  = heartbeatTimeout;
            _connectionPendingSendBytesThreshold = ESConsts.UnrestrictedPendingSendBytes;
            _connectionQueueSizeThreshold        = ESConsts.MaxConnectionQueueSize;

            _connectionEstablished = onConnectionEstablished;
            _connectionClosed      = onConnectionClosed;

            RemoteEndPoint = remoteEndPoint;
            _connection    = useSsl
                                ? connector.ConnectSslTo(ConnectionId, targetHost, remoteEndPoint.ResolveDnsToIPAddress(), ConnectionTimeout,
                                                         sslServerCertValidator, sslClientCertificatesSelector, OnConnectionEstablished, OnConnectionFailed)
                                : connector.ConnectTo(ConnectionId, remoteEndPoint.ResolveDnsToIPAddress(), ConnectionTimeout, OnConnectionEstablished,
                                                      OnConnectionFailed);
            _connection.ConnectionClosed += OnConnectionClosed;
            if (_connection.IsClosed)
            {
                OnConnectionClosed(_connection, SocketError.Success);
            }
        }
Exemplo n.º 10
0
 public TcpClient(IPEndPoint serverEndPoint, Action <byte[]> replyHandler, ISocketClientEventListener eventListener = null)
 {
     Ensure.NotNull(serverEndPoint, "serverEndPoint");
     Ensure.NotNull(replyHandler, "replyHandler");
     _serverEndPoint = serverEndPoint;
     _replyHandler   = replyHandler;
     _eventListener  = eventListener;
     _framer         = new LengthPrefixMessageFramer();
     _framer.RegisterMessageArrivedCallback(OnMessageArrived);
     _logger          = ObjectContainer.Resolve <ILoggerFactory>().Create(GetType().FullName);
     _startWaitHandle = new ManualResetEvent(false);
 }
Exemplo n.º 11
0
        public void SendMessages(int messageSize, int messageCount)
        {
            var data = new byte[messageSize];
            _framer = new LengthPrefixMessageFramer();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);
            var segment = _framer.FrameData(new ArraySegment<byte>(data, 0, data.Length));

            for (var i = 0; i < messageCount; i++)
            {
                _connection.EnqueueSend(segment);
            }
        }
        public TcpConnectionManager(string connectionName,
                                    TcpServiceType serviceType,
                                    ITcpDispatcher dispatcher,
                                    IPublisher publisher,
                                    ITcpConnection openedConnection,
                                    IPublisher networkSendQueue,
                                    IAuthenticationProvider authProvider,
                                    AuthorizationGateway authorization,
                                    TimeSpan heartbeatInterval,
                                    TimeSpan heartbeatTimeout,
                                    Action <TcpConnectionManager, SocketError> onConnectionClosed,
                                    int connectionPendingSendBytesThreshold,
                                    int connectionQueueSizeThreshold)
        {
            Ensure.NotNull(dispatcher, "dispatcher");
            Ensure.NotNull(publisher, "publisher");
            Ensure.NotNull(openedConnection, "openedConnnection");
            Ensure.NotNull(networkSendQueue, "networkSendQueue");
            Ensure.NotNull(authProvider, "authProvider");
            Ensure.NotNull(authorization, "authorization");
            ConnectionId   = openedConnection.ConnectionId;
            ConnectionName = connectionName;

            _serviceType   = serviceType;
            _tcpEnvelope   = new SendOverTcpEnvelope(this, networkSendQueue);
            _publisher     = publisher;
            _dispatcher    = dispatcher;
            _authProvider  = authProvider;
            _authorization = authorization;

            _framer = new LengthPrefixMessageFramer();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            _weakThisEnvelope  = new SendToWeakThisEnvelope(this);
            _heartbeatInterval = heartbeatInterval;
            _heartbeatTimeout  = heartbeatTimeout;
            _connectionPendingSendBytesThreshold = connectionPendingSendBytesThreshold;
            _connectionQueueSizeThreshold        = connectionQueueSizeThreshold;

            _connectionClosed = onConnectionClosed;

            RemoteEndPoint = openedConnection.RemoteEndPoint;
            _connection    = openedConnection;
            _connection.ConnectionClosed += OnConnectionClosed;
            if (_connection.IsClosed)
            {
                OnConnectionClosed(_connection, SocketError.Success);
                return;
            }

            ScheduleHeartbeat(0);
        }
Exemplo n.º 13
0
        public TcpTypedConnection(ITcpConnection connection,
                                  IMessageFormatter <T> formatter,
                                  IMessageFramer framer)
        {
            _connection = connection;
            _formatter  = formatter ?? throw new ArgumentNullException(nameof(formatter));
            _framer     = framer ?? throw new ArgumentNullException(nameof(framer));

            connection.ConnectionClosed += OnConnectionClosed;

            //Setup callback for incoming messages
            framer.RegisterMessageArrivedCallback(IncomingMessageArrived);
        }
Exemplo n.º 14
0
        public AcceptedConnection(ITcpConnection connection)
        {
            _framer = new LengthPrefixMessageFramer();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);
            _connection = connection;

            _connection.ConnectionClosed += OnConnectionClosed;
            if (_connection.IsClosed)
            {
                OnConnectionClosed(_connection, SocketError.Success);
                return;
            }
        }
Exemplo n.º 15
0
 public TcpClient(IPEndPoint localEndPoint, IPEndPoint serverEndPoint, Action<byte[]> replyHandler, ISocketClientEventListener eventListener = null)
 {
     Ensure.NotNull(serverEndPoint, "serverEndPoint");
     Ensure.NotNull(replyHandler, "replyHandler");
     _localEndPoint = localEndPoint;
     _serverEndPoint = serverEndPoint;
     _replyHandler = replyHandler;
     _eventListener = eventListener;
     _framer = new LengthPrefixMessageFramer();
     _framer.RegisterMessageArrivedCallback(OnMessageArrived);
     _waitHandle = new ManualResetEvent(false);
     _logger = ObjectContainer.Resolve<ILoggerFactory>().Create(GetType().FullName);
 }
Exemplo n.º 16
0
            public AcceptedConnection(ITcpConnection connection, ISocketServerEventListener eventListener, Action <ITcpConnection, byte[], Action <byte[]> > messageHandler)
            {
                _messageFramer = new LengthPrefixMessageFramer();
                _messageFramer.RegisterMessageArrivedCallback(OnMessageArrived);
                _connection     = connection;
                _eventListener  = eventListener;
                _messageHandler = messageHandler;
                _logger         = ObjectContainer.Resolve <ILoggerFactory>().Create(typeof(TcpServerListener).FullName);

                _connection.ConnectionClosed += OnConnectionClosed;
                if (_connection.IsClosed)
                {
                    OnConnectionClosed(_connection, SocketError.Success);
                    return;
                }
            }
Exemplo n.º 17
0
        public TcpConnectionManager(string connectionName,
                                    Guid connectionId,
                                    IPEndPoint remoteEndPoint,
                                    ITcpConnector connector,
                                    bool useSsl,
                                    string sslTargetHost,
                                    bool sslValidateServer,
                                    IMessageFramer framer,
                                    Action <TcpConnectionManager, byte[]> messageReceived,
                                    Action <TcpConnectionManager> onConnectionEstablished,
                                    Action <TcpConnectionManager, SocketError> onConnectionClosed)
        {
            Ensure.NotEmptyGuid(connectionId, "connectionId");
            Ensure.NotNull(remoteEndPoint, "remoteEndPoint");
            Ensure.NotNull(connector, "connector");
            Ensure.NotNull(framer, nameof(framer));
            if (useSsl)
            {
                Ensure.NotNull(sslTargetHost, "sslTargetHost");
            }

            ConnectionName = connectionName;
            ConnectionId   = connectionId;

            _framer = framer;
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            _messageReceived       = messageReceived;
            _connectionEstablished = onConnectionEstablished;
            _connectionClosed      = onConnectionClosed;

            RemoteEndPoint = remoteEndPoint;

            _tcpConnection = useSsl
                ? connector.ConnectSslTo(ConnectionId, remoteEndPoint, ConnectionTimeout, sslTargetHost, sslValidateServer, OnConnectionEstablished, OnConnectionFailed, true)
                : connector.ConnectTo(ConnectionId, remoteEndPoint, ConnectionTimeout, OnConnectionEstablished, OnConnectionFailed, true);

            _tcpConnection.ConnectionClosed += OnConnectionClosed;
            if (_tcpConnection.IsClosed)
            {
                OnConnectionClosed(_tcpConnection, SocketError.Success);
                return;
            }
        }
Exemplo n.º 18
0
        public TcpConnection(string name, Socket socket, FregataOptions fregataOptions, IBufferPool receivedBufferPool, IBufferPool sendBufferPool, ITcpConnectionHandler tcpConnectionHandler)
        {
            Id                           = SnowflakeId.Default().NextId();
            Name                         = name;
            Socket                       = socket;
            LocalEndPoint                = socket.LocalEndPoint;
            RemotingEndPoint             = socket.RemoteEndPoint;
            Setting                      = fregataOptions;
            _tcpConnectionEventListeners = new List <ITcpConnectionEventListener>();

            _tcpConnectionHandler = tcpConnectionHandler;

            _messageFramer = new LengthPrefixMessageFramer(Setting);
            _messageFramer.RegisterMessageArrivedCallback(OnMessageArrived);
            _receiveBuufferPipeline = new BufferPipeline(bufferPool: receivedBufferPool, littelEndian: fregataOptions.LittleEndian, coding: fregataOptions.Encode, writerFlushCompleted: null);

            _sendBuufferPipeline = new BufferPipeline(bufferPool: sendBufferPool, littelEndian: fregataOptions.LittleEndian, coding: fregataOptions.Encode, writerFlushCompleted: null);
            Task.Run(() => TryReceiveAsync());
        }
Exemplo n.º 19
0
        public TcpTypedConnection(TcpConnection connection,
                                  IMessageFormatter <T> formatter,
                                  IMessageFramer framer)
        {
            if (formatter == null)
            {
                throw new ArgumentNullException("formatter");
            }
            if (framer == null)
            {
                throw new ArgumentNullException("framer");
            }

            _connection       = connection;
            _formatter        = formatter;
            _framer           = framer;
            EffectiveEndPoint = connection.EffectiveEndPoint;

            connection.ConnectionClosed += OnConnectionClosed;

            //Setup callback for incoming messages
            framer.RegisterMessageArrivedCallback(IncomingMessageArrived);
        }
Exemplo n.º 20
0
        public TcpConnection(Socket socket, SocketSetting setting, IBufferPool receiveDataBufferPool, Action<ITcpConnection, byte[]> messageArrivedHandler, Action<ITcpConnection, SocketError> connectionClosedHandler)
        {
            Ensure.NotNull(socket, "socket");
            Ensure.NotNull(setting, "setting");
            Ensure.NotNull(receiveDataBufferPool, "receiveDataBufferPool");
            Ensure.NotNull(messageArrivedHandler, "messageArrivedHandler");
            Ensure.NotNull(connectionClosedHandler, "connectionClosedHandler");

            _socket = socket;
            _setting = setting;
            _flowControlThreshold = _setting.SendMessageFlowControlThreshold;
            _receiveDataBufferPool = receiveDataBufferPool;
            _localEndPoint = socket.LocalEndPoint;
            _remotingEndPoint = socket.RemoteEndPoint;
            _messageArrivedHandler = messageArrivedHandler;
            _connectionClosedHandler = connectionClosedHandler;

            //Initialize send socket async event args.
            for (var i = 0; i < 2; i++)
            {
                var sendSocketArgs = new SocketAsyncEventArgs();
                sendSocketArgs.AcceptSocket = _socket;
                sendSocketArgs.Completed += OnSendAsyncCompleted;
                _sendSocketArgsStack.Push(sendSocketArgs);
            }

            //Initialize receive socket async event args.
            _receiveSocketArgs = new SocketAsyncEventArgs();
            _receiveSocketArgs.AcceptSocket = socket;
            _receiveSocketArgs.Completed += OnReceiveAsyncCompleted;

            _logger = ObjectContainer.Resolve<ILoggerFactory>().Create(GetType().FullName);

            _framer = new LengthPrefixMessageFramer();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            TryReceive();
            TrySend();
        }
Exemplo n.º 21
0
        public TcpConnection(Socket socket, SocketSetting setting, IBufferPool receiveDataBufferPool, Action<ITcpConnection, byte[]> messageArrivedHandler, Action<ITcpConnection, SocketError> connectionClosedHandler)
        {
            Ensure.NotNull(socket, "socket");
            Ensure.NotNull(setting, "setting");
            Ensure.NotNull(receiveDataBufferPool, "receiveDataBufferPool");
            Ensure.NotNull(messageArrivedHandler, "messageArrivedHandler");
            Ensure.NotNull(connectionClosedHandler, "connectionClosedHandler");

            _socket = socket;
            _setting = setting;
            _receiveDataBufferPool = receiveDataBufferPool;
            _localEndPoint = socket.LocalEndPoint;
            _remotingEndPoint = socket.RemoteEndPoint;
            _messageArrivedHandler = messageArrivedHandler;
            _connectionClosedHandler = connectionClosedHandler;

            _sendSocketArgs = new SocketAsyncEventArgs();
            _sendSocketArgs.AcceptSocket = _socket;
            _sendSocketArgs.Completed += OnSendAsyncCompleted;

            _receiveSocketArgs = new SocketAsyncEventArgs();
            _receiveSocketArgs.AcceptSocket = socket;
            _receiveSocketArgs.Completed += OnReceiveAsyncCompleted;

            _logger = ObjectContainer.Resolve<ILoggerFactory>().Create(GetType().FullName);
            _framer = ObjectContainer.Resolve<IMessageFramer>();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            TryReceive();
            TrySend();
        }
Exemplo n.º 22
0
            public AcceptedConnection(ITcpConnection connection, ISocketServerEventListener eventListener, Action<ITcpConnection, byte[], Action<byte[]>> messageHandler)
            {
                _messageFramer = new LengthPrefixMessageFramer();
                _messageFramer.RegisterMessageArrivedCallback(OnMessageArrived);
                _connection = connection;
                _eventListener = eventListener;
                _messageHandler = messageHandler;
                _logger = ObjectContainer.Resolve<ILoggerFactory>().Create(typeof(TcpServerListener).FullName);

                _connection.ConnectionClosed += OnConnectionClosed;
                if (_connection.IsClosed)
                {
                    OnConnectionClosed(_connection, SocketError.Success);
                    return;
                }
            }
Exemplo n.º 23
0
 public ProtobufNetFormatter()
 {
     _framer = new ProtoBufNetMessageFramer();
 }
Exemplo n.º 24
0
        public TcpConnection(Socket socket, IBufferPool bufferPool, Action<ITcpConnection, byte[]> messageArrivedHandler, Action<ITcpConnection, SocketError> connectionClosedHandler)
        {
            Ensure.NotNull(socket, "socket");
            Ensure.NotNull(messageArrivedHandler, "messageArrivedHandler");
            Ensure.NotNull(connectionClosedHandler, "connectionClosedHandler");

            _socket = socket;
            _bufferPool = bufferPool;
            _localEndPoint = socket.LocalEndPoint;
            _remotingEndPoint = socket.RemoteEndPoint;
            _messageArrivedHandler = messageArrivedHandler;
            _connectionClosedHandler = connectionClosedHandler;

            _sendSocketArgsStack = new ConcurrentStack<SocketAsyncEventArgs>();

            for (var i = 0; i < 5; i++)
            {
                var sendSocketArgs = new SocketAsyncEventArgs();
                sendSocketArgs.AcceptSocket = socket;
                sendSocketArgs.Completed += OnSendAsyncCompleted;
                _sendSocketArgsStack.Push(sendSocketArgs);
            }

            _receiveSocketArgs = new SocketAsyncEventArgs();
            _receiveSocketArgs.AcceptSocket = socket;
            _receiveSocketArgs.Completed += OnReceiveAsyncCompleted;

            _logger = ObjectContainer.Resolve<ILoggerFactory>().Create(GetType().FullName);

            _framer = new LengthPrefixMessageFramer();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            _scheduleService.StartTask("PrintStreamBufferLength", () =>
            {
                _logger.InfoFormat("_sendingCount: {0}, _sentCount: {1}", _sendingCount, _sentCount);
            }, 1000, 1000);

            TryReceive();
            TrySend();
        }