Skip to content

Commit a50015d

Browse files
committed
improved abstract for error reason, breaks haxe 3 compatibility
1 parent d7f5a8b commit a50015d

File tree

7 files changed

+65
-42
lines changed

7 files changed

+65
-42
lines changed

src/peote/net/PeoteClient.hx

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class PeoteClient
180180
events.onError(this, errorNr );
181181
}
182182

183-
public function _onDisconnect(jointNr:Int, reason:Int):Void
183+
public function _onDisconnect(jointNr:Int, reason:Reason):Void
184184
{
185185
events.onDisconnect(this, reason);
186186
}

src/peote/net/PeoteClientEvents.hx

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import haxe.io.Bytes;
88

99
typedef PeoteClientEvents = {
1010
onEnter:PeoteClient -> Void,
11-
onError:PeoteClient -> Int -> Void,
12-
onDisconnect:PeoteClient -> Int -> Void,
11+
onError:PeoteClient -> Reason -> Void,
12+
onDisconnect:PeoteClient -> Reason -> Void,
1313
?onData:PeoteClient -> Bytes -> Void,
1414
?onDataChunk:PeoteClient -> Bytes -> Void,
1515
?onRemote:PeoteClient -> Int -> Void,

src/peote/net/PeoteNet.hx

+3-3
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class PeoteNet
244244
function (jointNr:Int):Void { peoteClient._onEnterJoint(p.peoteJointSocket, jointNr); },
245245
peoteClient._onData,
246246
//peoteClient.onDisconnect,
247-
function (jointNr:Int, reason:Int):Void { PeoteNet.onDisconnect(key, peoteClient, jointNr, reason); },
247+
function (jointNr:Int, reason:Reason):Void { PeoteNet.onDisconnect(key, peoteClient, jointNr, reason); },
248248
function (errorNr:Int):Void { PeoteNet.onEnterJointError(key, peoteClient, errorNr); }
249249
);
250250
} else peoteClient._onEnterJointError(Reason.ID);
@@ -384,7 +384,7 @@ class PeoteNet
384384
peoteClient._onEnterJointError(errorNr);
385385
}
386386

387-
public static function onDisconnect(key:String, peoteClient:PeoteClient, jointNr:Int, reason:Int):Void
387+
public static function onDisconnect(key:String, peoteClient:PeoteClient, jointNr:Int, reason:Reason):Void
388388
{
389389
if (sockets.exists(key))
390390
{
@@ -452,7 +452,7 @@ class PeoteNet
452452
function (jointNr:Int):Void { peoteClient._onEnterJoint(p.peoteJointSocket, jointNr); },
453453
peoteClient._onData,
454454
//peoteClient.onDisconnect,
455-
function (jointNr:Int, reason:Int):Void { PeoteNet.onDisconnect(key, peoteClient, jointNr, reason); },
455+
function (jointNr:Int, reason:Reason):Void { PeoteNet.onDisconnect(key, peoteClient, jointNr, reason); },
456456
function (errorNr:Int):Void { PeoteNet.onEnterJointError(key, peoteClient, errorNr); }
457457
);
458458
}

src/peote/net/PeoteServer.hx

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ class PeoteServer
225225
events.onUserConnect(this, userNr);
226226
}
227227

