コード例 #1
0
        private void HandleAccept(UdpReceiveResult udpReceiveResult)        //server do this
        {
            uint requestConn = BitConverter.ToUInt32(udpReceiveResult.Buffer, 4);

            if (requestChannels.ContainsKey(requestConn))
            {
                if (requestChannels[requestConn].isConnected)
                {
                    KChannel outkc;
                    requestChannels.TryRemove(requestConn, out outkc);
                    return;
                }
            }
            else
            {
                uint newid;
                do
                {
                    newid = this.IdGenerater++;
                }while (idChannels.ContainsKey(newid));
                KChannel channel = new KChannel(newid, requestConn, this.socket, udpReceiveResult.RemoteEndPoint, this);
                requestChannels.TryAdd(channel.requestConn, channel);
                idChannels.TryAdd(channel.Id, channel);
                onAcceptAKchannel.Invoke(ref channel);
            }
            requestChannels[requestConn].HandleAccept();
        }
コード例 #2
0
        public KChannel CreateAConnectChannel(IPEndPoint remotserveripEndPoint)        //client do this
        {
            uint id = (uint)RandomHelper.RandomNumber(100, int.MaxValue);
            //id = IdGenerater++;
            KChannel channel = new KChannel(id, this.socket, remotserveripEndPoint);

            idChannels.TryAdd(channel.requestConn, channel);
            return(channel);
        }
コード例 #3
0
        public MatchSeverProxy(int id, KChannel channel, ServertoDS servertods)
        {
            this.id                    = id;
            this.channel               = channel;
            this.servertods            = servertods;
            this.channel.ondisconnect += () => {
                // Console.WriteLine("ondisconnect");
                MatchSeverProxy matchserverproxy;
                bool            b = servertods.matchserverproxys.TryRemove(id, out matchserverproxy);
                if (b)
                {
                    Logger.log("remove a MatchSeverProxy sucessfully");
                }
                else
                {
                    Logger.log("this should not happen when remove a MatchSeverProxy");
                }
            };
            this.channel.onUserLevelReceivedCompleted += (ref byte[] buffer) =>
            {
                switch ((CMDLoadBalanceServer)buffer[0])
                {
                case CMDLoadBalanceServer.CREATEDS:
                    int roomid = BitConverter.ToInt32(buffer, 1);
                    servertods.GetABestDSM().DS_request(this.id, roomid, CMD.NEW_DS);
                    Logger.log("CMDLoadBalanceServer.CREATEDS MatchSeverid : " + id + "--roomid--" + roomid);
                    break;

                case CMDLoadBalanceServer.CREATEDSV1:
                    roomid = BitConverter.ToInt32(buffer, 1);
                    servertods.GetABestDSM().DS_request(this.id, roomid, CMD.NEW_DSV1);
                    Logger.log("CMDLoadBalanceServer.CREATEDS MatchSeverid : " + id + "--roomid--" + roomid);
                    break;

                case CMDLoadBalanceServer.DESTROY:
                    roomid = BitConverter.ToInt32(buffer, 1);
                    servertods.GetABestDSM().DS_request(this.id, roomid, CMD.KILL_DS);
                    Logger.log("CMDLoadBalanceServer.DESTROY");
                    break;

                case CMDLoadBalanceServer.DESTROYV1:
                    roomid = BitConverter.ToInt32(buffer, 1);
                    servertods.GetABestDSM().DS_request(this.id, roomid, CMD.KILL_DSV1);
                    Logger.log("CMDLoadBalanceServer.DESTROY");
                    break;

                default:
                    break;
                }
            };
        }
