Ejemplo n.º 1
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);
                }
        }
Ejemplo n.º 2
0
        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);
                }
        }
Ejemplo n.º 3
0
        public void Run(object arg = null)
        {
            socket = arg as AdvancedSocket;
            if (socket == null) return;
            if (StepAuth()) return;
            provider = new FolderProvider(userName, 
                ((IPEndPoint)socket.socket.RemoteEndPoint).Address.ToString());


            while (true)
                try
                {
                    packet = socket.RecivePacket(CommandType.WorkerCommands);
                    switch (packet.Command)
                    {
                        case CommandType.Add:
                            Add();
                            break;
                        case CommandType.Clone:
                            Clone();
                            break;
                        case CommandType.Commit:
                            Commit();
                            break;
                        case CommandType.Update:
                            Update();
                            break;
                        case CommandType.Revert:
                            Revert();
                            break;
                        case CommandType.Log:
                            Log();
                            break;
                        case CommandType.GoodBy:
                            Console.WriteLine($"[{Logger.Time()}] client say goodby");
                            return;
                        default:
                            return;
                    }
                }
                catch (SocketException)
                {
                    packet.ErrorInfo = "Connection lost";
                    return;
                }
                catch (GitHubException e)
                {
                    packet.ErrorInfo = e.Message;
                }
                catch (Exception e)
                {
                    packet.ErrorInfo = "Unknown server exception";
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    try{socket.SendPacket(packet);}
                    catch (Exception){/*ignored*/}
                    if(packet.Command != CommandType.Log)
                        Console.WriteLine($"[{Logger.Time()}][{userName}] {packet.Command} {Join(" ",packet.Args)} {packet.ErrorInfo}");
                }
            
        }