Exemple #1
0
 public void addplayerv1(playerinfor pi)
 {
     foreach (var v in players)
     {
         LoginServerProxy lsp;
         MatchServer.getsingleton().LoginServers.TryGetValue(pi.loginserverproxyid, out lsp);
         MemoryStream ms = new MemoryStream();
         Serializer.Serialize(ms, v.Value);
         byte[] btms    = ms.ToArray();
         byte[] t       = new byte[btms.Length + 4];
         byte[] ownerid = BitConverter.GetBytes(pi.playerid);
         Array.Copy(ownerid, 0, t, 0, 4);
         Array.Copy(btms, 0, t, 4, btms.Length);
         lsp.sendtologinserver((Byte)CMDMatchServer.OTHERPLAYERINFOR, t);
     }
     foreach (var v in players)
     {
         LoginServerProxy lsp;
         MatchServer.getsingleton().LoginServers.TryGetValue(v.Value.loginserverproxyid, out lsp);
         MemoryStream ms = new MemoryStream();
         Serializer.Serialize(ms, pi);
         byte[] btms    = ms.ToArray();
         byte[] t       = new byte[btms.Length + 4];
         byte[] ownerid = BitConverter.GetBytes(v.Value.playerid);
         Array.Copy(ownerid, 0, t, 0, 4);
         Array.Copy(btms, 0, t, 4, btms.Length);
         lsp.sendtologinserver((Byte)CMDMatchServer.OTHERPLAYERINFOR, t);
     }
     players.TryAdd(pi.playerid, pi);
     Logger.log("roomid : " + id + " number of players :" + players.Count);
     //if (players.Count == halfroomnumber)
     //{
     //}
 }
Exemple #2
0
        public void startgame()
        {
            int newid;

            if (RoomManager.getsingleton().creatingtocreated(id, out newid))
            {
                MatchServer.getsingleton().sendtoloadbalanceserver((byte)CMDLoadBalanceServer.CREATEDSV1, BitConverter.GetBytes(newid));
                Logger.log("startgame() create room =================================================== newid : " + newid + " id :" + id);
            }
        }
