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 }; }
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 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()); } }); }
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; } }; }