コード例 #4
0
        public void createchannel()
        {
            channel = Session.getnew().GetChannel(new IPEndPoint(ipAd, port));
            channel.onUserLevelReceivedCompleted += (ref byte[] buffer) => {
#if RTT
                var str = System.Text.Encoding.UTF8.GetString(buffer);
                str += TimeHelper.Now();
                string[] sd = new string[1];
                sd[0] = "server";
                string[] sa = str.Split(sd, StringSplitOptions.RemoveEmptyEntries);
                string   l1 = sa[0].Substring(9);
                string   l2 = sa[1].Substring(9);
                int      r  = Int32.Parse(l1);
                int      r1 = Int32.Parse(l2);
                int      rr = r1 - r;
                Logger.log(rr.ToString());
                //Console.WriteLine(str);
#else
                switch ((CMDPlayer)buffer[0])
                {
                case CMDPlayer.SINGUP:
                    Logger.log("Sing up ok");
                    break;

                case CMDPlayer.LOGIN:
                    id = BitConverter.ToInt32(buffer, 1);
                    string infor = Encoding.getstring(buffer, 5, buffer.Length - 5);
                    Logger.log("log in ok ,id : " + id + "infor : " + infor);
                    break;

                case CMDPlayer.CREATEROOM:
                    roomid = roomnumber = BitConverter.ToInt32(buffer, 1);
                    Logger.log("creatroom : " + roomnumber);
                    break;

                case CMDPlayer.JOINROOM:
                    roomid = roomnumber = BitConverter.ToInt32(buffer, 1);
                    Logger.log("joinroom : " + roomnumber);
                    break;

                case CMDPlayer.JOINROOMFAILED:
                    roomnumber = BitConverter.ToInt32(buffer, 1);
                    Logger.log("joinroom failed: " + roomnumber);
                    break;

                case CMDPlayer.STARTGAME:
                    int side   = BitConverter.ToInt32(buffer, 1);
                    int dsport = BitConverter.ToInt32(buffer, 5);
                    roomid = BitConverter.ToInt32(buffer, 9);
                    string dswan = Encoding.getstring(buffer, 13, buffer.Length - 13);
                    Logger.log("STARTGAME player : --side-- : " + side + "--dsport--" + dsport + "--dswan-- " + dswan + "--roomid-- " + roomid);
                    break;

                case CMDPlayer.MATCHREQUEST:
                    side   = BitConverter.ToInt32(buffer, 1);
                    dsport = BitConverter.ToInt32(buffer, 5);
                    roomid = BitConverter.ToInt32(buffer, 9);
                    dswan  = Encoding.getstring(buffer, 13, buffer.Length - 13);
                    Logger.log("MATCHREQUEST player : --side-- : " + side + "--dsport--" + dsport + "--dswan-- " + dswan + "--roomid-- " + roomid);
                    break;

                case CMDPlayer.OTHERPLAYERINFOR:
                    int    playerid    = BitConverter.ToInt32(buffer, 1);
                    int    playerside  = BitConverter.ToInt32(buffer, 5);
                    int    oldplayerid = BitConverter.ToInt32(buffer, 9);
                    string playerinfor = Encoding.getstring(buffer, 13, buffer.Length - 13);
                    Logger.log("OTHERPLAYERINFOR  otherplayerid : " + playerid + " otheroldplayerid : " + oldplayerid + " playerside : " + (int)playerside + " playerinfor : " + playerinfor);
                    break;

                case CMDPlayer.RECONNECTLOGIN:
                    id = BitConverter.ToInt32(buffer, 1);
                    Logger.log("RECONNECTLOGIN : "******"RECONNECT : " + result);     //failed when disconnected happen  when the roomid created but has not received by the client,so the client should launch a rematch
                    break;

                case CMDPlayer.RECONNECTV1:
                    id     = BitConverter.ToInt32(buffer, 1);
                    result = BitConverter.ToInt32(buffer, 5);
                    Logger.log("RECONNECTV1 : " + result);
                    break;

                case CMDPlayer.CHECKCONNECTION:
                    b_checkconnectionok = true;
                    Logger.log("CHECKCONNECTION ");
                    break;

                default:
                    break;
                }
#endif
            };
        }