228-
public inline function _onUserDisconnect(jointNr:Int, userNr:Int, reason:Int):Void
228+
public inline function _onUserDisconnect(jointNr:Int, userNr:Int, reason:Reason):Void
229229
{
230230
remotes[userNr] = null;
231231

src/peote/net/PeoteServerEvents.hx

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import haxe.io.Bytes;
88

99
typedef PeoteServerEvents = {
1010
onCreate: PeoteServer -> Void,
11-
onError:PeoteServer -> Int -> Int -> Void,
11+
onError:PeoteServer -> Int -> Reason -> Void,
1212
onUserConnect:PeoteServer -> Int -> Void,
13-
onUserDisconnect:PeoteServer -> Int -> Int -> Void,
13+
onUserDisconnect:PeoteServer -> Int -> Reason -> Void,
1414
?onData:PeoteServer -> Int -> Bytes -> Void,
1515
?onDataChunk:PeoteServer -> Int -> Bytes -> Void,
1616
?onRemote:PeoteServer -> Int -> Int -> Void,

src/peote/net/Reason.hx

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
package peote.net;
22

3-
#if (haxe_ver >= 4.0) enum #else @:enum#end
4-
abstract Reason(Int) from Int to Int
3+
enum abstract Reason(Int) from Int to Int
54
{
6-
public static inline var DISCONNECT :Int = 0; // disconnected from peote-server (joint-owner/user)
7-
public static inline var CLOSE :Int = 1; // owner closed joint or user leaves
8-
public static inline var KICK :Int = 2; // user was kicked by joint-owner
9-
10-
public static inline var ID :Int = 10; // can't enter/open joint with this id (another or none exists)
11-
public static inline var FULL :Int = 11; // channel is full (max of 256 users already connected) (max is 128).
12-
public static inline var MAX :Int = 12; // created/joined to much channels on this server (max is 128).
13-
14-
public static inline var MALICIOUS :Int = 20; // malicious input
15-
}
5+
var DISCONNECT = 0; // disconnected from peote-server (channel-owner/user)
6+
var CLOSE = 1; // owner closed channel or user leaves
7+
var KICK = 2; // user was kicked by channel-owner (TODO!!!)
8+
9+
var ID = 10; // can't enter/open channel with this id (server: wrong id or already used, client: no chan with this id)
10+
var FULL = 11; // channel is full (max of 256 users already connected) (max is 128)
11+
var MAX = 12; // server created or client joined to much channels on this server (max is 128)
12+
13+
var MALICIOUS = 20; // malicious input
14+
15+
@to public function toString():String {
16+
return switch(this)
17+
{
18+
case DISCONNECT : "DISCONNECT";
19+
case CLOSE : "CLOSE";
20+
case KICK : "KICK"; // not yet implemented !!!
21+
22+
case ID : "ID";
23+
case FULL : "FULL";
24+
case MAX : "MAX";
25+
26+
case MALICIOUS : "MALICIOUS";
27+
28+
default: "unknown";
29+
}
30+
}
31+
}

testing/peote-net-test/src/test/Stress.hx

+28-21
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import peote.net.Reason;
1515

1616
class Stress
1717
{
18-
1918
var host:String;
2019
var port:Int;
2120

@@ -70,28 +69,31 @@ class Stress
7069
log('Channel ${server.jointNr} created. ("${server.jointId}")', 0, server.jointNr);
7170
Timer.delay(createNext, 100);
7271
},
73-
onError: function(server:PeoteServer, userNr:Int, reason:Int) {
72+
onError: function(server:PeoteServer, userNr:Int, reason:Reason) {
7473
var isOk:Bool = false;
7574
switch(reason) {
76-
case Reason.DISCONNECT:log("Can't connect to peote-server.", 0, server.jointNr);
75+
case DISCONNECT:log("Can't connect to peote-server.", 0, server.jointNr);
7776
//stopOnError = true;
78-
case Reason.CLOSE: log("Connection to peote-server is closed.", 0, server.jointNr);
79-
case Reason.ID: log("There is another channel with same ID. (or wrong ID)", 0, server.jointNr);
77+
case CLOSE: log("Connection to peote-server is closed.", 0, server.jointNr);
78+
case ID: log("There is another channel with same ID. (or wrong ID)", 0, server.jointNr);
8079
isOk = true;
81-
case Reason.MAX: log("Created to much channels on this server (max is 128).", 0, server.jointNr);
82-
case Reason.MALICIOUS: if (userNr > 0) log('User $userNr sending malicious data.', 0, server.jointNr); // TODO: kick/bann user
80+
case MAX: log("Created to much channels on this server (max is 128).", 0, server.jointNr);
81+
case MALICIOUS: if (userNr > 0) log('User $userNr sending malicious data.', 0, server.jointNr); // TODO: kick/bann user
82+
83+
default: trace(reason);
8384
}
8485
activeServers--;
8586
if (!stopOnError || isOk) Timer.delay(createNext, 100);
8687
},
8788
onUserConnect: function(server:PeoteServer, userNr:Int) {
8889
log('New user connects: jointNr:${server.jointNr}, userNr=$userNr', 0, server.jointNr);
8990
},
90-
onUserDisconnect: function(server:PeoteServer, userNr:Int, reason:Int) {
91+
onUserDisconnect: function(server:PeoteServer, userNr:Int, reason:Reason) {
9192
log('User disconnects: jointNr=${server.jointNr}, userNr=$userNr', 0, server.jointNr);
9293
switch (reason) {
93-
case Reason.CLOSE: log("User leaves channel.", 0, server.jointNr);
94-
case Reason.DISCONNECT: log("User disconnected from peote-server.", 0, server.jointNr);
94+
case CLOSE: log("User leaves channel.", 0, server.jointNr);
95+
case DISCONNECT: log("User disconnected from peote-server.", 0, server.jointNr);
96+
default: trace(reason);
9597
}
9698
},
9799
onDataChunk: function(server:PeoteServer, userNr:Int, bytes:Bytes) {
@@ -101,7 +103,9 @@ class Stress
101103
else server.sendChunk(userNr, bytes);
102104
}
103105
};
106+
104107
// --------------------------------------------------------------------------
108+
105109
clientEvents = {
106110
maxChunkSize: maxBytes,
107111
onEnter: function(client:PeoteClient) {
@@ -110,27 +114,30 @@ class Stress
110114
Timer.delay(enterNext, 100);
111115
sendRandomBytes(client);
112116
},
113-
onError: function(client:PeoteClient, reason:Int) {
117+
onError: function(client:PeoteClient, reason:Reason) {
114118
var isOk:Bool = false;
115119
switch(reason) {
116-
case Reason.DISCONNECT:log("Can't connect to peote-server.", 1, client.jointNr);
120+
case DISCONNECT:log("Can't connect to peote-server.", 1, client.jointNr);
117121
//stopOnError = true;
118-
case Reason.CLOSE: log("Connection to peote-server is closed.",1, client.jointNr);
119-
case Reason.ID: log("No channel with this ID to enter.",1, client.jointNr);
122+
case CLOSE: log("Connection to peote-server is closed.",1, client.jointNr);
123+
case ID: log("No channel with this ID to enter.",1, client.jointNr);
120124
isOk = true;
121-
case Reason.MAX: log("Entered to much channels on this server (max is 128)",1, client.jointNr);
122-
case Reason.FULL: log("Channel is full (max of 256 users already connected to this channel).",1, client.jointNr);
123-
case Reason.MALICIOUS: log("Channel-owner sending malicious data.",1, client.jointNr);
125+
case MAX: log("Entered to much channels on this server (max is 128)",1, client.jointNr);
126+
case FULL: log("Channel is full (max of 256 users already connected to this channel).",1, client.jointNr);
127+
case MALICIOUS: log("Channel-owner sending malicious data.",1, client.jointNr);
128+
129+
default: trace(reason);
124130
}
125131
activeClients--;
126132
if (!stopOnError || isOk) Timer.delay(enterNext, 100);
127133
},
128-
onDisconnect: function(client:PeoteClient, reason:Int) {
134+
onDisconnect: function(client:PeoteClient, reason:Reason) {
129135
log('Disconnect: jointNr:${client.jointNr}',1, client.jointNr);
130136
switch (reason) {
131-
case Reason.CLOSE: log("Channel closed by owner.",1, client.jointNr);
132-
case Reason.DISCONNECT:log("Channel-owner disconnected.",1, client.jointNr);
133-
//case Reason.KICK: log("Kicked by channel-owner.",1, client.jointNr); // TODO
137+
case CLOSE: log("Channel closed by owner.",1, client.jointNr);
138+
case DISCONNECT:log("Channel-owner disconnected.",1, client.jointNr);
139+
//case KICK: log("Kicked by channel-owner.",1, client.jointNr); // TODO
140+
default: trace(reason);
134141
}
135142
activeClients--;
136143
Timer.delay(enterNext, 100);

0 commit comments

Comments
 (0)