1 jls.loader.provide('jls.net.ServerSocket'); 2 3 jls.loader.requireLibrary('jls_net'); 4 jls.loader.require('jls.net.Socket'); 5 jls.loader.require('jls.net.InetAddress'); 6 7 jls.net.ServerSocket = jls.lang.Class.create( /** @lends jls.net.ServerSocket.prototype */ 8 { 9 /** 10 * Creates a TCP server socket. 11 * 12 * @param {Number} [port] The port number to bind to. 13 * @param {Number} [backlog] The maximum length of the queue. 14 * @param {jls.net.InetAddress} [bindAddr] The host the server will bind to. 15 * @constructs 16 * @class This class represents a server TCP socket. 17 */ 18 initialize : function(port, backlog, bindAddr) { 19 this._fd = new _native.net.Socket(); 20 if (port) { 21 this.bind(port, backlog, bindAddr); 22 } 23 }, 24 /** 25 * Binds the ServerSocket to a specific address (IP address and port number). 26 * 27 * @param {Number} port The port number to bind to. 28 * @param {Number} [backlog] The maximum length of the queue. 29 * @param {jls.net.InetAddress} [bindAddr] The host the server will bind to. 30 */ 31 bind : function(port, backlog, bindAddr) { 32 this._backlog = backlog || 32; 33 if (bindAddr && (bindAddr instanceof jls.net.InetAddress)) { 34 this._fd.bind(port, bindAddr.getHostAddress()); 35 } else { 36 this._fd.bind(port); 37 } 38 this._fd.listen(this._backlog); 39 }, 40 /** 41 * Closes this server socket. 42 * 43 * @returns {jls.net.ServerSocket} This server socket. 44 */ 45 close : function() { 46 this._fd.close(); 47 return this; 48 }, 49 /** 50 * Listens for a connection to be made to this socket and accepts it. 51 * 52 * @returns {jls.net.Socket} The new socket. 53 */ 54 accept : function() { 55 return new jls.net.Socket(this._fd.accept()); 56 }, 57 /** 58 * Adjusts this server socket's blocking mode. 59 * 60 * @param {Boolean} block Whether the socket is placed in blocking mode. 61 * @returns {jls.net.ServerSocket} This server socket. 62 */ 63 configureBlocking : function(block) { 64 this._fd.configureBlocking(block); 65 return this; 66 }, 67 getChannel : function() { 68 return this; 69 }, 70 getFD : function() { 71 return this._fd; 72 } 73 }); 74 75