示例#1
0
 public void Close()
 {
     if (_socket == null)
     {
         return;
     }
     _active = false;
     try
     {
         _socket.Shutdown(SocketShutdown.Both);
     }
     catch
     {
     }
     finally
     {
         _socket.Close();
         _socket   = null;
         _receiver = null;
         if (ClosedHandler != null)
         {
             ClosedHandler(this);
         }
     }
 }
示例#2
0
 public Session(string name)
 {
     _name              = name ?? string.Empty;
     Type               = SessionType.Unknown;
     _socket            = null;
     _receiver          = null;
     Active             = false;
     _disposed          = false;
     ConnectedHandler   = null;
     ClosedHandler      = null;
     ErrorHandler       = null;
     CustomErrorHandler = null;
     ReceiveHandler     = null;
     SendHandler        = null;
 }
示例#3
0
        private void Initialize(AddressFamily addressFamily)
        {
            if (_socket != null)
            {
                Close();
                _socket = null;
            }
            switch (addressFamily)
            {
            case AddressFamily.InterNetwork:
                Type = SessionType.IPv4;
                break;

            case AddressFamily.InterNetworkV6:
                Type = SessionType.IPv6;
                break;

            default:
                throw new Exception($"Not supported address family '{addressFamily.ToString()}'.");
            }
            _socket   = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
            _receiver = new SessionReceiver(_defaultMaxPacketLength);
            _receiver.Reset(_defaultPacketLength);
        }