private void Receive()
{
StateObject state = new StateObject();
state.WorkSocket = socket;
disconnectionType = null;
socket.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
new Thread(delegate() {
while (socket.Connected && keepRunning)
{
int timeoutClient = (int)(DateTime.Now - packetSent).TotalSeconds;
int timeoutServer = (int)(DateTime.Now - packetReceived).TotalSeconds;
if (timeoutClient >= 5)
{
if (timeoutServer >= 20)
{
Disconnect(BattlEyeDisconnectionType.ConnectionLost);
keepRunning = true;
}
else
{
if (packetQueue.Count == 0)
{
SendCommandPacket(null, false);
}
}
}
if (socket.Connected && packetQueue.Count > 0 && socket.Available == 0)
{
try
{
int key = packetQueue.First().Key;
if (currentPacket == -1 || !packetQueue.ContainsKey(currentPacket)) {
currentPacket = key;
string value = packetQueue[key][0];
DateTime date = DateTime.Parse(packetQueue[key][1]);
int timeDiff = (int)(DateTime.Now - date).TotalSeconds;
sendPacket(ConstructPacket(BattlEyePacketType.Command, key, value));
}
}
catch
{
// Prevent possible crash when packet is received at the same moment it's trying to resend it.
}
}
Thread.Sleep(250);
}
if (!socket.Connected)
{
if (ReconnectOnPacketLoss && keepRunning)
{
Connect();
}
else if (!keepRunning)
{
//let the thread finish without further action
}
else
{
OnDisconnect(loginCredentials, BattlEyeDisconnectionType.ConnectionLost);
}
}
}).Start();
}