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"); } } }
public static void Handle(ServerOpcode opcode, byte[] packet) { Type type = OpcodesBinding.OpcodeTypes[opcode]; object data = Serializer.Deserialize(type, new MemoryStream(packet)); DataStorage.UpdateData(type, data); }
public void SendPacket(Socket sender, ServerOpcode opcode, byte[] data, uint sourceId = 0, uint targetId = 0) { sender.Send(new Packet(new GamePacket { Opcode = (ushort)opcode, Data = data }).ToBytes()); }
public static void Handle(ServerOpcode opcode, byte[] packet) { HandleData handleData = OpcodesBinding.Handlers[opcode]; Type type = handleData.dataType; object data = Serializer.Deserialize(type, new MemoryStream(packet)); handleData.handler(data); }
public Packet(ServerOpcode 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 void OnDataReceived(Socket server, byte[] data, int bytesRead) { if (_buffer == null) { if (bytesRead > 1024) { _buffer = new byte[bytesRead * 2]; } else { _buffer = new byte[1024]; } } else if (_buffer.Length < _bufferSize + bytesRead) { byte[] newBuffer = new byte[(_bufferSize + bytesRead) * 2]; Array.Copy(_buffer, 0, newBuffer, 0, _bufferSize); _buffer = newBuffer; } Array.Copy(data, 0, _buffer, _bufferSize, bytesRead); _bufferSize += bytesRead; while (_bufferSize > 3) { ushort size = BitConverter.ToUInt16(_buffer, 0); ushort opcodeNumber = BitConverter.ToUInt16(_buffer, 2); if (!Enum.IsDefined(typeof(ServerOpcode), (ServerOpcode)opcodeNumber)) { server.Close(); return; } ServerOpcode opcode = (ServerOpcode)opcodeNumber; if (_bufferSize >= size + 4) { byte[] packet = new byte[size]; Array.Copy(_buffer, 4, packet, 0, size); PacketHandler.Handle(opcode, packet); Array.Copy(_buffer, 4 + size, _buffer, 0, _bufferSize - size - 4); _bufferSize -= size + 4; } else { break; } } }
protected void SendPacket(Socket handler, ServerOpcode opcode, byte[] data) { uint characterId = User.Instance.Character.Id; GamePacket gamePacket = new GamePacket { Opcode = (ushort)opcode, Data = data }; Packet packet = new Packet(new SubPacket(gamePacket)); handler.Send(packet.ToBytes()); }
/// <summary> /// Send an inventory packet. /// </summary> /// <param name="handler"></param> /// <param name="opcode"></param> /// <param name="data"></param> private void SendPacket(Socket handler, ServerOpcode opcode, byte[] data) { GamePacket gamePacket = new GamePacket { Opcode = (ushort)opcode, Data = data }; Packet packet = new Packet(new SubPacket(gamePacket) { SourceId = User.Instance.Character.Id, TargetId = User.Instance.Character.Id }); handler.Send(packet.ToBytes()); }
/// <summary> /// Send change gear result packets and updates character appearance. /// </summary> /// <param name="sender"></param> /// <param name="opcode"></param> /// <param name="gearSlot"></param> /// <param name="invSlot"></param> private void SendChangeGearResult(Socket sender, ServerOpcode opcode, byte gearSlot, byte invSlot) { InventoryStart(sender); ChunkStart(sender, InventoryMaxSlots.Equipment, InventoryType.Equipment); byte[] data = new byte[0x08]; Buffer.BlockCopy(BitConverter.GetBytes(gearSlot), 0, data, 0, 1); Buffer.BlockCopy(BitConverter.GetBytes(invSlot), 0, data, 2, 1); SendPacket(sender, opcode, data); ChunkEnd(sender); InventoryEnd(sender); Update(sender); User.Instance.Character.SetAppearance(sender); }
/// <summary> /// Send one chunk of equipment slots. /// </summary> /// <param name="sender"></param> /// <param name="opcode"></param> /// <param name="chunkSize"></param> /// <param name="numSlots"></param> /// <param name="index"></param> /// <returns></returns> private int SendGearSlotChunk(Socket sender, ServerOpcode opcode, int chunkSize, int numSlots, ref int index) { int numChunks = numSlots < chunkSize ? 1 : numSlots / chunkSize; int gearSlotSize = 0x06; //writes numChunks number of packets of chunkSize size. for (int i = 0; i < numChunks; i++) { using (MemoryStream data = new MemoryStream()) { int numSlotsInChunk = 0; //write slots for (int j = 0; j < chunkSize; j++) { if ((index + j) > (GearSlots.Count - 1)) { int zeroFillerSize = gearSlotSize * (chunkSize - numSlots); //how many empty slots we have to write data.Write(new byte[zeroFillerSize], 0, zeroFillerSize); break; } else { data.Write(BitConverter.GetBytes(GearSlots.ElementAt(index + j).Key), 0, sizeof(ushort)); data.Write(BitConverter.GetBytes(GearSlots.ElementAt(index + j).Value), 0, sizeof(ushort)); data.Write(BitConverter.GetBytes((ushort)0), 0, sizeof(ushort)); numSlotsInChunk++; } } if (chunkSize == 8) { data.Write(BitConverter.GetBytes((long)numSlotsInChunk), 0, sizeof(long)); } //send chunk SendChunk(sender, opcode, data.ToArray()); } index += chunkSize; } return(numSlots < chunkSize ? 0 : numSlots - (chunkSize * numChunks)); }
/// <summary> /// Calculate how many chunk packets of the given size need to be sent and send them. /// </summary> /// <param name="sender"></param> /// <param name="opcode"></param> /// <param name="chunkSize"></param> /// <param name="numItems"></param> /// <param name="index"></param> /// <param name="itemList"></param> /// <returns></returns> private int SendItemsChunk(Socket sender, ServerOpcode opcode, int chunkSize, int numItems, ref int index, ref List <Item> itemList) { int numChunks = numItems < chunkSize ? 1 : numItems / chunkSize; int itemSlotSize = 0x70; //writes numChunks number of packets of chunkSize size. for (int i = 0; i < numChunks; i++) { int numItemsInChunk = 0; using (MemoryStream data = new MemoryStream()) { //write items for (int j = 0; j < chunkSize; j++) { if ((index + j) > (itemList.Count - 1)) { int zeroFillerSize = itemSlotSize * (chunkSize - numItems); //how many empty slots we have to write data.Write(new byte[zeroFillerSize], 0, zeroFillerSize); break; } else { data.Write(itemList[index + j].ToBytes(), 0, itemSlotSize); numItemsInChunk++; } } if (chunkSize == 8) { data.Write(BitConverter.GetBytes((long)numItemsInChunk), 0, sizeof(long)); } //send chunk SendChunk(sender, opcode, data.ToArray()); } index += chunkSize; } return(numItems < chunkSize ? 0 : numItems - (chunkSize * numChunks)); }
public void SendPacket(Socket sender, ServerOpcode opcode, byte[] data, uint sourceId = 0, uint targetId = 0) { GamePacket gamePacket = new GamePacket { Opcode = (ushort)opcode, Data = data }; if (sourceId == 0) { sourceId = Id; } //Packet packet = new Packet(new SubPacket(gamePacket) { SourceId = sourceId > 0 ? sourceId : Id, TargetId = targetId > 0 ? targetId : TargetId }); Packet packet = new Packet(new SubPacket(gamePacket) { SourceId = Id, TargetId = TargetId }); sender.Send(packet.ToBytes()); }
public void Send(Socket 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; ServerOpcode 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); Server.Send(client, buffer, buffer.Length); }
public HandlerAttribute(ServerOpcode opcode, HandlerPermission permission) => (Opcode, Permission) = (opcode, permission);
public OutPacket(ServerOpcode operationCode) : this((ushort)operationCode) { }
public static ushort LeToBeUInt16(ServerOpcode value) => (ushort)((ushort)(((ushort)value & 0xff) << 8) | (ushort)value >> 8 & 0xff);
private void RegisterHandler(ServerOpcode opc, Action <BinaryReader> action) { _handlers.Add((Byte)opc, action); }
public void SendTextSheetMessage(Socket sender, ServerOpcode opcode, byte[] data) => SendPacket(sender, opcode, data);
private void SendChunk(Socket sender, ServerOpcode opcode, byte[] data) => SendPacket(sender, opcode, data);
public void Send(ServerOpcode opcode) { Packet packet = new Packet(opcode, new byte[0]); _server.Send(Socket, packet); }