Esempio n. 1
0
        private async Task ProcessData(byte[] data, EndPoint clientEndPoint)
        {
            try
            {
                NetBase.WriteDebug($"Server received data: {PacketToStringRep(data)}");

                UdpClient clientRef = clientList.Find(c => c.EndPoint.Equals(clientEndPoint));
                if (clientRef == null)
                {
                    if (data.Length != 4 || BitConverter.ToUInt32(data, 0) != Secret)
                    {
                        NetBase.WriteDebug($"Client attempted to connect from {clientEndPoint} with a bad secret.");

                        return;
                    }
                    UdpClient rCl = new UdpClient(socket, clientEndPoint);

                    rCl.ReliableResendDelay        = this.ReliableResendDelay;
                    rCl.MaxResendAttempts          = this.MaxResendAttempts;
                    rCl.DisconnectOnFailedResponse = this.DisconnectOnFailedResponse;

                    rCl.ClientDisconnected += c => DisconnectEventHandler(c);
                    rCl.SendRaw(254, PacketFlags.None, BitConverter.GetBytes(Secret));
                    clientList.Add(rCl);

                    if (ClientConnected != null)
                    {
                        Array.ForEach(ClientConnected.GetInvocationList(), d => d.DynamicInvoke(rCl));
                    }
                }
                else
                {
                    if (data.Length < 12)
                    {
                        return;
                    }

                    byte        eventId     = data[0];
                    PacketFlags packetFlags = (PacketFlags)data[1];
                    long        packetId    = BitConverter.ToInt64(data, 2);
                    ushort      dataLength  = BitConverter.ToUInt16(data, 10);
                    byte[]      netData     = data.Skip(12).ToArray();

                    if (dataLength != netData.Length)
                    {
                        return;
                    }

                    Dictionary <byte, MethodInfo> eventsRef = packetFlags.HasFlag(PacketFlags.SystemMessage) ? systemDataEvents : netDataEvents;

                    if (packetFlags.HasFlag(PacketFlags.Reliable))
                    {
                        clientRef.SendF(2, PacketFlags.SystemMessage, packetId);
                    }


                    if (eventsRef.ContainsKey(eventId) && !clientRef.receivedReliablePacketInfo.Contains(packetId))
                    {
                        if (packetFlags.HasFlag(PacketFlags.Reliable))
                        {
                            if (!clientRef.receivedReliablePacketInfo.Contains(packetId))
                            {
                                lock (clientRef.receivedReliableDataLock)
                                {
                                    clientRef.receivedReliablePacketInfo.Add(packetId);
                                }
                            }
                        }

                        clientRef.lastMessageReceived = DateTime.UtcNow;

                        MethodInfo      netEventMethod = eventsRef[eventId];
                        ParameterInfo[] parameters     = netEventMethod.GetParameters().Skip(1).ToArray();
                        Type[]          parameterTypes = (from p in parameters
                                                          select p.ParameterType).ToArray();

                        object[] instances            = DynamicPacket.GetInstancesFromData(netData, converterInstance, parameterTypes);
                        object[] instancesWithNetBase = new object[1 + instances.Length];
                        instancesWithNetBase[0] = clientRef;
                        instances.CopyTo(instancesWithNetBase, 1);
                        netEventMethod.Invoke(netEventMethod.IsStatic ? null : this, instancesWithNetBase);
                    }
                }
            }
            catch (Exception ex)
            {
                NetBase.WriteDebug(ex.ToString());
            }
        }