public void ConnectToServer(IPEndPoint ipEndPoint)
        {
            Socket socket  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            var    awaiter = HandlerSaeaPool.Take();

            awaiter.Saea.RemoteEndPoint = ipEndPoint;

            SaeaExHelper.ConnectAsync(socket, awaiter, (a, e) =>
            {
                HandlerSaeaPool.Return(a);
                var session = SessionPool.Take();
                if (e != SocketError.Success)
                {
                    LogHelper.WriteLog("client_connect-error");
                    CompletetionNotify?.Invoke(TcpSocketCompletionNotify.OnClosed, session);
                    SessionPool.Return(session);
                    return;
                }
                TcpSocketSaeaSessions.Add(session);

                session.Attach(socket);
                CompletetionNotify?.Invoke(TcpSocketCompletionNotify.OnConnected, session);
                session.StartProcess();
            });
        }
Example #2
0
        private async Task Process(Socket acceptedSocket)
        {
            var session = _sessionPool.Take();

            session.Attach(acceptedSocket);

            if (_sessions.TryAdd(session.SessionKey, session))
            {
//                _log.DebugFormat("New session [{0}].", session);
                try
                {
                    await session.Start();
                }
                finally
                {
                    TcpSocketSaeaSession recycle;
                    if (_sessions.TryRemove(session.SessionKey, out recycle))
                    {
//                        _log.DebugFormat("Close session [{0}].", recycle);
                    }
                }
            }

            _sessionPool.Return(session);
        }
Example #3
0
        private void Accept(SaeaAwaiter awaiter, SocketError socketError)
        {
            if (socketError == SocketError.Success)
            {
                var acceptedSocket = awaiter.Saea.AcceptSocket;
                var session        = SessionPool.Take();
                session.Attach(acceptedSocket);
                this.TcpSocketSaeaSessions.Add(session);
                this.CompletetionNotify?.Invoke(TcpSocketCompletionNotify.OnConnected, session);

                session.StartProcess();
            }
            else
            {
                LogHelper.WriteLog("server_accept-fail");
            }

            if (!this._isRuning)
            {
                return;
            }

            awaiter.Saea.AcceptSocket = null;
            SaeaExHelper.AcceptAsync(_listener, awaiter, Accept);
        }