Beispiel #1
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>();

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

            TryReceive();
            TrySend();
        }
Beispiel #2
0
        public ServerSocket(IPEndPoint listeningEndPoint, SocketSetting setting, IBufferPool receiveDataBufferPool, Action <ITcpConnection, byte[], Action <byte[]> > messageArrivedHandler)
        {
            Ensure.NotNull(listeningEndPoint, "listeningEndPoint");
            Ensure.NotNull(setting, "setting");
            Ensure.NotNull(receiveDataBufferPool, "receiveDataBufferPool");
            Ensure.NotNull(messageArrivedHandler, "messageArrivedHandler");

            _listeningEndPoint        = listeningEndPoint;
            _setting                  = setting;
            _receiveDataBufferPool    = receiveDataBufferPool;
            _connectionEventListeners = new List <IConnectionEventListener>();
            _messageArrivedHandler    = messageArrivedHandler;
            _connectionDict           = new ConcurrentDictionary <Guid, ITcpConnection>();
            _socket                      = SocketUtils.CreateSocket(_setting.SendBufferSize, _setting.ReceiveBufferSize);
            _acceptSocketArgs            = new SocketAsyncEventArgs();
            _acceptSocketArgs.Completed += AcceptCompleted;
            _logger                      = null /* ObjectContainer.Resolve<ILoggerFactory>().Create(GetType().FullName)*/;
        }
Beispiel #3
0
        public ClientSocket(EndPoint serverEndPoint, EndPoint localEndPoint, SocketSetting setting, IBufferPool receiveDataBufferPool, Action <ITcpConnection, byte[]> messageArrivedHandler)
        {
            Ensure.NotNull(serverEndPoint, "serverEndPoint");
            Ensure.NotNull(setting, "setting");
            Ensure.NotNull(receiveDataBufferPool, "receiveDataBufferPool");
            Ensure.NotNull(messageArrivedHandler, "messageArrivedHandler");

            _connectionEventListeners = new List <IConnectionEventListener>();

            _serverEndPoint        = serverEndPoint;
            _localEndPoint         = localEndPoint;
            _setting               = setting;
            _receiveDataBufferPool = receiveDataBufferPool;
            _messageArrivedHandler = messageArrivedHandler;
            _waitConnectHandle     = new ManualResetEvent(false);
            _socket = SocketUtils.CreateSocket(_setting.SendBufferSize, _setting.ReceiveBufferSize);
            _logger = null /*ObjectContainer.Resolve<ILoggerFactory>().Create(GetType().FullName)*/;
            _flowControlThreshold = _setting.SendMessageFlowControlThreshold;
        }