protected virtual void HandlePacket(IPacketBase packet)
{
//Send auth responses for an auth request
if (packet is AuthRequest)
{
AuthResponse response = PacketFactory.CreatePacket<AuthResponse>();
//Tell them their world ID
response.WorldID = WorldID;
DeferredSendPacket(response);
AuthState = EntityAuthState.Authorised;
}
//Update auth state and world ID
else if (packet is AuthResponse)
{
AuthResponse response = (AuthResponse)packet;
WorldID = response.WorldID;
AuthState = EntityAuthState.Authorised;
}
//Unpack coalesced packets
else if (packet is CoalescedData)
{
CoalescedData data = (CoalescedData)packet;
unsafe
{
byte* ptr = data.DataBuffer;
//Start deserializing packets from the buffer
for (int i = 0; i < data.PacketCount; i++)
{
//Deserialize and advance pointer to next packet in the buffer
ptr += DeserializePacket((IntPtr)ptr);
}
}
}
//Synchronise clocks
else if (packet is ClockSyncResponse)
{
ClockSyncResponse response = (ClockSyncResponse)packet;
int rtt = Environment.TickCount - m_clockSyncSendTime;
m_roundTripTimes.Enqueue(rtt);
if (m_roundTripTimes.Count > 10)
{
m_roundTripTimes.Dequeue();
}
SyncClock(response.Time);
m_awaitingClockSyncResponse = false;
}
else if (packet is ClockSyncRequest)
{
ClockSyncResponse response = PacketFactory.CreatePacket<ClockSyncResponse>();
response.Time = Environment.TickCount;
SendPacket(response);
}
}