/// <summary> /// Raise PacketReceivedEvent. /// </summary> /// <param name="guid">The <see cref="Guid"/>.</param> /// <param name="packet">The <see cref="PacketData"/>.</param> public void RaiseEvent(Guid guid, PacketData packet) { if (PacketReceivedEvent != null) { PacketReceivedEvent.Invoke(guid, packet); } }
/// <summary> /// Handles the data received event. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="EnergonSoftware.Core.Net.Sessions.DataReceivedEventArgs" /> instance containing the event data.</param> private async void DataReceivedEventHandler(object sender, DataReceivedEventArgs e) { IPacket packet; await SessionReceiveBuffer.LockAsync().ConfigureAwait(false); try { SessionReceiveBuffer.Flip(); Logger.Debug("Attempting to parse a packet..."); packet = await new PacketReader().ReadAsync(PacketFactory, SessionReceiveBuffer).ConfigureAwait(false); if (null == packet) { Logger.Debug("No packet available!"); SessionReceiveBuffer.Reset(); return; } await SessionReceiveBuffer.CompactAsync().ConfigureAwait(false); } finally { SessionReceiveBuffer.Release(); } Logger.Debug($"{packet.Type} packet successfully parsed with ContentType={packet.ContentType}"); PacketReceivedEvent?.Invoke(this, new PacketReceivedEventArgs { Packet = packet } ); }
private static void ProcessIncomingQueue(Packet packet) { PacketReceivedEvent?.Invoke(packet.GetPacket(), packet.GetLength()); PacketHandler handler = IncomingPacketHandlers.GetHandler(packet.GetPacketID()); int length = _getPacketLength(packet.GetPacketID()); handler?.OnReceive?.Invoke(new PacketReader(packet.GetPacket(), packet.GetLength(), length > 0)); PacketWaitEntries.CheckWait(packet.GetPacket(), PacketDirection.Incoming); }
private static void ProcessIncomingQueue(Packet packet) { try { PacketReceivedEvent?.Invoke(packet.GetPacket(), packet.GetLength()); PacketHandler handler = IncomingPacketHandlers.GetHandler(packet.GetPacketID()); int length = _getPacketLength(packet.GetPacketID()); handler?.OnReceive?.Invoke(new PacketReader(packet.GetPacket(), packet.GetLength(), length > 0)); PacketWaitEntries.CheckWait(packet.GetPacket(), PacketDirection.Incoming); } catch (Exception e) { e.ToExceptionless().SetProperty("Packet", packet.GetPacket()) .SetProperty("Player", Player.ToString()).SetProperty("WorldItemCount", Items.Count()) .SetProperty("WorldMobileCount", Mobiles.Count()).Submit(); } }
/// <summary> /// Receives and unpacks an OSC packet. /// A <see cref="OSCPacket"/> /// </summary> private void Receive() { IPEndPoint ip = null; try { byte[] bytes = _udpClient.Receive(ref ip); if (bytes.Length > 0) { var packet = OSCPacket.Unpack(bytes); _lastReceivedPacket = packet; PacketReceivedEvent?.Invoke(this, _lastReceivedPacket); } } catch { throw new Exception(String.Format("Can't create server at port {0}", _localPort)); } }
/* Raises a PacketReceivedEvent if someone has subscribed to it */ private void RaisePacketReceived(ACReceivedEventArgs args) { PacketReceivedEvent?.Invoke(this, args); }
/// <summary> /// Fire /// </summary> /// <param name="buffer"></param> private void ExecuteReturnPacket(object buffer) { _OnPacketReceivedEvent?.Invoke((byte[])buffer); }
private void PacketReceiverThread(object param) { IgeNetStream stream = (IgeNetStream)param; Log(LogLevel.Debug, "PacketReceiverThread: start"); try { using (BinaryReader reader = new BinaryReader(stream)) { Packet packet; uint packetId, checksum, checksum2; ((IgeNetStream)reader.BaseStream).CalculateChecksums = true; while (true) { // Log(LogLevel.Debug, "PacketReceiverThread: resetting read checksum"); // stream.ResetReadChecksum(); //Thread.MemoryBarrier(); Log(LogLevel.Debug, "PacketReceiverThread: reading packet id"); packetId = reader.ReadUInt32(); Log(LogLevel.Debug, "PacketReceiverThread: creating packet 0x{0:X8} instance", packetId); packet = Packet.CreateInstance(packetId); switch (packet.Encryption) { case PacketEncryptionMethod.RSA: { int cryptoSize = reader.ReadInt32(); Log(LogLevel.Debug, "PacketReceiverThread: deserializing packet 0x{0:X8} from an RSA encrypted block ({1} bytes)", packetId, cryptoSize); using (BinaryReader cryptoReader = new BinaryReader(new MemoryStream(m_RSACryptoPrivate.Decrypt(reader.ReadBytes(cryptoSize), false)), Encoding.Unicode)) { packet.Deserialize(cryptoReader); } break; } case PacketEncryptionMethod.Rijndael: { int cryptoSize = reader.ReadInt32(); Log(LogLevel.Debug, "PacketReceiverThread: deserializing packet 0x{0:X8} from a Rijndael crypto stream ({1} bytes)", packetId, cryptoSize); using (MemoryStream mem = new MemoryStream(reader.ReadBytes(cryptoSize))) { using (BinaryReader cryptoReader = new BinaryReader(new CryptoStream(mem, m_RijCrypto.CreateDecryptor(), CryptoStreamMode.Read), Encoding.Unicode)) { packet.Deserialize(cryptoReader); } } break; } default: { Log(LogLevel.Debug, "PacketReceiverThread: deserializing packet 0x{0:X8} from a read stream", packetId); packet.Deserialize(reader); break; } } if (GameDebugger.MinNetLogLevel <= LogLevel.VerboseDebug) { Log(LogLevel.VerboseDebug, "PacketReceiverThread: received packet '{0}'. Packet contents: {1}", packet.GetType().FullName, packet.ToString()); } else { Log(LogLevel.Debug, "PacketReceiverThread: received packet '{0}'", packet.GetType().FullName); } //Thread.MemoryBarrier(); // packet.Deserialize(reader); checksum = stream.ReadChecksum ^ unchecked (m_ReceivePacketSerial++); //Thread.MemoryBarrier(); checksum2 = reader.ReadUInt32(); //Thread.MemoryBarrier(); Log(LogLevel.Debug, "PacketReceiverThread: comparing checksums (calculated=0x{0:X8}; received=0x{1:X8})", checksum, checksum2); if (checksum != checksum2) { Disconnect(); throw new UserFriendlyException("The protocol might be broken or some packet serializer and deserializer don't match each other.", "Connection error"); } MethodInfo method; if (packet is RSAServerPublicKeyPacket || packet is RSAClientPublicKeyPacket || packet is RijndaelEncryptionKeyPacket) { Log(LogLevel.Debug, "PacketReceiverThread: searching for handler method in IGE.Net.Client"); method = typeof(Client).GetMethod("OnPacketReceived", BindingFlags.ExactBinding | BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { packet.GetType() }, null); } else { m_CanProcessPackets.WaitOne(); // block any non encryption key exchange packets Log(LogLevel.Debug, "PacketReceiverThread: searching for handler method in derived class"); method = GetType().GetMethod("OnPacketReceived", BindingFlags.ExactBinding | BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { packet.GetType() }, null); } if (method != null) { Log(LogLevel.Debug, "PacketReceiverThread: invoking custom packet received event"); method.Invoke(this, new object[] { packet }); } else { Log(LogLevel.Debug, "PacketReceiverThread: invoking default packet received event"); OnPacketReceived(packet); } if (PacketReceivedEvent != null) { PacketReceivedEvent.Invoke(this, new PacketReceivedEventArgs(packet)); } } } } catch (Exception ex) { Log(LogLevel.Debug, "PacketReceiverThread exception: {0}", ex.ToString()); } finally { stream.Dispose(); } Log(LogLevel.Debug, "PacketReceiverThread: end"); }