mirror of
https://github.com/psforever/PSF-LoginServer.git
synced 2026-03-04 13:00:25 +00:00
The game uses a UDP-based protocol. Unlike TCP, UDP does not guarantee that packets arrive, or that they arrive in the correct order. For this reason, the game protocol implements those features using the following: * All packets have a sequence number that is utilized for reordering * Important packets are wrapped in a SlottedMetaPacket with a subslot number * RelatedA packets ae used to request lost packets using the subslot number * RelatedB packets are used to confirm received SlottedMetaPackets All of these go both ways, server <-> client. We used to only partially implement these features: Outgoing packet bundles used SMPs and could be resent, but not all packets were bundled and there was no logic for requesting lost packets from the client and there was no packet reordering, which resulted in dire consequences in the case of packet loss (zoning failures, crashes and many other odd bugs). This patch addresses all of these issues. * Packet bundling: Packets are now automatically bundled and sent as SlottedMetaPackets using a recurring timer. All manual bundling functionality was removed. * Packet reordering: Incoming packets, if received out of order, are stashed and reordered. The maximum wait time for reordering is 20ms. * Packet requesting: Missing SlottedMetaPackets are requested from the client. * PacketCoding refactor: Dropped confusing packet container types. Fixes #5. * Crypto rewrite: PSCrypto is based on a ancient buggy version of cryptopp. Updating to a current version was not possible because it removed the MD5-MAC algorithm. For more details, see Md5Mac.scala. This patch replaces PSCrypto with native Scala code. * Added two new actors: * SocketActor: A simple typed UDP socket actor * MiddlewareActor: The old session pipeline greatly simplified into a typed actor that does most of the things mentioned above. * Begun work on a headless client * Fixed anniversary gun breaking stamina regen * Resolved a few sentry errors
69 lines
2.5 KiB
Scala
69 lines
2.5 KiB
Scala
// Copyright (c) 2017 PSForever
|
|
import org.specs2.mutable._
|
|
import net.psforever.packet.{PlanetSideControlPacket, _}
|
|
import net.psforever.packet.control.{ClientStart, ServerStart}
|
|
import scodec.bits._
|
|
|
|
class PacketCodingTest extends Specification {
|
|
/*def roundTrip[Container <: PlanetSidePacketContainer, Packet <: PlanetSidePacket](cont : Container, pkt : Packet) = {
|
|
|
|
val filledContainer = cont match {
|
|
case x : ControlPacket => x.copy(packet = pkt.asInstanceOf[PlanetSideControlPacket])
|
|
}
|
|
val pktEncoded = PacketCoding.MarshalPacket(ControlPacket(packetUnderTest.opcode, packetUnderTest)).require
|
|
val pktDecoded = PacketCoding.UnMarshalPacket(pkt.toByteVector).require.asInstanceOf[ControlPacket]
|
|
val recvPkt = decoded.packet.asInstanceOf[ServerStart]
|
|
|
|
}*/
|
|
|
|
"Packet coding" should {
|
|
"correctly decode control packets" in {
|
|
val (packet, _) = PacketCoding.unmarshalPacket(hex"0001 00000002 00261e27 000001f0").require
|
|
|
|
packet.isInstanceOf[PlanetSideControlPacket] mustEqual true
|
|
|
|
val controlPacket = packet.asInstanceOf[PlanetSideControlPacket]
|
|
controlPacket.opcode mustEqual ControlPacketOpcode.ClientStart
|
|
controlPacket mustEqual ClientStart(656287232)
|
|
}
|
|
|
|
"encode and decode to identical packets" in {
|
|
val clientNonce = 213129
|
|
val serverNonce = 848483
|
|
|
|
val packetUnderTest = ServerStart(clientNonce, serverNonce)
|
|
val pkt = PacketCoding.marshalPacket(packetUnderTest).require
|
|
|
|
val decoded = PacketCoding.unmarshalPacket(pkt.toByteVector).require._1.asInstanceOf[PlanetSideControlPacket]
|
|
val recvPkt = decoded.asInstanceOf[ServerStart]
|
|
|
|
packetUnderTest mustEqual recvPkt
|
|
}
|
|
|
|
"reject corrupted control packets" in {
|
|
val packet = PacketCoding.unmarshalPacket(hex"0001 00001002 00261e27 004101f0")
|
|
|
|
packet.isSuccessful mustEqual false
|
|
}
|
|
|
|
"correctly decode crypto packets" in {
|
|
val (packet, _) = PacketCoding.unmarshalPacket(hex"0001 00000002 00261e27 000001f0").require
|
|
|
|
packet.isInstanceOf[PlanetSideControlPacket] mustEqual true
|
|
|
|
val controlPacket = packet.asInstanceOf[PlanetSideControlPacket]
|
|
controlPacket.opcode mustEqual ControlPacketOpcode.ClientStart
|
|
controlPacket mustEqual ClientStart(656287232)
|
|
}
|
|
|
|
"reject bad packet types" in {
|
|
PacketCoding.unmarshalPacket(hex"ff414141").isFailure mustEqual true
|
|
}
|
|
|
|
"reject small packets" in {
|
|
PacketCoding.unmarshalPacket(hex"00").isFailure mustEqual true
|
|
PacketCoding.unmarshalPacket(hex"").isFailure mustEqual true
|
|
}
|
|
}
|
|
|
|
}
|