socket.write(byte[]) does not work
cool2man opened this issue · comments
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.
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.
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.
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