Пример #1
0
        /// <summary>
        /// Remove all player reference from memory and closes the connection
        /// </summary>
        /// <param name="id">Player id</param>
        static void CloseConnection(int id)
        {
            ByteBuffer buffer = new ByteBuffer();

            buffer.WriteInt((int)Enums.AllEnums.SCloseConnection);
            buffer.WriteString(Globals.dicPlayers[id].uName);

            HandleClinet.SendToAllBut(id, buffer.ToArray());

            Globals.clients[id].Client.Close();
            Globals.clients[id] = null;
            Console.WriteLine(">> Closing connection from player " + Globals.dicPlayers[id].uName);
            DB.getInstance("", "").UpdatePlayerPlayTime(Globals.dicPlayers[id]);
            try
            {
                Globals.dicPlayers.Remove(id);
            }
            catch (Exception) { }
            //SendDefaultRespose(true, clNo);
        }
Пример #2
0
        public void Start()
        {
            httpSocket.Start();


            Task task = Task.Run(() =>
            {
                while (run)
                {
                    for (int i = 0; i < Globals.httpClient.Length; i++)
                    {
                        Globals.httpClient[i] = new TcpClient();
                        try
                        {
                            Globals.httpClient[i] = httpSocket.AcceptTcpClient();
                        }
                        catch { break; }
                        Console.WriteLine(">> New connection from http server");
                        HandleHttpClient httpClient = new HandleHttpClient();
                        httpClient.StartHttpClient(Globals.httpClient[i], i);
                    }
                    Thread.Sleep(1000);
                }
            });

            int counter = 0;

            JwtTokens.LoadKey("key.txt");

serverstart:
            serverSocket.Start();
            Globals.dicPlayers = new Dictionary <int, Player>();
            Console.WriteLine(">> TCP IP Server Started on port " + Globals.port);
            while (Globals.mainRun)
            {
                for (int i = 0; i < Globals.clients.Length; i++)
                {
                    if (Globals.clients[i] == null)
                    {
                        Globals.clients[i] = new TcpClient();
                        Globals.clients[i] = serverSocket.AcceptTcpClient();
                        if (!Globals.mainRun)
                        {
                            break;
                        }
                        Console.WriteLine(" >> Client No:" + Convert.ToString(counter) + " started! " + Globals.clients[i].Client.LocalEndPoint);
                        HandleClinet client = new HandleClinet();
                        client.startClient(Globals.clients[i], counter, Globals.clients[i].Client.LocalEndPoint.ToString());
                        counter++;
                    }
                }
            }

            CloseSocket();
            Console.WriteLine(">> Closing Server Socket");
            serverSocket.Stop();
            if (Globals.restart)
            {
                Globals.restart    = false;
                Globals.threadsRun = true;
                Globals.mainRun    = true;

                LoadConfig(path);
                serverSocket = new TcpListener(IPAddress.Any, Globals.port);
                goto serverstart;
            }
        }