Ejemplo n.º 1
0
        protected ICommunicationChannel CreateChannel(EndPoint endPoint)
        {
            ICommunicationChannel communicationChannel = null;

            if (_communicationChannelsPool.Count > 0)
            {
                communicationChannel = _communicationChannelsPool.Pop();

                Interlocked.Increment(ref _connectedSockets);

                Socket socket = CreateSocket();

                SocketAsyncEventArgs socketConnectAsyncEventArgs = new SocketAsyncEventArgs();
                socketConnectAsyncEventArgs.Completed     += Connect_Completed;
                socketConnectAsyncEventArgs.RemoteEndPoint = endPoint;
                socketConnectAsyncEventArgs.UserToken      = communicationChannel;

                bool willRaiseEvent = socket.ConnectAsync(socketConnectAsyncEventArgs);
                if (!willRaiseEvent)
                {
                    try
                    {
                        if (socketConnectAsyncEventArgs.SocketError == SocketError.Success)
                        {
                            communicationChannel.AcceptSocket(socket);
                        }
                        else
                        {
                            _communicationChannelsPool.Push(communicationChannel);
                        }
                    }
                    finally
                    {
                        socketConnectAsyncEventArgs.Completed -= Connect_Completed;
                    }
                }
            }
            else
            {
                _log.Error("No more room for communication channels left");
            }

            return(communicationChannel);
        }
Ejemplo n.º 2
0
        protected void InitializeCommunicationChannel(Socket socket)
        {
            ICommunicationChannel communicationChannel = _communicationChannelsPool.Pop();

            Int32 numberOfConnectedSockets = Interlocked.Increment(ref _connectedSockets);

            _log.Info($"Initializing communication channel for IP {socket.LocalEndPoint}, total concurrent accepted sockets is {numberOfConnectedSockets}");

            try
            {
                communicationChannel.AcceptSocket(socket);
                _clientConnectedList.Add(communicationChannel);
            }
            catch (Exception ex)
            {
                _log.Error($"Failed to accept connection by communication channel", ex);
                ReleaseClientHandler(communicationChannel);
            }
        }
Ejemplo n.º 3
0
        private void Connect_Completed(object sender, SocketAsyncEventArgs e)
        {
            _log.Info($"Connection to remote endpoint {e.RemoteEndPoint} completed");

            if (e.LastOperation == SocketAsyncOperation.Connect)
            {
                ICommunicationChannel communicationChannel = e.UserToken as ICommunicationChannel;

                if (e.SocketError == SocketError.Success)
                {
                    communicationChannel?.AcceptSocket(e.ConnectSocket);
                }
                else
                {
                    _communicationChannelsPool.Push(communicationChannel);
                }
            }
            else
            {
                throw new ArgumentException("The last operation completed on the socket was not a connect.");
            }
        }