/// <inheritdoc/> protected override void ProcessPacket(PacketProcessingEventArgs args) { var reader = args.Reader; switch (args.Label) { case "Authenticate": this.HandleAuthentication(reader); break; case "ValidatePin": this.HandlePinValidation(reader); break; case "AssignPin": this.HandlePinAssignment(reader); break; case "WorldListRequest": case "WorldListRefresh": this.HandleWorldListRequest(reader); break; case "ChannelSelect": this.HandleChannelSelect(reader); break; case "CharacterListRequest": this.HandleCharacterListRequest(reader); break; case "CharacterSelect": this.HandleCharacterSelect(reader); break; } }
private void OnPacketProcessing(object sender, PacketProcessingEventArgs e) { if (e.Label == "Pong") { this.HandlePong(); } else if (e.Label != null) { this.Logger.Debug("Received packet '{0}'", e.Label); this.HandlePacket(e); } else { var packetCode = e.PacketCode; var packetData = e.Reader.ReadFully().ToHex(true); this.Logger.Debug(@"Unrecognized packet code: 0x{0:X4}. Packet buffer: {1}", packetCode, packetData); } }
private void HandlePacket(PacketProcessingEventArgs e) { try { this.ProcessPacket(e); } catch (IllegalPacketException) { // TODO: Use IllegalPacketException for penalizing naughty clients. this.Logger.Info("Received illegal packet. Client disconnected."); this.Disconnect("Illegal packet."); } catch (PacketReadingException) { this.Logger.Info("Received incomplete packet. Client disconnected."); this.Disconnect("Incomplete packet."); } }
/// <summary> /// When implemented in a derived class, processes the provided packet data. /// </summary> /// <param name="args">The packet to be processed.</param> protected abstract void ProcessPacket(PacketProcessingEventArgs args);
/// <inheritdoc/> protected override void ProcessPacket(PacketProcessingEventArgs args) { // TODO: Channel packet handling, hello? }
/// <summary> /// Raises the <see cref="PacketProcessing"/> event for the specified packet data, if possible asynchronously. /// </summary> /// <param name="packet">The packet data to include for the raised event.</param> /// <returns> /// <see langword="false"/> if the data was handled synchronously and another call can be made; if there was a fatal error or the asynchronous call was successfully made, <see langword="false"/>.</returns> private bool TryPushAsync(byte[] packet) { var reader = new PacketReader(packet); ushort packetCode; if (!reader.TryReadUInt16(out packetCode)) { this.Close(@"Could not read packet code."); // Bad packet. We kill the session and stop pushing. return true; } // If this returns null, it's an unknown code and has no label. string label; this.packetCodeTable.TryGetIncomingLabel(packetCode, out label); // Invoke event asynchronously. var args = new PacketProcessingEventArgs(packetCode, label, reader); var handler = this.PacketProcessing; AsyncCallback callback = this.ContinuePushAsynchronous; var asyncResult = handler.BeginInvoke(this, args, callback, null); // If we completed synchronously, we'll take another one right away. // Otherwise, leave it to the asynchronous continuation. return asyncResult.CompletedSynchronously; }