Skip to content

Commit

Permalink
Adding timeout value to prevent 100% cpu
Browse files Browse the repository at this point in the history
  • Loading branch information
MattTuttle committed Feb 25, 2014
1 parent 8d773f1 commit 65333b9
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 21 deletions.
2 changes: 1 addition & 1 deletion hxnet/interfaces/Client.hx
Expand Up @@ -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 changes: 1 addition & 1 deletion hxnet/interfaces/Server.hx
Expand Up @@ -2,6 +2,6 @@ package hxnet.interfaces;

interface Server
{
public function update():Void;
public function update(timeout:Float=1):Void;
public function close():Void;
}
8 changes: 4 additions & 4 deletions hxnet/tcp/Client.hx
Expand Up @@ -40,7 +40,7 @@ class Client implements hxnet.interfaces.Client
}
}

public function update()
public function update(timeout:Float=1)
{
if (!connected) return;

Expand All @@ -52,7 +52,7 @@ class Client implements hxnet.interfaces.Client
}
else
{
select();
select(timeout);
}
}
catch (e:haxe.io.Eof)
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions hxnet/tcp/Server.hx
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion hxnet/udp/Client.hx
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion hxnet/udp/Server.hx
Expand Up @@ -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;
Expand Down
12 changes: 3 additions & 9 deletions test/TcpTest.hx
Expand Up @@ -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()
Expand All @@ -48,7 +42,7 @@ class TcpTest extends haxe.unit.TestCase
client.connect(serverPort);
rpc.call("ping");

updateClient(client);
client.update();

assertTrue(rpc.pingCount > 0);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion test/UdpTest.hx
Expand Up @@ -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)
Expand Down

0 comments on commit 65333b9

Please sign in to comment.