
1 // 2 // Copyright (c) 2007, Brian Frank and Andy Frank 3 // Licensed under the Academic Free License version 3.0 4 // 5 // History: 6 // 9 Feb 07 Brian Frank Creation 7 // 8 9 ** 10 ** SocketOptions groups together all the socket options used to tune a 11 ** TcpSocket, TcpListener, or UdpSocket. See the options method of each 12 ** of those classes for which options apply. Accessing an unsupported 13 ** option for a particular socket type will throw UnsupportedErr. 14 ** 15 final class SocketOptions 16 { 17 18 ////////////////////////////////////////////////////////////////////////// 19 // Construction 20 ////////////////////////////////////////////////////////////////////////// 21 22 ** 23 ** Attach this options instance to the specific socket (we just 24 ** use an Obj because everything is dynamically typed) 25 ** 26 internal new make(Obj socket) 27 { 28 this.socket = socket; 29 } 30 31 ////////////////////////////////////////////////////////////////////////// 32 // Streaming Options 33 ////////////////////////////////////////////////////////////////////////// 34 35 ** 36 ** The size in bytes for the sys::InStream buffer. A value of 0 or 37 ** null disables input stream buffing. This field may only be set before 38 ** the socket is connected otherwise Err is thrown. 39 ** 40 Int inBufferSize 41 { 42 get { return (Int)wrap |->Obj| { return socket->getInBufferSize } } 43 set { wrap |,| { socket->setInBufferSize(val) } } 44 } 45 46 ** 47 ** The size in bytes for the sys::OutStream buffer. A value of 0 or 48 ** null disables output stream buffing. This field may only be set before 49 ** the socket is connected otherwise Err is thrown. 50 ** 51 Int outBufferSize 52 { 53 get { return (Int)wrap |->Obj| { return socket->getOutBufferSize } } 54 set { wrap |,| { socket->setOutBufferSize(val) } } 55 } 56 57 ////////////////////////////////////////////////////////////////////////// 58 // Socket Options 59 ////////////////////////////////////////////////////////////////////////// 60 61 ** 62 ** SO_BROADCAST socket option. 63 ** 64 Bool broadcast 65 { 66 get { return (Bool)wrap |->Obj| { return socket->getBroadcast } } 67 set { wrap |,| { socket->setBroadcast(val) } } 68 } 69 70 ** 71 ** SO_KEEPALIVE socket option. 72 ** 73 Bool keepAlive 74 { 75 get { return (Bool)wrap |->Obj| { return socket->getKeepAlive } } 76 set { wrap |,| { socket->setKeepAlive(val) } } 77 } 78 79 ** 80 ** SO_RCVBUF option for the size in bytes of the IP stack buffers. 81 ** 82 Int receiveBufferSize 83 { 84 get { return (Int)wrap |->Obj| { return socket->getReceiveBufferSize } } 85 set { wrap |,| { socket->setReceiveBufferSize(val) } } 86 } 87 88 ** 89 ** SO_SNDBUF option for the size in bytes of the IP stack buffers. 90 ** 91 Int sendBufferSize 92 { 93 get { return (Int)wrap |->Obj| { return socket->getSendBufferSize } } 94 set { wrap |,| { socket->setSendBufferSize(val) } } 95 } 96 97 ** 98 ** SO_REUSEADDR socket option is used to control the time 99 ** wait state of a closed socket. 100 ** 101 Bool reuseAddress 102 { 103 get { return (Bool)wrap |->Obj| { return socket->getReuseAddress } } 104 set { wrap |,| { socket->setReuseAddress(val) } } 105 } 106 107 ** 108 ** SO_LINGER socket option controls the linger time or set 109 ** to null to disable linger. 110 ** 111 Duration linger 112 { 113 get { return (Duration)wrap |->Obj| { return socket->getLinger} } 114 set { wrap |,| { socket->setLinger(val) } } 115 } 116 117 ** 118 ** SO_TIMEOUT socket option controls the amount of time this socket 119 ** will block on a read call before throwing an IOErr timeout exception. 120 ** Null is used to indicate an infinite timeout. 121 ** 122 Duration receiveTimeout 123 { 124 get { return (Duration)wrap |->Obj| { return socket->getReceiveTimeout} } 125 set { wrap |,| { socket->setReceiveTimeout(val) } } 126 } 127 128 ** 129 ** TCP_NODELAY socket option specifies that send not be delayed 130 ** to merge packets (Nagle's algorthm). 131 ** 132 Bool noDelay 133 { 134 get { return (Bool)wrap |->Obj| { return socket->getNoDelay } } 135 set { wrap |,| { socket->setNoDelay(val) } } 136 } 137 138 ** 139 ** The type-of-class byte in the IP packet header. 140 ** 141 ** For IPv4 this value is detailed in RFC 1349 as the following bitset: 142 ** - IPTOS_LOWCOST (0x02) 143 ** - IPTOS_RELIABILITY (0x04) 144 ** - IPTOS_THROUGHPUT (0x08) 145 ** - IPTOS_LOWDELAY (0x10) 146 ** 147 ** For IPv6 this is the value placed into the sin6_flowinfo header field. 148 ** 149 Int trafficClass 150 { 151 get { return (Int)wrap |->Obj| { return socket->getTrafficClass } } 152 set { wrap |,| { socket->setTrafficClass(val) } } 153 } 154 155 ////////////////////////////////////////////////////////////////////////// 156 // Wrap 157 ////////////////////////////////////////////////////////////////////////// 158 159 internal Obj wrap(|->Obj| m) 160 { 161 try 162 { 163 return m() 164 } 165 catch (UnknownSlotErr e) 166 { 167 throw UnsupportedErr.make("Option not supported for $socket.type") 168 } 169 } 170 171 ////////////////////////////////////////////////////////////////////////// 172 // Fields 173 ////////////////////////////////////////////////////////////////////////// 174 175 private Obj socket 176 177 }