X11 sample rewritten; now it works again
[k8lst.git] / modules / socket.st
blob8cfb292a4a4494b5308cd9af9174497385cae2cd
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.
32 Package [
33   Socket
36 Object subclass: Inet [
37 ^firstIpStringFromHostname: nm [
38   <#SocketInet 0 nm>
41 ^firstIpAddressFromHostname: nm [
42   <#SocketInet 1 nm>
45 ^newIpAddressFromIpString: s [
46   <#SocketInet 2 s>
49 ^newSocketAddressFromIpAddress: addr port: p [
50   <#SocketInet 3 addr p>
53 ^newSocketAddressFromIpString: s port: p [
54   <#SocketInet 4 s p>
57 ^newSocketAddressFromHostname: nm port: p [
58   <#SocketInet 5 nm p>
62 Object subclass: Socket [
63 | sockfd |
65 ^createTCP [
66   <#SocketSocket 11>
69 ^createUDP [
70   <#SocketSocket 12>
73 ^newWithFD: fd [
74   | obj |
75   obj := self new.
76   obj socket: (self createTCP).
77   obj fd: fd.
78   ^obj
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>.
91   self primitiveFailed
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
106 socket [
107   ^sockfd.
110 socket: fd [
111   sockfd := fd.
114 fd [
115   "return old fd"
116   <#SocketSocket 28 sockfd>.
117   self primitiveFailed
120 fd: fd [
121   self fd < 0 ifFalse: [ self basicClose ].
122   <#SocketSocket 29 sockfd fd>.
123   self primitiveFailed
126 shutdown [
127   <#SocketSocket 21 sockfd 2>.
128   self primitiveFailed
131 basicClose [
132   self isClosed ifFalse: [
133     self shutdown.
134     <#SocketSocket 20 sockfd>.
135     self primitiveFailed
136   ].
139 close [
140   self basicClose.
141   sockfd := nil.
144 isClosed [
145   sockfd ifNil: [ ^true ].
146   self fd < 0 ifTrue: [ ^true ].
147   ^false
150 connect: socketAddr [
151   <#SocketSocket 60 sockfd socketAddr>.
152   self primitiveFailed
155 connect: iporaddr port: aPort [
156   | addr |
157   addr := Inet newSocketAddressFromHostname: iporaddr port: aPort.
158   ^self connect: addr
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>.
164   self primitiveFailed
167 canRead [
168   ^(self internalSelectFor: 1 timeout: -1) = 1
171 canReadWithTimeout: milliseconds [
172   ^(self internalSelectFor: 1 timeout: milliseconds) = 1
175 canWrite [
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
187 selectFor: cond [
188   ^self selectFor: cond timeout: -1
191 basicRecv: aBuffer size: n [
192   <#SocketSocket 80 sockfd aBuffer n>.
193   self primitiveFailed
196 basicRecv: aBuffer [
197   ^self basicRecv: aBuffer size: (aBuffer size).
200 recv: n [
201   | aBuffer got |
202   aBuffer := String new: n.
203   got := self basicRecv: aBuffer size: n.
204   got > 0
205     ifTrue: [ ^aBuffer from: 1 to: got ]
206     ifFalse: [ ^nil ].
209 recvAll [
210   | buf total rc |
211   total := ''.
212   buf := String new: 2048.
213   rc := self basicRecv: buf.
214   [ rc > 0 ] whileTrue: [
215     rc printNl.
216     total := total + (buf from: 1 to: rc).
217     (self canReadWithTimeout: 2500) ifFalse: [ ^total ].
218     "'next...' printNl."
219     rc := self basicRecv: buf.
220     "'got: ' print. rc printNl."
221     rc ifNil: [ ^total ].
222   ].
223   ^total
226 basicSend: bytes size: n [
227   <#SocketSocket 70 sockfd bytes n>.
228   self primitiveFailed
231 send: aString [
232   ^self basicSend: aString size: (aString size).
235 bind: socketAddr [
236   <#SocketSocket 30 sockfd socketAddr>.
237   self primitiveFailed
240 bind: iporaddr port: aPort [
241   | addr |
242   addr := Inet newSocketAddressFromHostname: iporaddr port: aPort.
243   ^self bind: addr
246 listen [
247   <#SocketSocket 40 sockfd 128>.
248   self primitiveFailed
251 internalAccept [
252   <#SocketSocket 50 sockfd>.
253   self primitiveFailed
256 accept [
257   | ns |
258   ns := self internalAccept.
259   ns < 0 ifTrue: [ ^nil ].
260   ^Socket newWithFD: ns
265 Socket subclass: TCPSocket [
266 ^newWith: fd [
267   | obj |
268   obj := super new.
269   obj socket: fd.
270   ^obj
273 ^new [
274   ^self newWith: (super createTCP)