2 Little Smalltalk, Version 5
4 Copyright (C) 1987-2005 by Timothy A. Budd
5 Copyright (C) 2007 by Charles R. Childers
6 Copyright (C) 2005-2007 by Danny Reinhold
7 Copyright (C) 2010 by Ketmar // Vampire Avalon
9 ============================================================================
10 This license applies to the virtual machine and to the initial image of
11 the Little Smalltalk system and to all files in the Little Smalltalk
12 packages except the files explicitly licensed with another license(s).
13 ============================================================================
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the 'Software'), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
21 The above copyright notice and this permission notice shall be included in
22 all copies or substantial portions of the Software.
24 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 DEALINGS IN THE SOFTWARE.
36 Object subclass: Inet [
37 ^firstIpStringFromHostname: nm [
41 ^firstIpAddressFromHostname: nm [
45 ^newIpAddressFromIpString: s [
49 ^newSocketAddressFromIpAddress: addr port: p [
50 <#SocketInet 3 addr p>
53 ^newSocketAddressFromIpString: s port: p [
57 ^newSocketAddressFromHostname: nm port: p [
62 Object subclass: Socket [
76 obj socket: (self createTCP).
82 /* args: rArray wArray eArray [timeoutMs] */
83 /* returns: patched arrays; for each item: boolean */
84 /* return nil on error */
85 /* return false if there is timeout */
86 /* return true if anything happens */
89 ^selectRead: rArray write: wArray exception: eArray timeout: ms [
90 <#SocketSocket 95 rArray wArray eArray ms>.
94 ^selectRead: rArray write: wArray timeout: ms [
95 ^self selectRead: rArray write: wArray exception: nil timeout: ms
98 ^selectRead: rArray timeout: ms [
99 ^self selectRead: rArray write: nil exception: nil timeout: ms
102 ^selectWrite: wArray timeout: ms [
103 ^self selectRead: nil write: wArray exception: nil timeout: ms
116 <#SocketSocket 28 sockfd>.
121 self fd < 0 ifFalse: [ self basicClose ].
122 <#SocketSocket 29 sockfd fd>.
127 <#SocketSocket 21 sockfd 2>.
132 self isClosed ifFalse: [
134 <#SocketSocket 20 sockfd>.
145 sockfd ifNil: [ ^true ].
146 self fd < 0 ifTrue: [ ^true ].
150 connect: socketAddr [
151 <#SocketSocket 60 sockfd socketAddr>.
155 connect: iporaddr port: aPort [
157 addr := Inet newSocketAddressFromHostname: iporaddr port: aPort.
161 internalSelectFor: cond timeout: ms [
162 "return number (0: timeout; 1: can read; 2: can write; 3: both; <0: error)"
163 <#SocketSocket 92 sockfd cond ms>.
168 ^(self internalSelectFor: 1 timeout: -1) = 1
171 canReadWithTimeout: milliseconds [
172 ^(self internalSelectFor: 1 timeout: milliseconds) = 1
176 ^(self internalSelectFor: 2 timeout: -1) = 2
179 canWriteWithTimeout: milliseconds [
180 ^(self internalSelectFor: 2 timeout: milliseconds) = 1
183 selectFor: cond timeout: msec [
184 ^self internalSelectFor: cond timeout: msec
188 ^self selectFor: cond timeout: -1
191 basicRecv: aBuffer size: n [
192 <#SocketSocket 80 sockfd aBuffer n>.
197 ^self basicRecv: aBuffer size: (aBuffer size).
202 aBuffer := String new: n.
203 got := self basicRecv: aBuffer size: n.
205 ifTrue: [ ^aBuffer from: 1 to: got ]
212 buf := String new: 2048.
213 rc := self basicRecv: buf.
214 [ rc > 0 ] whileTrue: [
216 total := total + (buf from: 1 to: rc).
217 (self canReadWithTimeout: 2500) ifFalse: [ ^total ].
219 rc := self basicRecv: buf.
220 "'got: ' print. rc printNl."
221 rc ifNil: [ ^total ].
226 basicSend: bytes size: n [
227 <#SocketSocket 70 sockfd bytes n>.
232 ^self basicSend: aString size: (aString size).
236 <#SocketSocket 30 sockfd socketAddr>.
240 bind: iporaddr port: aPort [
242 addr := Inet newSocketAddressFromHostname: iporaddr port: aPort.
247 <#SocketSocket 40 sockfd 128>.
252 <#SocketSocket 50 sockfd>.
258 ns := self internalAccept.
259 ns < 0 ifTrue: [ ^nil ].
260 ^Socket newWithFD: ns
265 Socket subclass: TCPSocket [
274 ^self newWith: (super createTCP)