public static void RegisterPacketType(int protocolId, Type packetType) { PacketKey key = new PacketKey(); key.ProtocolId = protocolId; packetStore[key] = packetType; }
public void RegisterPacketType(int protocolId, IPacketBuilder packetType) { PacketKey key = new PacketKey(); key.ProtocolId = protocolId; packetStore[key] = packetType; }
public static void RegisterPacketType(RdmCommands command, RdmParameters parameter, Type packetType) { PacketKey key = new PacketKey(); key.Command = command; key.Parameter = parameter; if (packetStore.ContainsKey(key)) { throw new InvalidOperationException(string.Format("The packet {0} is already registered for {1}.", parameter.ToString(), command.ToString())); } packetStore[key] = packetType; }
public override void Invoke(McConnectionContext ctx) { var array = ctx.Data; McVarint.TryParse(ref array, out int packetId); ctx.PacketId = packetId; ctx.Data = array; var key = new PacketKey(ctx.PacketId, ctx.ConnectionState, ctx.BoundTo); if (packets.ContainsKey(key)) { var packet = (BasePacket)Activator.CreateInstance(packets[key]); try { packet.Read(ctx.Data); } catch { Console.WriteLine($"Error 0x{packetId:X02} " + $"on {ctx.ConnectionState} " + $"to {ctx.BoundTo} " + $"with {Convert.ToBase64String(ctx.Data)}"); } ctx.packetEventHub.Invoke(key, packet); ctx.genericEventHub.Invoke(packets[key], packet); Program.Log($"0x{ctx.PacketId:X02} | {ctx.ConnectionState} | {ctx.BoundTo} | {packet}"); } else { string packetMessage = ctx.Data.ToHexString(); if (packetMessage.Length > MAX_LENGTH) { packetMessage = $"{packetMessage.Remove(MAX_LENGTH)}..."; } Program.Log($"1x{ctx.PacketId:X02} | {ctx.ConnectionState} | {ctx.BoundTo} | {packetMessage}"); } _next?.Invoke(ctx); }
/// <summary> /// Creates a new instance of the packet by initializing all its fields /// </summary> public MyChatMessagesInform(byte[] Avatar, String Message) : base(PacketKey.Get("F6F85E84")) { this.Avatar = Avatar; this.Message = Message; }
/// <summary> /// Creates a new empty instance of the packet /// </summary> public MyChatMessagesInform() : base(PacketKey.Get("F6F85E84")) { }
/// <summary> /// Creates a new instance of the packet by initializing all its fields /// </summary> public SendMyChatMessageRequest(String Message) : base(PacketKey.Get("BD7E2CA4")) { this.Message = Message; }
/// <summary> /// Creates a new empty instance of the packet /// </summary> public SendMyChatMessageRequest() : base(PacketKey.Get("BD7E2CA4")) { }
public void ReceiveData() { try { // Internal Channel, no fragmentation here SocketId socketId = new SocketId(); while (transport.enabled && Receive(out ProductUserId clientUserID, out socketId, out byte[] internalMessage, (byte)internal_ch)) { if (internalMessage.Length == 1) { OnReceiveInternalData((InternalMessages)internalMessage[0], clientUserID, socketId); return; // Wait one frame } else { Debug.Log("Incorrect package length on internal channel."); } } // Insert new packet at the correct location in the incoming queue for (int chNum = 0; chNum < channels.Length; chNum++) { while (transport.enabled && Receive(out ProductUserId clientUserID, out socketId, out byte[] receiveBuffer, (byte)chNum)) { PacketKey incomingPacketKey = new PacketKey(); incomingPacketKey.productUserId = clientUserID; incomingPacketKey.channel = (byte)chNum; Packet packet = new Packet(); packet.FromBytes(receiveBuffer); if (!incomingPackets.ContainsKey(incomingPacketKey)) { incomingPackets.Add(incomingPacketKey, new List <List <Packet> >()); } int packetListIndex = incomingPackets[incomingPacketKey].Count; for (int i = 0; i < incomingPackets[incomingPacketKey].Count; i++) { if (incomingPackets[incomingPacketKey][i][0].id == packet.id) { packetListIndex = i; break; } } if (packetListIndex == incomingPackets[incomingPacketKey].Count) { incomingPackets[incomingPacketKey].Add(new List <Packet>()); } int insertionIndex = -1; for (int i = 0; i < incomingPackets[incomingPacketKey][packetListIndex].Count; i++) { if (incomingPackets[incomingPacketKey][packetListIndex][i].fragment > packet.fragment) { insertionIndex = i; break; } } if (insertionIndex >= 0) { incomingPackets[incomingPacketKey][packetListIndex].Insert(insertionIndex, packet); } else { incomingPackets[incomingPacketKey][packetListIndex].Add(packet); } } } // Find fully received packets List <List <Packet> > emptyPacketLists = new List <List <Packet> >(); foreach (KeyValuePair <PacketKey, List <List <Packet> > > keyValuePair in incomingPackets) { for (int packetList = 0; packetList < keyValuePair.Value.Count; packetList++) { bool packetReady = true; int packetLength = 0; for (int packet = 0; packet < keyValuePair.Value[packetList].Count; packet++) { Packet tempPacket = keyValuePair.Value[packetList][packet]; if (tempPacket.fragment != packet || (packet == keyValuePair.Value[packetList].Count - 1 && tempPacket.moreFragments)) { packetReady = false; } else { packetLength += tempPacket.data.Length; } } if (packetReady) { byte[] data = new byte[packetLength]; int dataIndex = 0; for (int packet = 0; packet < keyValuePair.Value[packetList].Count; packet++) { Array.Copy(keyValuePair.Value[packetList][packet].data, 0, data, dataIndex, keyValuePair.Value[packetList][packet].data.Length); dataIndex += keyValuePair.Value[packetList][packet].data.Length; } OnReceiveData(data, keyValuePair.Key.productUserId, keyValuePair.Key.channel); //keyValuePair.Value[packetList].Clear(); emptyPacketLists.Add(keyValuePair.Value[packetList]); } } for (int i = 0; i < emptyPacketLists.Count; i++) { keyValuePair.Value.Remove(emptyPacketLists[i]); } emptyPacketLists.Clear(); } } catch (Exception e) { Debug.LogException(e); } }
public EcoServerApp(int port, NewRequestCallback newReuqestCallback /*, SessionHandler<EcoSession> newSessionCallBack*/) { server = new EcoServer(); //crypt = packetKey = new PacketKey(); packetKey.serverKey = new ServerKey(); // Add request handler server.NewRequestReceived += (session, reuqestInfo) => { //Logger.Debug("New Message Received."); if (session.ExchangeKeyFinished) { var data = session.Crypt.Decrypt(reuqestInfo.Body, 8); var packet = new BasePacket(data); newReuqestCallback(session, packet); } else //first connection { if (reuqestInfo.Body.SequenceEqual(INIT_PACKET)) { byte[] server_key_bytes = packetKey.serverKey.PublicKeyBytes; session.Send(server_key_bytes, 0, server_key_bytes.Length); Logger.Debug($"Send Data: {server_key_bytes.ToHexString()} to client, Session ID:{session.SessionID}"); } else { if (reuqestInfo.Body.Length != 260) { return; } packetKey.clientKey = new ClientKey(reuqestInfo.Body); Logger.Debug($"client public key:{packetKey.clientKey.PublicKey.ToString("x")}, length:{packetKey.clientKey.PublicKeySize}"); string sharedKey = packetKey.GetSharedKey(); Logger.Debug($"shared key:{sharedKey}, length:{sharedKey.Length}"); session.Crypt.SetAesKey(packetKey.GetRijndaelKey(Encoding.ASCII.GetBytes(sharedKey))); session.ExchangeKeyFinished = true; } } }; //server.NewSessionConnected += newSessionCallBack; var serverConfig = new ServerConfig { Port = port, //set the listening port Ip = "Any", Name = "EcoServer", //Other configuration options Mode = SocketMode.Tcp, MaxConnectionNumber = 1000, IdleSessionTimeOut = 100, SyncSend = false, LogFactory = "SuperSocket.SocketBase.Logging.ConsoleLogFactory", LogBasicSessionActivity = true, KeepAliveTime = 60, // 10 minutes KeepAliveInterval = 60, // 60 seconds //MaxRequestLength = 8096, LogAllSocketException = true }; if (!server.Setup(serverConfig)) { Console.WriteLine("server setup failed!"); } }
public void Subscribe(PacketKey t, IPacketEventHandler packetEventHandler) => packetEventHub.Subscribe(t, packetEventHandler);
protected bool TryGetBuilder(PacketKey key, out IPacketBuilder builder) { return(packetStore.TryGetValue(key, out builder)); }