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

socket.write(byte[]) does not work #484

Open
cool2man opened this issue Oct 26, 2016 · 3 comments
Open

socket.write(byte[]) does not work #484

cool2man opened this issue Oct 26, 2016 · 3 comments
Labels

Comments

@cool2man
Copy link

My code looks like:

socket = new Socket(host, port);
streamOut = new BufferedOutputStream(socket.getOutputStream());
byte[] b = new byte[] { 0x33, 0x66, (byte) 0x99, (byte) 0xcc };
streamOut.write(b);

This results into sending ONE byte (which is 0x00) instead of the FOUR bytes.

Having a look into websock.js I noticed a problem in the following function:

function send(arr) {
    Util.Debug(">> send_array: " + arr);
    sQ = sQ.concat(arr);
    return flush();
}

sQ is an empty array, while arr is an Uint8Array. Therefore sQ.concat(arr) results into an array with ONE entry which is the complete Uint8Array arr (and not in an array with 4 entries).

I changed the send function for my testing:

function send(arr) {
    Util.Debug(">> send_array: " + arr);
    // sQ = sQ.concat(arr);
    for (var i=0; i<arr.length; i++) {
        sQ.push(arr[i]);
    }
    return flush();
}

I think, that doppio should take care, that the parameter "arr" is of type array and not Unint8Array.

@jvilk
Copy link
Member

jvilk commented Oct 26, 2016

Yeah; because I never added automated tests for them, sockets gradually became broken and nobody noticed.

I began fixing sockets in this PR, but ran out of time to completely fix them. I'd like to return to the PR sometime to get them in ship-shape.

If you have any inclination to fix them yourself, let me know! I'd happily let you take over the PR.

@jvilk jvilk added the bug label Oct 26, 2016
@cool2man
Copy link
Author

I would invest the time to take over the PR - but my experience is too weak. I will try to fix the socket stuff for my use case (quick & half blind & dirty) and will you keep informed what I did.

@cool2man
Copy link
Author

cool2man commented Nov 2, 2016

This is my version of socketWrite0 which works well for me:

    public static 'socketWrite0(Ljava/io/FileDescriptor;[BII)V'(thread: JVMThread, javaThis: JVMTypes.java_net_SocketOutputStream, fd: JVMTypes.java_io_FileDescriptor, b: JVMTypes.JVMArray<number>, offset: number, len: number): void {
      var impl = <JVMTypes.java_net_PlainSocketImpl> javaThis['java/net/SocketOutputStream/impl'];
      if (impl.$is_shutdown === true) {
        thread.throwNewException('Ljava/io/IOException;', 'Socket is shutdown.');
      } else if (impl.$ws.get_raw_state() !== WebSocket.OPEN) {
        thread.throwNewException('Ljava/io/IOException;', 'Connection isn\'t open');
      } else {
        var a : any = [];
        for (var i=0; i<len; i++) {
          a.push(b.array[i+offset]);
        }
        impl.$ws.send(a);
        thread.setStatus(ThreadStatus.ASYNC_WAITING);
        setImmediate(() => {
          thread.asyncReturn();
        });
      }
    }

lines 8 to 12 were added/changed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants