Beispiel #1
0
        /// <summary>
        /// Ends the pending connection request to the server and requests login.
        /// </summary>
        private void OnConnect(IAsyncResult ar)
        {
            try
            {
                Socket socket = (Socket)ar.AsyncState;
                socket.EndConnect(ar);                
                // send keep alive after 10 minute of inactivity
                SockUtils.SetKeepAlive(socket, 600 * 1000, 60 * 1000);  

                // the socket was connected, create the ChatSocket used for communication
                this.sSocket = new SSocket(ref socket);
                this.sSocket.Received += new EventHandler(OnChatSocketReceived);
                this.sSocket.Disconnected += new EventHandler(OnChatSocketDisconnected);

                // set the connected time
                this.dtConnectedOn = DateTime.Now;

                // fire the Connected event
                if (Connected != null)
                    Connected(this, new TimedEventArgs());                

                sSocket.Receive();

                SendRequestLogin();
            }
            catch (Exception exc)
            {
                if (ConnectFailed != null)
                    ConnectFailed(this, new TimedEventArgs());

                if (!SockUtils.HandleSocketError(exc))
                    throw exc;
            }
        }
Beispiel #2
0
        /// <summary>
        /// A ClientManager is responsible for managing a connection from a client.
        /// For each client connected there will be one separate ClientManager object.
        /// The socket used by the ClientManager uses asynchronous call to Receive and 
        /// send data.
        /// </summary>
        public ClientManager(ref Socket clientSocket)
        {
            if (clientSocket == null)
                throw new Exception("Client socket is not initialized!");

            // the endpoint stores information from the client side
            socket = clientSocket;
            endPoint = (IPEndPoint)socket.RemoteEndPoint;

            SockUtils.SetKeepAlive(socket, 600 * 1000, 60 * 1000);

            sSocket = new SSocket(ref socket);
            sSocket.Received += new EventHandler(OnChatSocketReceived);
            sSocket.Disconnected += new EventHandler(OnChatSocketDisconnected);            
            sSocket.Receive();

            m_PingTimer = new System.Threading.Timer(m_PingTimerCallback);
        }
Beispiel #3
0
        private void ProcessConnect(SocketAsyncEventArgs args)
        {
            try
            {
                Socket socket = args.AcceptSocket;
                if (socket == null || !socket.Connected)
                {
                    if (ConnectFailed != null)
                        ConnectFailed(this, new TimedEventArgs());
                    return;
                }

                // send keep alive after 10 minute of inactivity
                SockUtils.SetKeepAlive(socket, 600 * 1000, 60 * 1000);

                // the socket was connected, create the ChatSocket used for communication
                this.sSocket = new SSocket(ref socket);
                this.sSocket.Received += new EventHandler(OnChatSocketReceived);
                this.sSocket.Disconnected += new EventHandler(OnChatSocketDisconnected);

                // set the connected time
                this.dtConnectedOn = DateTime.Now;

                // fire the Connected event
                if (Connected != null)
                    Connected(this, new TimedEventArgs());

                sSocket.Receive();

                SendRequestLogin();
            }
            catch
            {
                if (ConnectFailed != null)
                    ConnectFailed(this, new TimedEventArgs());
            }
        }