コード例 #5
0
ファイル: Player.cs プロジェクト: liupanfengfreedom/DSManager
        public Player(int id, KChannel channel, LoginServer loginserver)
        {
            this.id                    = id;
            this.channel               = channel;
            this.loginserver           = loginserver;
            this.channel.ondisconnect += () => {
                Player v;
                this.loginserver.Players.TryRemove(id, out v);
                loginserver.sendtomatchserver((byte)CMDMatchServer.PLAYEREXITQUEST, BitConverter.GetBytes(id));
                Logger.log(id + " :ondisconnect");
            };
            this.channel.onUserLevelReceivedCompleted += (ref byte[] buffer) =>
            {
#if RTT
                var str = System.Text.Encoding.UTF8.GetString(buffer);
                //Console.WriteLine(str);
                ASCIIEncoding asen    = new ASCIIEncoding();
                byte[]        buffer1 = asen.GetBytes(str + "server");
                send(ref buffer1);
#else
                switch ((CMDPlayer)buffer[0])
                {
                case CMDPlayer.SINGUP:
                    Logger.log("Sing up ");
                    //write data base
                    break;

                case CMDPlayer.LOGIN:
                    Logger.log("log in ");
                    var str = Encoding.getstring(buffer, 1, buffer.Length - 1);
                    Logger.log(str);    //username password ,these infor maybe used to query database
                    ////////////////////////////////////////////////////////////////////
                    //read data base
                    //simulate playerinfor
                    int simulateddata;
                    for (int i = 0; i < 2; i++)
                    {
                        simulateddata = RandomHelper.RandomNumber(1, 3);
                        playerinfor  += simulateddata.ToString() + "???";
                    }
                    ////////////////////////////////////////////////////////////////////////////////////
                    //ack
                    byte[] infor = Encoding.getbyte(playerinfor);
                    byte[] t     = new byte[playerinfor.Length + 4];
                    t.WriteTo(0, id);
                    Array.Copy(infor, 0, t, 4, infor.Length);
                    send((byte)CMDPlayer.LOGIN, t);
                    break;

                case CMDPlayer.MATCHREQUEST:
                    int halfroomnumber = BitConverter.ToInt32(buffer, 1);
                    Logger.log("match ");
                    Logger.log("halfroomnumber :" + halfroomnumber);
                    Logger.log(playerinfor);
                    var playerinfor_ = new playerinfor {
                        playerid         = id,
                        SimulateInforStr = playerinfor + halfroomnumber + "???",
                        SimulateInforInt = 1234
                    };
                    MemoryStream ms = new MemoryStream();
                    Serializer.Serialize(ms, playerinfor_);
                    loginserver.sendtomatchserver((byte)CMDMatchServer.MATCHREQUEST, ms.ToArray());
                    break;

                case CMDPlayer.CREATEROOM:
                    Logger.log("createroom ");
                    Logger.log(playerinfor);
                    playerinfor_ = new playerinfor
                    {
                        playerid         = id,
                        homeowner        = true,
                        SimulateInforStr = playerinfor,
                        halfroomnumber   = BitConverter.ToInt32(buffer, 1),  //here the halfroomnumber seem to be useless
                    };
                    ms = new MemoryStream();
                    Serializer.Serialize(ms, playerinfor_);
                    loginserver.sendtomatchserver((byte)CMDMatchServer.CREATEROOM, ms.ToArray());
                    break;

                case CMDPlayer.JOINROOM:
                    Logger.log("joinroom ");
                    Logger.log(playerinfor);
                    playerinfor_ = new playerinfor
                    {
                        playerid         = id,
                        homeowner        = false,
                        SimulateInforStr = playerinfor,
                        roomnumber       = BitConverter.ToInt32(buffer, 1),
                    };
                    ms = new MemoryStream();
                    Serializer.Serialize(ms, playerinfor_);
                    loginserver.sendtomatchserver((byte)CMDMatchServer.JOINROOM, ms.ToArray());
                    break;

                case CMDPlayer.STARTGAME:
                    Logger.log("startgame ");
                    Logger.log(playerinfor);
                    playerinfor_ = new playerinfor
                    {
                        playerid   = id,
                        homeowner  = true,
                        roomnumber = BitConverter.ToInt32(buffer, 1),
                    };
                    ms = new MemoryStream();
                    Serializer.Serialize(ms, playerinfor_);
                    loginserver.sendtomatchserver((byte)CMDMatchServer.STARTGAME, ms.ToArray());
                    break;

                case CMDPlayer.EXITREQUEST:
                    Logger.log("CMDPlayer.EXITREQUEST");
                    loginserver.sendtomatchserver((byte)CMDMatchServer.PLAYEREXITQUEST, BitConverter.GetBytes(id));

                    break;

                case CMDPlayer.RECONNECTLOGIN:    //
                    Logger.log("CMDPlayer.RECONNECTLOGIN");
                    str = Encoding.getstring(buffer, 1, buffer.Length - 1);
                    Logger.log(str);    //username password ,these infor maybe used to query database
                    ////////////////////////////////////////////////////////////////////
                    //read data base
                    //simulate playerinfor
                    for (int i = 0; i < 2; i++)
                    {
                        simulateddata = RandomHelper.RandomNumber(1, 3);
                        playerinfor  += simulateddata.ToString() + "???";
                    }
                    send((byte)CMDPlayer.RECONNECTLOGIN, BitConverter.GetBytes(id));
                    break;

                /*
                 * server side : here may just for ios device when front_back switch this player will be die so replace it
                 * client side : when front_back switch first check the connection to DS ,if it is connecting then do nothing ,if it is disconnect then reconnect to ds directly
                 */
                case CMDPlayer.RECONNECT:    //
                    Logger.log("CMDPlayer.RECONNECT");
                    int id_    = BitConverter.ToInt32(buffer, 1);
                    int roomid = BitConverter.ToInt32(buffer, 5);
                    halfroomnumber = BitConverter.ToInt32(buffer, 9);
                    str            = Encoding.getstring(buffer, 13, buffer.Length - 13);
                    Logger.log(str);    //username password ,these infor maybe used to query database
                    ////////////////////////////////////////////////////////////////////
                    //read data base
                    //simulate playerinfor
                    for (int i = 0; i < 2; i++)
                    {
                        simulateddata = RandomHelper.RandomNumber(1, 3);
                        playerinfor  += simulateddata.ToString() + "???";
                    }
                    playerinfor_ = new playerinfor
                    {
                        playerid         = id,
                        oldplayerid      = id_,
                        roomid           = roomid,
                        SimulateInforStr = playerinfor + halfroomnumber + "???",
                        SimulateInforInt = 1234
                    };
                    ms = new MemoryStream();
                    Serializer.Serialize(ms, playerinfor_);
                    loginserver.sendtomatchserver((byte)CMDMatchServer.RECONNECT, ms.ToArray());
                    break;

                case CMDPlayer.RECONNECTV1:    //
                    Logger.log("CMDPlayer.RECONNECTv1");
                    id_    = BitConverter.ToInt32(buffer, 1);
                    roomid = BitConverter.ToInt32(buffer, 5);
                    int owner = BitConverter.ToInt32(buffer, 9);
                    str = Encoding.getstring(buffer, 13, buffer.Length - 13);
                    Logger.log(str);    //username password ,these infor maybe used to query database
                    ////////////////////////////////////////////////////////////////////
                    //read data base
                    //simulate playerinfor
                    for (int i = 0; i < 2; i++)
                    {
                        simulateddata = RandomHelper.RandomNumber(1, 3);
                        playerinfor  += simulateddata.ToString() + "???";
                    }
                    playerinfor_ = new playerinfor
                    {
                        playerid         = id,
                        oldplayerid      = id_,
                        roomid           = roomid,
                        homeowner        = owner == 1? true : false,
                        SimulateInforStr = playerinfor,
                        SimulateInforInt = 1234
                    };
                    ms = new MemoryStream();
                    Serializer.Serialize(ms, playerinfor_);
                    loginserver.sendtomatchserver((byte)CMDMatchServer.RECONNECTV1, ms.ToArray());
                    break;

                case CMDPlayer.CHECKCONNECTION:    //
                    Logger.log("CMDPlayer.CHECKCONNECTION");
                    send((byte)CMDPlayer.CHECKCONNECTION, BitConverter.GetBytes(0));
                    break;

                default:
                    break;
                }
#endif
            };
        }
