Exemplo n.º 1
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;
                }
            };
        }
Exemplo n.º 2
0
        public LoginServer() : base("Loginserver")
        {
            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 = 0;
                do
                {
                    id = RandomHelper.RandomNumber(int.MinValue, int.MaxValue);
                } while (Players.ContainsKey(id));
                Players.TryAdd(id, new Player(id, channel, this));
                Logger.log("onaccept");
            };
////////////////////////////////////////////////////////////////////////////////////////
            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_matchserver = Session.getnew().GetChannel(new IPEndPoint(ipAd, port));
            channel_matchserver.onUserLevelReceivedCompleted += (ref byte[] buffer) => {
                switch ((CMDMatchServer)buffer[0])
                {
                case CMDMatchServer.MATCHREQUEST:
                    int    playerid = BitConverter.ToInt32(buffer, 1);
                    Player player;
                    bool   b = Players.TryGetValue(playerid, out player);
                    if (b)
                    {
                        int    side       = BitConverter.ToInt32(buffer, 5);
                        int    dsport     = BitConverter.ToInt32(buffer, 9);
                        int    roomid     = BitConverter.ToInt32(buffer, 13);
                        string dswan      = Encoding.getstring(buffer, 17, buffer.Length - 17);
                        byte[] tempbuffer = new byte[buffer.Length - 5];
                        Array.Copy(buffer, 5, tempbuffer, 0, tempbuffer.Length);
                        player.send((byte)CMDPlayer.MATCHREQUEST, tempbuffer);
                        Logger.log("--player.playerinfor-- : " + player.playerinfor + "--side-- : " + side + "--dsport--" + dsport + "--dswan-- " + dswan);
                    }
                    else
                    {
                    }

                    break;

                case CMDMatchServer.CREATEROOM:
                    playerinfor  pi = new playerinfor();
                    MemoryStream ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position = 0;
                    pi          = Serializer.Deserialize <playerinfor>(ms);
                    b           = Players.TryGetValue(pi.playerid, out player);
                    if (b)
                    {
                        player.send((byte)CMDPlayer.CREATEROOM, BitConverter.GetBytes(pi.roomnumber));
                        Logger.log(pi.roomnumber + " :pi.roomnumber :createroom");
                    }
                    else
                    {
                    }
                    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);
                    b           = Players.TryGetValue(pi.playerid, out player);
                    if (b)
                    {
                        player.send((byte)CMDPlayer.JOINROOM, BitConverter.GetBytes(pi.roomnumber));
                        Logger.log(pi.roomnumber + " :pi.roomnumber :joinroom");
                    }
                    else
                    {
                    }
                    Logger.log(" :joinroom");

                    break;

                case CMDMatchServer.JOINROOMFAILED:
                    pi = new playerinfor();
                    ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position = 0;
                    pi          = Serializer.Deserialize <playerinfor>(ms);
                    b           = Players.TryGetValue(pi.playerid, out player);
                    if (b)
                    {
                        player.send((byte)CMDPlayer.JOINROOMFAILED, BitConverter.GetBytes(pi.roomnumber));
                        Logger.log(pi.roomnumber + " JOINROOMFAILED");
                    }
                    else
                    {
                    }
                    Logger.log(" :joinroom");

                    break;

                case CMDMatchServer.STARTGAME:
                    playerid = BitConverter.ToInt32(buffer, 1);
                    b        = Players.TryGetValue(playerid, out player);
                    if (b)
                    {
                        int    side       = BitConverter.ToInt32(buffer, 5);
                        int    dsport     = BitConverter.ToInt32(buffer, 9);
                        int    roomid     = BitConverter.ToInt32(buffer, 13);
                        string dswan      = Encoding.getstring(buffer, 17, buffer.Length - 17);
                        byte[] tempbuffer = new byte[buffer.Length - 5];
                        Array.Copy(buffer, 5, tempbuffer, 0, tempbuffer.Length);
                        player.send((byte)CMDPlayer.STARTGAME, tempbuffer);
                        Logger.log("--player.playerinfor-- : " + player.playerinfor + "--side-- : " + side + "--dsport--" + dsport + "--dswan-- " + dswan);
                    }
                    else
                    {
                    }
                    Logger.log(" :joinroom");

                    break;

                case CMDMatchServer.PLAYEREXITQUEST:

                    Logger.log(" :playerexitquest");

                    break;

                case CMDMatchServer.OTHERPLAYERINFOR:
                    playerid = BitConverter.ToInt32(buffer, 1);
                    b        = Players.TryGetValue(playerid, out player);
                    if (b)
                    {
                        pi = new playerinfor();
                        ms = new MemoryStream();
                        ms.Write(buffer, 5, buffer.Length - 5);
                        ms.Position = 0;
                        pi          = Serializer.Deserialize <playerinfor>(ms);
                        byte[] otherinfor = Encoding.getbyte(pi.SimulateInforStr);
                        byte[] t          = new byte[otherinfor.Length + 12];
                        t.WriteTo(0, pi.playerid);
                        t.WriteTo(4, pi.side);
                        t.WriteTo(8, pi.oldplayerid);
                        Array.Copy(otherinfor, 0, t, 12, otherinfor.Length);
                        player.send((byte)CMDPlayer.OTHERPLAYERINFOR, t);
                        Logger.log("--otherplayer.playerinfor-- : " + pi.SimulateInforStr + "--side-- : " + pi.side + "--dsport--");
                    }
                    else
                    {
                    }
                    Logger.log(" :playerexitquest");

                    break;

                case CMDMatchServer.RECONNECT:
                    playerid = BitConverter.ToInt32(buffer, 1);
                    int result = BitConverter.ToInt32(buffer, 5);
                    b = Players.TryGetValue(playerid, out player);
                    byte[] byterosend = new byte[8];
                    byterosend.WriteTo(0, playerid);
                    byterosend.WriteTo(4, result);
                    if (b)
                    {
                        player.send((byte)CMDPlayer.RECONNECT, byterosend);
                        Logger.log("--playerid-- : " + playerid + "--reconnect-- : " + result);
                    }
                    else
                    {
                        Logger.log("player id not exist ,this should not happen");
                    }
                    break;

                case CMDMatchServer.RECONNECTV1:
                    playerid   = BitConverter.ToInt32(buffer, 1);
                    result     = BitConverter.ToInt32(buffer, 5);
                    b          = Players.TryGetValue(playerid, out player);
                    byterosend = new byte[8];
                    byterosend.WriteTo(0, playerid);
                    byterosend.WriteTo(4, result);
                    if (b)
                    {
                        player.send((byte)CMDPlayer.RECONNECT, byterosend);
                        Logger.log("--playerid-- : " + playerid + "--reconnect-- : " + result);
                    }
                    else
                    {
                        Logger.log("player id not exist ,this should not happen");
                    }
                    break;

                default:
                    break;
                }
            };
        }