Exemplo n.º 1
0
        void RecieveCallback(IAsyncResult result)
        {
            Chanel connection = (Chanel)result.AsyncState;

            if (chanels.Contains(connection))
            {
                //try
                //{
                connection.socket.EndReceive(result);

                Sending a = sendDecode(connection);

                if (a.name != string.Empty && Passwords.ContainsKey(a.name) && Passwords[a.name] == a.password)
                {
                    forceCallEvent(a.operation, a.data);
                }
                else
                {
                    Console.WriteLine("Хм, нам пытаются послать комманду с не корректным паролем. Казнить еретика?");
                }

                connection.socket.BeginReceive(connection.buffer, 0, Sending.SizeOfMessage, SocketFlags.None, new AsyncCallback(RecieveCallback), connection);
                //}

                //catch (Exception ex)
                //{
                //    Console.WriteLine(ex.Message);
                //    removeChanel(connection.who);
                //}
            }
        }
Exemplo n.º 2
0
        public void send(Player who, Sending command)
        {
            Chanel chan = chanels.Where(c => c.who == who).FirstOrDefault();

            if (chan != null)
            {
                send(chan, command);
            }
        }
Exemplo n.º 3
0
        void AcceptCallback(IAsyncResult result)
        {
            try
            {
                Socket s = (Socket)result.AsyncState;

                Chanel joiningPlayer = new Chanel();

                joiningPlayer.socket = s.EndAccept(result);
                joiningPlayer.buffer = new byte[Sending.SizeOfMessage];
                joiningPlayer.socket.BeginReceive(joiningPlayer.buffer, 0, Sending.SizeOfMessage, SocketFlags.None, new AsyncCallback(RecieveCallbackOnJoin), joiningPlayer);

                serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), result.AsyncState);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 4
0
        public void removeChanel(Player x)
        {
            x.online = false;

            if (chanels != null)
            {
                Chanel a = chanels.Where(c => c.who == x).FirstOrDefault();

                if (a != null)
                {
                    send(a, new Sending {
                        operation = "byebye"
                    });
                    chanels.Remove(a);
                }
            }

            Console.WriteLine("Отключен " + x.name);
        }
Exemplo n.º 5
0
        public void send(Chanel who, Sending command)
        {
            try
            {
                while (who.sendingNow)
                {
                    Thread.Sleep(10);
                }
                who.sendingNow = true;
                MemoryStream x = new MemoryStream();
                binFormat.Serialize(x, command);
                who.socket.Send(x.GetBuffer());
            }
            catch (Exception e)
            {
                Console.WriteLine("При предачи данных: " + e.Message);
            }

            Thread.Sleep(10);
            who.sendingNow = false;
        }
Exemplo n.º 6
0
        Sending sendDecode(Chanel inC)
        {
            MemoryStream x = new MemoryStream(inC.buffer);

            return((Sending)binFormat.Deserialize(x));
        }
Exemplo n.º 7
0
        void RecieveCallbackOnJoin(IAsyncResult result)
        {
            //try
            //{
            Chanel joined = (Chanel)result.AsyncState;

            Sending soWhatsHere = sendDecode(joined);

            Player me = (Player)soWhatsHere.data;



            Player old = players.Where(c => c.name == me.name).FirstOrDefault();

            //Вернулся.
            if (old != null)
            {
                if (Passwords[me.name] == soWhatsHere.password)
                {
                    if (old.online)
                    {
                        Sending nowOnline = new Sending();
                        nowOnline.operation = "Exception";
                        nowOnline.data      = new Exception("Вы уже онлайн");
                        Console.WriteLine("Уже онлайн! " + old.name);
                        send(joined, nowOnline);
                    }

                    else
                    {
                        old.online = true;
                        joined.who = old;

                        chanels.Add(joined);

                        Sending okay = new Sending();
                        okay.operation = "okYouOnline";
                        okay.data      = "Вы успешно зашли на сервер. Удачной игры!";
                        send(joined, okay);



                        joined.socket.BeginReceive(joined.buffer, 0, Sending.SizeOfMessage, SocketFlags.None, new AsyncCallback(RecieveCallback), joined);

                        serverWrite("В игру вернулся " + old.name);
                    }
                }

                else
                {
                    Sending paswNotCorrect = new Sending();
                    paswNotCorrect.operation = "Exception";
                    paswNotCorrect.data      = new Exception("Ошибка в пароле!");

                    Console.WriteLine(old.name + " ошибся с паролем");

                    send(joined, paswNotCorrect);
                }
            }

            //Значит новенький.
            else
            {
                me.online = true;
                players.Add(me);
                joined.who = me;
                chanels.Add(joined);


                Sending okay = new Sending();
                okay.operation = "okYouOnline";
                okay.data      = "Вы успешно зарегистрированы на сервере. Удачной игры!";

                Passwords.Add(me.name, soWhatsHere.password);

                send(me, okay);

                forceCallEvent("generatePlanetForNewPlayer", me);



                joined.socket.BeginReceive(joined.buffer, 0, Sending.SizeOfMessage, SocketFlags.None, new AsyncCallback(RecieveCallback), joined);


                serverWrite("Приветствуем нового игрока - " + me.name);
            }
            //}

            //catch (Exception e)
            //{

            //    Console.WriteLine("При новом подключении ошибка: " + e.Message);
            //    throw new Exception();
            //}
        }