1 jls.loader.provide('jls.net.Socket'); 2 3 jls.loader.requireLibrary('jls_net'); 4 jls.loader.require('jls.net.InetAddress'); 5 jls.loader.require('jls.net.InetSocketAddress'); 6 7 jls.net.Socket = jls.lang.Class.create( /** @lends jls.net.Socket.prototype */ 8 { 9 /** 10 * Creates a TCP socket. 11 * 12 * @param {String} [host] The host name to connect to, could be a jls.net.InetAddress. 13 * @param {Number} [port] The port number to connect to. 14 * @constructs 15 * @class This class represents a client TCP socket. 16 * A socket is an endpoint for communication between two machines. 17 * TCP (Transmission Control Protocol) is a connection-oriented, reliable byte-stream protocol of the TCP/IP protocol suite. 18 */ 19 initialize : function(host, port) { 20 this._fd = null; 21 this._address = null; 22 if (typeof host == 'undefined') { 23 // nothing to do 24 } else if (host instanceof _native.net.Socket) { 25 this._fd = host; 26 } else if (host instanceof jls.net.InetSocketAddress) { 27 this.connect(host); 28 } else if ((host instanceof jls.net.InetAddress) || (typeof host == 'string')) { 29 this.connect(new jls.net.InetSocketAddress(host, port)); 30 } else { 31 throw new jls.lang.Exception('Invalid arguments'); 32 } 33 }, 34 /** 35 * Connects this socket to the server. 36 * 37 * @returns {jls.net.InetSocketAddress} The socket address of the server. 38 */ 39 connect : function(endpoint) { 40 if (! (endpoint instanceof jls.net.InetSocketAddress)) { 41 throw new jls.lang.Exception('Invalid socket address'); 42 } 43 if (this._fd == null) { 44 this._fd = new _native.net.Socket(); 45 } 46 this._address = endpoint; 47 this._fd.connect(endpoint.getAddress().getHostAddress(), endpoint.getPort()); 48 return this; 49 }, 50 getRemoteSocketAddress : function() { 51 return this._address; 52 }, 53 /** 54 * Closes this socket. 55 * 56 * @returns {jls.net.Socket} This socket. 57 */ 58 close : function() { 59 this._fd.close(); 60 return this; 61 }, 62 /** 63 * Flushs this socket. 64 * 65 * @returns {jls.net.Socket} This socket. 66 */ 67 flush : function() { 68 return this; 69 }, 70 /** 71 * Reads a byte. 72 * 73 * @returns {Number} The unsigned byte or -1. 74 */ 75 readByte : function() { 76 return this._fd.recvByte(); 77 }, 78 /** 79 * Writes a byte. 80 * 81 * @param {Number} b The byte to write. 82 * @returns {Boolean} true if the byte has been write. 83 */ 84 writeByte : function(b) { 85 return this._fd.sendByte(b); 86 }, 87 /** 88 * Reads bytes into the specified byte array, starting at the given offset. 89 * 90 * @param {ByteArray} barray The destination byte array. 91 * @param {Number} offset The offset at which to start storing bytes. 92 * @param {Number} length The maximum number of bytes to read. 93 * @returns {Number} The total number of bytes read. 94 */ 95 readByteArray : function(barray, offset, length) { 96 offset = offset || 0; 97 length = length || barray.size() - offset; 98 return this._fd.recv(barray, offset, length); 99 }, 100 /** 101 * Writes bytes from the specified byte array starting at the given offset. 102 * 103 * @param {ByteArray} barray The source byte array. 104 * @param {Number} offset The offset at which to start getting bytes. 105 * @param {Number} length The maximum number of bytes to write. 106 * @returns {Number} The number of bytes written. 107 */ 108 writeByteArray : function(barray, offset, length) { 109 offset = offset || 0; 110 length = length || barray.size() - offset; 111 return this._fd.send(barray, offset, length); 112 }, 113 /** 114 * Reads a sequence of bytes from this channel into the given buffer. 115 * 116 * @param {jls.lang.Buffer} buffer The buffer into which bytes are to be transferred. 117 * @returns {Number} The total number of bytes read. 118 */ 119 read : function(buffer) { 120 var count = this._fd.recv(buffer.byteArray(), buffer.offset(), buffer.remaining()); 121 //jls.logger.logBuffer(jls.lang.Logger.WARN, buffer, 'Socket.read(), count: ' + count); 122 if (count > 0) { 123 buffer.incrementPosition(count); 124 } 125 return count; 126 }, 127 /** 128 * Writes a sequence of bytes to this channel from the given buffer. 129 * 130 * @param {jls.lang.Buffer} buffer The buffer from which bytes are to be retrieved. 131 * @returns {Number} The number of bytes written. 132 */ 133 write : function(buffer) { 134 //jls.logger.logBuffer(jls.lang.Logger.WARN, buffer, 'Socket.write()'); 135 var count = this._fd.send(buffer.byteArray(), buffer.offset(), buffer.remaining()); 136 if (count > 0) { 137 buffer.incrementPosition(count); 138 } 139 return count; 140 }, 141 /** 142 * Adjusts this socket's blocking mode. 143 * 144 * @param {Boolean} block Whether the socket is placed in blocking mode. 145 * @returns {jls.net.Socket} This socket. 146 */ 147 configureBlocking : function(block) { 148 this._fd.configureBlocking(block); 149 return this; 150 }, 151 getChannel : function() { 152 return this; 153 }, 154 /* 155 * Returns the native file descriptor object that represents this socket. 156 * 157 * @returns {_native.net.Socket} the native file descriptor. 158 */ 159 getFD : function() { 160 return this._fd; 161 } 162 }); 163 164