Permalink
Browse files

Connect/disconnect now working

  • Loading branch information...
jfd committed Jul 6, 2010
1 parent 101c99d commit b6a03b1aef11544a6076fea6a05f3641f0a50fef
@@ -101,6 +101,19 @@ package {
function(event:HydnaStreamEvent) : void {
var data:String = event.buffer.readUTF();
output.appendText("Receivied '" + data + "' from network...\n\n");
+ hydna.shutdown();
+ }
+ );
+
+ stream.addEventListener(Event.CLOSE,
+ function(event:Event) : void {
+ output.appendText("Stream is now closed\n");
+ }
+ );
+
+ hydna.addEventListener(Event.CLOSE,
+ function(event:Event) : void {
+ output.appendText("Hydna is now closed\n\n");
}
);
}
@@ -98,7 +98,7 @@ package {
if (++messagesReceived == MESSAGE_COUNT) {
var time:Number = ((new Date()).getTime() - starttime);
output.appendText("Done in " + time + " milliseconds\n");
-/* hydna.close();*/
+ hydna.shutdown();
}
}
);
View
@@ -190,11 +190,9 @@ package com.hydna {
private function connectHandler(event:Event) : void {
dispatchEvent(event);
- trace("connectHandler: " + event);
}
private function closeHandler(event:Event) : void {
- trace("closeHandler: " + event);
if (_finalized || _shutdown) {
dispatchEvent(event);
} else {
@@ -247,7 +245,6 @@ package com.hydna {
// Ignore packet
trace("ignore packet, stream not found");
} else {
-
switch (packetFlag) {
case HydnaPacket.OPENSTAT:
@@ -264,6 +261,8 @@ package com.hydna {
}
}
+ receiveBuffer.position = packetLength;
+
if (receiveBuffer.length > packetLength) {
var newSize:Number = receiveBuffer.length - packetLength;
var tempBuffer:ByteArray = new ByteArray();
@@ -338,7 +337,7 @@ package com.hydna {
var dataLength:Number = packetLength - HydnaPacket.HEADER_LENGTH;
var event:HydnaStreamEvent;
var code:Number;
-
+
if (dataStream == null) {
return;
}
@@ -360,34 +359,36 @@ package com.hydna {
function eventloop() : void {
connectedStreams.pop();
-
if (connectedStreams.length == 0) {
_finalized = true;
if (socket.connected) {
socket.close();
- } else {
- dispatchEvent(new Event(Event.CLOSE));
}
+ dispatchEvent(new Event(Event.CLOSE));
}
}
for (var key:String in _streams) {
stream = HydnaStream(_streams[key]);
if (stream != null && stream.connected) {
+
stream.addEventListener(Event.CLOSE, eventloop);
connectedStreams.push(stream);
}
}
- index = connectedStreams.length;
-
- while (index--) {
- stream = HydnaStream(connectedStreams[index]);
- stream.close();
- }
+ if (connectedStreams.length == 0) {
+ eventloop();
+ } else {
+ index = connectedStreams.length;
+ while (index--) {
+ stream = HydnaStream(connectedStreams[index]);
+ stream.close();
+ }
+ }
}
}
@@ -155,7 +155,6 @@ package com.hydna {
override public function close() : Boolean {
if (super.close()) {
-
if (socket.connected) {
socket.writeShort(HydnaPacket.HEADER_LENGTH);
socket.writeByte(HydnaPacket.CLOSE);
@@ -167,6 +166,7 @@ package com.hydna {
}
return true;
}
+
return false;
}
@@ -127,8 +127,8 @@ package com.hydna {
* Closes the stream
*/
public function close() : Boolean {
- if (_connected == false || _closing == false) return false;
- _closing = false;
+ if (_connected == false || _closing == true) return false;
+ _closing = true;
return true;
}

0 comments on commit b6a03b1

Please sign in to comment.