コード例 #6
0
        public DSMproxy(KChannel channel, ServertoDS servertods)
        {
            this.channel               = channel;
            this.servertods            = servertods;
            this.channel.ondisconnect += () => {
                this.servertods.DSMchannels.Remove(this);
                // Console.WriteLine("ondisconnect");
                Logger.log("ondisconnect");
            };
            this.channel.onUserLevelReceivedCompleted += (ref byte[] buffer) =>
            {
#if RTT
                var str = System.Text.Encoding.UTF8.GetString(buffer);
                //Console.WriteLine(str);
                ASCIIEncoding asen    = new ASCIIEncoding();
                byte[]        buffer1 = asen.GetBytes(str + "server");
                send(ref buffer1);
#else
                switch ((CMD)buffer[0])
                {
                case CMD.WANIP:
                    wan = new byte[buffer.Length - 1];
                    Array.Copy(buffer, 1, wan, 0, wan.Length);
                    break;

                case CMD.NEW_DS:
                    int             matchserverid = BitConverter.ToInt32(buffer, 1);
                    int             roomid        = BitConverter.ToInt32(buffer, 5);
                    int             port          = BitConverter.ToInt32(buffer, 9);
                    MatchSeverProxy matchserverproxy;
                    servertods.matchserverproxys.TryGetValue(matchserverid, out matchserverproxy);
                    byte[] sumbuffer = new byte[8 + wan.Length];
                    sumbuffer.WriteTo(0, roomid);
                    sumbuffer.WriteTo(4, port);
                    Array.Copy(wan, 0, sumbuffer, 8, wan.Length);
                    matchserverproxy.send((byte)CMDLoadBalanceServer.CREATEDS, sumbuffer);
                    Logger.log("matchserverid :" + matchserverid + " -- roomid :" + roomid + " -- port : " + port);
                    break;

                case CMD.NEW_DSV1:
                    matchserverid = BitConverter.ToInt32(buffer, 1);
                    roomid        = BitConverter.ToInt32(buffer, 5);
                    port          = BitConverter.ToInt32(buffer, 9);
                    servertods.matchserverproxys.TryGetValue(matchserverid, out matchserverproxy);
                    sumbuffer = new byte[8 + wan.Length];
                    sumbuffer.WriteTo(0, roomid);
                    sumbuffer.WriteTo(4, port);
                    Array.Copy(wan, 0, sumbuffer, 8, wan.Length);
                    matchserverproxy.send((byte)CMDLoadBalanceServer.CREATEDSV1, sumbuffer);
                    Logger.log("matchserverid :" + matchserverid + " -- roomid :" + roomid + " -- port : " + port);
                    break;

                case CMD.KILL_DS:
                    break;

                default:
                    break;
                }
#endif
            };
            send((byte)CMD.WANIP, BitConverter.GetBytes(0));
        }
