Esempio n. 1
0
        /// <summary>
        /// Called before the call to select().
        /// </summary>
        public void BeforeSelect(SelectSockets selectSockets)
        {
            try
            {
                if (Status == EAnpThreadChannelStatus.Initial) BeforeSelectInitial(selectSockets);
                else if (Status == EAnpThreadChannelStatus.Connecting) BeforeSelectConnecting(selectSockets);
                else if (Status == EAnpThreadChannelStatus.Handshake) BeforeSelectHandshake(selectSockets);
                else if (Status == EAnpThreadChannelStatus.Open) BeforeSelectOpen(selectSockets);
            }

            catch (Exception ex)
            {
                HandleError(ex);
                selectSockets.LowerTimeout(0);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Try to connect to the KWM.
        /// </summary>
        public override void BeforeSelectInitial(SelectSockets selectSockets)
        {
            // Get the connection info.
            ClientThread.GetConnectInfo();

            // Try to connect.
            Sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Sock.Blocking = false;

            try
            {
                Sock.Connect(new IPEndPoint(IPAddress.Loopback, ClientThread.Port));
            }

            catch (SocketException ex)
            {
                // This exception will always happen after we try async
                // connect. Awesome API there.
                if (ex.SocketErrorCode != SocketError.WouldBlock) throw;
            }

            // We're now connecting.
            Status = EAnpThreadChannelStatus.Connecting;
            selectSockets.LowerTimeout(0);
        }
Esempio n. 3
0
 public override void BeforeSelectHandshake(SelectSockets selectSockets)
 {
     double timeout = GetHandshakeTimeout();
     if (timeout <= 0) throw new Exception("timeout waiting for client authentication data");
     selectSockets.AddRead(Sock);
     selectSockets.LowerTimeout((int)timeout);
 }
Esempio n. 4
0
        /// <summary>
        /// Check if we are connected.
        /// </summary>
        public override void BeforeSelectConnecting(SelectSockets selectSockets)
        {
            // The socket is connected.
            if (Sock.Poll(0, SelectMode.SelectWrite))
            {
                Status = EAnpThreadChannelStatus.Handshake;
                selectSockets.LowerTimeout(0);
            }

            // The connection has failed.
            else if (Sock.Poll(0, SelectMode.SelectError))
            {
                throw new Exception("the connection to the KWM could not be established");
            }

            // Add the socket in the write set and wait forever.
            else
            {
                selectSockets.AddWrite(Sock);
            }
        }