Beispiel #1
0
        void Dialogue(NetworkStream stream)
        {
            bool authorized = false;

            if (stream == null)
            {
                return;
            }
            int msg = 0;

            while (true)
            {
                if (!stream.DataAvailable)
                {
                    continue;
                }
                msg = (int)formatter.Deserialize(stream);
                if (msg == NetCode["authorizationRequest"])
                {
                    formatter.Serialize(stream, NetCode["authorizationBegin"]);
                    byte[]        byteLoginPass = new byte[128];
                    StringBuilder builder       = new StringBuilder();
                    int           bytes         = 0;
                    do
                    {
                        bytes = stream.Read(byteLoginPass, 0, byteLoginPass.Length);
                        builder.Append(Encoding.Unicode.GetString(byteLoginPass, 0, bytes));
                    }while (stream.DataAvailable);
                    string loginPass = builder.ToString();
                    if (Accounts.Authorize(loginPass.Split(' ')[0], loginPass.Split(' ')[1]))
                    {
                        authorized = true;
                        playerID   = Accounts.GetPlayerID(loginPass.Split(' ')[0]);
                        Console.WriteLine($"{loginPass} authorized!");
                        formatter.Serialize(stream, NetCode["authorizationSuccessful"]);
                    }
                    else
                    {
                        formatter.Serialize(stream, NetCode["authorizationFailed"]);
                    }
                }
                if (authorized)
                {
                    if (msg == NetCode["getFieldDimensions"])
                    {
                        Console.WriteLine($"Клиент {Thread.CurrentThread.GetHashCode()} запросил размеры поля");
                        Console.WriteLine($"Поле: {ThreadMaster.getDim()[0]}x{ThreadMaster.getDim()[1]}");
                        formatter.Serialize(stream, ThreadMaster.getDim());
                        Console.WriteLine($"Размеры отправлены.");
                    }

                    if (msg == NetCode["struct"])
                    {
                        formatter.Serialize(stream, NetCode["acceptStruct"]);
                        int[] lifeStructure = (int[])formatter.Deserialize(stream);
                        ThreadMaster.ClientAddCells(lifeStructure, playerID);
                    }
                }
            }
        }
Beispiel #2
0
        void StreamField(NetworkStream stream)
        {
            if (stream == null)
            {
                return;
            }
            ClientThreads.AddHandler(this.ID, wh);
            bool isAlive = true;

            int[]  cells;
            byte[] data;
            while (isAlive)
            {
                wh.WaitOne();
                cells = ThreadMaster.ClientGetCells();
                // sending metadata
                data = new byte[sizeof(int)];
                Buffer.BlockCopy(new int[] { cells.Length }, 0, data, 0, data.Length);
                stream.Write(data, 0, data.Length);

                // sending data
                data = new byte[cells.Length * sizeof(int)];
                Buffer.BlockCopy(cells, 0, data, 0, data.Length);
                stream.Write(data, 0, data.Length);
            }
        }
Beispiel #3
0
 static void Main(string[] args)
 {
     ThreadMaster.RUN();
 }