package; import haxe.io.Bytes; import peote.net.PeoteClient; import peote.io.PeoteBytesOutput; import peote.io.PeoteBytesInput; class Client { var peoteClient:PeoteClient; var netModel:ModelInterface; var inputBuffer:PeoteBytesInput; // stores not fully readed chunk var chunk_size:Int = 0; public function new () { } public function open (server:String, port:Int, joint:String, onModelConnect:ModelInterface->Void, onModelDisconnect:ModelInterface->Void) { peoteClient = new PeoteClient({ onEnterJoint: function(jointNr:Int) { trace("onEnterJoint: jointNr=" + jointNr); netModel = new NetModel(peoteClient); onModelConnect( netModel ); }, onEnterJointError: function(errorNr:Int) { trace("onEnterJointError:"+errorNr); }, onDisconnect: function(jointNr:Int, reason:Int) { trace("onDisconnect: jointNr=" + jointNr + ", reason=" + reason); onModelDisconnect( netModel ); }, onData: function(jointNr:Int, bytes:Bytes ) { trace("Server sends some bytes on channel " + jointNr); // TODO: PeoteNet erweitern um: // peoteClient.readChunk(jointNr, bytes, onChunk) inputBuffer.append( bytes ); if (chunk_size == 0 && inputBuffer.bytesLeft() >=2 ) { chunk_size = inputBuffer.readUInt16(); // read chunk size } if ( chunk_size != 0 && inputBuffer.bytesLeft() >= chunk_size ) { onDataChunk( inputBuffer, chunk_size ); chunk_size = 0; } // todo: readbyte // erstes byte is immer die view/controler + nr. // view.interfacemethod(params) var viewNr:Int = bytes.get(0); if (viewNr > Server.MAX_VIEWS_PER_CLIENT) { // COMMUNICATIONPROBLEM } // ansonsten schauen von welchem netModel.view // welche Funktion+params aufgerufen werden soll // netModel.view[viewNr].???(???) } }); peoteClient.enterJoint(server, port, joint); } // this will called if full chunk arrives public function onDataChunk( input:PeoteBytesInput, chunk_size:Int ):Void { //input.readString(); } public function close():Void { peoteClient.leaveJoint(); } }