Permalink
Browse files

Adding timeout value to prevent 100% cpu

  • Loading branch information...
MattTuttle committed Feb 25, 2014
1 parent 8d773f1 commit 65333b9cba6e6d4ea27a656a32529870ea21656f
Showing with 14 additions and 21 deletions.
  1. +1 −1 hxnet/interfaces/Client.hx
  2. +1 −1 hxnet/interfaces/Server.hx
  3. +4 −4 hxnet/tcp/Client.hx
  4. +3 −3 hxnet/tcp/Server.hx
  5. +1 −1 hxnet/udp/Client.hx
  6. +1 −1 hxnet/udp/Server.hx
  7. +3 −9 test/TcpTest.hx
  8. +0 −1 test/UdpTest.hx
@@ -3,6 +3,6 @@ package hxnet.interfaces;
interface Client
{
public function connect(?hostname:String, ?port:Null<Int>):Void;
public function update():Void;
public function update(timeout:Float=1):Void;
public function close():Void;
}
@@ -2,6 +2,6 @@ package hxnet.interfaces;
interface Server
{
public function update():Void;
public function update(timeout:Float=1):Void;
public function close():Void;
}
View
@@ -40,7 +40,7 @@ class Client implements hxnet.interfaces.Client
}
}
public function update()
public function update(timeout:Float=1)
{
if (!connected) return;
@@ -52,7 +52,7 @@ class Client implements hxnet.interfaces.Client
}
else
{
select();
select(timeout);
}
}
catch (e:haxe.io.Eof)
@@ -63,9 +63,9 @@ class Client implements hxnet.interfaces.Client
}
}
private inline function select()
private inline function select(timeout:Float=1)
{
var select = Socket.select(readSockets, null, null, 0);
var select = Socket.select(readSockets, null, null, timeout);
var byte:Int = 0,
len = buffer.length,
bytesReceived:Int;
View
@@ -25,16 +25,16 @@ class Server implements hxnet.interfaces.Server
this.port = port;
listener = new Socket();
listener.bind(new Host(host), port);
listener.bind(#if flash host #else new Host(host) #end, port);
listener.listen(1);
listener.setBlocking(blocking);
readSockets = [listener];
}
public function update()
public function update(timeout:Float=1)
{
var select = Socket.select(readSockets, null, null, 0);
var select = Socket.select(readSockets, null, null, timeout);
var byte:Int = 0,
len = buffer.length,
bytesReceived:Int;
View
@@ -30,7 +30,7 @@ class Client implements hxnet.interfaces.Client
protocol.makeConnection(connection);
}
public function update()
public function update(timeout:Float=1)
{
if (!connected) return;
View
@@ -35,7 +35,7 @@ class Server implements hxnet.interfaces.Server
lastUpdate = Timer.stamp();
}
public function update()
public function update(timeout:Float=1)
{
var now = Timer.stamp();
var delta = now - lastUpdate;
View
@@ -31,12 +31,6 @@ class TcpTest extends haxe.unit.TestCase
public override function tearDown()
{
serverThread.sendMessage("finish");
Sys.sleep(1);
}
private inline function updateClient(client:hxnet.tcp.Client, times:Int=100)
{
while (times-- > 0) client.update();
}
public function testRPC()
@@ -48,7 +42,7 @@ class TcpTest extends haxe.unit.TestCase
client.connect(serverPort);
rpc.call("ping");
updateClient(client);
client.update();
assertTrue(rpc.pingCount > 0);
}
@@ -62,7 +56,7 @@ class TcpTest extends haxe.unit.TestCase
client.connect(serverPort);
rpc.call("pong", [1, 12.4]);
updateClient(client);
client.update();
assertTrue(rpc.pingCount > 0);
}
@@ -76,7 +70,7 @@ class TcpTest extends haxe.unit.TestCase
client.connect(serverPort);
rpc.call("foo", [1, 20.4, "hi"]); // this call should fail
updateClient(client, 1000);
client.update(0.1);
assertEquals(0, rpc.pingCount);
}
View
@@ -31,7 +31,6 @@ class UdpTest extends haxe.unit.TestCase
public override function tearDown()
{
serverThread.sendMessage("finish");
Sys.sleep(1);
}
private inline function updateClient(client:hxnet.udp.Client, times:Int=100)

0 comments on commit 65333b9

Please sign in to comment.