Beispiel #1
0
    void Update()
    {
        while (networkTcpIp.QueueEmpty() == false)
        {
            var packet = networkTcpIp.GetPacket();

            packetProcessor.ProcessPacket(packet);
        }
    }
Beispiel #2
0
    private void ReceiveCallBack(IAsyncResult aSyncResult)
    {
        try
        {
            int bytesRead = socket.EndReceive(aSyncResult);


            if (bytesRead > 0)
            {
                int offset = 0;

                //build/compile packets until can no longer or data is finished
                while (true)
                {
                    BasePacket basePacket = BuildPacket(ref offset, buffer, bytesRead);
                    if (basePacket == null)
                    {
                        break;
                    }
                    else
                    {
                        packetProcessor.ProcessPacket(basePacket);
                    }
                }
                //Not all bytes consumed, transfer leftover to beginning
                if (offset < bytesRead)
                {
                    Array.Copy(buffer, offset, buffer, 0, bytesRead - offset);
                }

                Array.Clear(buffer, bytesRead - offset, buffer.Length - (bytesRead - offset));

                if (offset < bytesRead)
                //need offset since not all bytes consumed
                {
                    socket.BeginReceive(buffer, bytesRead - offset, buffer.Length - (bytesRead - offset), SocketFlags.None, new AsyncCallback(ReceiveCallBack), socket);
                }
                else
                {
                    socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), socket);
                }
            }
            else
            {
                Debug.Log("Lost connection to server");
            }
        }
        catch (Exception e) {
            Debug.Log("something went wrong");
            Debug.Log(e);
        }
    }
Beispiel #3
0
        public bool ProcessPacket(Guid sourcePlayerId, Packet packet)
        {
#if DEBUG
            Console.WriteLine($"Handling packet {packet.GetType().Name} from {sourcePlayerId}");
#endif
            Type packetType = packet.GetType();
            if (!registeredProcessors.ContainsKey(packetType))
            {
                return(false);
            }

            PacketProcessor processor = registeredProcessors.First(p => p.Key == packetType).Value;
            processor.ProcessPacket(sourcePlayerId, packet, processorContext);
            return(true);
        }
Beispiel #4
0
        private void ProcessUnauthenticated(Packet packet, NitroxConnection connection)
        {
            try
            {
                Type serverPacketProcessorType = typeof(UnauthenticatedPacketProcessor <>);
                Type packetType          = packet.GetType();
                Type packetProcessorType = serverPacketProcessorType.MakeGenericType(packetType);

                PacketProcessor processor = (PacketProcessor)NitroxServiceLocator.LocateService(packetProcessorType);
                processor.ProcessPacket(packet, connection);
            }
            catch (Exception ex)
            {
                Log.Error(ex, $"Received invalid, unauthenticated packet: {packet}");
            }
        }
Beispiel #5
0
        public void ProcessPackets()
        {
            Queue <Packet> packets = chunkAwarePacketReceiver.GetReceivedPackets();

            foreach (Packet packet in packets)
            {
                if (packetProcessorsByType.ContainsKey(packet.GetType()))
                {
                    PacketProcessor processor = packetProcessorsByType[packet.GetType()];
                    processor.ProcessPacket(packet);
                }
                else
                {
                    Console.WriteLine("No packet processor for the given type: " + packet.GetType());
                }
            }
        }
Beispiel #6
0
        private void ProcessAuthenticated(Packet packet, Player player)
        {
            Type serverPacketProcessorType = typeof(AuthenticatedPacketProcessor <>);
            Type packetType          = packet.GetType();
            Type packetProcessorType = serverPacketProcessorType.MakeGenericType(packetType);

            Optional <object> opProcessor = NitroxServiceLocator.LocateOptionalService(packetProcessorType);

            if (opProcessor.HasValue)
            {
                PacketProcessor processor = (PacketProcessor)opProcessor.Value;
                processor.ProcessPacket(packet, player);
            }
            else
            {
                defaultServerPacketProcessor.ProcessPacket(packet, player);
            }
        }
Beispiel #7
0
        public void ProcessPackets()
        {
            Queue <Packet> packets = packetReceiver.GetReceivedPackets();

            foreach (Packet packet in packets)
            {
                try
                {
                    Type clientPacketProcessorType = typeof(ClientPacketProcessor <>);
                    Type packetType          = packet.GetType();
                    Type packetProcessorType = clientPacketProcessorType.MakeGenericType(packetType);

                    PacketProcessor processor = (PacketProcessor)NitroxServiceLocator.LocateService(packetProcessorType);
                    processor.ProcessPacket(packet, null);
                }
                catch (Exception ex)
                {
                    Log.Error("Error processing packet: " + packet, ex);
                }
            }
        }
        public void ProcessPackets()
        {
            Queue <Packet> packets = chunkAwarePacketReceiver.GetReceivedPackets();

            foreach (Packet packet in packets)
            {
                if (packetProcessorsByType.ContainsKey(packet.GetType()))
                {
                    try
                    {
                        PacketProcessor processor = packetProcessorsByType[packet.GetType()];
                        processor.ProcessPacket(packet, null);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error processing packet: " + packet + ": " + ex);
                    }
                }
                else
                {
                    ClientLogger.Debug("No packet processor for the given type: " + packet.GetType());
                }
            }
        }
Beispiel #9
0
        public void ProcessPackets()
        {
            Queue <Packet> packets = packetReceiver.GetReceivedPackets();

            foreach (Packet packet in packets)
            {
                if (PacketProcessorsByType.ContainsKey(packet.GetType()))
                {
                    try
                    {
                        PacketProcessor processor = PacketProcessorsByType[packet.GetType()];
                        processor.ProcessPacket(packet, null);
                    }
                    catch (Exception ex)
                    {
                        Log.Error("Error processing packet: " + packet, ex);
                    }
                }
                else
                {
                    Log.Debug("No packet processor for the given type: " + packet.GetType());
                }
            }
        }
Beispiel #10
0
 public override void OnPacket(String message)
 {
     if (this.Out.getDecryptor() != null)
     {
         try
         {
             message = Out.getDecryptor().decrypt(message);
         }
         catch (Exception e)
         {
             Logger.Error("Fail to Parse -> " + e.ToString());
             return;
         }
     }
     Logger.Debug("Received packet from client : " + message);
     try
     {
         PacketProcessor.ProcessPacket(this, message);
     }
     catch (Exception e)
     {
         Logger.Error("Can't handle packet : " + e.ToString());
     }
 }
Beispiel #11
0
        private void ProcessPackets(Dictionary <Type, PacketProcessor> packetProcessorMap)
        {
            Queue <Packet> packets = packetReceiver.GetReceivedPackets();

            foreach (Packet packet in packets)
            {
                if (packetProcessorMap.ContainsKey(packet.GetType()))
                {
                    try
                    {
                        PacketProcessor processor = packetProcessorMap[packet.GetType()];
                        processor.ProcessPacket(packet, null);
                    }
                    catch (Exception ex)
                    {
                        Log.Error("Error processing packet: " + packet, ex);
                    }
                }
                else
                {
                    Log.Debug("No packet processor for the given type: " + packet.GetType());
                }
            }
        }