Merge pull request #194 from Fate-JH/pca

Packet Splitting, Bundling
This commit is contained in:
Fate-JH 2018-04-02 09:39:55 -04:00 committed by GitHub
commit fc3837268c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 1156 additions and 268 deletions

View file

@ -10,6 +10,7 @@ import scodec.bits._
import MDCContextAware.Implicits._
import com.github.mauricio.async.db.{Connection, QueryResult, RowData}
import com.github.mauricio.async.db.mysql.exceptions.MySQLException
import net.psforever.objects.DefaultCancellable
import net.psforever.types.PlanetSideEmpire
import scala.concurrent.{Await, Future}
@ -25,7 +26,7 @@ class LoginSessionActor extends Actor with MDCContextAware {
var leftRef : ActorRef = ActorRef.noSender
var rightRef : ActorRef = ActorRef.noSender
var updateServerListTask : Cancellable = LoginSessionActor.DefaultCancellable
var updateServerListTask : Cancellable = DefaultCancellable.obj
override def postStop() = {
if(updateServerListTask != null)
@ -54,59 +55,22 @@ class LoginSessionActor extends Actor with MDCContextAware {
def Started : Receive = {
case UpdateServerList() =>
updateServerList()
case ctrl @ ControlPacket(_, _) =>
handlePktContainer(ctrl)
case game @ GamePacket(_, _, _) =>
handlePktContainer(game)
case default => failWithError(s"Invalid packet class received: $default")
}
def handlePkt(pkt : PlanetSidePacket) : Unit = pkt match {
case ctrl : PlanetSideControlPacket =>
case ControlPacket(_, ctrl) =>
handleControlPkt(ctrl)
case game : PlanetSideGamePacket =>
case GamePacket(_, _, game) =>
handleGamePkt(game)
case default => failWithError(s"Invalid packet class received: $default")
}
def handlePktContainer(pkt : PlanetSidePacketContainer) : Unit = pkt match {
case ctrl @ ControlPacket(opcode, ctrlPkt) =>
handleControlPkt(ctrlPkt)
case game @ GamePacket(opcode, seq, gamePkt) =>
handleGamePkt(gamePkt)
case default => failWithError(s"Invalid packet container class received: $default")
}
def handleControlPkt(pkt : PlanetSideControlPacket) = {
pkt match {
case SlottedMetaPacket(slot, subslot, innerPacket) =>
// Meta packets are like TCP packets - then need to be ACKed to the client
sendResponse(PacketCoding.CreateControlPacket(SlottedMetaAck(slot, subslot)))
// Decode the inner packet and handle it or error
PacketCoding.DecodePacket(innerPacket).fold({
error => log.error(s"Failed to decode inner packet of SlottedMetaPacket: $error")
}, {
handlePkt(_)
})
/// TODO: figure out what this is what what it does for the PS client
/// I believe it has something to do with reliable packet transmission and resending
case sync @ ControlSync(diff, unk, f1, f2, f3, f4, fa, fb) =>
case sync @ ControlSync(diff, _, _, _, _, _, fa, fb) =>
log.trace(s"SYNC: $sync")
val serverTick = Math.abs(System.nanoTime().toInt) // limit the size to prevent encoding error
sendResponse(PacketCoding.CreateControlPacket(ControlSyncResp(diff, serverTick,
fa, fb, fb, fa)))
case MultiPacket(packets) =>
sendResponse(PacketCoding.CreateControlPacket(ControlSyncResp(diff, serverTick, fa, fb, fb, fa)))
/// Extract out each of the subpackets in the MultiPacket and handle them or raise a packet error
packets.foreach { pkt =>
PacketCoding.DecodePacket(pkt).fold({ error =>
log.error(s"Failed to decode inner packet of MultiPacket: $error")
}, {
handlePkt(_)
})
}
case default =>
log.error(s"Unhandled ControlPacket $default")
}
@ -119,7 +83,6 @@ class LoginSessionActor extends Actor with MDCContextAware {
// TESTING CODE FOR ACCOUNT LOOKUP
def accountLookup(username : String, password : String) : Boolean = {
val connection: Connection = DatabaseConnector.getAccountsConnection
Await.result(connection.connect, 5 seconds)
// create account
@ -135,13 +98,13 @@ class LoginSessionActor extends Actor with MDCContextAware {
case Some(resultSet) =>
val row : RowData = resultSet.head
row(0)
case None => -1
}
)
case None =>
-1
})
try {
// XXX: remove awaits
val result = Await.result( mapResult, 5 seconds )
Await.result( mapResult, 5 seconds )
return true
} catch {
case e : MySQLException =>
@ -151,7 +114,6 @@ class LoginSessionActor extends Actor with MDCContextAware {
} finally {
connection.disconnect
}
false
}
@ -190,22 +152,25 @@ class LoginSessionActor extends Actor with MDCContextAware {
log.info(s"Failed login to account $username")
sendResponse(PacketCoding.CreateGamePacket(0, response))
}
case ConnectToWorldRequestMessage(name, _, _, _, _, _, _) =>
log.info(s"Connect to world request for '$name'")
val response = ConnectToWorldMessage(serverName, serverAddress.getHostString, serverAddress.getPort)
sendResponse(PacketCoding.CreateGamePacket(0, response))
sendResponse(DropSession(sessionId, "user transferring to world"))
case default => log.debug(s"Unhandled GamePacket $pkt")
case _ =>
log.debug(s"Unhandled GamePacket $pkt")
}
def updateServerList() = {
val msg = VNLWorldStatusMessage("Welcome to PlanetSide! ",
Vector(
WorldInformation(serverName, WorldStatus.Up, ServerType.Beta,
Vector(WorldConnectionInfo(serverAddress)), PlanetSideEmpire.VS)
))
WorldInformation(
serverName, WorldStatus.Up, ServerType.Beta, Vector(WorldConnectionInfo(serverAddress)), PlanetSideEmpire.VS
)
)
)
sendResponse(PacketCoding.CreateGamePacket(0, msg))
}
@ -216,22 +181,13 @@ class LoginSessionActor extends Actor with MDCContextAware {
def sendResponse(cont : Any) = {
log.trace("LOGIN SEND: " + cont)
MDC("sessionId") = sessionId.toString
rightRef !> cont
}
def sendRawResponse(pkt : ByteVector) = {
log.trace("LOGIN SEND RAW: " + pkt)
MDC("sessionId") = sessionId.toString
rightRef !> RawPacket(pkt)
}
}
object LoginSessionActor {
final val DefaultCancellable = new Cancellable() {
def isCancelled : Boolean = true
def cancel : Boolean = true
}
}

