void Register(Type packettype, ServerPacketId PacketID, bool isFixedLength, ushort fixedlength) { MethodInfo method = packettype.GetMethod("Instantiate", BindingFlags.Static | BindingFlags.NonPublic); ServerPacketInstanciator factory; if (method == null) { factory = null; } else { try { factory = Delegate.CreateDelegate(typeof(ServerPacketInstanciator), method) as ServerPacketInstanciator; } catch (Exception ex) { throw new InvalidPacketException(packettype, string.Format("Server Packet {0} must implement or omit internal static Instantiate(PacketReader reader)", packettype), ex); } } if (_registry.ContainsKey(PacketID)) { throw new DuplicatePacketRegistrationException(packettype, _registry[PacketID].Type); } else { _registry.Add(PacketID, new ServerPacketInfo() { packetid = PacketID, isFixedLen = isFixedLength, FixedLength = fixedlength, Type = packettype, Factory = factory }); } }
/// <summary> /// Register a packet for the given ID. /// </summary> /// <param name="packetId">The server packet ID.</param> /// <param name="handler">The handler for the data.</param> /// <typeparam name="T">The type of the packet data passed as parameter to the handler.</typeparam> public void RegisterServerPacketHandler <T>( ServerPacketId packetId, GenericServerPacketHandler <T> handler ) where T : IPacketData => RegisterServerPacketHandler( packetId, (id, iPacket) => handler(id, (T)iPacket) );
internal ServerPacketInfo GetInfoForRegisteredPacketID(ServerPacketId packetid) { ServerPacketInfo toReturn; _registry.TryGetValue(packetid, out toReturn); return(toReturn); }
void Register <T>() where T : ServerPacket { ServerPacketId packetid = ServerPacket.GetPacketID <T>(); bool isfixedlength = !ServerPacket.GetIsDynamicLength <T>(); ushort fixedlength = ServerPacket.GetFixedPacketLength <T>(); Register(typeof(T), packetid, isfixedlength, fixedlength); }
public void ServerPacketPacketIDTest() { ServerPacket target = new FixedLenPacket(); ServerPacketId expected = FixedLenPacketId; ServerPacketId actual = target.PacketID; Assert.AreEqual(expected, actual); }
byte[] CreatePacket <T>(IPacket <T> data, ServerPacketId Id) { byte[] msg = data.GetPacketData(); byte[] header = CreateHeader(data, Id); byte[] packet = CombineByte(header, msg); return(packet); }
public void RegisterServerPacketHandler(ServerPacketId packetId, EmptyServerPacketHandler handler) { if (_serverPacketHandlers.ContainsKey(packetId)) { Logger.Get().Error(this, $"Tried to register already existing client packet handler: {packetId}"); return; } _serverPacketHandlers[packetId] = (id, iPacket) => { handler(id); }; }
/// <summary> /// Register a packet handler for the given ID. /// </summary> /// <param name="packetId">The server packet ID.</param> /// <param name="handler">The handler for the data.</param> private void RegisterServerPacketHandler(ServerPacketId packetId, ServerPacketHandler handler) { if (_serverPacketHandlers.ContainsKey(packetId)) { Logger.Get().Error(this, $"Tried to register already existing client packet handler: {packetId}"); return; } _serverPacketHandlers[packetId] = handler; }
public void DeregisterServerPacketHandler(ServerPacketId packetId) { if (!_serverPacketHandlers.ContainsKey(packetId)) { Logger.Error(this, $"Tried to remove non-existent server packet handler: {packetId}"); return; } _serverPacketHandlers.Remove(packetId); }
byte[] CreateResultPacket(byte[] msg, ServerPacketId Id) { HeaderData headerData = new HeaderData(); HeaderSerializer HeaderSerializer = new HeaderSerializer(); headerData.Id = (byte)Id; headerData.length = (short)msg.Length; HeaderSerializer.Serialize(headerData); msg = CombineByte(HeaderSerializer.GetSerializedData(), msg); return(msg); }
public ServerPacketAttribute(ServerPacketId packetId, ushort fixedLength = 0, string startversion = null, string endversion = null) #endif { PacketID = packetId; isFixedLength = fixedLength > 0; FixedLength = fixedLength; StartVersion = startversion == null?ClientVersion.Instantiate(ClientVersionStruct.Min) : ClientVersion.Instantiate(startversion); EndVersion = endversion == null?ClientVersion.Instantiate(ClientVersionStruct.Max) : ClientVersion.Instantiate(endversion); }
public void RegisterPacketWithoutFactoryTest() { ServerPacketFactory_Accessor target = new ServerPacketFactory_Accessor(version); target.Register <RegisterablePacketWithoutInstanciator>(); ServerPacketId id = ServerPacket.GetPacketID <RegisterablePacketWithoutInstanciator>(); var info = target.GetInfoForRegisteredPacketID(id); Assert.IsNull(info.Factory); }
/// <summary> /// Sends only packetId /// </summary> private static void SendOnlyPacketId(long connectionId, ServerPacketId packetId) { //New packet ByteBuffer buffer = new ByteBuffer(); //Add packet id buffer.WriteLong((long)packetId); //Send packet SendDataTo(connectionId, buffer.ToArray()); }
public void RegisterServerPacketHandler <T>(ServerPacketId packetId, GenericServerPacketHandler <T> packetHandler) where T : IPacketData { if (_serverPacketHandlers.ContainsKey(packetId)) { Logger.Get().Error(this, $"Tried to register already existing server packet handler: {packetId}"); return; } // We can't store these kinds of generic delegates in a dictionary, // so we wrap it in a function that casts it _serverPacketHandlers[packetId] = (id, iPacket) => { packetHandler(id, (T)iPacket); }; }
public void DataHandle() { if (receiveMsgs.Count != 0) { //패킷을 Dequeue 한다 //패킷 : 메시지 타입 + 메시지 내용 DataPacket packet = receiveMsgs.Dequeue(); msg = packet.msg; ipEndPoint = packet.endPoint; Debug.Log("Dequeue Message Length : " + msg.Length); //출처 분리 byte source = msg[0]; //타입 분리 byte[] Id = DataReceiver.ResizeByteArray(1, NetworkManager.packetId, ref msg); HeaderData headerData = new HeaderData(); Debug.Log(source); Debug.Log(Id[0]); if (source == (byte)Source.ServerSource) { if (server_notifier.TryGetValue(Id[0], out serverRecvNotifier)) { ServerPacketId packetId = serverRecvNotifier(msg); } else { Debug.Log("DataHandler::Server.TryGetValue 에러 " + Id); headerData.Id = (byte)ServerPacketId.None; } } else if (source == (byte)Source.ClientSource) { if (p2p_notifier.TryGetValue(Id[0], out p2pRecvNotifier)) { P2PPacketId packetId = p2pRecvNotifier(msg); } else { Debug.Log("DataHandler::P2P.TryGetValue 에러 " + Id); headerData.Id = (byte)P2PPacketId.None; } } } }
public static byte[] BuildConsoleDataPacketBuffer(string text) { ServerPacketId packetid = ServerPacketId.AdminConsoleData; ushort packetLen = (ushort)(4 + text.Length); byte[] packetBytes = new byte[packetLen]; packetBytes[0] = (byte)packetid; packetBytes[1] = (byte)((packetLen & 0xFF00) << 8); packetBytes[2] = (byte)(packetLen & 0xFF); packetBytes[3] = (byte)ConsoleDataPacket.DataType.String; Utility.WriteStringToBuffer(text, packetBytes, 4); return(packetBytes); }
byte[] CreateHeader <T>(IPacket <T> data, ServerPacketId Id) { byte[] msg = data.GetPacketData(); HeaderData headerData = new HeaderData(); HeaderSerializer headerSerializer = new HeaderSerializer(); headerData.Id = (byte)Id; headerData.length = (short)msg.Length; headerSerializer.Serialize(headerData); byte[] header = headerSerializer.GetSerializedData(); return(header); }
/** * Executes the correct packet handler corresponding to this packet. * Assumes that the packet is not read yet. */ private void ExecuteServerPacketHandler(ushort id, ServerPacketId packetId, IPacketData packetData) { if (!_serverPacketHandlers.ContainsKey(packetId)) { Logger.Get().Warn(this, $"There is no server packet handler registered for ID: {packetId}"); return; } // Invoke the packet handler for this ID directly, in contrast to the client packet handling. // We don't do anything game specific with server packet handler, so there's no need to do it // on the Unity main thread try { _serverPacketHandlers[packetId].Invoke(id, packetData); } catch (Exception e) { Logger.Get().Error(this, $"Exception occured while executing server packet handler for packet ID: {packetId}, message: {e.Message}, stacktrace: {e.StackTrace}"); } }
public static ConsoleDataPacket BuildConsoleDataPacket(char text) { ServerPacketId packetid = ServerPacketId.AdminConsoleData; ushort packetLen = (ushort)(4 + 1); byte[] packetBytes = new byte[packetLen]; packetBytes[0] = (byte)packetid; packetBytes[1] = (byte)((packetLen & 0xFF00) << 8); packetBytes[2] = (byte)(packetLen & 0xFF); packetBytes[3] = (byte)ConsoleDataPacket.DataType.Char; packetBytes[4] = (byte)text; PacketBuffer buffer = new PacketBuffer(packetBytes); PacketReader reader = new PacketReader(ClientVersion.vMAX, buffer, (ushort)packetBytes.Length); return(ConsoleDataPacket.Instantiate(reader)); }
internal ServerPacket RecieveSinglePacketIfAvailable(PacketBuffer pBuffer) { if (pBuffer.Length <= 0) { return(null); } ServerPacketId packetid = (ServerPacketId)pBuffer[0]; ServerPacketInfo info; if (!_registry.TryGetValue(packetid, out info)) { InvokeOnUnknownPacket(string.Format("Invalid packet {0}, clearing buffer.", packetid), pBuffer.buffer); pBuffer.ClearIncoming(); } else { ushort packetlen; if (!info.isFixedLen) { if (pBuffer.Length < 3) { return(null); } packetlen = (ushort)(pBuffer[1] << 8 | pBuffer[2]); } else { packetlen = info.FixedLength; } if (pBuffer.Length >= packetlen) { ServerPacket packet = null; if (info.Factory != null) { packet = info.Factory(new PacketReader(Version, pBuffer, packetlen)); } pBuffer.RemoveBufferHead(packetlen); return(packet); } } return(null); }
public void DataHandle() { while (true) { if (receiveMsgs.Count != 0) { //패킷을 Dequeue 한다 패킷 : 메시지 타입 + 메시지 내용 lock (receiveLock) { tcpPacket = receiveMsgs.Dequeue(); } //타입과 내용을 분리한다 byte Id = tcpPacket.msg[0]; msg = new byte[tcpPacket.msg.Length - 1]; Array.Copy(tcpPacket.msg, 1, msg, 0, msg.Length); HeaderData headerData = new HeaderData(); //Dictionary에 등록된 델리게이트형 메소드에서 msg를 반환받는다. if (m_notifier.TryGetValue(Id, out recvNotifier)) { ServerPacketId packetId = recvNotifier(msg); //send 할 id를 반환받음 if (packetId != ServerPacketId.None) { tcpPacket = new TcpPacket(msg, tcpPacket.client); sendMsgs.Enqueue(tcpPacket); } } else { Console.WriteLine("DataHandler::TryGetValue 에러 " + Id); headerData.Id = (byte)ServerPacketId.None; } } } }
public void DataHandle() { if (receiveMsgs.Count != 0) { //패킷을 Dequeue 한다 //패킷 : 메시지 타입 + 메시지 내용 DataPacket packet = receiveMsgs.Dequeue(); msg = packet.msg; ipEndPoint = packet.endPoint; Debug.Log("Dequeue Message Length : " + msg.Length); if (packet.headerData.source == (byte)Source.ServerSource) { if (server_notifier.TryGetValue(packet.headerData.id, out serverRecvNotifier)) { ServerPacketId packetId = serverRecvNotifier(msg); } else { Debug.Log("DataHandler::Server.TryGetValue 에러 " + packet.headerData.id); packet.headerData.id = (byte)ServerPacketId.None; } } else if (packet.headerData.source == (byte)Source.ClientSource) { if (p2p_notifier.TryGetValue(packet.headerData.id, out p2pRecvNotifier)) { P2PPacketId packetId = p2pRecvNotifier(msg); } else { Debug.Log("DataHandler::P2P.TryGetValue 에러 " + packet.headerData.id); packet.headerData.id = (byte)P2PPacketId.None; } } } }
public PacketStructAttribute(ServerPacketId packetId) : this((int)packetId) { IsServerPacket = true; }
public ServerPacketAttribute(ServerPacketId packetId, ushort fixedLength) : this(packetId, fixedLength, null, null) { }
public ServerPacketAttribute(ServerPacketId packetId) : this(packetId, 0, null, null) { }
public ServerPacketAttribute(ServerPacketId packetId, ushort fixedLength, string startversion) : this(packetId, fixedLength, startversion, null) { }
/// <summary> /// Register a data-independent packet handler for the given ID. /// </summary> /// <param name="packetId">The server packet ID.</param> /// <param name="handler">The handler for the data.</param> public void RegisterServerPacketHandler( ServerPacketId packetId, EmptyServerPacketHandler handler ) => RegisterServerPacketHandler(packetId, (id, _) => handler(id));
public ServerPacketAttribute(ServerPacketId packetId, string startversion, string endversion) : this(packetId, 0, startversion, endversion) { }
public ServerPacketAttribute(ServerPacketId packetId, ushort fixedLength, string startversion, string endversion)
public RemoteAdminServerPacketAttribute(ServerPacketId packetId, ushort fixedLength = 0) : base(packetId, fixedLength) { StartVersion = ClientVersion.vAdminClient; EndVersion = ClientVersion.vAdminClient; }