-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a1fc068
Showing
25 changed files
with
2,364 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
bin/ | ||
obj/ | ||
all_objs | ||
|
||
Thumbs.db | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
all: lib testing | ||
|
||
lib: | ||
cd project && haxelib run hxcpp Build.xml | ||
cd project && haxelib run hxcpp Build.xml -Dfulldebug | ||
cd project && haxelib run hxcpp Build.xml -DHXCPP_M64 | ||
cd project && haxelib run hxcpp Build.xml -DHXCPP_M64 -Dfulldebug | ||
|
||
ios: | ||
cd project && haxelib run hxcpp Build.xml -Dios | ||
cd project && haxelib run hxcpp Build.xml -Dios -Dsimulator | ||
cd project && haxelib run hxcpp Build.xml -Dios -DHXCPP_ARMV7 | ||
|
||
testing: | ||
cd test && haxe build.hxml | ||
test/bin/NetTest | ||
|
||
clean: | ||
rm -rf ndll | ||
rm -rf project/obj | ||
rm project/all_objs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "hxnet", | ||
"url" : "http://matttuttle.com", | ||
"license": "MIT", | ||
"tags": ["neko", "cpp"], | ||
"description": "Haxe networking helpers", | ||
"version": "1.0.0", | ||
"releasenote": "Initial version", | ||
"contributors": ["heardtheword"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package hxnet; | ||
|
||
#if (neko || cpp) | ||
|
||
#if neko | ||
import neko.Lib; | ||
#elseif cpp | ||
import cpp.Lib; | ||
#end | ||
|
||
typedef BonjourAddress = { | ||
var ip:String; | ||
var port:Int; | ||
var ipv6:Bool; | ||
}; | ||
|
||
typedef BonjourService = { | ||
var name:String; | ||
var port:Int; | ||
var type:String; | ||
var domain:String; | ||
@:optional var addresses:Array<BonjourAddress>; | ||
@:optional var hostName:String; | ||
} | ||
|
||
typedef BonjourCallbackData = { | ||
var type:String; | ||
var service:BonjourService; | ||
} | ||
|
||
typedef BonjourCallback = BonjourService->Void; | ||
|
||
class Bonjour | ||
{ | ||
|
||
public var domain:String; | ||
public var type:String; | ||
public var name:String; | ||
public var port:Int = 0; | ||
|
||
public static inline var WILL_PUBLISH:String = "willPublish"; | ||
public static inline var DID_PUBLISH:String = "didPublish"; | ||
public static inline var DID_NOT_PUBLISH:String = "didNotPublish"; | ||
public static inline var WILL_RESOLVE:String = "willResolve"; | ||
public static inline var DID_RESOLVE:String = "didResolve"; | ||
public static inline var DID_NOT_RESOLVE:String = "didNotResolve"; | ||
public static inline var DID_STOP:String = "didStop"; | ||
|
||
public function new(domain:String, type:String, name:String) | ||
{ | ||
this.domain = domain; | ||
this.type = type; | ||
this.name = name; | ||
_listeners = new Map<String, Array<BonjourCallback>>(); | ||
} | ||
|
||
public function publish(port:Int) | ||
{ | ||
stop(); | ||
_handle = hxnet_publish_bonjour_service(domain, type, name, port, bonjour_callback); | ||
} | ||
|
||
public function resolve(timeout:Float = 0.0) | ||
{ | ||
stop(); | ||
_handle = hxnet_resolve_bonjour_service(domain, type, name, timeout, bonjour_callback); | ||
} | ||
|
||
private function bonjour_callback(e:BonjourCallbackData) | ||
{ | ||
#if neko | ||
e = neko.Lib.nekoToHaxe(e); | ||
#end | ||
dispatchEvent(e.type, e.service); | ||
} | ||
|
||
public function stop() | ||
{ | ||
if (_handle != null) | ||
{ | ||
hxnet_bonjour_stop(_handle); | ||
_handle = null; | ||
} | ||
} | ||
|
||
public function addEventListener(event:String, callback:BonjourCallback) | ||
{ | ||
var callbacks:Array<BonjourCallback>; | ||
if (_listeners.exists(event)) | ||
{ | ||
callbacks = _listeners.get(event); | ||
} | ||
else | ||
{ | ||
callbacks = new Array<BonjourCallback>(); | ||
_listeners.set(event, callbacks); | ||
} | ||
callbacks.push(callback); | ||
} | ||
|
||
public function removeEventListener(event:String, callback:BonjourCallback) | ||
{ | ||
if (_listeners.exists(event)) | ||
{ | ||
var callbacks = _listeners.get(event); | ||
callbacks.remove(callback); | ||
} | ||
} | ||
|
||
private function dispatchEvent(event:String, service:BonjourService) | ||
{ | ||
if (_listeners.exists(event)) | ||
{ | ||
var callbacks = _listeners.get(event); | ||
for (callback in callbacks) | ||
{ | ||
callback(service); | ||
} | ||
} | ||
} | ||
|
||
private var _handle:Dynamic = null; | ||
private var _listeners:Map<String, Array<BonjourCallback>>; | ||
|
||
private static var hxnet_resolve_bonjour_service = Lib.load("hxnet", "hxnet_resolve_bonjour_service", 5); | ||
private static var hxnet_publish_bonjour_service = Lib.load("hxnet", "hxnet_publish_bonjour_service", 5); | ||
private static var hxnet_bonjour_stop = Lib.load("hxnet", "hxnet_bonjour_stop", 1); | ||
|
||
} | ||
|
||
#end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package hxnet; | ||
|
||
import haxe.io.Bytes; | ||
|
||
interface Connection | ||
{ | ||
public function writeBytes(bytes:Bytes):Void; | ||
public function close():Void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package hxnet; | ||
|
||
import haxe.io.Input; | ||
|
||
interface Protocol | ||
{ | ||
public function makeConnection(cnx:Connection):Void; | ||
public function loseConnection(?reason:String):Void; | ||
public function dataReceived(input:Input):Void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package hxnet.protocols; | ||
|
||
import hxnet.Connection; | ||
import haxe.io.Input; | ||
|
||
class BaseProtocol implements hxnet.Protocol | ||
{ | ||
public function new() { } | ||
|
||
public function makeConnection(cnx:Connection) | ||
{ | ||
this.cnx = cnx; | ||
} | ||
|
||
public function dataReceived(input:Input) | ||
{ | ||
} | ||
|
||
public function loseConnection(?reason:String) | ||
{ | ||
this.cnx = null; | ||
} | ||
|
||
private var cnx:Connection; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package hxnet.protocols; | ||
|
||
import haxe.io.BytesOutput; | ||
import haxe.io.Input; | ||
import haxe.io.Eof; | ||
|
||
class RPC extends BaseProtocol | ||
{ | ||
|
||
public override function dataReceived(input:Input) | ||
{ | ||
try | ||
{ | ||
var func = readString(input); | ||
var numArgs = input.readInt16(); | ||
var arguments = new Array<Dynamic>(); | ||
for (i in 0...numArgs) | ||
{ | ||
switch(input.readInt8()) | ||
{ | ||
case TYPE_INT: | ||
arguments.push(input.readInt32()); | ||
case TYPE_FLOAT: | ||
arguments.push(input.readFloat()); | ||
case TYPE_BOOL: | ||
arguments.push(input.readInt8() == 1 ? true : false); | ||
case TYPE_STRING: | ||
arguments.push(readString(input)); | ||
case TYPE_OBJECT: | ||
arguments.push(haxe.Unserializer.run(readString(input))); | ||
} | ||
} | ||
var rpcCall = Reflect.field(this, func); | ||
if (rpcCall != null) | ||
{ | ||
Reflect.callMethod(this, rpcCall, arguments); | ||
} | ||
} | ||
catch (e:Eof) | ||
{ | ||
#if debug | ||
trace("Invalid RPC data received"); | ||
#end | ||
} | ||
catch (e:Dynamic) | ||
{ | ||
// most likely an invalid call was made | ||
#if debug | ||
trace("RPC Error: " + e); | ||
#end | ||
} | ||
} | ||
|
||
public function call(func:String, ?arguments:Array<Dynamic>) | ||
{ | ||
if (arguments == null) arguments = []; | ||
|
||
var o = new BytesOutput(); | ||
writeString(o, func); | ||
o.writeInt16(arguments.length); | ||
for (arg in arguments) | ||
{ | ||
if (Std.is(arg, Float)) | ||
{ | ||
o.writeInt8(TYPE_FLOAT); | ||
o.writeFloat(arg); | ||
} | ||
else if (Std.is(arg, Int)) | ||
{ | ||
o.writeInt8(TYPE_INT); | ||
o.writeInt32(arg); | ||
} | ||
else if (Std.is(arg, Bool)) | ||
{ | ||
o.writeInt8(TYPE_BOOL); | ||
o.writeInt8(arg == true ? 1 : 0); | ||
} | ||
else if (Std.is(arg, String)) | ||
{ | ||
o.writeInt8(TYPE_STRING); | ||
writeString(o, arg); | ||
} | ||
else | ||
{ | ||
o.writeInt8(TYPE_OBJECT); | ||
writeString(o, haxe.Serializer.run(arg)); | ||
} | ||
} | ||
cnx.writeBytes(o.getBytes()); | ||
} | ||
|
||
private inline function readString(i:Input):String | ||
{ | ||
var len = i.readInt32(); | ||
return i.readString(len); | ||
} | ||
|
||
private inline function writeString(o:BytesOutput, value:String) | ||
{ | ||
o.writeInt32(value.length); | ||
o.writeString(value); | ||
} | ||
|
||
private static inline var TYPE_INT:Int = 0; | ||
private static inline var TYPE_FLOAT:Int = 1; | ||
private static inline var TYPE_BOOL:Int = 2; | ||
private static inline var TYPE_STRING:Int = 3; | ||
private static inline var TYPE_OBJECT:Int = 4; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package hxnet.protocols; | ||
|
||
import haxe.io.Input; | ||
|
||
class Telnet extends BaseProtocol | ||
{ | ||
public override function dataReceived(input:Input) | ||
{ | ||
var buffer = input.readLine(); | ||
if (buffer == "exit") | ||
{ | ||
cnx.close(); | ||
} | ||
lineReceived(buffer); | ||
} | ||
|
||
private function lineReceived(line:String) { } | ||
} |
Oops, something went wrong.