Skip to content

Commit

Permalink
[Truffle] Add io_socket_read primitive
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Fish committed Oct 4, 2016
1 parent 82c5784 commit 4a365b9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
3 changes: 0 additions & 3 deletions spec/truffle/tags/library/socket/basicsocket/send_tags.txt

This file was deleted.

Expand Up @@ -256,6 +256,33 @@ public DynamicObject ensureOpen(VirtualFrame frame, DynamicObject file,

}


@Primitive(name = "io_socket_read", lowerFixnum = {1, 2, 3, 4}, unsafe = UnsafeGroup.IO)
public static abstract class IOSocketReadNode extends IOPrimitiveArrayArgumentsNode {

@TruffleBoundary(throwsControlFlowException = true)
@Specialization
public Object socketRead(DynamicObject io, int length, int flags, int type) {
final int sockfd = Layouts.IO.getDescriptor(io);

if (type != 0) {
throw new UnsupportedOperationException();
}

final ByteBuffer buffer = ByteBuffer.allocate(length);
final int bytesRead = getContext().getThreadManager().runUntilResult(this, new ThreadManager.BlockingAction<Integer>() {
@Override
public Integer block() throws InterruptedException {
return ensureSuccessful(nativeSockets().recvfrom(sockfd, buffer, length, flags, PointerPrimitiveNodes.NULL_POINTER, PointerPrimitiveNodes.NULL_POINTER));
}
});
buffer.position(bytesRead);

return createString(new ByteList(buffer.array(), buffer.arrayOffset(), buffer.position(), false));
}

}

@Primitive(name = "io_read_if_available", lowerFixnum = 1, unsafe = UnsafeGroup.IO)
public static abstract class IOReadIfAvailableNode extends IOPrimitiveArrayArgumentsNode {

Expand Down
Expand Up @@ -13,6 +13,8 @@
import jnr.posix.Timeval;
import org.jruby.truffle.platform.posix.Sockets;

import java.nio.ByteBuffer;

public class JavaSockets implements Sockets {

@Override
Expand Down Expand Up @@ -85,6 +87,11 @@ public int connect(int socket, Pointer address, int address_len) {
throw new UnsupportedOperationException();
}

@Override
public int recvfrom(int sockfd, ByteBuffer buf, int len, int flags, Pointer src_addr, Pointer addrlen) {
throw new UnsupportedOperationException();
}

@Override
public int send(int sockfd, Pointer buf, int len, int flags) {
throw new UnsupportedOperationException();
Expand Down
Expand Up @@ -12,6 +12,8 @@
import jnr.ffi.Pointer;
import jnr.posix.Timeval;

import java.nio.ByteBuffer;

public interface Sockets {

/*
Expand Down Expand Up @@ -119,6 +121,17 @@ public interface Sockets {

int connect(int socket, Pointer address, int address_len);

/**
* int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen
* @param sockfd
* @param buf
* @param len
* @param flags
* @return
*/
int recvfrom(int sockfd, ByteBuffer buf, int len, int flags, Pointer src_addr, Pointer addrlen);

/**
* int send(int sockfd, Pointer buf, int len, int flags);
*/
Expand Down

0 comments on commit 4a365b9

Please sign in to comment.