Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement read() and write() for sockets
  • Loading branch information
Tadeusz Sośnierz committed Aug 11, 2013
1 parent 968b9f8 commit cf06a85
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/vm/jvm/QAST/Compiler.nqp
Expand Up @@ -1912,6 +1912,8 @@ QAST::OperationsJAST.map_classlib_core_op('symlink', $TYPE_OPS, 'symlink', [$RT_

QAST::OperationsJAST.map_classlib_core_op('socket', $TYPE_OPS, 'socket', [], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('connect', $TYPE_OPS, 'connect', [$RT_OBJ, $RT_STR, $RT_INT], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('read', $TYPE_OPS, 'read', [$RT_OBJ, $RT_OBJ, $RT_INT], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('write', $TYPE_OPS, 'write', [$RT_OBJ, $RT_OBJ], $RT_OBJ, :tc);

QAST::OperationsJAST.map_classlib_core_op('opendir', $TYPE_OPS, 'opendir', [$RT_STR], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('nextfiledir', $TYPE_OPS, 'nextfiledir', [$RT_OBJ], $RT_STR, :tc);
Expand Down
18 changes: 17 additions & 1 deletion src/vm/jvm/runtime/org/perl6/nqp/io/NotQuiteSocket.java
Expand Up @@ -22,12 +22,28 @@ public void close(ThreadContext tc) {
}
}

public void connect(ThreadContext tc, String hostname, long port){
public void connect(ThreadContext tc, String hostname, long port) {
InetSocketAddress addr = new InetSocketAddress(hostname, (int)port);
try {
sock.connect(addr);
} catch (IOException e) {
throw ExceptionHandling.dieInternal(tc, e);
}
}

public long read(ThreadContext tc, byte[] buf) {
try {
return (long)sock.getInputStream().read(buf);
} catch (IOException e) {
throw ExceptionHandling.dieInternal(tc, e);
}
}

public void write(ThreadContext tc, byte[] buf) {
try {
sock.getOutputStream().write(buf);
} catch (IOException e) {
throw ExceptionHandling.dieInternal(tc, e);
}
}
}
28 changes: 28 additions & 0 deletions src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java
Expand Up @@ -373,6 +373,34 @@ public static SixModelObject connect(SixModelObject obj, String host, long port,
return obj;
}

public static SixModelObject read(SixModelObject obj, SixModelObject buf, long size, ThreadContext tc) {
IOHandleInstance h = (IOHandleInstance)obj;
NotQuiteSocket sock = (NotQuiteSocket)h.handle;

if (buf instanceof VMArrayInstance_i8) {
VMArrayInstance_i8 arr8 = (VMArrayInstance_i8)buf;
arr8.set_elems(tc, size);
arr8.elems = (int)sock.read(tc, arr8.slots);
arr8.start = 0;
return (SixModelObject)arr8;
} else {
throw ExceptionHandling.dieInternal(tc, "Cannot read() into an object of this type");
}
}

public static SixModelObject write(SixModelObject obj, SixModelObject buf, ThreadContext tc) {
IOHandleInstance h = (IOHandleInstance)obj;
NotQuiteSocket sock = (NotQuiteSocket)h.handle;

if (buf instanceof VMArrayInstance_i8) {
VMArrayInstance_i8 arr8 = (VMArrayInstance_i8)buf;
sock.write(tc, arr8.slots);
return (SixModelObject)arr8;
} else {
throw ExceptionHandling.dieInternal(tc, "Cannot read() into an object of this type");
}
}

public static SixModelObject setencoding(SixModelObject obj, String encoding, ThreadContext tc) {
if (obj instanceof IOHandleInstance) {
IOHandleInstance h = (IOHandleInstance)obj;
Expand Down

0 comments on commit cf06a85

Please sign in to comment.