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 ** TcpSocket manages a TCP/IP endpoint.
11 **
12 ** Note: TcpSocket is marked as a const class to give protocol developers
13 ** the flexibility to process sockets on multiple threads. However TcpSocket
14 ** is inherently thread unsafe - therefore it is the developers responsibility
15 ** to use this API in a thread safe manner.
16 **
17 const class TcpSocket
18 {
19
20 //////////////////////////////////////////////////////////////////////////
21 // Construction
22 //////////////////////////////////////////////////////////////////////////
23
24 **
25 ** Make a new unbound, unconnected TCP socket.
26 **
27 new make() {}
28
29 //////////////////////////////////////////////////////////////////////////
30 // State
31 //////////////////////////////////////////////////////////////////////////
32
33 **
34 ** Is this socket bound to a local address and port.
35 **
36 native Bool isBound()
37
38 **
39 ** Is this socket connected to the remote host.
40 **
41 native Bool isConnected()
42
43 **
44 ** Is this socket closed.
45 **
46 native Bool isClosed()
47
48 //////////////////////////////////////////////////////////////////////////
49 // End Points
50 //////////////////////////////////////////////////////////////////////////
51
52 **
53 ** Get the bound local address or null if unbound.
54 **
55 native IpAddress localAddress()
56
57 **
58 ** Get the bound local port or null if unbound.
59 **
60 native Int localPort()
61
62 **
63 ** Get the remote address or null if not connected.
64 **
65 native IpAddress remoteAddress()
66
67 **
68 ** Get the remote port or null if not connected.
69 **
70 native Int remotePort()
71
72 //////////////////////////////////////////////////////////////////////////
73 // Communication
74 //////////////////////////////////////////////////////////////////////////
75
76 **
77 ** Bind this socket to the specified local address. If addr is null
78 ** then the default IpAddress for the local host is selected. If port
79 ** is null an ephemeral port is selected. Throw IOErr if the port is
80 ** already bound or the bind fails. Return this.
81 **
82 native TcpSocket bind(IpAddress addr, Int port)
83
84 **
85 ** Connect this socket to the specified address and port. This method
86 ** will block until the connection is made. Throw IOErr if there is a
87 ** connection error. If a non-null timeout is specified, then block no
88 ** longer then the specified timeout before raising an IOErr.
89 **
90 native TcpSocket connect(IpAddress addr, Int port, Duration timeout := null)
91
92 **
93 ** Get the input stream used to read data from the socket. The input
94 ** stream is automatically buffered according to SocketOptions.inBufferSize.
95 ** If not connected then throw IOErr.
96 **
97 native InStream in()
98
99 **
100 ** Get the output stream used to write data to the socket. The output
101 ** stream is automatically buffered according to SocketOptions.outBufferSize
102 ** If not connected then throw IOErr.
103 **
104 native OutStream out()
105
106 **
107 ** Close this socket and its associated IO streams. This method is
108 ** guaranteed to never throw an IOErr. Return true if the socket was
109 ** closed successfully or false if the socket was closed abnormally.
110 **
111 native Bool close()
112
113 //////////////////////////////////////////////////////////////////////////
114 // Socket Options
115 //////////////////////////////////////////////////////////////////////////
116
117 **
118 ** Access the SocketOptions used to tune this socket. The
119 ** following options apply to TcpSockets:
120 ** - inBufferSize
121 ** - outBufferSize
122 ** - keepAlive
123 ** - receiveBufferSize
124 ** - sendBufferSize
125 ** - reuseAddress
126 ** - linger
127 ** - receiveTimeout
128 ** - noDelay
129 ** - trafficClass
130 ** Accessing other option fields will throw UnsupportedErr.
131 **
132 native SocketOptions options()
133
134 internal native Int getInBufferSize()
135 internal native Void setInBufferSize(Int v)
136
137 internal native Int getOutBufferSize()
138 internal native Void setOutBufferSize(Int v)
139
140 internal native Bool getKeepAlive()
141 internal native Void setKeepAlive(Bool v)
142
143 internal native Int getReceiveBufferSize()
144 internal native Void setReceiveBufferSize(Int v)
145
146 internal native Int getSendBufferSize()
147 internal native Void setSendBufferSize(Int v)
148
149 internal native Bool getReuseAddress()
150 internal native Void setReuseAddress(Bool v)
151
152 internal native Duration getLinger()
153 internal native Void setLinger(Duration v)
154
155 internal native Duration getReceiveTimeout()
156 internal native Void setReceiveTimeout(Duration v)
157
158 internal native Bool getNoDelay()
159 internal native Void setNoDelay(Bool v)
160
161 internal native Int getTrafficClass()
162 internal native Void setTrafficClass(Int v)
163
164 }