Ejemplo n.º 1
0
        public ClientHandler(ConnectionHandler connHandler)
        {
            this._connHandler = connHandler;
            this.Connection.Disconnected += new EventHandler(this.connHandler_Disconnected);
            this.Connection.MessageReceived += new EventHandler<Networking.EventArgs.MessageEventArgs>(this.connHandler_MessageReceived);

            this._messageParser = new MessageParser(this);

            this.Connection.EnqueueMessage(PC.GET_PREFIX + "." + GetRequest.IMEI);
            SMLogger.LogThis("ClientHandler Initialized...requesting IMEI.");
        }
        public void StartListening()
        {
            SharedStateObj = new SharedState();
            SharedStateObj.ContinueProcess = true;
            SharedStateObj.NumberOfClients = 0;
            SharedStateObj.Ev = new AutoResetEvent(false);

            TcpListener listener = new TcpListener(portNum);
            try
            {
                listener.Start();

                int ClientNbr = 0;

                // Start listening for connections.
                Debug.WriteLine("Waiting for a connection...");
                while (SharedStateObj.ContinueProcess)
                {
                    TcpClient handler = listener.AcceptTcpClient();
                    SharedStateObj.NumberOfClients++;
                    Debug.WriteLine("Client#{0} accepted!", ClientNbr);

                    ConnectionHandler _connection = new ConnectionHandler(handler);
                    _connection.Disconnected += new EventHandler(this.client_Disconnected);
                    ThreadPool.QueueUserWorkItem(new WaitCallback(_connection.Process), SharedStateObj);
                    this.Clients.Add(_connection);

                    this.RaiseClientConnected(_connection);
                }

                listener.Stop();

            }
            catch (Exception ex)
            {
                Debug.WriteLine("StealME.Exception:" + ex.ToString());
            }

            // Stop and wait all Client connections to end
            SharedStateObj.ContinueProcess = false;
            SharedStateObj.Ev.WaitOne();
        }
 private void RaiseClientConnected(ConnectionHandler _connection)
 {
     if (this.ClientConnected != null)
         this.ClientConnected(this, new ClientEventArgs { Connection = _connection });
 }