Exemple #3
0
        public RoomManager()
        {
            waitingRooms  = new ConcurrentDictionary <int, Room>();
            fightingRooms = new ConcurrentDictionary <int, Room>();

            CreatingRooms = new ConcurrentDictionary <int, Room>();
            CreatedRooms  = new ConcurrentDictionary <int, Room>();
            Task.Run(async() =>
            {
                try
                {
                    while (true)
                    {
                        await Task.Delay(1000);
                        foreach (var v in fightingRooms)
                        {
                            int number = v.Value.NumberOfOnlinePlayers();
                            if (number == 0)
                            {
                                MatchServer.getsingleton().sendtoloadbalanceserver((byte)CMDLoadBalanceServer.DESTROY, BitConverter.GetBytes(v.Key));
                                Room room;
                                fightingRooms.TryRemove(v.Key, out room);
                            }
                        }
                        //////////////////////////////////////////////////////////////////////////////
                        foreach (var v in CreatedRooms)
                        {
                            int number = v.Value.NumberOfOnlinePlayers();
                            if (number == 0)
                            {
                                MatchServer.getsingleton().sendtoloadbalanceserver((byte)CMDLoadBalanceServer.DESTROYV1, BitConverter.GetBytes(v.Key));
                                Room room;
                                CreatedRooms.TryRemove(v.Key, out room);
                                Logger.log("matchserver tell loadbalance destory a room");
                            }
                        }
                        foreach (var v in CreatingRooms)
                        {
                            int number = v.Value.NumberOfOnlinePlayers();
                            if (number == 0)
                            {
                                Room room;
                                CreatingRooms.TryRemove(v.Key, out room);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.log(e.ToString());
                }
            });
        }
Exemple #4
0
 public void rejoin(playerinfor pi)
 {
     foreach (var v in players)
     {
         if (v.Value.offline)
         {
             continue;
         }
         LoginServerProxy lsp;
         MatchServer.getsingleton().LoginServers.TryGetValue(pi.loginserverproxyid, out lsp);
         MemoryStream ms = new MemoryStream();
         Serializer.Serialize(ms, v.Value);
         byte[] btms    = ms.ToArray();
         byte[] t       = new byte[btms.Length + 4];
         byte[] ownerid = BitConverter.GetBytes(pi.playerid);
         Array.Copy(ownerid, 0, t, 0, 4);
         Array.Copy(btms, 0, t, 4, btms.Length);
         lsp.sendtologinserver((Byte)CMDMatchServer.OTHERPLAYERINFOR, t);
     }
     foreach (var v in players)
     {
         if (v.Value.offline)
         {
             continue;
         }
         LoginServerProxy lsp;
         MatchServer.getsingleton().LoginServers.TryGetValue(v.Value.loginserverproxyid, out lsp);
         MemoryStream ms = new MemoryStream();
         Serializer.Serialize(ms, pi);
         byte[] btms    = ms.ToArray();
         byte[] t       = new byte[btms.Length + 4];
         byte[] ownerid = BitConverter.GetBytes(v.Value.playerid);
         Array.Copy(ownerid, 0, t, 0, 4);
         Array.Copy(btms, 0, t, 4, btms.Length);
         lsp.sendtologinserver((Byte)CMDMatchServer.OTHERPLAYERINFOR, t);
     }
     players.TryAdd(pi.playerid, pi);
 }
Exemple #5
0
        public void addplayer(playerinfor pi)
        {
            foreach (var v in players)
            {
                if (v.Value.offline)
                {
                    continue;
                }
                LoginServerProxy lsp;
                MatchServer.getsingleton().LoginServers.TryGetValue(pi.loginserverproxyid, out lsp);
                MemoryStream ms = new MemoryStream();
                Serializer.Serialize(ms, v.Value);
                byte[] btms    = ms.ToArray();
                byte[] t       = new byte[btms.Length + 4];
                byte[] ownerid = BitConverter.GetBytes(pi.playerid);
                Array.Copy(ownerid, 0, t, 0, 4);
                Array.Copy(btms, 0, t, 4, btms.Length);
                lsp.sendtologinserver((Byte)CMDMatchServer.OTHERPLAYERINFOR, t);
            }
            foreach (var v in players)
            {
                if (v.Value.offline)
                {
                    continue;
                }
                LoginServerProxy lsp;
                MatchServer.getsingleton().LoginServers.TryGetValue(v.Value.loginserverproxyid, out lsp);
                MemoryStream ms = new MemoryStream();
                Serializer.Serialize(ms, pi);
                byte[] btms    = ms.ToArray();
                byte[] t       = new byte[btms.Length + 4];
                byte[] ownerid = BitConverter.GetBytes(v.Value.playerid);
                Array.Copy(ownerid, 0, t, 0, 4);
                Array.Copy(btms, 0, t, 4, btms.Length);
                lsp.sendtologinserver((Byte)CMDMatchServer.OTHERPLAYERINFOR, t);
            }
////////////////////
            if (players.Count >= halfroomnumber)
            {
                pi.side = 1;
            }
            else
            {
                pi.side = 0;
            }
            players.TryAdd(pi.playerid, pi);
            if (players.Count == halfroomnumber)
            {
                b_wholeteam = true;
            }
            else
            {
                b_wholeteam = false;
            }
            if (players.Count == halfroomnumber * 2)
            {
                isfull = true;
                lock (RoomsLock)
                {
                    if (!b_created)
                    {
                        b_created = true;
                        Logger.log("room is full so create ds request=================================================== : " + id);
                        RoomManager.getsingleton().waitingtofighting(id);
                        MatchServer.getsingleton().sendtoloadbalanceserver((byte)CMDLoadBalanceServer.CREATEDS, BitConverter.GetBytes(id));
                    }
                }
            }
            Task.Run(async() =>
            {
                try
                {
                    await Task.Delay(System.TimeSpan.FromMilliseconds(Room.expiredtime));
                    Logger.log("room time expired 000");
                    b_anyplayerisok = true;
                    /////////////////////////////////////////////////////////////////////////
                    ///
                    await Task.Delay(System.TimeSpan.FromMilliseconds(Room.expiredtime));
                    lock (RoomsLock)
                    {
                        if (!b_created)
                        {
                            b_created = true;
                            Logger.log("room time expired 111===============================================================: " + id);
                            RoomManager.getsingleton().waitingtofighting(id);
                            MatchServer.getsingleton().sendtoloadbalanceserver((byte)CMDLoadBalanceServer.CREATEDS, BitConverter.GetBytes(id));
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.log(e.ToString());
                }
            });
        }
Exemple #6
0
        public MatchServer() : base("MatchServer")
        {
            LoginServers = new ConcurrentDictionary <int, LoginServerProxy>();
            matchserver  = this;
            LuaTable server     = GetValueFromLua <LuaTable>("server");
            string   nettype    = (string)server["nettype"];
            LuaTable serveraddr = (LuaTable)server[nettype];
            int      port       = (int)(Int64)serveraddr["port"];
            KService service    = Session.createorget(port);

            service.onAcceptAKchannel += (ref KChannel channel) => {
                int id;
                do
                {
                    id = RandomHelper.RandomNumber(int.MinValue, int.MaxValue);
                } while (LoginServers.ContainsKey(id));
                LoginServers.TryAdd(id, new LoginServerProxy(id, channel, this));
                Logger.log("loinginserver in");
            };
            Task.Run(async() =>
            {
                try
                {
                    while (true)
                    {
                        await Task.Delay(50000);
                        Logger.log(matchpool.Count + " :matchpool size");
                    }
                }
                catch (Exception e)
                {
                    Logger.log(e.ToString());
                }
            });
            ////////////////////////////////////////////////////////////////////////////////////////
            LuaTable remoteserver = GetValueFromLua <LuaTable>("remoteserver");

            nettype    = (string)remoteserver["nettype"];
            serveraddr = (LuaTable)remoteserver[nettype];
            string serverip = (string)serveraddr["serverip"];

            port = (int)(Int64)serveraddr["port"];
            IPAddress ipAd = IPAddress.Parse(serverip);//local ip address  "172.16.5.188"

            channel_loadbalance = Session.getnew().GetChannel(new IPEndPoint(ipAd, port));
            channel_loadbalance.onUserLevelReceivedCompleted += (ref byte[] buffer) => {
                switch ((CMDLoadBalanceServer)buffer[0])
                {
                case CMDLoadBalanceServer.CREATEDS:
                    int    roomid = BitConverter.ToInt32(buffer, 1);
                    int    dsport = BitConverter.ToInt32(buffer, 5);
                    string dswan  = Encoding.getstring(buffer, 9, buffer.Length - 9);
                    Room   room;
                    RoomManager.getsingleton().fightingRooms.TryGetValue(roomid, out room);
                    foreach (var v in room.players.Values)
                    {
                        if (v.offline)
                        {
                            Logger.log("this player is offline ,player id is  : " + v.playerid);
                            continue;
                        }
                        LoginServerProxy lsp;
                        bool             b = LoginServers.TryGetValue(v.loginserverproxyid, out lsp);
                        if (b)
                        {
                            byte[] wanbytes  = Encoding.getbyte(dswan);
                            byte[] sumbuffer = new byte[16 + wanbytes.Length];
                            sumbuffer.WriteTo(0, v.playerid);
                            sumbuffer.WriteTo(4, v.side);
                            sumbuffer.WriteTo(8, dsport);
                            sumbuffer.WriteTo(12, roomid);
                            Array.Copy(wanbytes, 0, sumbuffer, 16, wanbytes.Length);
                            lsp.sendtologinserver((byte)CMDMatchServer.MATCHREQUEST, sumbuffer);
                        }
                        else
                        {
                        }
                    }
                    Logger.log("--roomid-- : " + roomid + "--dsport--" + dsport + "--dswan-- " + dswan);
                    break;

                case CMDLoadBalanceServer.CREATEDSV1:
                    roomid = BitConverter.ToInt32(buffer, 1);
                    dsport = BitConverter.ToInt32(buffer, 5);
                    dswan  = Encoding.getstring(buffer, 9, buffer.Length - 9);
                    RoomManager.getsingleton().CreatedRooms.TryGetValue(roomid, out room);
                    foreach (var v in room.players.Values)
                    {
                        if (v.offline)
                        {
                            Logger.log("this player is offline ,player id is  : " + v.playerid);
                            continue;
                        }
                        LoginServerProxy lsp;
                        bool             b = LoginServers.TryGetValue(v.loginserverproxyid, out lsp);
                        if (b)
                        {
                            byte[] wanbytes  = Encoding.getbyte(dswan);
                            byte[] sumbuffer = new byte[16 + wanbytes.Length];
                            sumbuffer.WriteTo(0, v.playerid);
                            sumbuffer.WriteTo(4, v.side);
                            sumbuffer.WriteTo(8, dsport);
                            sumbuffer.WriteTo(12, roomid);
                            Array.Copy(wanbytes, 0, sumbuffer, 16, wanbytes.Length);
                            lsp.sendtologinserver((byte)CMDMatchServer.STARTGAME, sumbuffer);
                        }
                        else
                        {
                        }
                    }
                    Logger.log("--roomid-- : " + roomid + "--dsport--" + dsport + "--dswan-- " + dswan);
                    break;

                case CMDLoadBalanceServer.DESTROY:
                    Logger.log("CMDLoadBalanceServer.DESTROY");
                    break;

                default:
                    break;
                }
            };
        }
        public LoginServerProxy(int id, KChannel channel, MatchServer matchserver)
        {
            this.id = id;
            this.channel_loginserver = channel;
            this.matchserver         = matchserver;
            this.channel_loginserver.ondisconnect += () => {
                LoginServerProxy lsp;
                bool             b = this.matchserver.LoginServers.TryRemove(id, out lsp);
                if (b)
                {
                    Logger.log("channel_loginserver.ondisconnect successfully");
                }
                else
                {
                    Logger.log("this should not happen for channel_loginserver.ondisconnect");
                }
            };
            channel_loginserver.onUserLevelReceivedCompleted += (ref byte[] buffer) => {
                //var str = Encoding.getstring(buffer, 1, buffer.Length - 1);
                switch ((CMDMatchServer)buffer[0])
                {
                case CMDMatchServer.MATCHREQUEST:
                    playerinfor  pi = new playerinfor();
                    MemoryStream ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position           = 0;
                    pi                    = Serializer.Deserialize <playerinfor>(ms);
                    pi.loginserverproxyid = this.id;
                    Logger.log(pi.SimulateInforStr);
                    matchserver.addtomatchpool(pi);
                    Logger.log(pi.playerid + " :matchrequest");

                    break;

                case CMDMatchServer.CREATEROOM:
                    pi = new playerinfor();
                    ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position           = 0;
                    pi                    = Serializer.Deserialize <playerinfor>(ms);
                    pi.loginserverproxyid = this.id;
                    Logger.log(pi.SimulateInforStr);
                    matchserver.addtomatchpoolV1(pi);
                    Room room = RoomManager.getsingleton().createroom(pi.halfroomnumber);
                    room.addplayerv1(pi);
                    /////////////////////////////////////////
                    pi.roomnumber = room.id;
                    ms            = new MemoryStream();
                    Serializer.Serialize(ms, pi);
                    sendtologinserver((byte)CMDMatchServer.CREATEROOM, ms.ToArray());
                    Logger.log(pi.roomnumber + " :pi.roomnumber CREATEROOM ,pi.halfroomnumber : " + pi.halfroomnumber);
                    break;

                case CMDMatchServer.JOINROOM:
                    pi = new playerinfor();
                    ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position           = 0;
                    pi                    = Serializer.Deserialize <playerinfor>(ms);
                    pi.loginserverproxyid = this.id;
                    Logger.log(pi.SimulateInforStr);
                    matchserver.addtomatchpoolV1(pi);
                    if (RoomManager.getsingleton().CreatingRooms.TryGetValue(pi.roomnumber, out room))
                    {
                        room.addplayerv1(pi);
                        /////////////////////////////////////////
                        pi.roomnumber = room.id;
                        ms            = new MemoryStream();
                        Serializer.Serialize(ms, pi);
                        sendtologinserver((byte)CMDMatchServer.JOINROOM, ms.ToArray());
                        Logger.log(pi.roomnumber + " :pi.roomnumber JOINROOM");
                    }
                    else
                    {
                        ms = new MemoryStream();
                        Serializer.Serialize(ms, pi);
                        sendtologinserver((byte)CMDMatchServer.JOINROOMFAILED, ms.ToArray());
                        Logger.log("join room but the room with specific number is not found ,this roomnumber is : " + pi.roomnumber);
                    }


                    break;

                case CMDMatchServer.STARTGAME:
                    pi = new playerinfor();
                    ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position           = 0;
                    pi                    = Serializer.Deserialize <playerinfor>(ms);
                    pi.loginserverproxyid = this.id;
                    Logger.log(id + " :startgame");
                    if (RoomManager.getsingleton().CreatingRooms.TryGetValue(pi.roomnumber, out room))
                    {
                        Logger.log(pi.roomnumber + " :pi.roomnumber STARTGAME");
                        room.startgame();
                    }
                    else
                    {
                        Logger.log("join room but the room with specific number is not found");
                    }


                    break;

                case CMDMatchServer.PLAYEREXITQUEST:
                    int playerid = BitConverter.ToInt32(buffer, 1);
                    matchserver.removefrommatchpool(playerid);
                    Logger.log("loginserverproxyid: " + id + " :playerexitquest playerid " + playerid);

                    break;

                case CMDMatchServer.RECONNECT:

                    pi = new playerinfor();
                    ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position           = 0;
                    pi                    = Serializer.Deserialize <playerinfor>(ms);
                    pi.loginserverproxyid = this.id;
                    Logger.log("loginserverproxyid: " + id + " playerid RECONNECT:  " + pi.playerid);
                    byte[] feedbackbuffer = new byte[8];
                    feedbackbuffer.WriteTo(0, pi.playerid);
                    Filter filter;
                    if (Filter.Filtertracker.TryGetValue(pi.oldplayerid, out filter))
                    {
                        playerinfor p1;
                        filter.players.TryRemove(pi.oldplayerid, out p1);
                        if (p1 == null)
                        {
                        }
                        else
                        {
                            matchserver.removefrommatchpool(pi.oldplayerid);
                            matchserver.onlyaddtomatchpool(pi);
                            filter.players.TryAdd(pi.playerid, pi);
                            feedbackbuffer.WriteTo(4, 1);
                            sendtologinserver((byte)CMDMatchServer.RECONNECT, feedbackbuffer);
                            Logger.log("=========Filter.Filtertracker:   playerid :" + pi.playerid + "oldplayerid : " + pi.oldplayerid);
                        }

                        break;
                    }
                    if (RoomManager.getsingleton().waitingRooms.TryGetValue(pi.roomid, out room))
                    {
                        playerinfor p1 = room.removeplayer(pi.oldplayerid);    //waitingrooms and fightingrooms may contain the same roomid but playid is Globally unique
                        //if (p1 == null)
                        //{ }
                        //else
                        {
                            matchserver.removefrommatchpool(pi.oldplayerid);
                            matchserver.onlyaddtomatchpool(pi);
                            room.rejoin(pi);
                            feedbackbuffer.WriteTo(4, 1);
                            sendtologinserver((byte)CMDMatchServer.RECONNECT, feedbackbuffer);
                            Logger.log("=========waitingRooms:   playerid :" + pi.playerid + "oldplayerid : " + pi.oldplayerid);
                        }
                        break;
                    }
                    if (RoomManager.getsingleton().fightingRooms.TryGetValue(pi.roomid, out room))
                    {
                        playerinfor p1 = room.removeplayer(pi.oldplayerid);
                        //if (p1 == null)
                        //{ }
                        //else
                        {
                            matchserver.removefrommatchpool(pi.oldplayerid);
                            matchserver.onlyaddtomatchpool(pi);
                            room.rejoin(pi);
                            feedbackbuffer.WriteTo(4, 1);
                            sendtologinserver((byte)CMDMatchServer.RECONNECT, feedbackbuffer);
                            Logger.log("=========fightingRooms:   playerid :" + pi.playerid + "oldplayerid : " + pi.oldplayerid);
                        }

                        break;
                    }

                    feedbackbuffer.WriteTo(4, 0);
                    sendtologinserver((byte)CMDMatchServer.RECONNECT, feedbackbuffer);
                    Logger.log("=========failed:   playerid :" + pi.playerid + "oldplayerid : " + pi.oldplayerid);
                    break;

                case CMDMatchServer.RECONNECTV1:

                    pi = new playerinfor();
                    ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position           = 0;
                    pi                    = Serializer.Deserialize <playerinfor>(ms);
                    pi.loginserverproxyid = this.id;
                    Logger.log("loginserverproxyid: " + id + " playerid RECONNECT:  " + pi.playerid);
                    feedbackbuffer = new byte[8];
                    feedbackbuffer.WriteTo(0, pi.playerid);
                    if (RoomManager.getsingleton().CreatingRooms.TryGetValue(pi.roomid, out room))
                    {
                        playerinfor p1 = room.getplayer(pi.oldplayerid);
                        if (p1 == null)
                        {
                        }
                        else
                        {
                            matchserver.removefrommatchpool(pi.oldplayerid);
                        }
                        matchserver.onlyaddtomatchpool(pi);
                        room.rejoin(pi);
                        feedbackbuffer.WriteTo(4, 1);
                        sendtologinserver((byte)CMDMatchServer.RECONNECTV1, feedbackbuffer);
                        break;
                    }

                    if (RoomManager.getsingleton().CreatedRooms.TryGetValue(pi.roomid, out room))
                    {
                        playerinfor p1 = room.getplayer(pi.oldplayerid);
                        if (p1 == null)
                        {
                        }
                        else
                        {
                            matchserver.removefrommatchpool(pi.oldplayerid);
                        }
                        matchserver.onlyaddtomatchpool(pi);
                        room.rejoin(pi);
                        feedbackbuffer.WriteTo(4, 1);
                        sendtologinserver((byte)CMDMatchServer.RECONNECTV1, feedbackbuffer);
                        break;
                    }

                    feedbackbuffer.WriteTo(4, 0);
                    sendtologinserver((byte)CMDMatchServer.RECONNECT, feedbackbuffer);
                    break;

                default:
                    break;
                }
            };
        }