示例#1
0
        public void SendTo(string sessionKey, byte[] data, int offset, int count)
        {
            GuardRunning();

            if (string.IsNullOrEmpty(sessionKey))
            {
                throw new ArgumentNullException("sessionKey");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            TcpSocketSession session = null;

            if (_sessions.TryGetValue(sessionKey, out session))
            {
                session.Send(data, offset, count);
            }
            else
            {
                _log.WarnFormat("Cannot find session [{0}].", sessionKey);
            }
        }
 public TcpClientDataReceivedEventArgs(TcpSocketSession session, byte[] data, int dataOffset, int dataLength)
 {
     Session = session;
     Data = data;
     DataOffset = dataOffset;
     DataLength = dataLength;
 }
        public TcpClientConnectedEventArgs(TcpSocketSession session)
        {
            if (session == null)
                throw new ArgumentNullException("session");

            this.Session = session;
        }
示例#4
0
 public TcpClientDataReceivedEventArgs(TcpSocketSession session, byte[] data, int dataOffset, int dataLength)
 {
     Session    = session;
     Data       = data;
     DataOffset = dataOffset;
     DataLength = dataLength;
 }
示例#5
0
        public IAsyncResult BeginSendTo(TcpSocketSession session, byte[] data, int offset, int count, AsyncCallback callback, object state)
        {
            GuardRunning();

            if (session == null)
            {
                throw new ArgumentNullException("session");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            TcpSocketSession writeSession = null;

            if (_sessions.TryGetValue(session.SessionKey, out writeSession))
            {
                return(session.BeginSend(data, offset, count, callback, state));
            }
            else
            {
                _log.WarnFormat("Cannot find session [{0}].", session);
            }

            return(null);
        }
示例#6
0
        public void BeginSendTo(TcpSocketSession session, byte[] data, int offset, int count)
        {
            GuardRunning();

            if (session == null)
            {
                throw new ArgumentNullException("session");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            TcpSocketSession writeSession = null;

            if (_sessions.TryGetValue(session.SessionKey, out writeSession))
            {
                session.BeginSend(data, offset, count);
            }
            else
            {
                _log.WarnFormat("Cannot find session [{0}].", session);
            }
        }
示例#7
0
        public TcpSocketSession GetSession(string sessionKey)
        {
            TcpSocketSession session = null;

            _sessions.TryGetValue(sessionKey, out session);
            return(session);
        }
        public TcpClientDisconnectedEventArgs(TcpSocketSession session)
        {
            if (session == null)
            {
                throw new ArgumentNullException("session");
            }

            this.Session = session;
        }
示例#9
0
        public void CloseSession(string sessionKey)
        {
            TcpSocketSession session = null;

            if (_sessions.TryGetValue(sessionKey, out session))
            {
                session.Close(); // parent server close session by session-key
            }
        }
示例#10
0
        public void CloseSession(string sessionKey)
        {
            TcpSocketSession session = null;

            if (_sessions.TryGetValue(sessionKey, out session))
            {
                session.Close();
            }
        }
示例#11
0
        private void CloseSession(TcpSocketSession session)
        {
            if (session == null)
            {
                return;
            }

            _sessions.TryRemove(session.SessionKey, out var sessionToBeThrowAway);
            session.Close(); // parent server close session
        }
示例#12
0
        private void HandleTcpClientAccepted(IAsyncResult ar)
        {
            if (!_isListening)
            {
                return;
            }

            TcpListener listener = (TcpListener)ar.AsyncState;

            try
            {
                TcpClient tcpClient = listener.EndAcceptTcpClient(ar);
                if (!tcpClient.Connected)
                {
                    return;
                }

                var  session          = new TcpSocketSession(tcpClient, _configuration, _configuration.BufferManager, this);
                bool isSessionStarted = false;
                try
                {
                    _sessions.AddOrUpdate(session.SessionKey, session, (n, o) => { return(o); });
                    session.Start();
                    isSessionStarted = true;
                }
                catch (Exception ex)
                {
                    _log.Error(ex.Message, ex);
                }

                if (!isSessionStarted)
                {
                    CloseSession(session); // session was not started
                }
            }
            catch (Exception ex)
            {
                if (!ShouldThrow(ex))
                {
                    _log.Error(ex.Message, ex);
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                try
                {
                    ContinueAcceptSession(listener);
                }
                catch { }
            }
        }
示例#13
0
        private void CloseSession(TcpSocketSession session)
        {
            TcpSocketSession sessionToBeThrowAway;

            _sessions.TryRemove(session.SessionKey, out sessionToBeThrowAway);

            if (session != null)
            {
                session.Close(); // parent server close session
            }
        }
示例#14
0
 internal void RaiseClientConnected(TcpSocketSession session)
 {
     try
     {
         if (ClientConnected != null)
         {
             ClientConnected(this, new TcpClientConnectedEventArgs(session));
         }
     }
     catch (Exception ex)
     {
         HandleUserSideError(session, ex);
     }
 }
示例#15
0
 internal void RaiseClientDataReceived(TcpSocketSession session, byte[] data, int dataOffset, int dataLength)
 {
     try
     {
         if (ClientDataReceived != null)
         {
             ClientDataReceived(this, new TcpClientDataReceivedEventArgs(session, data, dataOffset, dataLength));
         }
     }
     catch (Exception ex)
     {
         HandleUserSideError(session, ex);
     }
 }
示例#16
0
        public void EndSendTo(TcpSocketSession session, IAsyncResult asyncResult)
        {
            GuardRunning();

            if (session == null)
            {
                throw new ArgumentNullException("session");
            }

            if (_sessions.TryGetValue(session.SessionKey, out var writeSession))
            {
                session.EndSend(asyncResult);
            }
        }
示例#17
0
        public void EndSendTo(string sessionKey, IAsyncResult asyncResult)
        {
            GuardRunning();

            if (string.IsNullOrEmpty(sessionKey))
            {
                throw new ArgumentNullException("sessionKey");
            }

            TcpSocketSession session = null;

            if (_sessions.TryGetValue(sessionKey, out session))
            {
                session.EndSend(asyncResult);
            }
        }
示例#18
0
        public IAsyncResult BeginSendTo(TcpSocketSession session, byte[] data, AsyncCallback callback, object state)
        {
            GuardRunning();

            if (session == null)
            {
                throw new ArgumentNullException("session");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            return(BeginSendTo(session, data, 0, data.Length, callback, state));
        }
示例#19
0
        public void BeginSendTo(TcpSocketSession session, byte[] data)
        {
            GuardRunning();

            if (session == null)
            {
                throw new ArgumentNullException("session");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            BeginSendTo(session, data, 0, data.Length);
        }
示例#20
0
 internal void RaiseClientDisconnected(TcpSocketSession session)
 {
     try
     {
         if (ClientDisconnected != null)
         {
             ClientDisconnected(this, new TcpClientDisconnectedEventArgs(session));
         }
     }
     catch (Exception ex)
     {
         HandleUserSideError(session, ex);
     }
     finally
     {
         TcpSocketSession sessionToBeThrowAway;
         _sessions.TryRemove(session.SessionKey, out sessionToBeThrowAway);
     }
 }
 public TcpClientDataReceivedEventArgs(TcpSocketSession session, byte[] data)
     : this(session, data, 0, data.Length)
 {
 }
示例#22
0
        public void SendToAsync(TcpSocketSession session, byte[] data, int offset, int count)
        {
            GuardRunning();

            if (session == null)
                throw new ArgumentNullException("session");

            if (data == null)
                throw new ArgumentNullException("data");

            TcpSocketSession writeSession = null;
            if (_sessions.TryGetValue(session.SessionKey, out writeSession))
            {
                session.SendAsync(data, offset, count);
            }
            else
            {
                _log.WarnFormat("Cannot find session [{0}].", session);
            }
        }
示例#23
0
        public void SendToAsync(TcpSocketSession session, byte[] data)
        {
            GuardRunning();

            if (session == null)
                throw new ArgumentNullException("session");

            if (data == null)
                throw new ArgumentNullException("data");

            SendToAsync(session, data, 0, data.Length);
        }
示例#24
0
 internal void RaiseClientConnected(TcpSocketSession session)
 {
     try
     {
         if (ClientConnected != null)
         {
             ClientConnected(this, new TcpClientConnectedEventArgs(session));
         }
     }
     catch (Exception ex)
     {
         HandleUserSideError(ex);
     }
 }
示例#25
0
 internal void RaiseClientDisconnected(TcpSocketSession session)
 {
     try
     {
         if (ClientDisconnected != null)
         {
             ClientDisconnected(this, new TcpClientDisconnectedEventArgs(session));
         }
     }
     catch (Exception ex)
     {
         HandleUserSideError(ex);
     }
     finally
     {
         TcpSocketSession sessionToBeThrowAway;
         _sessions.TryRemove(session.SessionKey, out sessionToBeThrowAway);
     }
 }
示例#26
0
 internal void RaiseClientDataReceived(TcpSocketSession session, byte[] data, int dataOffset, int dataLength)
 {
     try
     {
         if (ClientDataReceived != null)
         {
             ClientDataReceived(this, new TcpClientDataReceivedEventArgs(session, data, dataOffset, dataLength));
         }
     }
     catch (Exception ex)
     {
         HandleUserSideError(ex);
     }
 }
示例#27
0
 public TcpClientDataReceivedEventArgs(TcpSocketSession session, byte[] data)
     : this(session, data, 0, data.Length)
 {
 }
示例#28
0
        private void CloseSession(TcpSocketSession session)
        {
            TcpSocketSession sessionToBeThrowAway;
            _sessions.TryRemove(session.SessionKey, out sessionToBeThrowAway);

            if (session != null)
            {
                session.Close();
            }
        }
示例#29
0
 private void HandleUserSideError(TcpSocketSession session, Exception ex)
 {
     _log.Error(string.Format("Session [{0}] error occurred in user side [{1}].", session, ex.Message), ex);
 }
示例#30
0
        private void HandleTcpClientAccepted(IAsyncResult ar)
        {
            if (!_isListening) return;

            try
            {
                TcpListener listener = (TcpListener)ar.AsyncState;

                TcpClient tcpClient = listener.EndAcceptTcpClient(ar);
                if (!tcpClient.Connected) return;

                var session = new TcpSocketSession(tcpClient, _configuration, _bufferManager, this);
                bool isSessionStarted = false;
                try
                {
                    _sessions.AddOrUpdate(session.SessionKey, session, (n, o) => { return o; });
                    session.Start();
                    isSessionStarted = true;
                }
                catch (Exception ex)
                {
                    _log.Error(ex.Message, ex);
                }

                if (isSessionStarted)
                {
                    ContinueAcceptSession(listener);
                }
                else
                {
                    CloseSession(session);
                }
            }
            catch (Exception ex)
            {
                if (!ShouldThrow(ex))
                {
                    _log.Error(ex.Message, ex);
                }
                else throw;
            }
        }