PSF-LoginServer/src/test/scala/game/HotSpotUpdateMessageTest.scala
Jakob Gillich 407429ee21 Networking
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
2020-09-26 23:58:09 +02:00

97 lines
2.9 KiB
Scala

// Copyright (c) 2017 PSForever
package game
import org.specs2.mutable._
import net.psforever.packet._
import net.psforever.packet.game._
import scodec.bits._
class HotSpotUpdateMessageTest extends Specification {
val stringClear = hex"9F 0500 1 000"
val stringOne = hex"9F 0500 1 010 002E9 00145 80000 0"
val stringTwo = hex"9F 0500 5 020 00D07 008CA 80000 00BEA 004C4 80000"
val stringThree = hex"9F 0A00 4 030 00FC8 00F0A 80000 002E9 00BEA 80000 00FC8 00BEA 80000 0"
"decode (clear)" in {
PacketCoding.decodePacket(stringClear).require match {
case HotSpotUpdateMessage(continent_id, unk, spots) =>
continent_id mustEqual 5
unk mustEqual 1
spots.size mustEqual 0
case _ =>
ko
}
}
"decode (one)" in {
PacketCoding.decodePacket(stringOne).require match {
case HotSpotUpdateMessage(continent_id, unk, spots) =>
continent_id mustEqual 5
unk mustEqual 1
spots.size mustEqual 1
spots.head mustEqual HotSpotInfo(4700.0f, 2600.0f, 64.0f)
case _ =>
ko
}
}
"decode (two)" in {
PacketCoding.decodePacket(stringTwo).require match {
case HotSpotUpdateMessage(continent_id, unk, spots) =>
continent_id mustEqual 5
unk mustEqual 5
spots.size mustEqual 2
spots.head mustEqual HotSpotInfo(4000.0f, 5400.0f, 64.0f)
spots(1) mustEqual HotSpotInfo(5500.0f, 2200.0f, 64.0f)
case _ =>
ko
}
}
"decode (three)" in {
PacketCoding.decodePacket(stringThree).require match {
case HotSpotUpdateMessage(continent_id, unk, spots) =>
continent_id mustEqual 10
unk mustEqual 4
spots.size mustEqual 3
spots.head mustEqual HotSpotInfo(4600.0f, 5600.0f, 64.0f)
spots(1) mustEqual HotSpotInfo(4700.0f, 5500.0f, 64.0f)
spots(2) mustEqual HotSpotInfo(4600.0f, 5500.0f, 64.0f)
case _ =>
ko
}
}
"encode (clear)" in {
val msg = HotSpotUpdateMessage(5, 1, Nil)
val pkt = PacketCoding.encodePacket(msg).require.toByteVector
pkt mustEqual stringClear
}
"encode (one)" in {
val msg = HotSpotUpdateMessage(5, 1, List(HotSpotInfo(4700.0f, 2600.0f, 64.0f)))
val pkt = PacketCoding.encodePacket(msg).require.toByteVector
pkt mustEqual stringOne
}
"encode (two)" in {
val msg =
HotSpotUpdateMessage(5, 5, List(HotSpotInfo(4000.0f, 5400.0f, 64.0f), HotSpotInfo(5500.0f, 2200.0f, 64.0f)))
val pkt = PacketCoding.encodePacket(msg).require.toByteVector
pkt mustEqual stringTwo
}
"encode (three)" in {
val msg = HotSpotUpdateMessage(
10,
4,
List(
HotSpotInfo(4600.0f, 5600.0f, 64.0f),
HotSpotInfo(4700.0f, 5500.0f, 64.0f),
HotSpotInfo(4600.0f, 5500.0f, 64.0f)
)
)
val pkt = PacketCoding.encodePacket(msg).require.toByteVector
pkt mustEqual stringThree
}
}