private void ProcessPacket(byte[] packet, int bytes) { var offset = 0; const int SIZE_OF_PACKET = 100; //104 on new server try { if (bytes > 0) { // we have received some bytes, check if this is the beginning of a new packet or a chunk of an existing one. offset = CheckStart(packet); eq.BeginProcessPacket(); //clears spawn&ground arrays for (; offset + SIZE_OF_PACKET <= bytes; offset += SIZE_OF_PACKET) { Spawninfo si = new Spawninfo(); if (offset < 0) { // copy the missing chunk of the incomplete packet to the incomplete packet buffer try { PacketCopy(packet, SIZE_OF_PACKET); } catch (Exception ex) { LogLib.WriteLine("Error: ProcessPacket: Copy Incomplete packet buffer: ", ex); } incompleteCount = 0; if (incompletebuffer.Length == 0) { numPackets = 0; break; } si.Frombytes(incompletebuffer, 0); } else { si.Frombytes(packet, offset); } numProcessed++; ProcessPacket(si); } eq.ProcessSpawnList(f1.SpawnList); eq.ProcessGroundItemList(f1.GroundItemList); } } catch (Exception ex) { LogLib.WriteLine("Error: ProcessPacket: ", ex); } ProcessedPackets(packet, bytes, offset); }
private void ProcessPacket(byte[] packet, int bytes) { int offset = 0; const int SIZE_OF_PACKET = 100; //104 on new server try { if (bytes > 0) { // we have received some bytes, check if this is the beginning of a new packet or a chunk of an existing one. if (numPackets == 0) { // The first word in the data stream is the number of packets numPackets = BitConverter.ToInt32(packet, 0); offset = 4; f1.StartNewPackets(); } else { // We havent finished processing packets, so check if we have any extra bytes stored in our incomplete buffer. offset = -incompleteCount; } eq.BeginProcessPacket(); for (; (offset + SIZE_OF_PACKET) <= bytes; offset += SIZE_OF_PACKET) { SPAWNINFO si = new SPAWNINFO(); if (offset < 0) { // copy the missing chunk of the incomplete packet to the incomplete packet buffer try { if (incompleteCount > 0 && packet.Length > 0) { Array.Copy(packet, 0, incompletebuffer, incompleteCount, SIZE_OF_PACKET - incompleteCount); } } catch (Exception ex) { LogLib.WriteLine("Error in ProcessPacket() Copy Incomplete packet buffer: ", ex); } incompleteCount = 0; if (incompletebuffer.Length == 0) { numPackets = 0; break; } si.frombytes(incompletebuffer, 0); } else { si.frombytes(packet, offset); } numProcessed++; f1.ProcessPacket(si, update_hidden); } f1.ProcessSpawnList(); f1.ProcessGroundItemList(); } } catch (Exception ex) { LogLib.WriteLine("Error in ProcessPacket(): ", ex); } if (numProcessed < numPackets) { if (offset < bytes) { // Copy unprocessed bytes into the incomplete buffer incompleteCount = bytes - offset; try { Array.Copy(packet, offset, incompletebuffer, 0, incompleteCount); } catch (Exception ex) { LogLib.WriteLine("Error in ProcessPacket(): Copy to Incomplete Buffer: ", ex); LogLib.WriteLine($"Packet Size: {packet.Length} Offset: {offset}"); LogLib.WriteLine($"Buffer Size: {incompletebuffer.Length} Incomplete Size: {incompleteCount}"); } } } else { // Finished proceessing the request RequestPending = false; if (update_hidden) { update_hidden = false; } numPackets = numProcessed = 0; incompleteCount = 0; // Make sure that the incomplete buffer is actually empty if (incompletebuffer.Length > 0) { for (int pp = 0; pp < incompletebuffer.Length; pp++) { incompletebuffer[pp] = 0; } } f1.checkMobs(); f1.mapCon.Invalidate(); } }