private OpcodeFinder() { NetworkController.Instance.UiUpdateKnownOpcode = new Dictionary <OpcodeId, OpcodeEnum> { { 19900, OpcodeEnum.C_CHECK_VERSION }, { 19901, OpcodeEnum.S_CHECK_VERSION } }; NetworkController.Instance.UiUpdateData = new List <ParsedMessage>(); var mainMethod = "Process"; var classes = AppDomain.CurrentDomain.GetAssemblies() .Select(x => x.GetTypes()) .SelectMany(x => x) .Where(x => x.Namespace == typeof(Heuristic.C_CHECK_VERSION).Namespace) .Where(x => x.GetMethod(mainMethod) != null); foreach (Type cl in classes) { var method = cl.GetMethod(mainMethod); var obj = Activator.CreateInstance(method.DeclaringType); if (cl.Name.StartsWith("C_")) { ClientOpcode.Add(method, obj); } else if (cl.Name.StartsWith("S_")) { ServerOpcode.Add(method, obj); } else { throw new Exception("invalid class name"); } } }
protected override void OnMessage(MessageEventArgs e) { try { switch (e.Type) { case Opcode.TEXT: { JArray dcode = JArray.Parse(e.Data); ClientOpcode opcode = (ClientOpcode)int.Parse(dcode[0].ToString()); LogConsole.Show(LogType.DEBUG, "opcode: {0}", opcode); Program.PacketsManager.ProcessBytes(_session, ref dcode); break; } default: { break; } } } catch (Exception Ex) { LogConsole.Show(LogType.ERROR, "{0}", Ex.ToString()); } }
public void OnDataReceived(Socket client, byte[] data, int bytesRead) { ClientState state = _clients[client]; if (state.buffer == null) { if (bytesRead > 1024) { state.buffer = new byte[bytesRead * 2]; } else { state.buffer = new byte[1024]; } } else if (state.buffer.Length < state.bufferSize + bytesRead) { byte[] newBuffer = new byte[(state.bufferSize + bytesRead) * 2]; Array.Copy(state.buffer, 0, newBuffer, 0, state.bufferSize); state.buffer = newBuffer; } Array.Copy(data, 0, state.buffer, state.bufferSize, bytesRead); state.bufferSize += bytesRead; while (state.bufferSize > 3) { ushort size = BitConverter.ToUInt16(state.buffer, 0); ushort opcodeNumber = BitConverter.ToUInt16(state.buffer, 2); if (!Enum.IsDefined(typeof(ClientOpcode), (ClientOpcode)opcodeNumber)) { IPEndPoint endPoint = client.RemoteEndPoint as IPEndPoint; Console.WriteLine("Received unknown opcode from {0}:{1}.", endPoint.Address, endPoint.Port); client.Close(); return; } ClientOpcode opcode = (ClientOpcode)opcodeNumber; if (state.bufferSize >= size + 4) { byte[] packet = new byte[size]; Array.Copy(state.buffer, 4, packet, 0, size); PacketHandler.Handle(state, opcode, packet); Array.Copy(state.buffer, 4 + size, state.buffer, 0, state.bufferSize - size - 4); state.bufferSize -= size + 4; } else { break; } } }
public Packet(ClientOpcode opcode, byte[] data) { if (data.Length > UInt16.MaxValue) { throw new ArgumentException("data.Length > max value"); } _opcode = opcode; _data = data;// ?? throw new NullReferenceException("data is null"); }
public Task SendToUpgradeLayer(ClientOpcode command, string arg) { var pack = new GenericInterop { Command = command.ToString(), Arg = arg, }; return(this.ipc.SendMessage(pack)); }
public static void Handle(ClientState state, ClientOpcode opcode, byte[] packet) { object data = null; if (OpcodesBinding.Handlers[opcode].dataType != null) { data = Serializer.Deserialize(OpcodesBinding.Handlers[opcode].dataType, new MemoryStream(packet)); } OpcodesBinding.Handlers[opcode].handler(state.client, data); }
public static async Task RequestData(ClientOpcode opcode) { if (!OpcodesBinding.RequestResponseOpcodes.ContainsKey(opcode)) { throw new ArgumentException("RequestResponseOpcodes doesn`t contain the response type"); } Packet packet = new Packet(opcode, new byte[0]); Type responseType = OpcodesBinding.RequestResponseOpcodes[opcode]; if (responseType != typeof(EmptyResponse)) { if (!_dataUpdateNotifiers.ContainsKey(responseType)) { _dataUpdateNotifiers[responseType] = new EventWaitHandle(false, EventResetMode.ManualReset); } _dataUpdateNotifiers[responseType].Reset(); } try { await _controllerClient.SendAsync(_client, packet); } catch (SocketException ex) { _connected = false; _client.Disconnect(); throw ex; } if (responseType != typeof(EmptyResponse)) { bool isSignaled = await AsyncFactory.FromWaitHandle( _dataUpdateNotifiers[responseType], TimeSpan.FromMilliseconds(Config.DataTimeout)); if (!isSignaled) { throw new TimeoutException("Server didn`t respont in time"); } } }
public async Task SendAsync(TCPClient client, Packet packet) { if (packet == null) { throw new NullReferenceException("packet is null"); } if (client == null) { throw new NullReferenceException("client is null"); } byte[] data = packet.Data; ClientOpcode opcode = packet.Opcode; byte[] buffer = new byte[data.Length + 4]; Array.Copy(BitConverter.GetBytes((ushort)data.Length), 0, buffer, 0, 2); Array.Copy(BitConverter.GetBytes((ushort)opcode), 0, buffer, 2, 2); Array.Copy(data, 0, buffer, 4, data.Length); await client.SendAsync(buffer, buffer.Length); }
private void RegisterHandler(ClientOpcode opc, Action <BinaryReader> action) { _handlers.Add((Byte)opc, action); }
public static ushort LeToBeUInt16(ClientOpcode value) => (ushort)((ushort)(((ushort)value & 0xff) << 8) | (ushort)value >> 8 & 0xff);
protected override void OnMessage(MessageEventArgs e) { try { switch (e.Type) { case Opcode.TEXT: { JArray dcode = JArray.Parse(e.Data); ClientOpcode opcode = (ClientOpcode)int.Parse(dcode[0].ToString()); LogConsole.Show(LogType.DEBUG, "opcode: {0}", opcode); switch (opcode) { case ClientOpcode.login: { //[0,41,1,"123456789",1] User.version = dcode[1].ToObject <int>(); User.user_id = dcode[2].ToObject <int>(); User.user_key = dcode[3].ToObject <string>(); User.Login(); LogConsole.Show(LogType.DEBUG, "User_Id: {0}", User.user_id); Chat.ChatManager.Notice(User); Chat.ChatManager.UpdateBoddy(this); Channel.SendList(); User.ListFriends(); break; } case ClientOpcode.chat: { string chtm = dcode[1].ToString(); if (User.rank == 24) { string[] cd = chtm.Split(' '); switch (cd[0]) { case "/mobil": { Channel _cha = Program.Chanels.Single(a => a.UserIn(User) == true); if (_cha != null) { int _mobil = int.Parse(cd[1]); _cha.ChangeMobil(User, _mobil); } break; } } } Broadcast(Chat.ChatManager.Msj(chtm, User)); break; } case ClientOpcode.room_create: { //test script Channel _new_c = new Channel(); _new_c.channel_id = new Random().Next(2, 9); _new_c.channel_name = dcode[1].ToObject <string>(); _new_c.channel_password = dcode[2].ToObject <string>(); _new_c.channel_maxplayers = dcode[3].ToObject <int>(); _new_c.channel_gamemode = dcode[4].ToObject <int>(); User.Is_Master = 1; _new_c.Join(User); Program.Chanels.Add(_new_c); Channel.SendList(); break; } case ClientOpcode.room_join: { try { Channel _chan = Program.Chanels.Single(a => a.UserIn(User) == true); if (_chan != null) { _chan.RemoveUser(User); } } catch { } int _sala_id = dcode[1].ToObject <int>(); try { Channel _cha = Program.Chanels.Single(a => a.channel_id == _sala_id); if (_cha != null) { if (_cha.UserIn(User)) { _cha.RemoveUser(User); } _cha.Join(User); } } catch { } break; } case ClientOpcode.channel_join: { try { Channel _chan = Program.Chanels.Single(a => a.UserIn(User) == true); if (_chan != null) { _chan.RemoveUser(User); } } catch { } User.PlayerInfo(); Chat.ChatManager.UpdateBoddy(this); Channel.SendList(); break; } case ClientOpcode.mobile: { Channel _cha = Program.Chanels.Single(a => a.UserIn(User) == true); if (_cha != null) { int _mobil = dcode[1].ToObject <int>(); _cha.ChangeMobil(User, _mobil); } break; } case ClientOpcode.get_my_avatars: { User.GetAvatars(); break; } case ClientOpcode.equip: { User.SendEquip(dcode[1].ToString()); break; } case ClientOpcode.buy: { User.BuyItems(dcode[1].ToObject <string>(), dcode[2].ToObject <string>(), dcode[3].ToObject <int>(), dcode[4].ToObject <int>()); break; } case ClientOpcode.change_name: { User.ChangeName(dcode[1].ToObject <string>()); User.PlayerInfo(); Chat.ChatManager.UpdateBoddy(this); break; } case ClientOpcode.tab: { int idt = dcode[1].ToObject <int>(); switch (idt) { case 0: { Chat.ChatManager.UpdateBoddy(this); break; } case 2: { //User.sep.Send("[19]"); User.Guildinfo(); break; } } break; } case ClientOpcode.guild_create: { string guild_name = dcode[1].ToObject <string>(); LogConsole.Show(LogType.DEBUG, "GuildC: {0}", guild_name); break; } case ClientOpcode.refresh_friends: { User.ListFriends(); break; } case ClientOpcode.refresh_guildies: { User.Guildinfo(); break; } case ClientOpcode.room_change_ready: { Channel _chan = Program.Chanels.Single(a => a.UserIn(User) == true); if (_chan != null) { _chan.ChangeReady(User, dcode[1].ToObject <int>()); } break; } case ClientOpcode.game_start: { Channel _chan = Program.Chanels.Single(a => a.UserIn(User) == true); if (_chan != null) { _chan.GameStart(); } break; } case ClientOpcode.game_move: { Channel _chan = Program.Chanels.Single(a => a.UserIn(User) == true); if (_chan != null) { User.x = dcode[1].ToObject <int>(); User.y = dcode[2].ToObject <int>(); User.body_move = dcode[3].ToObject <int>(); User.look = dcode[4].ToObject <int>(); User.ang = dcode[5].ToObject <int>(); _chan.Move(User); } break; } case ClientOpcode.game_shoot: { Channel _chan = Program.Chanels.Single(a => a.UserIn(User) == true); if (_chan != null) { _chan.GameShoot(User, dcode[1].ToObject <int>(), dcode[2].ToObject <int>(), dcode[3].ToObject <int>(), dcode[4].ToObject <int>(), dcode[5].ToObject <int>(), dcode[6].ToObject <int>(), dcode[7].ToObject <int>(), dcode[8].ToObject <int>()); } break; } case ClientOpcode.game_pass_turn: { //[14,1585,98,-18,0,24,6] //[28,[4,0,1585,98,0,585,1,[],652,-436,18,0,2,0,99]] Channel _chan = Program.Chanels.Single(a => a.UserIn(User) == true); if (_chan != null) { int _uid = dcode[1].ToObject <int>(); int _x = dcode[2].ToObject <int>(); int _y = dcode[3].ToObject <int>(); int _body = dcode[4].ToObject <int>(); int _look = dcode[5].ToObject <int>(); int _angl = dcode[6].ToObject <int>(); int _time = dcode[7].ToObject <int>(); } break; } case ClientOpcode.room_change_team: { Channel _chan = Program.Chanels.Single(a => a.UserIn(User) == true); if (_chan != null) { _chan.ChangeTeam(User, dcode[1].ToObject <string>()); } break; } case ClientOpcode.select_bot: { //[22,1,"0"] slot - id_bot //[35,[2,1,[],1,131702018,"Clown Stripe",0,0,0,1,"m",0,[17,31],1,0]] Channel _chan = Program.Chanels.Single(a => a.UserIn(User) == true); if (_chan != null) { _chan.AddBot(dcode[1].ToObject <int>(), dcode[1].ToObject <string>()); } break; } default: { Console.WriteLine("opcode: {0}", opcode); Console.WriteLine("data: {0}", e.Data); break; } } break; } default: { Console.WriteLine("tp: {0}", e.Type); break; } } } catch (Exception Ex) { LogConsole.Show(LogType.ERROR, "{0}", Ex.ToString()); } }
protected override void OnMessage(MessageEventArgs e) { switch (e.Type) { case Opcode.TEXT: { JArray dcode = JArray.Parse(e.Data); ClientOpcode opc = (ClientOpcode)int.Parse(dcode[0].ToString()); switch (opc) { case ClientOpcode.login: { User.version = dcode[1].ToObject <int>(); User.user_id = dcode[2].ToObject <int>(); User.user_key = dcode[3].ToObject <string>(); User.Login(); Chat.ChatManager.Notice(User); Chat.ChatManager.UpdateBoddy(this); Chanel.SendList(this); break; } case ClientOpcode.chat: { Broadcast(Chat.ChatManager.Msj(dcode[1].ToString(), User)); break; } case ClientOpcode.room_create: { Chanel _new_c = new Chanel(); _new_c.chanel_id = new Random().Next(2, 9); _new_c.chanel_name = dcode[1].ToObject <string>(); _new_c.chanel_password = dcode[2].ToObject <string>(); _new_c.chanel_maxplayers = dcode[3].ToObject <int>(); _new_c.chanel_gamemode = dcode[4].ToObject <int>(); User.Is_Master = 1; _new_c.Join(User); Program.Chanels.Add(_new_c); Chanel.SendList(this); break; } case ClientOpcode.room_join: { int _sala_id = dcode[1].ToObject <int>(); Chanel _cha = Program.Chanels.Single(a => a.chanel_id == _sala_id); if (_cha != null) { _cha.Join(User); } break; } case ClientOpcode.channel_join: { Chanel _chan = Program.Chanels.Single(a => a.UserIn(User) == true); if (_chan != null) { _chan.RemoveUser(User); } User.PlayerInfo(); Chat.ChatManager.UpdateBoddy(this); Chanel.SendList(this); break; } case ClientOpcode.mobile: { Chanel _cha = Program.Chanels.Single(a => a.UserIn(User) == true); if (_cha != null) { int _mobil = dcode[1].ToObject <int>(); _cha.ChangeMobil(User, _mobil); } break; } case ClientOpcode.get_my_avatars: { User.GetAvatars(); break; } case ClientOpcode.equip: { User.SendEquip(dcode[1].ToString()); break; } case ClientOpcode.buy: { User.BuyItems(dcode[1].ToObject <string>(), dcode[2].ToObject <string>(), dcode[3].ToObject <int>(), dcode[4].ToObject <int>()); break; } case ClientOpcode.change_name: { User.ChangeName(dcode[1].ToObject <string>()); User.PlayerInfo(); Chat.ChatManager.UpdateBoddy(this); break; } case ClientOpcode.room_change_ready: { Chanel _chan = Program.Chanels.Single(a => a.UserIn(User) == true); if (_chan != null) { _chan.ChangeReady(User, dcode[1].ToObject <int>()); } break; } case ClientOpcode.game_start: { Chanel _chan = Program.Chanels.Single(a => a.UserIn(User) == true); if (_chan != null) { _chan.GameStart(); } break; } case ClientOpcode.game_move: { Chanel _chan = Program.Chanels.Single(a => a.UserIn(User) == true); if (_chan != null) { User.x = dcode[1].ToObject <int>(); User.y = dcode[2].ToObject <int>(); User.body_move = dcode[3].ToObject <int>(); User.look = dcode[4].ToObject <int>(); User.ang = dcode[5].ToObject <int>(); _chan.Move(User); } break; } case ClientOpcode.game_shoot: { Chanel _chan = Program.Chanels.Single(a => a.UserIn(User) == true); if (_chan != null) { _chan.GameShoot(User, dcode[1].ToObject <int>(), dcode[2].ToObject <int>(), dcode[3].ToObject <int>(), dcode[4].ToObject <int>(), dcode[5].ToObject <int>(), dcode[6].ToObject <int>(), dcode[7].ToObject <int>(), dcode[8].ToObject <int>()); } break; } case ClientOpcode.game_pass_turn: { Chanel _chan = Program.Chanels.Single(a => a.UserIn(User) == true); if (_chan != null) { } break; } case ClientOpcode.room_change_team: { Chanel _chan = Program.Chanels.Single(a => a.UserIn(User) == true); if (_chan != null) { _chan.ChangeTeam(User, dcode[1].ToObject <string>()); } break; } default: { Console.WriteLine("opcode: {0}", opc); Console.WriteLine("data: {0}", e.Data); break; } } break; } default: { Console.WriteLine("tp: {0}", e.Type); break; } } }