예제 #1
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;
                }
            };
        }