static void Main(string[] args)
 {
     Console.WriteLine("{0}Hello client{0}", new string('-', 30));
     socket =
        new AdvancedSocket(
            new Socket(
                AddressFamily.InterNetwork,
                SocketType.Stream,
                ProtocolType.Tcp));
     try
     {
         //HELLO
         Hello();
         Console.WriteLine("{0}GetStart{0}", new string('-', 30));
         //WORK
         Work();
     }
     catch (SocketException)
     {
         Console.WriteLine("{0}Connection lost{0}", new string('-', 30));
     }
     catch (Exception)
     {
         // ignored
     }
     finally
     {
         socket.Close();
     }
 }
示例#2
0
        private static void MainLoop(IWorker currentWorker)
        {
            socket.Bind(
                new IPEndPoint(
                    IPAddress.Any,
                    AdvancedSocket.Port));
            socket.Listen(maxClientsCount + 1);
            while (true)
                try
                {
                    AdvancedSocket _socket = new AdvancedSocket(socket.Accept());
                    var packet = _socket.RecivePacket(CommandType.Hello);

                    if (currentTasksCount == maxClientsCount)
                        packet.ErrorInfo = "Server is bussy, please try again later";
                    _socket.SendPacket(packet);

                    if (packet.Error != 0)
                    {
                        _socket.Close();
                        continue;
                    }

                    var task = Task.Factory.StartNew(() =>
                    {
                        currentTasksCount++;
                        int numOfTask = listTasks.Count;
                        Console.WriteLine($"[{Logger.Time()}] TaskN{numOfTask} start working");
                        new BaseServerWorker().Run(_socket);
                        _socket.Close();
                        return numOfTask;
                    });

                    listTasks.Add(task);

                    task.ContinueWith(numOfTask =>
                    {
                        currentTasksCount--;
                        Console.WriteLine($"[{Logger.Time()}] TaskN{numOfTask.Result} end working");
                    });
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
        }
        private static void MainLoop(IWorker currentWorker)
        {
            socket.Bind(
                new IPEndPoint(
                    IPAddress.Any,
                    AdvancedSocket.Port));
            socket.Listen(maxClientsCount + 1);
            while (true)
                try
                {
                    var _socket = new AdvancedSocket(socket.Accept());
                    var packet = _socket.RecivePacket(CommandType.Hello);

                    if (currentTasksCount == maxClientsCount)
                        packet.ErrorInfo = "Server is bussy, please try again later";
                    _socket.SendPacket(packet);

                    if (packet.Error != 0)
                    {
                        _socket.Close();
                        continue;
                    }

                    var task = Task.Factory.StartNew(() =>
                    {
                        currentTasksCount++;
                        var numOfTask = listTasks.Count;
                        Console.WriteLine($"[{DateTime.Now}] TaskN{numOfTask} start working");
                        var worker = new BaseServerWorker();
                        worker.Init(_socket);
                        UpdateAll += worker.Update;
                        while (true) Thread.Sleep(int.MaxValue);
                    });

                    listTasks.Add(task);

                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
        }