[common] bullet with bricks collision
This commit is contained in:
80
src/common/haxe/ru/m/connect/PacketQueue.hx
Executable file
80
src/common/haxe/ru/m/connect/PacketQueue.hx
Executable file
@@ -0,0 +1,80 @@
|
||||
package ru.m.connect;
|
||||
|
||||
import ru.m.connect.IConnection.IPacketBuilder;
|
||||
import protohx.Message;
|
||||
import haxe.io.BytesInput;
|
||||
import haxe.io.BytesBuffer;
|
||||
import haxe.io.Bytes;
|
||||
|
||||
class PacketQueue {
|
||||
|
||||
public static inline var HEADER_SIZE:Int = 4;
|
||||
|
||||
private var builder:IPacketBuilder;
|
||||
private var bytesBuff:Bytes;
|
||||
private var msgs:List<Message>;
|
||||
|
||||
public function new(builder:IPacketBuilder) {
|
||||
this.builder = builder;
|
||||
msgs = new List<Message>();
|
||||
}
|
||||
|
||||
public inline function hasMsg():Bool {
|
||||
return !msgs.isEmpty();
|
||||
}
|
||||
|
||||
public inline function popMsg():Message {
|
||||
return msgs.pop();
|
||||
}
|
||||
|
||||
public inline function addMsg(msg:Message):Void {
|
||||
msgs.add(msg);
|
||||
}
|
||||
|
||||
public function addBytes(bytes:Bytes) {
|
||||
if (bytes == null) {
|
||||
return;
|
||||
}
|
||||
if (bytesBuff == null) {
|
||||
bytesBuff = bytes;
|
||||
} else {
|
||||
var buffer = new BytesBuffer();
|
||||
buffer.add(bytesBuff);
|
||||
buffer.add(bytes);
|
||||
bytesBuff = buffer.getBytes();
|
||||
}
|
||||
if (bytesBuff == null || bytesBuff.length < HEADER_SIZE) {
|
||||
return;
|
||||
}
|
||||
var available = bytesBuff.length;
|
||||
var bi = new BytesInput(bytesBuff);
|
||||
bi.bigEndian = false;
|
||||
while (available >= HEADER_SIZE) {
|
||||
var family = bi.readByte();
|
||||
var id = bi.readByte();
|
||||
var packetSize = bi.readUInt16();
|
||||
available -= HEADER_SIZE;
|
||||
if (packetSize <= available) {
|
||||
available -= packetSize;
|
||||
var msgBytes = bi.read(packetSize);
|
||||
var packet = builder.buildPacket({family:family, id:id});
|
||||
packet.mergeFrom(msgBytes);
|
||||
addMsg(packet);
|
||||
} else {
|
||||
available += HEADER_SIZE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (available == 0) {
|
||||
bytesBuff = null;
|
||||
} else if (available > 0) {
|
||||
if (bytesBuff.length != available) {
|
||||
var pos = bytesBuff.length - available;
|
||||
bytesBuff = bytesBuff.sub(pos, available);
|
||||
}
|
||||
} else {
|
||||
throw "Wrong available: " + available;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user