Exemple #1
0
        public void StartListening()
        {
            ClientService ClientTask;

            // Client Connections Pool
            ClientConnectionPool ConnectionPool = new ClientConnectionPool();

            // Client Task to handle client requests
            ClientTask = new ClientService(ConnectionPool);

            ClientTask.Start();

            // return;

            // while (Program.ServerForm.DoExit == false)
            {
                var users = FakeData(1);
                foreach (var u in users)
                {
                    ConnectionPool.Enqueue(new MyClientHandler(u));
                    Program.ServerForm.incrementNbReceive(); // +1
                }
            }

            ClientTask.Stop();
        }
Exemple #2
0
        public void StartListening()
        {
            ClientService        ClientTask;
            ClientConnectionPool ConnectionPool = new ClientConnectionPool();

            ClientTask = new ClientService(ConnectionPool);
            ClientTask.Start();

            IPAddress localAddr  = IPAddress.Parse(Program.SERVER_ADDRESS);
            int       serverPort = Program.SERVER_PORT;

            TcpClient   client   = new TcpClient();
            TcpListener listener = new TcpListener(localAddr, serverPort);

            try
            {
                listener.Start();

                int ClientNbr = 0;

                // Start listening for connections.
                log.Debug("Waiting for a connection...");

                while (Program.ServerForm.DoExit == false)
                {
                    try
                    {
                        client = listener.AcceptTcpClient();
                        if (client != null)
                        {
                            ClientNbr++;
                            ConnectionPool.Enqueue(new TCPClientHandler(client, ClientNbr));
                            Program.ServerForm.updateNbConnection(+1);
                        }
                        else
                        {
                            break;
                        }
                    }
                    catch (Exception errX)
                    {
                        log.Error(errX);
                    }
                }

                log.Debug("+------------- closing Listener---------------+");

                listener.Stop();

                // Stop client requests handling
                ClientTask.Stop();
            }
            catch (Exception e)
            {
                log.Error(e.ToString());
            }
        }
Exemple #3
0
 public ClientService(ClientConnectionPool ConnectionPool)
 {
     this.ConnectionPool = ConnectionPool;
 }
Exemple #4
0
        public void StartListening()
        {
            try
            {
                ClientService ClientTask;

                // Client Connections Pool
                ClientConnectionPool ConnectionPool = new ClientConnectionPool();

                // Client Task to handle client requests
                ClientTask = new ClientService(ConnectionPool);

                ClientTask.Start();

                IPAddress serverAddr = IPAddress.Parse(Program.SERVER_ADDRESS);
                int       serverPort = Program.SERVER_PORT;

                Socket     mySocketServerUdp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                IPEndPoint localIpEndPoint   = new IPEndPoint(serverAddr, serverPort);

                try
                {
                    mySocketServerUdp.Bind(localIpEndPoint);
                    int ClientNbr = 0;
                    // Start listening for connections.
                    while (Program.ServerForm.DoExit == false)
                    {
                        Byte[]   received      = null;
                        EndPoint remoteEP      = (localIpEndPoint);
                        int      bytesReceived = 0;

                        try
                        {
                            if (mySocketServerUdp.Available > 0)
                            {
                                int nbToRead = Math.Max(Program.BUFFER_SIZE, mySocketServerUdp.Available);
                                received      = new Byte[nbToRead];
                                bytesReceived = mySocketServerUdp.ReceiveFrom(received, ref remoteEP);
                            }
                            else
                            {
                                Thread.Sleep(10);
                                continue;
                            }
                        }
                        catch (Exception exx)
                        {
                            // Case device has been power off :(
                            log.Error("UdpServer.cs exception");
                            continue;
                        }

                        // did we received something ?
                        if (bytesReceived > 0)
                        {
                            StringBuilder dataReceived = new StringBuilder();
                            dataReceived.Append(System.Text.Encoding.ASCII.GetString(received));

                            // An incoming message needs to be processed.
                            ConnectionPool.Enqueue(new UDPClientHandler(remoteEP, ClientNbr, dataReceived.ToString()));

                            // Send ACK to the client
                            //byte[] cmdBytes = new byte[] { };
                            //cmdBytes = Encoding.ASCII.GetBytes(requiredCommand);
                            //var sendSackResult = mySocketServerUdp.SendTo(cmdBytes, cmdBytes.Length, SocketFlags.None, remoteEP);
                        }
                        else
                        {
                            // Program.ServerForm.updateNbConnection(ConnectionPool.Count);
                            Thread.Sleep(100);
                        }
                    }
                    // Stop client requests handling
                    ClientTask.Stop();

                    log.Debug("Closing Listener...");

                    mySocketServerUdp.Shutdown(SocketShutdown.Both);
                    //mySocketServerUdp.Disconnect(false);
                    mySocketServerUdp.Close(2);
                    mySocketServerUdp = null;
                }
                catch (Exception e)
                {
                    log.Error(e);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }