private static string GetMessage(PacketError code) { return(code switch { PacketError.ConversionError => "See the inner exception for details", PacketError.Overflow => "Data length overflow", PacketError.InvalidPath => "Path does not exist", _ => "Undefined error", });
private static string GetMessage(PacketError code) { switch (code) { case PacketError.ConvertError: return("See inner exception for more information"); case PacketError.Overflow: return("Data length overflow"); case PacketError.PathError: return("Path not exists"); case PacketError.RecursiveError: return("Recursion limit has been reached"); default: return("Undefined error"); } }
public PacketException(PacketError code) : base(GetMessage(code)) => error = code;
private void ProcessPackets(byte[] payloadData) { try { if (payloadData == null || payloadData.Length < 2) { return; } //get packetlength byte[] tmparray = new byte[2]; tmparray[0] = payloadData[0]; tmparray[1] = payloadData[1]; ushort packetLength = BitConverter.ToUInt16(tmparray, 0); if (payloadData.Length >= packetLength) { byte spacer = payloadData[2]; // skip 1 byte byte[] packetData = new byte [payloadData.Length - 3]; //payloadData.CopyTo(packetData, 3); Array.Copy(payloadData, 3, packetData, 0, packetLength - 3); DecryptPacket(packetData); byte[] packetBytes = packetData; //Get packet id L2RPacket packetReader = new L2RPacket(packetData); ushort packetId = (ushort)(packetReader.ReadUInt16() - 1); PacketFactory factory = new ConcretePacketFactory(); IL2RPacket l2rpckt = factory.GetPacket(packetId, packetReader); /*** Fixed Queue ***/ _triggerEvent = true; foreach (var id in _packetIdQueue) { if (id == packetId) { _triggerEvent = false; } } _packetIdQueue.Enqueue(packetId); if (_triggerEvent) { //FIRE L2RPacketArrivalEvent L2RPacketArrivalEventArgs args = new L2RPacketArrivalEventArgs { ID = packetId, Packet = l2rpckt, PacketBytes = packetBytes }; OnL2RPacketArrival(args); //packetQue.Enqueue(l2rpckt); } /*** end Fixed Queue ***/ } } catch (Exception ex) { _incomingBuffer.Clear(); PacketError?.Invoke(this, new L2RPacketErrorEventArgs(ex)); } }
/// <summary> /// Checks for queued packets. If any exist it locks the QueueLock, saves a /// reference of the current queue for itself, puts a new queue back into /// place into PacketQueue and unlocks QueueLock. This is a minimal amount of /// work done while the queue is locked. /// /// The background thread can then process queue that it saved without holding /// the queue lock. /// </summary> private void BackgroundThread() { while (!BackgroundThreadStop) { bool shouldSleep = true; lock (QueueLock) { if (PacketQueue.Count != 0) { shouldSleep = false; } } if (shouldSleep) { System.Threading.Thread.Sleep(250); } else // should process the queue { List <RawCapture> ourQueue; lock (QueueLock) { // swap queues, giving the capture callback a new one ourQueue = PacketQueue; PacketQueue = new List <RawCapture>(); } ProcessQueueTick?.Invoke(this, new L2RProcessQueueTickEventArgs(ourQueue.Count)); foreach (RawCapture packet in ourQueue) { // Here is where we can process our packets freely without // holding off packet capture. // // NOTE: If the incoming packet rate is greater than // the packet processing rate these queues will grow // to enormous sizes. Packets should be dropped in these // cases try { PacketWrapper packetWrapper = new PacketWrapper(packetCount, packet); Packet parsepacket = Packet.ParsePacket(packet.LinkLayerType, packet.Data); TcpPacket tcpPacket = (TcpPacket)parsepacket.Extract(typeof(TcpPacket)); if (tcpPacket != null) { byte[] packetData = tcpPacket.PayloadData; if (packetData != null) { ProcessPackets(packetData); } packetCount++; } if (statisticsUiNeedsUpdate) { UpdateCaptureStatistics(); statisticsUiNeedsUpdate = false; } } catch (Exception ex) { PacketError?.Invoke(this, new L2RPacketErrorEventArgs(ex, packet)); throw; } } } } }
void Client_DataReceived(object sender, DataReceivedEventArgs e) { Gtk.Application.Invoke(delegate { Console.WriteLine($"[<-] {e.Data}"); Packets.IPacket packet; try { packet = ParserPacket.ParsePacket(e.Data); } catch (ParsePacketException ex) { Console.WriteLine(ex.ToString()); return; } switch (packet.Command) { case PacketType.Connected: { ChatWidget broadcastWidget = new ChatWidget(true, "Broadcast", client.SendPacket); AddChatWidget(broadcastWidget); // we want the list of users... client.SendPacket(new PacketListUsers()); } break; case PacketType.Disconnected: { // handle leaving server } break; case PacketType.ChatReceived: { PacketChatReceived chatPak = (PacketChatReceived)packet; ShowBroadcastMessage(chatPak.FromUser, chatPak.Message); } break; case PacketType.WhisperReceived: { PacketWhisperReceived whispPak = (PacketWhisperReceived)packet; if (!ConnectedUsers.ContainsKey(whispPak.FromUser)) { AddChatWidget(new ChatWidget(false, whispPak.FromUser, client.SendPacket)); } ConnectedUsers[whispPak.FromUser].NewMessage(whispPak.FromUser, whispPak.Message); UpdatePageLabel(); } break; case PacketType.WhisperSent: { PacketWhisperSent whispPak = (PacketWhisperSent)packet; ConnectedUsers[whispPak.ToUser].NewMessage(Username, whispPak.Message); UpdatePageLabel(); } break; case PacketType.Users: { PacketUsers usersPak = (PacketUsers)packet; foreach (string user in usersPak.Users) { ChatWidget widget = new ChatWidget(false, user, client.SendPacket); AddChatWidget(widget); } } break; case PacketType.UserJoined: { PacketUserJoined usrJnPak = (PacketUserJoined)packet; if (!ConnectedUsers.ContainsKey(usrJnPak.Username)) { AddChatWidget(new ChatWidget(false, usrJnPak.Username, client.SendPacket)); ShowBroadcastMessage("server", $"User '{usrJnPak.Username}' joined the server."); } } break; case PacketType.UserLeft: { PacketUserLeft usrLfPak = (PacketUserLeft)packet; if (ConnectedUsers.ContainsKey(usrLfPak.Username)) { ShowBroadcastMessage("server", $"User '{usrLfPak.Username}' left the server."); RemoveChatWidget(ConnectedUsers[usrLfPak.Username]); } } break; case PacketType.Error: { PacketError errPak = (PacketError)packet; ShowBroadcastMessage("ERROR", errPak.Message); } break; case PacketType.FatalError: { PacketFatalError errPak = (PacketFatalError)packet; ShowBroadcastMessage("FATAL ERROR", errPak.Message); } break; } }); }
public Protocol(string message) { _resultInfo = new PacketError(); _message = message; TryParse(); }