-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
9560549
commit 0f5aed8
Showing
12 changed files
with
7,959 additions
and
399 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,60 @@ | ||
package backend; | ||
|
||
#if sys | ||
import sys.net.Host; | ||
import sys.net.Socket as SysSocket; | ||
|
||
@:keep | ||
class Socket implements flixel.util.FlxDestroyUtil.IFlxDestroyable { | ||
public var socket:SysSocket; | ||
|
||
public function new(?socket:SysSocket) { | ||
this.socket = socket; | ||
if (this.socket == null) | ||
this.socket = new SysSocket(); | ||
this.socket.setFastSend(true); | ||
this.socket.setBlocking(false); | ||
} | ||
|
||
public function read():String { | ||
try { | ||
return this.socket.input.readUntil(('\n').charCodeAt(0)).replace("\\n", "\n"); | ||
} catch(e) trace('ERROR SOCKET READ - ${e}'); | ||
return null; | ||
} | ||
|
||
public function write(str:String):Bool { | ||
try { | ||
this.socket.output.writeString(str.replace("\n", "\\n")); | ||
return true; | ||
} catch(e) trace('ERROR SOCKET WRITE - ${e}'); | ||
return false; | ||
} | ||
|
||
public function host(host:Host, port:Int, nbConnections:Int = 1) { | ||
socket.bind(host, port); | ||
socket.listen(nbConnections); | ||
socket.setFastSend(true); | ||
} | ||
|
||
public function hostAndWait(h:Host, port:Int) { | ||
host(h, port); | ||
return acceptConnection(); | ||
} | ||
|
||
public function acceptConnection():Socket { | ||
socket.setBlocking(true); | ||
var accept = new Socket(socket.accept()); | ||
socket.setBlocking(false); | ||
return accept; | ||
} | ||
|
||
public function connect(host:Host, port:Int) { | ||
socket.connect(host, port); | ||
} | ||
|
||
public function destroy() { | ||
if (socket != null) socket.close(); | ||
} | ||
} | ||
#end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.