View file

@ -5,7 +5,10 @@ import scodec.Attempt.{Failure, Successful}
import scodec.bits._
import org.log4s.MDC
import MDCContextAware.Implicits._
import net.psforever.packet.control.{HandleGamePacket, SlottedMetaPacket}
import net.psforever.packet.control.{HandleGamePacket, _}
import scala.annotation.tailrec
import scala.collection.mutable
/**
* In between the network side and the higher functioning side of the simulation:
@ -30,13 +33,14 @@ import net.psforever.packet.control.{HandleGamePacket, SlottedMetaPacket}
*/
class PacketCodingActor extends Actor with MDCContextAware {
private var sessionId : Long = 0
private var subslot : Int = 0
private var subslotOutbound : Int = 0
private var subslotInbound : Int = 0
private var leftRef : ActorRef = ActorRef.noSender
private var rightRef : ActorRef = ActorRef.noSender
private[this] val log = org.log4s.getLogger
override def postStop() = {
subslot = 0 //in case this `Actor` restarts
subslotOutbound = 0 //in case this `Actor` restarts
super.postStop()
}
@ -68,12 +72,7 @@ class PacketCodingActor extends Actor with MDCContextAware {
mtuLimit(msg)
}
else {//from network, to LSA, WSA, etc. - decode
PacketCoding.unmarshalPayload(0, msg) match { //TODO is it safe for this to always be 0?
case Successful(packet) =>
sendResponseRight(packet)
case Failure(ex) =>
log.info(s"Failed to marshal a packet: $ex")
}
UnmarshalInnerPacket(msg, "a packet")
}
//known elevated packet type
case ctrl @ ControlPacket(_, packet) =>
@ -88,7 +87,7 @@ class PacketCodingActor extends Actor with MDCContextAware {
else { //deprecated; ControlPackets should not be coming from this direction
log.warn(s"DEPRECATED CONTROL PACKET SEND: $ctrl")
MDC("sessionId") = sessionId.toString
sendResponseRight(ctrl)
handlePacketContainer(ctrl) //sendResponseRight
}
//known elevated packet type
case game @ GamePacket(_, _, packet) =>
@ -105,33 +104,36 @@ class PacketCodingActor extends Actor with MDCContextAware {
MDC("sessionId") = sessionId.toString
sendResponseRight(game)
}
//bundling packets into a SlottedMetaPacket0/MultiPacketEx
case msg @ MultiPacketBundle(list) =>
log.trace(s"BUNDLE PACKET REQUEST SEND, LEFT (always): $msg")
handleBundlePacket(list)
//etc
case msg =>
log.trace(s"PACKET SEND, LEFT: $msg")
if(sender == rightRef) {
log.trace(s"BASE CASE PACKET SEND, LEFT: $msg")
MDC("sessionId") = sessionId.toString
leftRef !> msg
}
else {
log.trace(s"BASE CASE PACKET SEND, RIGHT: $msg")
MDC("sessionId") = sessionId.toString
rightRef !> msg
}
// case default =>
// failWithError(s"Invalid message '$default' received in state Established")
}
/**
* Retrieve the current subslot number.
* Increment the `subslot` for the next time it is needed.
* @return a 16u number starting at 0
* @return a `16u` number starting at 0
*/
def Subslot : Int = {
if(subslot == 65536) { //TODO what is the actual wrap number?
subslot = 0
subslot
if(subslotOutbound == 65536) { //TODO what is the actual wrap number?
subslotOutbound = 0
subslotOutbound
} else {
val curr = subslot
subslot += 1
val curr = subslotOutbound
subslotOutbound += 1
curr
}
}
@ -173,16 +175,74 @@ class PacketCodingActor extends Actor with MDCContextAware {
def handleSplitPacket(data : ByteVector) : Unit = {
val lim = PacketCodingActor.MTU_LIMIT_BYTES - 4 //4 bytes is the base size of SlottedMetaPacket
data.grouped(lim).foreach(bvec => {
val pkt = PacketCoding.CreateControlPacket(SlottedMetaPacket(4, Subslot, bvec))
PacketCoding.EncodePacket(pkt.packet) match {
PacketCoding.EncodePacket(SlottedMetaPacket(4, Subslot, bvec)) match {
case Successful(bdata) =>
sendResponseLeft(bdata.toByteVector)
case f @ Failure(_) =>
case f : Failure =>
log.error(s"$f")
}
})
}
/**
* Accept a `List` of packets and sequentially re-package the elements from the list into multiple container packets.<br>
* <br>
* The original packets are encoded then paired with their encoding lengths plus extra space to prefix the length.
* Encodings from these pairs are drawn from the list until into buckets that fit a maximum byte stream length.
* The size limitation on any bucket is the MTU limit.
* less by the base sizes of `MultiPacketEx` (2) and of `SlottedMetaPacket` (4).
* @param bundle the packets to be bundled
*/
def handleBundlePacket(bundle : List[PlanetSidePacket]) : Unit = {
val packets : List[ByteVector] = recursiveEncode(bundle.iterator)
recursiveFillPacketBuckets(packets.iterator, PacketCodingActor.MTU_LIMIT_BYTES - 6)
.foreach( list => {
handleBundlePacket(list.toVector)
})
}
/**
* Accept a `Vector` of encoded packets and re-package them.
* The normal order is to package the elements of the vector into a `MultiPacketEx`.
* If the vector only has one element, it will get packaged by itself in a `SlottedMetaPacket`.
* If that one element risks being too big for the MTU, however, it will be handled off to be split.
* Splitting should preserve `Subslot` ordering with the rest of the bundling.
* @param vec a specific number of byte streams
*/
def handleBundlePacket(vec : Vector[ByteVector]) : Unit = {
if(vec.size == 1) {
val elem = vec.head
if(elem.length > PacketCodingActor.MTU_LIMIT_BYTES - 4) {
handleSplitPacket(PacketCoding.CreateControlPacket(HandleGamePacket(elem)))
}
else {
handleBundlePacket(elem)
}
}
else {
PacketCoding.EncodePacket(MultiPacketEx(vec)) match {
case Successful(bdata) =>
handleBundlePacket(bdata.toByteVector)
case Failure(e) =>
log.warn(s"bundling failed on MultiPacketEx creation: - $e")
}
}
}
/**
* Accept `ByteVector` data and package it into a `SlottedMetaPacket`.
* Send it (towards the network) upon successful encoding.
* @param data an encoded packet
*/
def handleBundlePacket(data : ByteVector) : Unit = {
PacketCoding.EncodePacket(SlottedMetaPacket(0, Subslot, data)) match {
case Successful(bdata) =>
sendResponseLeft(bdata.toByteVector)
case Failure(e) =>
log.warn(s"bundling failed on SlottedMetaPacket creation: - $e")
}
}
/**
* Encoded sequence of data going towards the network.
* @param cont the data
@ -193,6 +253,72 @@ class PacketCodingActor extends Actor with MDCContextAware {
leftRef !> RawPacket(cont)
}
/**
* Transform data into a container packet and re-submit that container to the process that handles the packet.
* @param data the packet data
* @param description an explanation of the input `data`
*/
def UnmarshalInnerPacket(data : ByteVector, description : String) : Unit = {
PacketCoding.unmarshalPayload(0, data) match { //TODO is it safe for this to always be 0?
case Successful(packet) =>
handlePacketContainer(packet)
case Failure(ex) =>
log.info(s"Failed to unmarshal $description: $ex")
}
}
/**
* Sort and redirect a container packet bound for the server by type of contents.
* `GamePacket` objects can just onwards without issue.
* `ControlPacket` objects may need to be dequeued.
* All other container types are invalid.
* @param container the container packet
*/
def handlePacketContainer(container : PlanetSidePacketContainer) : Unit = {
container match {
case _ : GamePacket =>
sendResponseRight(container)
case ControlPacket(_, ctrlPkt) =>
handleControlPacket(container, ctrlPkt)
case default =>
log.warn(s"Invalid packet container class received: ${default.getClass.getName}") //do not spill contents in log
}
}
/**
* Process a control packet or determine that it does not need to be processed at this level.
* Primarily, if the packet is of a type that contains another packet that needs be be unmarshalled,
* that/those packet must be unwound.<br>
* <br>
* The subslot information is used to identify these nested packets after arriving at their destination,
* to establish order for sequential packets and relation between divided packets.
* @param container the original container packet
* @param packet the packet that was extracted from the container
*/
def handleControlPacket(container : PlanetSidePacketContainer, packet : PlanetSideControlPacket) = {
packet match {
case SlottedMetaPacket(slot, subslot, innerPacket) =>
subslotInbound = subslot
self.tell(PacketCoding.CreateControlPacket(RelatedB(slot, subslot)), rightRef) //will go to the network
UnmarshalInnerPacket(innerPacket, "the inner packet of a SlottedMetaPacket")
case MultiPacket(packets) =>
packets.foreach { UnmarshalInnerPacket(_, "the inner packet of a MultiPacket") }
case MultiPacketEx(packets) =>
packets.foreach { UnmarshalInnerPacket(_, "the inner packet of a MultiPacketEx") }
case RelatedA(slot, subslot) =>
log.error(s"result $slot: subslot $subslot was in error")
case RelatedB(slot, subslot) =>
log.trace(s"result $slot: subslot $subslot accepted")
case _ =>
sendResponseRight(container)
}
}
/**
* Decoded packet going towards the simulation.
* @param cont the packet
@ -202,6 +328,71 @@ class PacketCodingActor extends Actor with MDCContextAware {
MDC("sessionId") = sessionId.toString
rightRef !> cont
}
/**
* Accept a series of packets and transform it into a series of packet encodings.
* Packets that do not encode properly are simply excluded for the product.
* This is not treated as an error or exception; a warning will mrely be logged.
* @param iter the `Iterator` for a series of packets
* @param out updated series of byte stream data produced through successful packet encoding;
* defaults to an empty list
* @return a series of byte stream data produced through successful packet encoding
*/
@tailrec private def recursiveEncode(iter : Iterator[PlanetSidePacket], out : List[ByteVector] = List()) : List[ByteVector] = {
if(!iter.hasNext) {
out
}
else {
import net.psforever.packet.{PlanetSideControlPacket, PlanetSideGamePacket}
iter.next match {
case msg : PlanetSideGamePacket =>
PacketCoding.EncodePacket(msg) match {
case Successful(bytecode) =>
recursiveEncode(iter, out :+ bytecode.toByteVector)
case Failure(e) =>
log.warn(s"game packet $msg, part of a bundle, did not encode - $e")
recursiveEncode(iter, out)
}
case msg : PlanetSideControlPacket =>
PacketCoding.EncodePacket(msg) match {
case Successful(bytecode) =>
recursiveEncode(iter, out :+ bytecode.toByteVector)
case Failure(e) =>
log.warn(s"control packet $msg, part of a bundle, did not encode - $e")
recursiveEncode(iter, out)
}
case _ =>
recursiveEncode(iter, out)
}
}
}
/**
* Accept a series of byte stream data and sort into sequential size-limited buckets of the same byte streams.
* Note that elements that exceed `lim` by themselves are always sorted into their own buckets.
* @param iter an `Iterator` of a series of byte stream data
* @param lim the maximum stream length permitted
* @param curr the stream length of the current bucket
* @param out updated series of byte stream data stored in buckets
* @return a series of byte stream data stored in buckets
*/
@tailrec private def recursiveFillPacketBuckets(iter : Iterator[ByteVector], lim : Int, curr : Int = 0, out : List[mutable.ListBuffer[ByteVector]] = List(mutable.ListBuffer())) : List[mutable.ListBuffer[ByteVector]] = {
if(!iter.hasNext) {
out
}
else {
val data = iter.next
var len = data.length.toInt
len = len + (if(len < 256) { 1 } else if(len < 65536) { 2 } else { 4 }) //space for the prefixed length byte(s)
if(curr + len > lim && out.last.nonEmpty) { //bucket must have something in it before swapping
recursiveFillPacketBuckets(iter, lim, len, out :+ mutable.ListBuffer(data))
}
else {
out.last += data
recursiveFillPacketBuckets(iter, lim, curr + len, out)
}
}
}
}
object PacketCodingActor {

View file

@ -140,10 +140,10 @@ class WorldSessionActor extends Actor with MDCContextAware {
galaxy = endpoint
log.info("ID: " + sessionId + " Got galaxy service " + endpoint)
case ctrl @ ControlPacket(_, _) =>
handlePktContainer(ctrl)
case game @ GamePacket(_, _, _) =>
handlePktContainer(game)
case ControlPacket(_, ctrl) =>
handleControlPkt(ctrl)
case GamePacket(_, _, pkt) =>
handleGamePkt(pkt)
// temporary hack to keep the client from disconnecting
case PokeClient() =>
sendResponse(KeepAliveMessage())
@ -1110,61 +1110,12 @@ class WorldSessionActor extends Actor with MDCContextAware {
log.warn(s"Invalid packet class received: $default")
}
def handlePkt(pkt : PlanetSidePacket) : Unit = pkt match {
case ctrl : PlanetSideControlPacket =>
handleControlPkt(ctrl)
case game : PlanetSideGamePacket =>
handleGamePkt(game)
case default => log.error(s"Invalid packet class received: $default")
}
def handlePktContainer(pkt : PlanetSidePacketContainer) : Unit = pkt match {
case ctrl @ ControlPacket(opcode, ctrlPkt) =>
handleControlPkt(ctrlPkt)
case game @ GamePacket(opcode, seq, gamePkt) =>
handleGamePkt(gamePkt)
case default => log.warn(s"Invalid packet container class received: $default")
}
def handleControlPkt(pkt : PlanetSideControlPacket) = {
pkt match {
case SlottedMetaPacket(slot, subslot, innerPacket) =>
sendResponse(SlottedMetaAck(slot, subslot))
PacketCoding.DecodePacket(innerPacket) match {
case Failure(e) =>
log.error(s"Failed to decode inner packet of SlottedMetaPacket: $e")
case Successful(v) =>
handlePkt(v)
}
case sync @ ControlSync(diff, unk, f1, f2, f3, f4, fa, fb) =>
case sync @ ControlSync(diff, _, _, _, _, _, fa, fb) =>
log.debug(s"SYNC: $sync")
val serverTick = Math.abs(System.nanoTime().toInt) // limit the size to prevent encoding error
sendResponse(ControlSyncResp(diff, serverTick, fa, fb, fb, fa))
case MultiPacket(packets) =>
packets.foreach { pkt =>
PacketCoding.DecodePacket(pkt) match {
case Failure(e) =>
log.error(s"Failed to decode inner packet of MultiPacket: $e")
case Successful(v) =>
handlePkt(v)
}
}
case MultiPacketEx(packets) =>
packets.foreach { pkt =>
PacketCoding.DecodePacket(pkt) match {
case Failure(e) =>
log.error(s"Failed to decode inner packet of MultiPacketEx: $e")
case Successful(v) =>
handlePkt(v)
}
}
case RelatedA0(subslot) =>
log.error(s"Client not ready for last control packet with subslot $subslot; potential system disarray")
case RelatedB0(subslot) =>
log.trace(s"Good control packet received $subslot")
case TeardownConnection(_) =>
log.info("Good bye")
@ -3190,6 +3141,11 @@ class WorldSessionActor extends Actor with MDCContextAware {
sendResponse(cont.asInstanceOf[Any])
}
def sendResponse(cont : MultiPacketBundle) : Unit = {
log.trace("WORLD SEND: " + cont)
sendResponse(cont.asInstanceOf[Any])
}
def sendResponse(msg : Any) : Unit = {
MDC("sessionId") = sessionId.toString
rightRef !> msg

View file

@ -2,10 +2,11 @@
import akka.actor.{ActorRef, Props}
import akka.testkit.TestProbe
import net.psforever.packet.control.ControlSync
import net.psforever.packet.game.objectcreate.ObjectClass
import net.psforever.packet.{ControlPacket, GamePacket, PacketCoding}
import net.psforever.packet.control.{ControlSync, MultiPacketBundle, SlottedMetaPacket}
import net.psforever.packet.{ControlPacket, GamePacket, GamePacketOpcode, PacketCoding}
import net.psforever.packet.game._
import net.psforever.packet.game.objectcreate.ObjectClass
import net.psforever.types._
import scodec.bits._
import scala.concurrent.duration._
@ -364,6 +365,398 @@ class PacketCodingActorDTest extends ActorTest {
}
}
class PacketCodingActorETest extends ActorTest {
"PacketCodingActor" should {
"unwind l-originating hexadecimal data into multiple r-facing packets (MultiPacket -> 2 PlayerStateMessageUpstream)" in {
val string_hex = RawPacket(hex"00 03 18 BD E8 04 5C 02 60 E3 F9 19 0E C1 41 27 00 04 02 60 20 0C 58 0B 20 00 00 18 BD E8 04 86 02 62 13 F9 19 0E D8 40 4D 00 04 02 60 20 0C 78 0A 80 00 00")
val string_obj1 = GamePacket(GamePacketOpcode.PlayerStateMessageUpstream, 0, PlayerStateMessageUpstream(PlanetSideGUID(1256),Vector3(3076.7188f,4734.1094f,56.390625f),Some(Vector3(4.0625f,4.59375f,0.0f)),36.5625f,357.1875f,0.0f,866,0,false,false,false,false,178,0))
val string_obj2 = GamePacket(GamePacketOpcode.PlayerStateMessageUpstream, 0, PlayerStateMessageUpstream(PlanetSideGUID(1256),Vector3(3077.0469f,4734.258f,56.390625f),Some(Vector3(5.5f,1.1875f,0.0f)),36.5625f,357.1875f,0.0f,867,0,false,false,false,false,168,0))
val probe1 = TestProbe()
val probe2 = system.actorOf(Props(classOf[ActorTest.MDCTestProbe], probe1), "mdc-probe")
val pca : ActorRef = system.actorOf(Props[PacketCodingActor], "pca")
pca ! HelloFriend(135, List(probe2).iterator)
probe1.receiveOne(100 milli) //consume
pca ! string_hex
val reply = probe1.receiveN(2, 200 milli)
assert(reply.head == string_obj1)
assert(reply(1) == string_obj2)
probe1.expectNoMsg(100 milli)
}
}
}
class PacketCodingActorFTest extends ActorTest {
"PacketCodingActor" should {
"unwind l-originating hexadecimal data into an r-facing packet (MultiPacket -> RelatedB + GenericObjectStateMsg)" in {
val string_hex = RawPacket(hex"00 03 04 00 15 02 98 0B 00 09 0C 0A 1D F2 00 10 00 00 00")
val string_obj = GamePacket(GamePacketOpcode.GenericObjectStateMsg, 0, GenericObjectStateMsg(PlanetSideGUID(242), 16))
val probe1 = TestProbe()
val probe2 = system.actorOf(Props(classOf[ActorTest.MDCTestProbe], probe1), "mdc-probe")
val pca : ActorRef = system.actorOf(Props[PacketCodingActor], "pca")
pca ! HelloFriend(135, List(probe2).iterator)
probe1.receiveOne(100 milli) //consume
pca ! string_hex
val reply = probe1.receiveN(1, 200 milli)
assert(reply.head == string_obj)
//the RelatedB message - 00 15 02 98 - is consumed by pca
probe1.expectNoMsg(100 milli)
}
}
}
class PacketCodingActorGTest extends ActorTest {
"PacketCodingActor" should {
"unwind l-originating hexadecimal data into an r-facing packet (MultiPacketEx -> RelatedA + GenericObjectStateMsg)" in {
val string_hex = RawPacket(hex"00 19 04 00 11 02 98 0B 00 09 0C 0A 1D F2 00 10 00 00 00")
val string_obj = GamePacket(GamePacketOpcode.GenericObjectStateMsg, 0, GenericObjectStateMsg(PlanetSideGUID(242), 16))
val probe1 = TestProbe()
val probe2 = system.actorOf(Props(classOf[ActorTest.MDCTestProbe], probe1), "mdc-probe")
val pca : ActorRef = system.actorOf(Props[PacketCodingActor], "pca")
pca ! HelloFriend(135, List(probe2).iterator)
probe1.receiveOne(100 milli) //consume
pca ! string_hex
val reply = probe1.receiveN(1, 200 milli)
assert(reply.head == string_obj)
//the RelatedA message - 00 11 02 98 - is consumed by pca; should see error log message in console
probe1.expectNoMsg(100 milli)
}
}
}
class PacketCodingActorHTest extends ActorTest {
"PacketCodingActor" should {
"unwind l-originating hexadecimal data into two r-facing packets (SlottedMetaPacket/MultiPacketEx -> 2 ObjectDeleteMessage)" in {
val string_hex = RawPacket(hex"00 09 0A E1 00 19 04 19 4F 04 40 04 19 51 04 40")
val string_obj1 = GamePacket(GamePacketOpcode.ObjectDeleteMessage, 0, ObjectDeleteMessage(PlanetSideGUID(1103), 2))
val string_obj2 = GamePacket(GamePacketOpcode.ObjectDeleteMessage, 0, ObjectDeleteMessage(PlanetSideGUID(1105), 2))
val probe1 = TestProbe()
val probe2 = system.actorOf(Props(classOf[ActorTest.MDCTestProbe], probe1), "mdc-probe")
val pca : ActorRef = system.actorOf(Props[PacketCodingActor], "pca")
pca ! HelloFriend(135, List(probe2).iterator)
probe1.receiveOne(100 milli) //consume
pca ! string_hex
val reply = probe1.receiveN(2, 200 milli)
assert(reply.head == string_obj1)
assert(reply(1) == string_obj2)
probe1.expectNoMsg(100 milli)
}
}
}
class PacketCodingActorITest extends ActorTest {
"PacketCodingActor" should {
"bundle an r-originating packet into an l-facing SlottedMetaPacket byte stream data (SlottedMetaPacket)" in {
import net.psforever.packet.game.objectcreate._
val obj = DetailedCharacterData(
CharacterAppearanceData(
PlacementData(Vector3.Zero, Vector3.Zero),
BasicCharacterData("IlllIIIlllIlIllIlllIllI", PlanetSideEmpire.VS, CharacterGender.Female, 41, 1),
3,
false,
false,
ExoSuitType.Standard,
"",
0,
false,
2.8125f, 210.9375f,
true,
GrenadeState.None,
false,
false,
false,
RibbonBars()
),
0,
0,
100, 100,
50,
1, 7, 7,
100, 100,
List(CertificationType.StandardAssault,CertificationType.MediumAssault,CertificationType.ATV,CertificationType.Harasser,CertificationType.StandardExoSuit,CertificationType.AgileExoSuit,CertificationType.ReinforcedExoSuit),
List(),
List(),
List.empty,
None,
Some(InventoryData(Nil)),
DrawnSlot.None
)
val pkt = MultiPacketBundle(List(ObjectCreateDetailedMessage(0x79, PlanetSideGUID(75), obj)))
val string_hex = hex"000900001879060000bc84b000000000000000000002040000097049006c006c006c004900490049006c006c006c0049006c0049006c006c0049006c006c006c0049006c006c0049008452700000000000000000000000000000002000000fe6a703fffffffffffffffffffffffffffffffc00000000000000000000000000000000000000019001900064000001007ec800c80000000000000000000000000000000000000001c00042c54686c7000000000000000000000000000000000000000000000000000000000000000000000000200700"
val probe1 = TestProbe()
val probe2 = system.actorOf(Props(classOf[ActorTest.MDCTestProbe], probe1), "mdc-probe")
val pca : ActorRef = system.actorOf(Props[PacketCodingActor], "pca")
pca ! HelloFriend(135, List(probe2).iterator)
probe1.receiveOne(100 milli) //consume
probe2 ! pkt
val reply1 = receiveN(1, 200 milli) //we get a MdcMsg message back
probe1.receiveN(1, 200 milli) //flush contents
probe2 ! reply1.head //by feeding the MdcMsg into the actor, we get normal output on the probe
probe1.receiveOne(100 milli) match {
case RawPacket(data) =>
assert(data == string_hex)
PacketCoding.DecodePacket(data).require match {
case _ : SlottedMetaPacket =>
assert(true)
case _ =>
assert(false)
}
case e =>
assert(false)
}
}
}
}
class PacketCodingActorJTest extends ActorTest {
"PacketCodingActor" should {
"bundle r-originating packets into a number of MTU-acceptable l-facing byte streams (1 packets into 1)" in {
val pkt = MultiPacketBundle(
List(ObjectDeleteMessage(PlanetSideGUID(1103), 2), ObjectDeleteMessage(PlanetSideGUID(1105), 2), ObjectDeleteMessage(PlanetSideGUID(1107), 2))
)
val string_hex = hex"00090000001904194f044004195104400419530440"
val probe1 = TestProbe()
val probe2 = system.actorOf(Props(classOf[ActorTest.MDCTestProbe], probe1), "mdc-probe")
val pca : ActorRef = system.actorOf(Props[PacketCodingActor], "pca")
pca ! HelloFriend(135, List(probe2).iterator)
probe1.receiveOne(100 milli) //consume
probe2 ! pkt
val reply1 = receiveN(1, 200 milli) //we get a MdcMsg message back
probe1.receiveN(1, 200 milli) //flush contents
probe2 ! reply1.head //by feeding the MdcMsg into the actor, we get normal output on the probe
probe1.receiveOne(100 milli) match {
case RawPacket(data) =>
assert(data == string_hex)
case e =>
assert(false)
}
}
}
}
class PacketCodingActorKTest extends ActorTest {
import net.psforever.packet.game.objectcreate._
val obj = DetailedCharacterData(
CharacterAppearanceData(
PlacementData(Vector3.Zero, Vector3.Zero),
BasicCharacterData("IlllIIIlllIlIllIlllIllI", PlanetSideEmpire.VS, CharacterGender.Female, 41, 1),
3,
false,
false,
ExoSuitType.Standard,
"",
0,
false,
2.8125f, 210.9375f,
true,
GrenadeState.None,
false,
false,
false,
RibbonBars()
),
0,
0,
100, 100,
50,
1, 7, 7,
100, 100,
List(CertificationType.StandardAssault, CertificationType.MediumAssault, CertificationType.ATV, CertificationType.Harasser, CertificationType.StandardExoSuit, CertificationType.AgileExoSuit, CertificationType.ReinforcedExoSuit),
List(),
List("xpe_sanctuary_help", "xpe_th_firemodes", "used_beamer", "map13"),
List.empty,
None,
Some(InventoryData(Nil)),
DrawnSlot.None
)
val list = List(
ObjectCreateDetailedMessage(0x79, PlanetSideGUID(75), obj),
ObjectDeleteMessage(PlanetSideGUID(1103), 2),
ObjectDeleteMessage(PlanetSideGUID(1105), 2),
ObjectCreateDetailedMessage(0x79, PlanetSideGUID(175), obj),
ObjectCreateDetailedMessage(0x79, PlanetSideGUID(275), obj),
ObjectDeleteMessage(PlanetSideGUID(1107), 2)
)
"PacketCodingActor" should {
"bundle r-originating packets into a number of MTU-acceptable l-facing byte streams (6 packets into 2)" in {
val pkt = MultiPacketBundle(list)
val probe1 = TestProbe()
val probe2 = system.actorOf(Props(classOf[ActorTest.MDCTestProbe], probe1), "mdc-probe")
val pca : ActorRef = system.actorOf(Props[PacketCodingActor], "pca")
pca ! HelloFriend(135, List(probe2).iterator)
probe1.receiveOne(100 milli) //consume
probe2 ! pkt
val reply1 = receiveN(2, 200 milli)
probe1.receiveN(1, 200 milli) //flush contents
probe2 ! reply1.head //by feeding the MdcMsg into the actor, we get normal output on the probe
val reply3 = probe1.receiveOne(100 milli).asInstanceOf[RawPacket]
pca ! reply3 //reconstruct original three packets from the first bundle
val reply4 = probe1.receiveN(3, 200 milli)
var i = 0
reply4.foreach{
case GamePacket(_, _, packet) =>
assert(packet == list(i))
i += 1
case _ =>
assert(false)
}
}
}
}
class PacketCodingActorLTest extends ActorTest {
val string_obj = PropertyOverrideMessage(
List(
GamePropertyScope(0,
GamePropertyTarget(GamePropertyTarget.game_properties, List(
"purchase_exempt_vs" -> "",
"purchase_exempt_tr" -> "",
"purchase_exempt_nc" -> ""
)
)),
GamePropertyScope(17,
GamePropertyTarget(ObjectClass.katana, "allowed" -> "false")
),
GamePropertyScope(18,
GamePropertyTarget(ObjectClass.katana, "allowed" -> "false")
),
GamePropertyScope(19,
GamePropertyTarget(ObjectClass.katana, "allowed" -> "false")
),
GamePropertyScope(20,
GamePropertyTarget(ObjectClass.katana, "allowed" -> "false")
),
GamePropertyScope(21,
GamePropertyTarget(ObjectClass.katana, "allowed" -> "false")
),
GamePropertyScope(22,
GamePropertyTarget(ObjectClass.katana, "allowed" -> "false")
),
GamePropertyScope(29, List(
GamePropertyTarget(ObjectClass.aphelion_flight, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.aphelion_gunner, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.aurora, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.battlewagon, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.colossus_flight, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.colossus_gunner, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.flail, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.galaxy_gunship, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.lasher, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.liberator, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.lightgunship, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.maelstrom, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.magrider, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.mini_chaingun, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.peregrine_flight, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.peregrine_gunner, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.prowler, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.r_shotgun, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.thunderer, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.vanguard, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.vulture, "allowed" -> "false")
)),
GamePropertyScope(30, List(
GamePropertyTarget(ObjectClass.aphelion_flight, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.aphelion_gunner, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.aurora, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.battlewagon, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.colossus_flight, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.colossus_gunner, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.flail, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.galaxy_gunship, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.lasher, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.liberator, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.lightgunship, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.maelstrom, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.magrider, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.mini_chaingun, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.peregrine_flight, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.peregrine_gunner, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.prowler, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.r_shotgun, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.thunderer, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.vanguard, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.vulture, "allowed" -> "false")
)),
GamePropertyScope(31, List(
GamePropertyTarget(ObjectClass.aphelion_flight, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.aphelion_gunner, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.aurora, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.battlewagon, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.colossus_flight, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.colossus_gunner, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.flail, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.galaxy_gunship, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.lasher, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.liberator, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.lightgunship, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.maelstrom, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.magrider, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.mini_chaingun, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.peregrine_flight, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.peregrine_gunner, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.prowler, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.r_shotgun, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.thunderer, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.vanguard, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.vulture, "allowed" -> "false")
)),
GamePropertyScope(32, List(
GamePropertyTarget(ObjectClass.aphelion_flight, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.aphelion_gunner, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.aurora, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.battlewagon, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.colossus_flight, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.colossus_gunner, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.flail, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.galaxy_gunship, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.lasher, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.liberator, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.lightgunship, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.maelstrom, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.magrider, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.mini_chaingun, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.peregrine_flight, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.peregrine_gunner, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.prowler, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.r_shotgun, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.thunderer, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.vanguard, "allowed" -> "false"),
GamePropertyTarget(ObjectClass.vulture, "allowed" -> "false")
))
)
)
"PacketCodingActor" should {
"split, rather than bundle, r-originating packets into a number of MTU-acceptable l-facing byte streams" in {
val probe1 = TestProbe()
val probe2 = system.actorOf(Props(classOf[ActorTest.MDCTestProbe], probe1), "mdc-probe")
val pca : ActorRef = system.actorOf(Props[PacketCodingActor], "pca")
pca ! HelloFriend(135, List(probe2).iterator)
probe1.receiveOne(100 milli) //consume
val msg = MultiPacketBundle(List(string_obj))
pca ! msg
receiveN(4)
}
}
}
object PacketCodingActorTest {
//decoy
}