.
<?php
/* WARNING - UNTESTED CODE - WARNING */
function onMCI() {
extract($this->parent->packet);
if (!isset($this->$PLID->X))
$this->$PLID->X = 0;
if (!isset($this->$PLID->Y))
$this->$PLID->Y = 0;
if (!isset($this->$PLID->Z))
$this->$PLID->Z = 0;
$dist = ($this->$PLID->X[$PLID] - $X) + ($this->$PLID->Y - $Y) + ($this->$PLID->Z - $Z);
$this->$PLID->X = $X;
$this->$PLID->Y = $Y;
$this->$PLID->Z = $Z;
return $dist;
}
/* WARNING - UNTESTED CODE - WARNING */
?>
// Convert speed into meters per second.
float mps = (speed / 32768f) * 100f);
// Convert meters per second into MPH/KPH...
float mph = mps * 2.2369362920544f;
float kph = mps * 3.6f;
// Convert X, Y and Z into meters.
float currentX = x / 65536f;
float currentY = y / 65536f;
float currentZ = z / 65536f;
// Figure out how many meters we have moved since the previous MCI packet was received.
float distanceX = currentX - previousX;
float distanceY = currentY - previousY;
float distanceZ = currentZ - previousZ;
float metersTravelled = Math.Sqrt((distanceX * distanceX) + (distanceY * distanceY) + (distanceZ * distanceZ));
// Add this to our total.
totalMetersTravelled += metersTravelled.
// Convert the total into MPH/KPH...
float kilometersTravelled = totalMetersTravelled / 1000f;
float milesTravelled = totalMetersTravelled / 1609.344f;
private void RecieveWorker()
{
byte[] buffer = new byte[MaxBuffer];
int BytesRecieved = 0;
int CurrentPacketSize = 0;
while (!_exit)
{
try { BytesRecieved = Client.Receive(buffer); }
catch { }
if (BytesRecieved == 0)
continue;
CurrentPacketSize = buffer[0];
while ((CurrentPacketSize != 0) & (BytesRecieved >= CurrentPacketSize))
{
byte[] Packet = new byte[CurrentPacketSize];
Buffer.BlockCopy(buffer, 0, Packet, 0, CurrentPacketSize);
Array.Clear(buffer, 0, CurrentPacketSize);
ProcessPacket(Packet);
CurrentPacketSize = buffer[0];
}
}
}
const int BufferSize = 512;
void ReceiveThread()
{
// Global buffer where we store our packets between receive calls.
List<byte> buffer = new List<byte>();
while (true)
{
// We read the incoming TCP stream into our receiveBuffer.
byte[] receiveBuffer = new byte[BufferSize];
int bytesReceived = socket.Receive(receiveBuffer);
// If zero bytes were received it means the TCP connection has been lost.
if (bytesReceived == 0)
{
// Handle lost connection.
return;
}
// We copy the packets out of the receiveBuffer and add them to our
// global buffer.
byte[] tempBuffer = new byte[bytesReceived];
Array.Copy(receiveBuffer, tempBuffer, bytesReceived);
buffer.AddRange(tempBuffer);
// We loop through the global buffer and parse any completed packets out.
byte packetSize = GetNextPacketSize(buffer);
while ((packetSize != 0) && (buffer.Count >= packetSize))
{
byte[] packet = new byte[packetSize];
buffer.CopyTo(0, packet, 0, packetSize);
buffer.RemoveRange(0, packetSize);
// INSERT CODE TO FIRE PACKET RECEIVED EVENT
// OnPacketReceived(packet);
if (buffer.Count > 0)
packetSize = GetNextPacketSize(buffer);
else
packetSize = 0; // Setting this to zero breaks the loop.
}
}
}