Skip to content

Commit 59fb7d2

Browse files
committed
Initial ops for socket IO using NIO.
1 parent 83ea6f2 commit 59fb7d2

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/vm/jvm/QAST/Compiler.nqp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,9 @@ QAST::OperationsJAST.map_classlib_core_op('openasync', $TYPE_OPS, 'openasync', [
19301930
QAST::OperationsJAST.map_classlib_core_op('slurpasync', $TYPE_OPS, 'slurpasync', [$RT_OBJ, $RT_OBJ, $RT_OBJ, $RT_OBJ], $RT_OBJ, :tc);
19311931
QAST::OperationsJAST.map_classlib_core_op('linesasync', $TYPE_OPS, 'linesasync', [$RT_OBJ, $RT_OBJ, $RT_INT, $RT_OBJ, $RT_OBJ, $RT_OBJ], $RT_OBJ, :tc);
19321932

1933+
QAST::OperationsJAST.map_classlib_core_op('socket', $TYPE_OPS, 'socket', [], $RT_OBJ, :tc);
1934+
QAST::OperationsJAST.map_classlib_core_op('connect', $TYPE_OPS, 'connect', [$RT_OBJ, $RT_STR, $RT_INT], $RT_OBJ, :tc);
1935+
19331936
QAST::OperationsJAST.map_classlib_core_op('debugnoop', $TYPE_OPS, 'debugnoop', [$RT_OBJ], $RT_OBJ, :tc);
19341937

19351938
# terms

src/vm/jvm/runtime/org/perl6/nqp/io/SocketHandle.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@
1010

1111
public class SocketHandle extends SyncHandle {
1212

13-
public SocketHandle(ThreadContext tc, String host, int port) {
13+
public SocketHandle(ThreadContext tc) {
1414
try {
15-
InetSocketAddress addr = InetSocketAddress.createUnresolved(host, port);
16-
chan = SocketChannel.open(addr);
15+
chan = SocketChannel.open();
1716
setEncoding(tc, Charset.forName("UTF-8"));
1817
} catch (IOException e) {
1918
throw ExceptionHandling.dieInternal(tc, e);
2019
}
2120
}
21+
22+
public void connect(ThreadContext tc, String host, int port) {
23+
try {
24+
InetSocketAddress addr = new InetSocketAddress(host, port);
25+
((SocketChannel)chan).connect(addr);
26+
} catch (IOException e) {
27+
throw ExceptionHandling.dieInternal(tc, e);
28+
}
29+
}
2230
}

src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
import java.math.BigInteger;
1212
import java.math.RoundingMode;
1313
import java.nio.ByteBuffer;
14+
import java.nio.channels.SocketChannel;
1415
import java.nio.charset.Charset;
1516
import java.nio.file.DirectoryStream;
1617
import java.nio.file.Files;
1718
import java.nio.file.LinkOption;
18-
import java.nio.file.NoSuchFileException;
1919
import java.nio.file.Path;
2020
import java.nio.file.Paths;
2121
import java.nio.file.attribute.FileTime;
@@ -43,6 +43,7 @@
4343
import org.perl6.nqp.io.IIOSeekable;
4444
import org.perl6.nqp.io.IIOSyncReadable;
4545
import org.perl6.nqp.io.IIOSyncWritable;
46+
import org.perl6.nqp.io.SocketHandle;
4647
import org.perl6.nqp.io.StandardReadHandle;
4748
import org.perl6.nqp.io.StandardWriteHandle;
4849
import org.perl6.nqp.jast2bc.JASTToJVMBytecode;
@@ -280,6 +281,21 @@ public static SixModelObject openasync(String path, String mode, ThreadContext t
280281
return h;
281282
}
282283

284+
public static SixModelObject socket(ThreadContext tc) {
285+
SixModelObject IOType = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.ioType;
286+
IOHandleInstance h = (IOHandleInstance)IOType.st.REPR.allocate(tc, IOType.st);
287+
h.handle = new SocketHandle(tc);
288+
return h;
289+
}
290+
291+
public static SixModelObject connect(SixModelObject obj, String host, long port, ThreadContext tc) {
292+
IOHandleInstance h = (IOHandleInstance)obj;
293+
if (h.handle instanceof SocketHandle) {
294+
((SocketHandle)h.handle).connect(tc, host, (int) port);
295+
}
296+
return obj;
297+
}
298+
283299
public static long filereadable(String path, ThreadContext tc) {
284300
Path path_o;
285301
long res;

0 commit comments

Comments
 (0)