
1 // 2 // Copyright (c) 2007, Brian Frank and Andy Frank 3 // Licensed under the Academic Free License version 3.0 4 // 5 // History: 6 // 10 Feb 07 Brian Frank Creation 7 // 8 9 ** 10 ** TcpListener is a server socket that listens to a local well 11 ** known port for incoming TcpSockets. 12 ** 13 class TcpListener 14 { 15 16 ////////////////////////////////////////////////////////////////////////// 17 // Construction 18 ////////////////////////////////////////////////////////////////////////// 19 20 ** 21 ** Make a new unbound TCP server socket. 22 ** 23 new make() {} 24 25 ////////////////////////////////////////////////////////////////////////// 26 // State 27 ////////////////////////////////////////////////////////////////////////// 28 29 ** 30 ** Is this socket bound to a local address and port. 31 ** 32 native Bool isBound() 33 34 ** 35 ** Is this socket closed. 36 ** 37 native Bool isClosed() 38 39 ////////////////////////////////////////////////////////////////////////// 40 // End Points 41 ////////////////////////////////////////////////////////////////////////// 42 43 ** 44 ** Get the bound local address or null if unbound. 45 ** 46 native IpAddress localAddress() 47 48 ** 49 ** Get the bound local port or null if unbound. 50 ** 51 native Int localPort() 52 53 ////////////////////////////////////////////////////////////////////////// 54 // Communication 55 ////////////////////////////////////////////////////////////////////////// 56 57 ** 58 ** Bind this listener to the specified local address. If addr is null 59 ** then the default IpAddress for the local host is selected. If port 60 ** is null an ephemeral port is selected. Throw IOErr if the port is 61 ** already bound or the bind fails. Return this. 62 ** 63 native TcpListener bind(IpAddress addr, Int port, Int backlog := 50) 64 65 ** 66 ** Accept the next incoming connection. This method blocks the 67 ** calling thread until a new connection is established. If this 68 ** listener's receiveTimeout option is configured, then accept 69 ** will timeout with an IOErr. 70 ** 71 TcpSocket accept() { return doAccept } 72 private native TcpSocket doAccept() 73 74 ** 75 ** Close this server socket. This method is guaranteed to never 76 ** throw an IOErr. Return true if the socket was closed successfully 77 ** or false if the socket was closed abnormally. 78 ** 79 native Bool close() 80 81 ////////////////////////////////////////////////////////////////////////// 82 // Socket Options 83 ////////////////////////////////////////////////////////////////////////// 84 85 ** 86 ** Access the SocketOptions used to tune this server socket. 87 ** The following options apply to TcpListeners: 88 ** - receiveBufferSize 89 ** - reuseAddress 90 ** - receiveTimeout 91 ** Accessing other option fields will throw UnsupportedErr. 92 ** 93 SocketOptions options() 94 { 95 return SocketOptions.make(this) 96 } 97 98 internal native Int getReceiveBufferSize() 99 internal native Void setReceiveBufferSize(Int v) 100 101 internal native Bool getReuseAddress() 102 internal native Void setReuseAddress(Bool v) 103 104 internal native Duration getReceiveTimeout() 105 internal native Void setReceiveTimeout(Duration v) 106 107 }