コード例 #7
0
        public DSClient() : base("DSClient")
        {
            ts  = GetValueFromLua <string>("testmessage");
            wan = GetValueFromLua <string>("wan");

            LuaTable  remoteserver = GetValueFromLua <LuaTable>("remoteserver");
            string    nettype      = (string)remoteserver["nettype"];
            LuaTable  serveraddr   = (LuaTable)remoteserver[nettype];
            string    serverip     = (string)serveraddr["serverip"];
            int       port         = (int)(Int64)serveraddr["port"];
            IPAddress ipAd         = IPAddress.Parse(serverip);//local ip address  "172.16.5.188"

            channel = Session.getnew().GetChannel(new IPEndPoint(ipAd, port));
            channel.onUserLevelReceivedCompleted += (ref byte[] buffer) => {
#if RTT
                var str = System.Text.Encoding.UTF8.GetString(buffer);
                str += TimeHelper.Now();
                string[] sd = new string[1];
                sd[0] = "server";
                string[] sa = str.Split(sd, StringSplitOptions.RemoveEmptyEntries);
                string   l1 = sa[0].Substring(9);
                string   l2 = sa[1].Substring(9);
                int      r  = Int32.Parse(l1);
                int      r1 = Int32.Parse(l2);
                int      rr = r1 - r;
                Logger.log(rr.ToString());
                //Console.WriteLine(str);
#else
                switch ((CMD)buffer[0])
                {
                case CMD.WANIP:
                    send((byte)CMD.WANIP, Encoding.getbyte(wan));
                    break;

                case CMD.NEW_DS:
                    int matchserverid = BitConverter.ToInt32(buffer, 1);
                    int roomid        = BitConverter.ToInt32(buffer, 5);

                    createRooms.TryAdd(roomid, new roomcreatinfor(roomid, matchserverid));

                    //dsinfor infor = DSManager.GetSingleton().LaunchADS(roomid);
                    //byte[] sumbuffer = new byte[12];
                    //sumbuffer.WriteTo(0, matchserverid);
                    //sumbuffer.WriteTo(4, roomid);
                    //sumbuffer.WriteTo(8, infor.port);
                    //send((byte)CMD.NEW_DS, sumbuffer);
                    break;

                case CMD.NEW_DSV1:
                    matchserverid = BitConverter.ToInt32(buffer, 1);
                    roomid        = BitConverter.ToInt32(buffer, 5);
                    createRoomsv1.TryAdd(roomid, new roomcreatinfor(roomid, matchserverid));

                    //infor = DSManager.GetSingleton().LaunchADSV1(roomid);
                    //sumbuffer = new byte[12];
                    //sumbuffer.WriteTo(0, matchserverid);
                    //sumbuffer.WriteTo(4, roomid);
                    //sumbuffer.WriteTo(8, infor.port);
                    //send((byte)CMD.NEW_DSV1, sumbuffer);
                    break;

                case CMD.KILL_DS:
                    matchserverid = BitConverter.ToInt32(buffer, 1);
                    roomid        = BitConverter.ToInt32(buffer, 5);
                    // destoryRooms.TryAdd(roomid, new roomcreatinfor(roomid, matchserverid));
                    DSManager.GetSingleton().killds(roomid);
                    break;

                case CMD.KILL_DSV1:
                    matchserverid = BitConverter.ToInt32(buffer, 1);
                    roomid        = BitConverter.ToInt32(buffer, 5);
                    //destoryRoomsv1.TryAdd(roomid, new roomcreatinfor(roomid, matchserverid));

                    DSManager.GetSingleton().killdsV1(roomid);
                    Logger.log("kill " + roomid);
                    break;

                default:
                    break;
                }
#endif
            };
            Task.Run(async() =>
            {
                try
                {
                    while (true)
                    {
                        foreach (var v in createRooms)
                        {
                            dsinfor infor    = DSManager.GetSingleton().LaunchADS(v.Value.roomid);
                            byte[] sumbuffer = new byte[12];
                            sumbuffer.WriteTo(0, v.Value.matchserverid);
                            sumbuffer.WriteTo(4, v.Value.roomid);
                            sumbuffer.WriteTo(8, infor.port);
                            send((byte)CMD.NEW_DS, sumbuffer);
                            roomcreatinfor outrc;
                            createRooms.TryRemove(v.Key, out outrc);
                            break;
                        }
                        foreach (var v in createRoomsv1)
                        {
                            dsinfor infor    = DSManager.GetSingleton().LaunchADSV1(v.Value.roomid);
                            byte[] sumbuffer = new byte[12];
                            sumbuffer.WriteTo(0, v.Value.matchserverid);
                            sumbuffer.WriteTo(4, v.Value.roomid);
                            sumbuffer.WriteTo(8, infor.port);
                            send((byte)CMD.NEW_DSV1, sumbuffer);
                            roomcreatinfor outrc;
                            createRoomsv1.TryRemove(v.Key, out outrc);
                            break;
                        }

                        //foreach (var v in destoryRooms)
                        //{
                        //    DSManager.GetSingleton().killds(v.Key);
                        //    roomcreatinfor outrc;
                        //    destoryRooms.TryRemove(v.Key, out outrc);
                        //    break;
                        //}
                        //foreach (var v in destoryRoomsv1)
                        //{
                        //    DSManager.GetSingleton().killdsV1(v.Key);
                        //    roomcreatinfor outrc;
                        //    destoryRoomsv1.TryRemove(v.Key, out outrc);
                        //    break;
                        //}
                        await Task.Delay(500);
                    }
                }
                catch (Exception e)
                {
                    Logger.log(e.ToString());
                }
            });
        }
コード例 #8
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;
                }
            };
        }
コード例 #9
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;
                }
            };
        }
コード例 #10
0
        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;
                }
            };
        }