Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make basic Socket writing, so Http could work #187

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/sys/net/Socket.hx
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
package sys.net;
import haxe.io.Output;
import js.Node;
import haxe.io.Bytes;

class SocketOutput extends Output {

var socket:Socket;

public function new(socket:Socket) {
this.socket = socket;
}

override function writeByte(c:Int):Void {
var b = new js.node.Buffer([c]);
socket._write(b);
}

/**
Write `len` bytes from `s` starting by position specified by `pos`.

Returns the actual length of written data that can differ from `len`.

See `writeFullBytes` that tries to write the exact amount of specified bytes.
**/
override function writeBytes(s:Bytes, pos:Int, len:Int):Int {
var b = js.node.Buffer.hxFromBytes(s);
socket._write(b);
return b.length;
}

}

class SocketInput extends haxe.io.Input {
var s:Socket;
Expand Down Expand Up @@ -50,9 +81,11 @@ class Socket {
var inputPos:Int = 0;

public var input:haxe.io.Input;
public var output:haxe.io.Output;

public function new() {
input = new SocketInput(this);
output = new SocketOutput(this);
}

public function connect(host:Host, port:Int) {
Expand All @@ -65,11 +98,23 @@ class Socket {
if (inputData.length == 0)
NodeSync.wait(function() return inputData.length > 0);
}

public function _write(buf){
s.write(buf);
}

public function close() {
if (s != null) {
s.destroy();
s = null;
}
}

public function shutdown(a,b){
close();
}

public function setTimeout(ms){

}
}