author | Claus Gittinger <cg@exept.de> |
Tue, 09 Nov 2004 18:30:22 +0100 | |
changeset 1486 | 61ee314126a5 |
parent 1485 | d8578cf3504b |
child 1499 | 1ff98f9bc58f |
permissions | -rw-r--r-- |
0 | 1 |
" |
4 | 2 |
COPYRIGHT (c) 1992 by Claus Gittinger |
42 | 3 |
All Rights Reserved |
0 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
888 | 13 |
"{ Package: 'stx:libbasic2' }" |
14 |
||
63 | 15 |
NonPositionableExternalStream subclass:#Socket |
1186 | 16 |
instanceVariableNames:'domain socketType protocol port serviceName peer peerName' |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
17 |
classVariableNames:'' |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
18 |
poolDictionaries:'' |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
19 |
category:'Streams-External' |
0 | 20 |
! |
21 |
||
126 | 22 |
!Socket primitiveDefinitions! |
23 |
%{ |
|
444 | 24 |
|
1009 | 25 |
/* #define DGRAM_DEBUG /* */ |
26 |
||
1343 | 27 |
#include "stxOSDefs.h" |
126 | 28 |
|
29 |
#include <stdio.h> |
|
30 |
#include <errno.h> |
|
31 |
||
1343 | 32 |
#ifdef WANT__TCP_DOT_H |
33 |
# include <netinet/tcp.h> |
|
34 |
#endif |
|
35 |
||
1344 | 36 |
#ifdef WANT__NETDB_DOT_H |
37 |
# include <netdb.h> |
|
1343 | 38 |
#endif |
39 |
||
739
eb3878300c3b
removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents:
726
diff
changeset
|
40 |
#if defined(TRY_AGAIN) || defined(HOST_NOT_FOUND) |
eb3878300c3b
removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents:
726
diff
changeset
|
41 |
# define USE_H_ERRNO |
eb3878300c3b
removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents:
726
diff
changeset
|
42 |
#endif |
eb3878300c3b
removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents:
726
diff
changeset
|
43 |
|
126 | 44 |
/* |
45 |
* on some systems errno is a macro ... check for it here |
|
46 |
*/ |
|
47 |
#ifndef errno |
|
48 |
extern errno; |
|
49 |
#endif |
|
50 |
||
648
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
51 |
#ifdef USE_H_ERRNO |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
52 |
# ifndef h_errno |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
53 |
extern h_errno; |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
54 |
# endif |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
55 |
#endif |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
56 |
|
126 | 57 |
#ifdef DEBUG |
58 |
# define DBGPRINTF(x) { if (__debugging__) printf x; } |
|
821
40b0b69bf4b5
more debugs; fixed at-addresses
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
59 |
# define DBGFPRINTF(x) { if (__debugging__) fprintf x; } |
126 | 60 |
#else |
61 |
# define DBGPRINTF(x) /* as nothing */ |
|
821
40b0b69bf4b5
more debugs; fixed at-addresses
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
62 |
# define DBGFPRINTF(x) /* as nothing */ |
126 | 63 |
#endif |
64 |
||
65 |
%} |
|
66 |
! ! |
|
67 |
||
511 | 68 |
!Socket primitiveVariables! |
69 |
%{ |
|
70 |
static int __debugging__ = 0; |
|
71 |
%} |
|
72 |
! ! |
|
73 |
||
464 | 74 |
!Socket class methodsFor:'documentation'! |
25 | 75 |
|
31 | 76 |
copyright |
77 |
" |
|
78 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
42 | 79 |
All Rights Reserved |
31 | 80 |
|
81 |
This software is furnished under a license and may be used |
|
82 |
only in accordance with the terms of that license and with the |
|
83 |
inclusion of the above copyright notice. This software may not |
|
84 |
be provided or otherwise made available to, or used by, any |
|
85 |
other person. No title to or ownership of the software is |
|
86 |
hereby transferred. |
|
87 |
" |
|
88 |
! |
|
89 |
||
25 | 90 |
documentation |
91 |
" |
|
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
92 |
This class provides access to sockets for interprocess communication. |
25 | 93 |
The message protocol is preliminary, until someone tells me how |
31 | 94 |
other smalltalk's socket interfaces look like. |
25 | 95 |
|
525 | 96 |
Currently, only IP and UNIX domain sockets are supported. |
97 |
Code for appletalk is included, but was never tested ... |
|
98 |
More may be added in the future. |
|
99 |
(the code is prepared for things like SNA or decNet; |
|
100 |
however, right now, this code is empty and needs a little work. |
|
101 |
Implementing those is pretty straight forward, once the address |
|
102 |
data structures are known.) |
|
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
103 |
|
28 | 104 |
Due to historic reasons (I started this class, before I got hold of some |
105 |
code using ST-80 Sockets i.e. RemoteInvocation), there is some old interface |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
106 |
still provided. |
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
107 |
This will vanish; use the #family:type: or #newTCPxxx and #newUDPxxx interfaces, |
71 | 108 |
together with the bind/listen and accept calls, |
109 |
which are meant to be compatible to ST-80's UnixSocketAccessor interface. |
|
110 |
||
869 | 111 |
ST/X does not yet use IPSocketAddress, UDSocketAddress etc; all addressing |
71 | 112 |
is done by passing appropriate string- or byteArray objects containing |
113 |
the addresses. This may change, too. |
|
114 |
||
115 |
TODO: cleanup historic leftovers, implement other than inet domain stuff. |
|
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
116 |
(mhmh - how can I test those ?) |
532 | 117 |
change to raise more signals on errors. |
529 | 118 |
Help - could someone who has a linux machine connected to an appletalk |
533
ced9950b551f
added query about which protocols are supported
Claus Gittinger <cg@exept.de>
parents:
532
diff
changeset
|
119 |
net please test this (and send bug fixes ...) |
259 | 120 |
|
121 |
[author:] |
|
425 | 122 |
Claus Gittinger |
48 | 123 |
" |
124 |
! |
|
28 | 125 |
|
48 | 126 |
examples |
127 |
" |
|
28 | 128 |
example (get help info from an nntp server): |
1343 | 129 |
[exBegin] |
130 |
|sock host| |
|
131 |
||
132 |
host := OperatingSystem getEnvironment:'NNTPSERVER'. |
|
133 |
||
134 |
sock := Socket newTCPclientToHost:host port:'nntp'. |
|
135 |
Transcript showCR:sock nextLine. |
|
136 |
sock buffered:false. |
|
137 |
||
138 |
sock nextPutAll:'HELP'; cr. |
|
139 |
[:exit | |
|
140 |
|line| |
|
141 |
||
142 |
line := sock nextLine. |
|
143 |
line = '.' ifTrue:[exit value:nil]. |
|
144 |
Transcript showCR:line. |
|
145 |
] loopWithExit. |
|
146 |
sock close |
|
147 |
[exEnd] |
|
209 | 148 |
|
28 | 149 |
|
209 | 150 |
example (connect to finger daemon, get users entry): |
1343 | 151 |
[exBegin] |
152 |
|sock host entry| |
|
153 |
||
154 |
host := OperatingSystem getHostName. |
|
155 |
||
156 |
sock := Socket newTCPclientToHost:host port:'finger'. |
|
157 |
sock isNil ifTrue:[ |
|
158 |
self warn:'no finger daemon is running'. |
|
159 |
^ self |
|
160 |
]. |
|
161 |
sock useCRLF:true. |
|
162 |
sock buffered:false. |
|
163 |
sock isNil ifTrue:[ |
|
164 |
Transcript showCR:'cannot connect to local finger daemon' |
|
165 |
] ifFalse:[ |
|
166 |
sock nextPutAll:(OperatingSystem getLoginName). |
|
167 |
sock cr. |
|
168 |
||
169 |
entry := sock nextLine. |
|
170 |
Transcript showCR:entry. |
|
171 |
||
172 |
sock close |
|
173 |
] |
|
174 |
[exEnd] |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
175 |
|
28 | 176 |
example (connect to an ftp server): |
1343 | 177 |
[exBegin] |
178 |
|sock host| |
|
179 |
||
180 |
host := OperatingSystem getHostName. |
|
181 |
sock := Socket newTCPclientToHost:host port:'ftp'. |
|
182 |
||
183 |
sock buffered:false. |
|
184 |
Transcript showCR:sock nextLine. |
|
185 |
sock nextPutAll:('USER ' , 'anonymous'); cr. |
|
186 |
Transcript showCR:sock nextLine. |
|
187 |
sock nextPutAll:('PASS ' , 'fooBar'); cr. |
|
188 |
Transcript showCR:sock nextLine. |
|
189 |
sock nextPutAll:'LIST'; cr. |
|
190 |
Transcript showCR:sock nextLine. |
|
191 |
sock close. |
|
192 |
||
193 |
'dont know enough of the ftp protocol to continue here ...' |
|
194 |
[exEnd] |
|
28 | 195 |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
196 |
|
93 | 197 |
example (connect to an snmp server [UDP]): |
1343 | 198 |
[exBegin] |
199 |
|sock port| |
|
200 |
||
201 |
sock := Socket newUDP. |
|
202 |
port := Socket portOfService:'snmp'. |
|
203 |
sock connectTo:(OperatingSystem getHostName) port:port. |
|
204 |
sock buffered:false. |
|
205 |
Transcript showCR:'got it'. |
|
206 |
sock close. |
|
207 |
[exEnd] |
|
42 | 208 |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
209 |
|
42 | 210 |
example (await connection from a client and read some data): |
1343 | 211 |
[exBegin] |
212 |
|connectSock sock| |
|
213 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
214 |
connectSock := Socket newTCPserverAtPort:9998. |
1343 | 215 |
connectSock isNil ifTrue:[ |
216 |
Transcript showCR:'socket setup failed.'. |
|
217 |
] ifFalse:[ |
|
218 |
Transcript showCR:'listen ..'. |
|
219 |
(connectSock listenFor:5) ifFalse:[ |
|
220 |
Transcript showCR:'listen failed.'. |
|
221 |
] ifTrue:[ |
|
222 |
Transcript showCR:'wait'. |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
223 |
connectSock readWait. |
1343 | 224 |
Transcript showCR:'accept'. |
225 |
sock := connectSock accept. |
|
226 |
sock isNil ifTrue:[ |
|
227 |
Transcript showCR:'accept failed.'. |
|
228 |
] ifFalse:[ |
|
229 |
sock buffered:false. |
|
230 |
Transcript showCR:'server: got it'. |
|
231 |
'can now do transfer via sock'. |
|
232 |
Transcript showCR:'read'. |
|
233 |
Transcript showCR:('got: ' , sock nextLine). |
|
234 |
||
235 |
Transcript showCR:'close'. |
|
236 |
sock close |
|
237 |
]. |
|
238 |
connectSock close. |
|
239 |
] |
|
240 |
] |
|
241 |
[exEnd] |
|
42 | 242 |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
243 |
|
42 | 244 |
example (connect to above server and send some data): |
1343 | 245 |
[exBegin] |
246 |
|sock| |
|
247 |
||
248 |
sock := Socket newTCPclientToHost:(OperatingSystem getHostName) port:9998. |
|
249 |
sock isNil ifTrue:[ |
|
250 |
Transcript showCR:'nope' |
|
251 |
] ifFalse:[ |
|
252 |
sock buffered:false. |
|
253 |
Transcript showCR:'client: got it'. |
|
254 |
'can now do transfer via sock'. |
|
255 |
Transcript showCR:'sending <hello>'. |
|
256 |
sock nextPutLine:'hello'. |
|
257 |
sock close |
|
258 |
] |
|
259 |
[exEnd] |
|
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
260 |
|
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
261 |
example: UNIX domain socket (await connection from a client and read some data): |
1009 | 262 |
|
1343 | 263 |
|connectSock sock| |
264 |
||
265 |
'/tmp/ud_socket' asFilename remove. |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
266 |
connectSock := Socket newUNIXserverAt:'/tmp/ud_socket'. |
1343 | 267 |
connectSock isNil ifTrue:[ |
268 |
Transcript showCR:'socket setup failed.'. |
|
269 |
] ifFalse:[ |
|
270 |
Transcript showCR:'listen ..'. |
|
271 |
(connectSock listenFor:5) ifFalse:[ |
|
272 |
Transcript showCR:'listen failed.'. |
|
273 |
] ifTrue:[ |
|
274 |
Transcript showCR:'wait'. |
|
275 |
connectSock buffered:false. |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
276 |
connectSock readWait. |
1343 | 277 |
Transcript showCR:'accept'. |
278 |
sock := connectSock accept. |
|
279 |
sock isNil ifTrue:[ |
|
280 |
Transcript showCR:'accept failed.'. |
|
281 |
] ifFalse:[ |
|
282 |
sock buffered:false. |
|
283 |
Transcript showCR:'server: got it'. |
|
284 |
'can now do transfer via sock'. |
|
285 |
Transcript showCR:'read'. |
|
286 |
Transcript showCR:('got: ' , sock nextLine). |
|
287 |
||
288 |
Transcript showCR:'close'. |
|
289 |
sock close |
|
290 |
]. |
|
291 |
connectSock close. |
|
292 |
] |
|
293 |
] |
|
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
294 |
|
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
295 |
|
118 | 296 |
example (connect to above server and send some data; |
1343 | 297 |
Notice, this fails, if above server code is executed in the same ST/X image |
298 |
(at least on LINUX), since the OS does not correctly handle |
|
299 |
a connect from within an interrupted accept system call |
|
300 |
On SGI's SVR4, this works ok |
|
301 |
[exBegin] |
|
302 |
|sock| |
|
303 |
||
304 |
sock := Socket newUNIXclientTo:'/tmp/ud_socket'. |
|
305 |
sock isNil ifTrue:[ |
|
306 |
Transcript showCR:'nope' |
|
307 |
] ifFalse:[ |
|
308 |
sock buffered:false. |
|
309 |
Transcript showCR:'client: got it'. |
|
310 |
'can now do transfer via sock'. |
|
311 |
Transcript showCR:'sending <hello>'. |
|
312 |
sock nextPutLine:'hello'. |
|
313 |
sock close |
|
314 |
] |
|
315 |
[exEnd] |
|
1007 | 316 |
|
317 |
||
318 |
example (UDP await packet from a client and read some data): |
|
1343 | 319 |
[exBegin] |
320 |
|udpSock sock addr n dataBuffer| |
|
321 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
322 |
udpSock := Socket newUDPserverAtPort:9999. |
1343 | 323 |
udpSock isNil ifTrue:[ |
324 |
Transcript showCR:'socket setup failed.'. |
|
325 |
] ifFalse:[ |
|
326 |
Transcript showCR:'wait'. |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
327 |
udpSock readWait. |
1343 | 328 |
|
329 |
addr := IPSocketAddress new. |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
330 |
dataBuffer := ByteArray new:1000. |
1343 | 331 |
n := udpSock receiveFrom:addr buffer:dataBuffer start:1 for:dataBuffer size. |
332 |
n > 0 ifTrue:[ |
|
333 |
Transcript showCR:('got: ' , n printString , 'bytes from ' , addr printString). |
|
334 |
Transcript showCR:('data: ' , (dataBuffer copyTo:n) printString). |
|
335 |
] ifFalse:[ |
|
336 |
Transcript showCR:'read failed'. |
|
337 |
]. |
|
338 |
||
339 |
Transcript showCR:'close'. |
|
340 |
udpSock close |
|
341 |
] |
|
342 |
[exEnd] |
|
1007 | 343 |
example (connect to above UDP server and send some data; |
1343 | 344 |
[exBegin] |
345 |
|sock| |
|
346 |
||
347 |
sock := Socket newUDP. |
|
348 |
sock isNil ifTrue:[ |
|
349 |
Transcript showCR:'nope' |
|
350 |
] ifFalse:[ |
|
351 |
sock sendTo:(IPSocketAddress new hostName:'localhost' port:9999) buffer:'hello world'. |
|
352 |
sock close |
|
353 |
] |
|
354 |
[exEnd] |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
355 |
|
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
356 |
example: pingWalk (try to ping hosts on the local network) |
1343 | 357 |
[exBegin] |
358 |
|myName myAddress list top hosts walkProcess port| |
|
359 |
||
360 |
myName := OperatingSystem getHostName. |
|
361 |
myAddress := Socket ipAddressOfHost:myName. |
|
362 |
||
363 |
port := Socket portOfService:'echo'. |
|
364 |
port isNil ifTrue:[ |
|
365 |
self error:'dont know echo port'. |
|
366 |
^ self |
|
367 |
]. |
|
368 |
||
369 |
top := StandardSystemView new. |
|
370 |
top label:'PING net walk'. |
|
371 |
||
372 |
list := ScrollableView for:ListView in:top. |
|
373 |
list origin:0.0@0.0 corner:1.0@1.0. |
|
374 |
||
375 |
top openAndWait. |
|
376 |
||
377 |
walkProcess := [ |
|
378 |
|l low hi direction tryHostID dottedName hostName conn addr| |
|
379 |
||
380 |
l := SortedCollection new. |
|
381 |
||
382 |
' only works with type C-net |
|
383 |
the code below could simply do 1 to:254 do:[:hostID } |
|
384 |
but, to probe likely hosts earlier, the probing is done |
|
385 |
ping-pong like around my ip-address (assuming, that other machines |
|
386 |
have numbers around my own)'. |
|
387 |
||
388 |
low := hi := (myAddress at:4). |
|
389 |
direction := 1. |
|
390 |
||
391 |
[low > 0 or:[hi < 255]] whileTrue:[ |
|
392 |
direction > 0 ifTrue:[ |
|
393 |
hi := hi + 1. |
|
394 |
tryHostID := hi. |
|
395 |
direction := -1. |
|
396 |
] ifFalse:[ |
|
397 |
low := low - 1. |
|
398 |
tryHostID := low. |
|
399 |
direction := 1. |
|
400 |
]. |
|
401 |
(tryHostID between:1 and:254) ifTrue:[ |
|
402 |
dottedName := (myAddress at:1) printString |
|
403 |
, '.' , (myAddress at:2) printString |
|
404 |
, '.' , (myAddress at:3) printString |
|
405 |
, '.' , tryHostID printString. |
|
406 |
||
407 |
top label:'PING net walk - trying ' , dottedName. |
|
408 |
||
409 |
top windowGroup withCursor:Cursor wait do:[ |
|
410 |
conn := Socket newTCPclientToHost:dottedName port:port withTimeout:1000. |
|
411 |
conn notNil ifTrue:[ |
|
412 |
addr := Socket ipAddressOfHost:dottedName. |
|
413 |
hostName := Socket hostWithIpAddress:addr. |
|
414 |
hostName isNil ifTrue:[ |
|
415 |
hostName :='?' |
|
416 |
]. |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
417 |
l add:(dottedName paddedTo:15 with:Character space) |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
418 |
, ' ' |
1343 | 419 |
, (hostName paddedTo:15 with:Character space) |
420 |
, ' up & reachable'. |
|
421 |
list list:l. |
|
422 |
conn close. |
|
423 |
] |
|
424 |
]. |
|
425 |
]. |
|
426 |
]. |
|
427 |
top label:'PING reachable hosts'. |
|
428 |
] forkAt:(Processor userBackgroundPriority). |
|
429 |
walkProcess name:'ping net walker'. |
|
430 |
[exEnd] |
|
431 |
||
432 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
433 |
This example creates a simple UDP server that accepts |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
434 |
single packets from anybody and broadcasts them to all |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
435 |
clients that have connected so far. |
1343 | 436 |
|
437 |
[exBegin] |
|
438 |
| socket address buffer msgSize clients | |
|
439 |
clients := Set new. |
|
440 |
address := IPSocketAddress new. |
|
441 |
buffer := String new: 1024. |
|
442 |
||
443 |
socket := self newUDPserverAtPort: 6666. |
|
444 |
||
445 |
Transcript showCR: 'server starting'. |
|
446 |
||
447 |
[ |
|
448 |
[true] whileTrue: [ |
|
449 |
(socket readWaitWithTimeoutMs: 200) ifFalse: [ |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
450 |
msgSize := socket |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
451 |
receiveFrom: address |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
452 |
buffer: buffer |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
453 |
start: 1 |
1343 | 454 |
for: buffer size. |
455 |
||
456 |
clients add: address copy. |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
457 |
clients do: [ :clientAddress | |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
458 |
socket |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
459 |
sendTo: clientAddress |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
460 |
buffer: buffer |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
461 |
start: 1 |
1343 | 462 |
for: msgSize]] |
463 |
] |
|
464 |
] valueNowOrOnUnwindDo:[ |
|
465 |
Transcript showCR: 'server shutting down'. |
|
466 |
socket close |
|
467 |
] |
|
468 |
[exEnd] |
|
817 | 469 |
|
470 |
send a datagram to above server: |
|
1343 | 471 |
[exBegin] |
472 |
||
473 |
| socket address buffer host msg | |
|
474 |
||
475 |
host := Dialog |
|
476 |
request: 'What is the name of the server''s host?' |
|
477 |
initialAnswer: 'localhost'. |
|
478 |
||
479 |
socket := self newUDP. |
|
480 |
||
481 |
address := IPSocketAddress hostName: host port: 6666. |
|
482 |
||
483 |
buffer := ByteArray new: 1000. |
|
484 |
[ |
|
485 |
[(msg := Dialog request: 'Say something') isEmpty] whileFalse:[ |
|
486 |
| replySize stream | |
|
487 |
||
488 |
socket writeWait. |
|
489 |
stream := buffer writeStream. |
|
490 |
stream nextPutAll: msg. |
|
491 |
socket sendTo:address buffer:buffer start:1 for:stream position. |
|
492 |
socket readWait. |
|
493 |
||
494 |
replySize := socket receiveFrom:address buffer:buffer. |
|
495 |
replySize > 0 ifTrue: [ |
|
496 |
Transcript cr; nextPutAll: 'Server acknowledged: '. |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
497 |
Transcript show: ((buffer copyFrom: 1 to: replySize) asString) |
1343 | 498 |
] |
499 |
] |
|
500 |
] valueNowOrOnUnwindDo: [socket close]. |
|
501 |
Transcript cr |
|
502 |
[exEnd] |
|
1316 | 503 |
|
504 |
||
505 |
loopBack: |
|
1343 | 506 |
[exBegin] |
507 |
||
508 |
|readerTask readingSocket writingSocket| |
|
509 |
||
510 |
readingSocket := self newTCPserverAtPort:9999. |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
511 |
readerTask := |
1343 | 512 |
[ |
513 |
|connection| |
|
514 |
||
515 |
readingSocket listenFor:1. |
|
516 |
connection := readingSocket accept. |
|
517 |
readingSocket close. |
|
518 |
[connection atEnd] whileFalse:[ |
|
519 |
Transcript showCR:(connection nextLine). |
|
520 |
]. |
|
521 |
connection close. |
|
522 |
] fork. |
|
523 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
524 |
Delay waitForSeconds:1. |
1343 | 525 |
writingSocket := self newTCPclientToHost:(OperatingSystem getHostName) port:9999. |
526 |
writingSocket nextPutLine:'Hello'. |
|
527 |
writingSocket nextPutLine:'World'. |
|
528 |
writingSocket close. |
|
529 |
[exEnd] |
|
25 | 530 |
" |
531 |
! ! |
|
532 |
||
1462 | 533 |
!Socket class methodsFor:'instance creation'! |
126 | 534 |
|
535 |
domain:domainSymbol type:type |
|
536 |
"create a socket for domain and type - |
|
537 |
neither any connect nor binding is done. |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
538 |
Domain must be one of the symbols: |
819 | 539 |
#inet, #unix, #appletalk, #decnet, #xns, ...; |
540 |
Type must be: |
|
541 |
#stream, #datagram or #raw |
|
126 | 542 |
|
543 |
XXX: currently only the #inet domain is supported" |
|
544 |
||
545 |
^ self new domain:domainSymbol type:type |
|
546 |
||
547 |
" |
|
548 |
Socket domain:#inet type:#stream |
|
549 |
Socket domain:#inet type:#datagram |
|
550 |
Socket domain:#unix type:#stream |
|
551 |
Socket domain:#appletalk type:#stream |
|
819 | 552 |
Socket domain:#decnet type:#stream |
869 | 553 |
Socket domain:#irda type:#stream |
126 | 554 |
" |
555 |
! |
|
556 |
||
576 | 557 |
new |
558 |
"create a TCP socket" |
|
559 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
560 |
^ super new buffered:false |
576 | 561 |
! |
562 |
||
126 | 563 |
newTCP |
564 |
"create a TCP socket - no binding or other setup is done, |
|
565 |
neither connect nor connect-wait is done." |
|
566 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
567 |
^ self new domain:#inet type:#stream |
126 | 568 |
|
569 |
"Socket newUDP" |
|
570 |
! |
|
571 |
||
572 |
newTCP:aServiceOrNil |
|
573 |
"create a TCP socket for a service - |
|
574 |
neither connect nor connect-wait is done." |
|
575 |
||
576 |
|newSock| |
|
577 |
||
578 |
newSock := self newTCP. |
|
579 |
(newSock notNil and:[aServiceOrNil notNil]) ifTrue:[ |
|
819 | 580 |
(newSock bindTo:(self portOfService:aServiceOrNil) address:nil) ifFalse:[ |
581 |
^ nil |
|
582 |
] |
|
126 | 583 |
]. |
584 |
^ newSock |
|
585 |
||
586 |
||
587 |
"Socket newTCP:'nntp'" |
|
588 |
"Socket newTCP:9995" |
|
589 |
! |
|
590 |
||
576 | 591 |
newTCPclientToAddress:aHostAddress port:aService |
592 |
"create a new TCP client socket connecting to a service. |
|
593 |
Return a socket instance if ok, nil on failure. |
|
594 |
Block until a connection is established (but only the current thread; |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
595 |
not the whole smalltalk). |
576 | 596 |
See also: #newTCPclientToAddress:port:withTimeout:" |
597 |
||
598 |
^ self newTCPclientToAddress:aHostAddress port:aService withTimeout:nil |
|
599 |
! |
|
600 |
||
601 |
newTCPclientToAddress:aHostAddress port:aService withTimeout:millis |
|
602 |
"create a new TCP client socket connecting to a service. |
|
603 |
Return a socket instance if ok, nil on failure. |
|
604 |
If the millis arg is nonNil, stop trying to connect after that many milliseconds |
|
811 | 605 |
and return nil." |
576 | 606 |
|
607 |
^ self newTCPclientToHost:aHostAddress port:aService withTimeout:millis |
|
608 |
! |
|
609 |
||
126 | 610 |
newTCPclientToHost:hostname port:aService |
611 |
"create a new TCP client socket connecting to a service. |
|
612 |
Return a socket instance if ok, nil on failure. |
|
613 |
Block until a connection is established (but only the current thread; |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
614 |
not the whole smalltalk). |
126 | 615 |
See also: #newTCPclientToHost:port:withTimeout:" |
616 |
||
617 |
^ self newTCPclientToHost:hostname port:aService withTimeout:nil |
|
618 |
||
619 |
" |
|
620 |
Socket newTCPclientToHost:'localhost' port:'nntp' |
|
621 |
" |
|
622 |
||
623 |
"Created: 31.10.1995 / 18:54:11 / cg" |
|
624 |
! |
|
625 |
||
626 |
newTCPclientToHost:hostname port:aService withTimeout:millis |
|
627 |
"create a new TCP client socket connecting to a service. |
|
628 |
Return a socket instance if ok, nil on failure. |
|
629 |
If the millis arg is nonNil, stop trying to connect after that many milliseconds |
|
630 |
and return nil.." |
|
631 |
||
632 |
|newSock| |
|
633 |
||
634 |
newSock := self newTCP. |
|
635 |
newSock notNil ifTrue:[ |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
636 |
(newSock connectTo:hostname |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
637 |
port:(self portOfService:aService protocol:'tcp') |
648
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
638 |
withTimeout:millis |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
639 |
) ifFalse:[ |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
640 |
newSock close. |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
641 |
^ nil |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
642 |
] |
126 | 643 |
]. |
644 |
^ newSock |
|
645 |
" |
|
646 |
same as: |
|
647 |
^ (self new) for:hostname port:(self portOfService:aPort). |
|
648 |
" |
|
649 |
" |
|
650 |
Socket newTCPclientToHost:'slsv6bt' port:'nntp' |
|
651 |
Socket newTCPclientToHost:'localhost' port:'nntp' withTimeout:1000 |
|
652 |
" |
|
604 | 653 |
|
654 |
"Modified: / 16.1.1998 / 09:47:06 / stefan" |
|
126 | 655 |
! |
656 |
||
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
657 |
newTCPserverAtAnonymousPort |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
658 |
"create a new TCP server socket providing service on |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
659 |
a new anonymous port. The portNr is assigned by the OS." |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
660 |
|
811 | 661 |
^ self newTCPserverAtPort:0 |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
662 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
663 |
! |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
664 |
|
126 | 665 |
newTCPserverAtPort:aService |
666 |
"create a new TCP server socket providing service." |
|
667 |
||
668 |
|newSock| |
|
669 |
||
670 |
newSock := self newTCP. |
|
671 |
newSock notNil ifTrue:[ |
|
1343 | 672 |
(newSock bindTo:(self portOfService:aService) address:nil) ifFalse:[ |
673 |
^ nil |
|
674 |
] |
|
126 | 675 |
]. |
676 |
^ newSock |
|
677 |
! |
|
678 |
||
679 |
newUDP |
|
680 |
"create a UDP socket - no binding or other setup is done, |
|
681 |
neither connect nor connect-wait is done." |
|
682 |
||
683 |
^ self new domain:#inet type:#datagram |
|
684 |
||
685 |
"Socket newUDP" |
|
686 |
! |
|
687 |
||
688 |
newUDP:aServiceOrNil |
|
689 |
"create a UDP socket for a service - |
|
690 |
neither connect nor connect-wait is done." |
|
691 |
||
692 |
|newSock| |
|
693 |
||
694 |
newSock := self newUDP. |
|
695 |
(newSock notNil and:[aServiceOrNil notNil]) ifTrue:[ |
|
819 | 696 |
(newSock bindTo:(self portOfService:aServiceOrNil) address:nil) ifFalse:[ |
697 |
^ nil |
|
698 |
] |
|
126 | 699 |
]. |
700 |
^ newSock |
|
701 |
||
702 |
"Socket newUDP:nil" |
|
703 |
! |
|
704 |
||
705 |
newUDPserverAtPort:aService |
|
706 |
"create a new UDP server socket providing service." |
|
707 |
||
708 |
|newSock| |
|
709 |
||
710 |
newSock := self newUDP. |
|
711 |
newSock notNil ifTrue:[ |
|
712 |
(newSock bindTo:(self portOfService:aService) address:nil) ifFalse:[ |
|
713 |
^ nil |
|
714 |
] |
|
715 |
]. |
|
716 |
^ newSock |
|
717 |
" |
|
718 |
same as: |
|
719 |
^ (self new) for:nil udpPort:aPort |
|
720 |
" |
|
721 |
! |
|
722 |
||
723 |
newUNIX |
|
724 |
"create a UNIX domain socket - no binding or other setup is done, |
|
811 | 725 |
neither connect nor connect-wait is done. |
726 |
If the system does not support unix domain sockets (i.e. VMS or MSDOS), |
|
727 |
return nil." |
|
126 | 728 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
729 |
^ self new domain:#unix type:#stream |
126 | 730 |
|
731 |
" |
|
732 |
Socket newUNIX |
|
733 |
" |
|
734 |
! |
|
735 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
736 |
newUNIXclientTo:pathName |
126 | 737 |
"create a new UNIX client socket connecting to a pathname. |
738 |
Return a socket instance if ok, nil on failure. |
|
739 |
Block until a connection is established (but only the current thread; |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
740 |
not the whole smalltalk). |
811 | 741 |
If the system does not support unix domain sockets (i.e. VMS or MSDOS), |
742 |
return nil. |
|
126 | 743 |
See also: #newUNIXclientTo:withTimeout:" |
744 |
||
745 |
^ self newUNIXclientTo:pathName withTimeout:nil |
|
746 |
||
747 |
! |
|
748 |
||
749 |
newUNIXclientTo:pathName withTimeout:millis |
|
750 |
"create a new UNIX client socket connecting to a pathname. |
|
751 |
Return a socket instance if ok, nil on failure. |
|
752 |
If the millis arg is nonNil, stop trying to connect after that many milliseconds |
|
811 | 753 |
and return nil. |
754 |
If the system does not support unix domain sockets (i.e. VMS or MSDOS), |
|
755 |
return nil." |
|
126 | 756 |
|
757 |
|newSock| |
|
758 |
||
759 |
newSock := self newUNIX. |
|
760 |
newSock notNil ifTrue:[ |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
761 |
(newSock connectTo:'localhost' port:pathName withTimeout:millis) ifFalse:[ |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
762 |
newSock close. |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
763 |
^ nil |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
764 |
] |
126 | 765 |
]. |
766 |
^ newSock |
|
767 |
||
768 |
" |
|
769 |
|s| |
|
770 |
||
771 |
s := Socket newUNIXclientTo:'/tmp/foo' |
|
772 |
" |
|
773 |
! |
|
774 |
||
775 |
newUNIXserverAt:pathName |
|
811 | 776 |
"create a new UNIX server socket providing service at a pathname. |
777 |
If the system does not support unix domain sockets (i.e. VMS or MSDOS), |
|
778 |
return nil." |
|
126 | 779 |
|
780 |
|newSock| |
|
781 |
||
782 |
newSock := self newUNIX. |
|
783 |
newSock notNil ifTrue:[ |
|
812 | 784 |
(newSock bindTo:pathName address:nil) ifFalse:[ |
785 |
^ nil |
|
786 |
] |
|
126 | 787 |
]. |
788 |
^ newSock |
|
789 |
||
790 |
" |
|
791 |
|s s2| |
|
792 |
||
793 |
s := Socket newUNIXserverAt:'/tmp/foo'. |
|
794 |
s listenFor:5. |
|
795 |
s2 := s accept. |
|
796 |
" |
|
797 |
! ! |
|
798 |
||
1462 | 799 |
!Socket class methodsFor:'Compatibility-ST80'! |
800 |
||
801 |
family:domainSymbol type:typeSymbol |
|
802 |
"create a socket for domain and type - ST80 simply uses a different name. |
|
803 |
Domain must be one of the symbols: #inet, #unix, #ns, #appletalk or #ns; |
|
804 |
Type must be #stream, #datagram or #raw |
|
805 |
||
806 |
XXX: currently only the #inet and #unix domains is supported" |
|
807 |
||
808 |
^ self domain:domainSymbol type:typeSymbol |
|
809 |
||
810 |
" |
|
811 |
Socket family:#inet type:#stream |
|
812 |
Socket family:#inet type:#datagram |
|
813 |
Socket family:#unix type:#stream |
|
814 |
" |
|
815 |
! |
|
816 |
||
817 |
getHostname |
|
818 |
"return the computers hostname string" |
|
819 |
||
820 |
^ OperatingSystem getHostName |
|
821 |
||
822 |
"Created: / 27.2.1998 / 02:32:17 / cg" |
|
823 |
! |
|
824 |
||
825 |
sockStream |
|
826 |
"return the type code for stream sockets" |
|
827 |
||
828 |
^ #stream |
|
829 |
! ! |
|
830 |
||
831 |
!Socket class methodsFor:'Compatibility-VW'! |
|
832 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
833 |
AF_INET |
1462 | 834 |
^ #AF_INET |
835 |
! |
|
836 |
||
837 |
SOCK_STREAM |
|
838 |
^ #SOCK_STREAM |
|
839 |
! ! |
|
840 |
||
841 |
!Socket class methodsFor:'Signal constants'! |
|
842 |
||
843 |
brokenConnectionSignal |
|
844 |
"return the signal used to tell broken connections. |
|
845 |
Since in unix, this is the same as the broken pipe signal, |
|
846 |
return that one. |
|
847 |
(for other Operatingsystems, this may change ..)" |
|
848 |
||
849 |
^ PipeStream brokenPipeSignal |
|
850 |
! |
|
851 |
||
852 |
invalidArgumentsSignal |
|
853 |
"dummy for compatibility" |
|
854 |
||
855 |
^ self errorSignal |
|
856 |
! ! |
|
857 |
||
858 |
!Socket class methodsFor:'debugging'! |
|
859 |
||
860 |
debug:aBoolean |
|
861 |
"turn on/off internal debugprints. |
|
862 |
This method is for ST/X debugging only and |
|
863 |
may be removed in later versions" |
|
864 |
||
865 |
%{ /* NOCONTEXT */ |
|
866 |
||
867 |
__debugging__ = (aBoolean == true); |
|
868 |
%} |
|
869 |
" |
|
870 |
Socket debug:true |
|
871 |
Socket debug:false |
|
872 |
" |
|
873 |
! ! |
|
874 |
||
525 | 875 |
!Socket class methodsFor:'host queries'! |
876 |
||
877 |
appletalkAddressOfHost:aHostName |
|
878 |
"return the APPLETALK address for a hostname as a byteArray, |
|
819 | 879 |
where the network bytes come first (no matter what the local byteorder is) |
880 |
followed by the node byte. |
|
525 | 881 |
If the host is unknown, return nil. |
812 | 882 |
This is the reverse operation to #hostWithAppletalkAddress:. |
883 |
WARNING: untested code - I have no appletalk to test this." |
|
525 | 884 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
885 |
NameLookupError |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
886 |
handle:[:ex | |
1343 | 887 |
^ nil |
888 |
] |
|
889 |
do:[ |
|
890 |
^ (AppletalkSocketAddress hostName:aHostName) address |
|
891 |
] |
|
525 | 892 |
! |
893 |
||
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
894 |
hostWithAppletalkAddress:addrByteArray |
525 | 895 |
"return the hostname for an APPLETALK address. |
896 |
The address is supposed to be a byteArray consisting of 3 bytes, |
|
897 |
the network bytes come first (no matter what the local byteorder is). |
|
898 |
The last byte is the node number. |
|
899 |
Nil is returned for an unknown host or if its not an appletalk host. |
|
812 | 900 |
This is is the reverse operation to #appletalkAddressOfHost:. |
901 |
WARNING: untested code - I have no appletalk to test this." |
|
525 | 902 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
903 |
NameLookupError |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
904 |
handle:[:ex | |
1343 | 905 |
^ nil |
906 |
] |
|
907 |
do:[ |
|
908 |
^ (AppletalkSocketAddress hostAddress:addrByteArray) hostName |
|
909 |
] |
|
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
910 |
|
525 | 911 |
" |
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
912 |
Socket appletalkAddressOfHost:'yourAppleHere' |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
913 |
Socket hostWithAppletalkAddress:#[1 2 3] |
525 | 914 |
" |
126 | 915 |
! |
916 |
||
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
917 |
hostWithIpAddress:addrByteArray |
126 | 918 |
"return the hostname for an IP (internet-) address. |
919 |
The address is supposed to be a byteArray consisting of 4 bytes, |
|
920 |
the network bytes come first (no matter what the local byteorder is). |
|
525 | 921 |
Nil is returned for an unknown host or if its not an internet host. |
126 | 922 |
This is the reverse operation to #ipAddressOfHost:." |
923 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
924 |
NameLookupError |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
925 |
handle:[:ex | |
1343 | 926 |
^ nil |
927 |
] |
|
928 |
do:[ |
|
929 |
^ (IPSocketAddress hostAddress:addrByteArray) hostName |
|
930 |
] |
|
126 | 931 |
|
932 |
" |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
933 |
Socket ipAddressOfHost:'clam' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
934 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'clam') |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
935 |
Socket ipAddressOfHost:'porty' |
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
936 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'porty') |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
937 |
Socket hostWithIpAddress:#[1 2 3 4] |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
938 |
Socket hostWithIpAddress:#[127 0 0 1] |
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
939 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'1.2.3.4') |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
940 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'www.altavista.com') |
126 | 941 |
" |
942 |
! |
|
943 |
||
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
944 |
hostWithIpV6Address:addrByteArray |
820
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
945 |
"return the hostname for an IPv6 (internet-) address. |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
946 |
The address is supposed to be a byteArray consisting ??? bytes, |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
947 |
the network bytes come first (no matter what the local byteorder is). |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
948 |
Nil is returned for an unknown host or if its not an internet host. |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
949 |
This is the reverse operation to #ipV6AddressOfHost:." |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
950 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
951 |
NameLookupError |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
952 |
handle:[:ex | |
1343 | 953 |
^ nil |
954 |
] |
|
955 |
do:[ |
|
956 |
^ (IPv6SocketAddress hostAddress:addrByteArray) hostName |
|
957 |
] |
|
820
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
958 |
|
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
959 |
" |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
960 |
Socket ipV6AddressOfHost:'clam' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
961 |
Socket hostWithIpV6Address:(Socket ipAddressOfHost:'clam') |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
962 |
Socket ipV6AddressOfHost:'porty' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
963 |
Socket hostWithIpV6Address:(Socket ipAddressOfHost:'porty') |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
964 |
Socket hostWithIpV6Address:#[1 2 3 4 5 6 7 8 9 10 11 12 13 14] |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
965 |
Socket ipV6AddressOfHost:'www.exept.de' |
820
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
966 |
" |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
967 |
! |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
968 |
|
6 | 969 |
ipAddressOfHost:aHostName |
98
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
970 |
"return the IP (internet-) number for a hostname as a byteArray, |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
971 |
where the network bytes come first (no matter what the cpus byteOrder is). |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
972 |
If the host is unknown, return nil. |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
973 |
This is the reverse operation to #hostWithIpAddress:." |
6 | 974 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
975 |
NameLookupError |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
976 |
handle:[:ex | |
1343 | 977 |
^ nil |
978 |
] |
|
979 |
do:[ |
|
980 |
^ (IPSocketAddress hostName:aHostName) address |
|
981 |
] |
|
21 | 982 |
|
71 | 983 |
" |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
984 |
Socket ipAddressOfHost:'localhost' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
985 |
Socket ipAddressOfHost:'exept' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
986 |
Socket ipAddressOfHost:'1.2.3.4' |
1274
f257a1b16b4d
use naemLookup from SocketAddress for ipv6
Claus Gittinger <cg@exept.de>
parents:
1263
diff
changeset
|
987 |
Socket ipAddressOfHost:'193.15.16.17' |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
988 |
Socket ipAddressOfHost:'josef' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
989 |
Socket ipAddressOfHost:'styx.com' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
990 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'localhost') |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
991 |
Socket ipAddressOfHost:(Socket hostWithIpAddress:'127.0.0.1') |
71 | 992 |
" |
819 | 993 |
! |
994 |
||
995 |
ipV6AddressOfHost:aHostName |
|
996 |
"return the IPv6 (internet-) number for a hostname as a byteArray, |
|
997 |
where the network bytes come first (no matter what the cpus byteOrder is). |
|
998 |
If the host is unknown, return nil. |
|
999 |
This is the reverse operation to #hostWithIpV6Address:." |
|
1000 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1001 |
NameLookupError |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1002 |
handle:[:ex | |
1343 | 1003 |
^ nil |
1004 |
] |
|
1005 |
do:[ |
|
1006 |
^ (IPv6SocketAddress hostName:aHostName) address |
|
1007 |
] |
|
819 | 1008 |
|
1009 |
" |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1010 |
Socket ipV6AddressOfHost:'localhost' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1011 |
Socket ipV6AddressOfHost:'exept' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1012 |
Socket ipV6AddressOfHost:'exept.exept.de' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1013 |
Socket ipV6AddressOfHost:'www.google.de' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1014 |
Socket ipV6AddressOfHost:'1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1015 |
Socket ipV6AddressOfHost:'josef' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1016 |
Socket ipV6AddressOfHost:'styx.com' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1017 |
Socket hostWithIpV6Address:(Socket ipV6AddressOfHost:'localhost') |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1018 |
Socket ipV6AddressOfHost:(Socket hostV6WithIpAddress:'127.0.0.1') |
819 | 1019 |
" |
531 | 1020 |
! ! |
1021 |
||
1209 | 1022 |
!Socket class methodsFor:'obsolete'! |
1023 |
||
1024 |
connectTo:service on:host |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1025 |
"standard & easy client setup: |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1026 |
create new client tcp socket, bind and connect; |
1343 | 1027 |
return the socket. |
1282 | 1028 |
The thread block (interruptable), until the connection is established." |
1029 |
||
1030 |
^ self newTCPclientToHost:host port:(self portOfService:service). |
|
1209 | 1031 |
|
1032 |
" |
|
1033 |
Socket connectTo:9995 on:'clam' |
|
1034 |
Socket connectTo:4711 on:'exept' |
|
1035 |
Socket connectTo:'finger' on:'clam' |
|
1036 |
Socket connectTo:'ftp' on:'exept' |
|
1037 |
Socket connectTo:'nntp' on:(OperatingSystem getEnvironment:'NNTPSERVER') |
|
1038 |
" |
|
1039 |
! |
|
1040 |
||
1282 | 1041 |
provide:aService |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1042 |
"standard & easy server setup: |
1209 | 1043 |
create a new TCP server socket providing a service." |
1044 |
||
1045 |
<resource:#obsolete> |
|
1046 |
||
1047 |
|newSock| |
|
1048 |
||
1282 | 1049 |
self obsoleteMethodWarning:'use newTCPserverAtPort: / listen'. |
1050 |
||
1051 |
newSock := self newTCPserverAtPort:(self portOfService:aService). |
|
1209 | 1052 |
newSock notNil ifTrue:[ |
1343 | 1053 |
newSock listenFor:5. |
1209 | 1054 |
]. |
1055 |
^ newSock |
|
1056 |
||
1057 |
" |
|
1058 |
Socket provide:9995 |
|
1059 |
(Socket provide:9996) accept |
|
1060 |
Socket provide:'nntp' |
|
1061 |
" |
|
1062 |
! ! |
|
1063 |
||
531 | 1064 |
!Socket class methodsFor:'queries'! |
1065 |
||
1066 |
domainOfProtocol:aProtocol |
|
1067 |
"given a protocols name (i.e. tcp, udp etc) return the domain. |
|
1068 |
This method needs more ... - or is there a way to get this from the system ?" |
|
1069 |
||
1070 |
" |
|
1071 |
tcp/ip stuff |
|
1072 |
" |
|
1073 |
(aProtocol = 'tcp') ifTrue:[^ #inet]. |
|
1074 |
(aProtocol = 'udp') ifTrue:[^ #inet]. |
|
1075 |
(aProtocol = 'ip') ifTrue:[^ #inet]. |
|
1076 |
" |
|
1077 |
unix domain |
|
1078 |
" |
|
1079 |
(aProtocol = 'ud') ifTrue:[^ #unix]. |
|
1080 |
||
1081 |
" |
|
1082 |
add x25 stuff (if any) here ... |
|
1083 |
" |
|
1084 |
" |
|
1085 |
add appletalk stuff (if any) here ... |
|
1086 |
" |
|
1087 |
" |
|
1088 |
add other stuff (if any) here ... |
|
1089 |
" |
|
1090 |
^ nil |
|
1091 |
||
1092 |
" |
|
1093 |
Socket domainOfProtocol:'tcp' |
|
1094 |
Socket domainOfProtocol:'ucp' |
|
1095 |
Socket domainOfProtocol:(Socket protocolOfService:'nntp') |
|
1096 |
Socket domainOfProtocol:(Socket protocolOfService:'echo') |
|
1097 |
" |
|
6 | 1098 |
! |
1099 |
||
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1100 |
networkLongOrderIsMSB |
964 | 1101 |
"return true, if the network-byte-order of longs is MSB (which it is). |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1102 |
To be used as in: |
1009 | 1103 |
'aStream nextPutLong:someValue MSB:(Socket networkLongOrderIsMSB)'." |
964 | 1104 |
|
1105 |
^ true |
|
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1106 |
|
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1107 |
" |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1108 |
Socket networkLongOrderIsMSB |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1109 |
" |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1110 |
! |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1111 |
|
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1112 |
networkShortOrderIsMSB |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1113 |
"return true, if the network-byte-order of shorts is MSB. |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1114 |
To be used as in: |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1115 |
'aStream nextPutShort:someValue MSB:(Socket networkShortOrderIsMSB)'." |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1116 |
|
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1117 |
%{ /* NOCONTEXT */ |
418 | 1118 |
#ifndef NO_SOCKET |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1119 |
/* |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1120 |
* mhmh - do c-compilers optimize this away ? |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1121 |
*/ |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1122 |
if (htons(0x3210) == 0x3210) { |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1123 |
RETURN (true); |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1124 |
} |
418 | 1125 |
#endif |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1126 |
%}. |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1127 |
^ false |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1128 |
|
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1129 |
" |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1130 |
Socket networkShortOrderIsMSB |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1131 |
" |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1132 |
! |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1133 |
|
1260
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1134 |
peerFromDomain:domain name:peerName port:port |
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1135 |
|addrClass| |
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1136 |
|
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1137 |
addrClass := self socketAddressClassForDomain:domain. |
1263
23161346a162
no longer callBack into sockets getHostName/getAddress code.
Claus Gittinger <cg@exept.de>
parents:
1260
diff
changeset
|
1138 |
^ addrClass hostName:peerName serviceName:port type:nil |
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1139 |
! |
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1140 |
|
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1141 |
peerNameFromDomain:domain peer:peer |
1260
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1142 |
|addrClass| |
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1143 |
|
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1144 |
addrClass := self socketAddressClassForDomain:domain. |
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1145 |
^ addrClass peerNameFromPeer:peer |
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1146 |
! |
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1147 |
|
0 | 1148 |
portOfService:aNameOrNumber |
819 | 1149 |
"returns the port-number for a given IP-service |
0 | 1150 |
or nil if no such service exists; |
1151 |
- used to convert service names to portNumbers" |
|
1152 |
||
91 | 1153 |
^ self portOfService:aNameOrNumber protocol:nil |
1154 |
||
28 | 1155 |
" |
1156 |
Socket portOfService:'finger' |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1157 |
Socket portOfService:'nntp' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1158 |
Socket portOfService:'echo' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1159 |
Socket portOfService:'snmp' |
28 | 1160 |
" |
0 | 1161 |
! |
1162 |
||
91 | 1163 |
portOfService:aNameOrNumber protocol:aProtocol |
819 | 1164 |
"returns the port-number for a given IP-service |
91 | 1165 |
or nil if no such service exists; |
1166 |
- used to convert service names to portNumbers" |
|
1167 |
||
604 | 1168 |
%{ /* NOCONTEXT */ |
418 | 1169 |
#ifndef NO_SOCKET |
91 | 1170 |
struct servent *servent = NULL; |
1171 |
char *protocol; |
|
1172 |
int tryBoth = 0; |
|
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
1173 |
short portNo; |
91 | 1174 |
|
604 | 1175 |
if (__isSmallInteger(aNameOrNumber)) { |
648
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
1176 |
RETURN ( aNameOrNumber ); |
92 | 1177 |
} |
1178 |
||
604 | 1179 |
if (__isString(aProtocol)) { |
648
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
1180 |
protocol = (char *)__stringVal(aProtocol); |
604 | 1181 |
} else { |
648
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
1182 |
protocol = "tcp"; |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
1183 |
tryBoth = 1; |
91 | 1184 |
} |
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
1185 |
|
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
1186 |
if (__isString(aNameOrNumber) || __isSymbol(aNameOrNumber)) { |
648
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
1187 |
servent = getservbyname((char *) __stringVal(aNameOrNumber), protocol); |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
1188 |
if (servent != NULL) { |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
1189 |
RETURN ( __MKSMALLINT(ntohs(servent->s_port)) ); |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
1190 |
} |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
1191 |
if (tryBoth) { |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
1192 |
servent = getservbyname((char *) __stringVal(aNameOrNumber), "udp"); |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
1193 |
if (servent != NULL) { |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
1194 |
RETURN ( __MKSMALLINT(ntohs(servent->s_port)) ); |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
1195 |
} |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
1196 |
} |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
1197 |
RETURN ( nil ); |
91 | 1198 |
} |
418 | 1199 |
#endif |
91 | 1200 |
RETURN ( nil ); |
1201 |
%} |
|
1202 |
" |
|
1203 |
Socket portOfService:'echo' protocol:'udp' |
|
1204 |
Socket portOfService:'echo' protocol:'tcp' |
|
1205 |
" |
|
1206 |
! |
|
1207 |
||
0 | 1208 |
protocolOfService:aNameOrNumber |
819 | 1209 |
"returns the protocol (as string) for a given IP-service |
28 | 1210 |
or nil if no such service exists." |
0 | 1211 |
%{ |
418 | 1212 |
#ifndef NO_SOCKET |
0 | 1213 |
struct servent *servent = NULL; |
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
1214 |
short portNo; |
0 | 1215 |
|
58 | 1216 |
if (__isSmallInteger(aNameOrNumber)) { |
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
1217 |
portNo = __intVal(aNameOrNumber); |
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
1218 |
servent = getservbyport(htons(portNo), "tcp") ; |
42 | 1219 |
if (servent == NULL) { |
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
1220 |
servent = getservbyport(htons(portNo), "udp") ; |
42 | 1221 |
if (servent == NULL) { |
1222 |
RETURN ( nil ); |
|
1223 |
} |
|
1224 |
} |
|
0 | 1225 |
} else { |
42 | 1226 |
if (__isString(aNameOrNumber)) { |
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
1227 |
servent = getservbyname((char *) __stringVal(aNameOrNumber), "tcp"); |
42 | 1228 |
if (servent == NULL) { |
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
1229 |
servent = getservbyname((char *) __stringVal(aNameOrNumber), "udp"); |
42 | 1230 |
if (servent == NULL) { |
1231 |
RETURN ( nil ); |
|
1232 |
} |
|
1233 |
} |
|
1234 |
} |
|
0 | 1235 |
} |
1236 |
if (servent) { |
|
448
2203a4bb620d
removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
1237 |
RETURN ( __MKSTRING(servent->s_proto) ); |
0 | 1238 |
} |
418 | 1239 |
#endif |
0 | 1240 |
RETURN ( nil ); |
1241 |
%} |
|
28 | 1242 |
" |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1243 |
Socket protocolOfService:'finger' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1244 |
Socket protocolOfService:'nntp' |
28 | 1245 |
Socket protocolOfService:'xxx' |
1246 |
Socket protocolOfService:79 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1247 |
Socket protocolOfService:'snmp' |
28 | 1248 |
" |
1249 |
! |
|
1250 |
||
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1251 |
socketAddressClassForDomain:domain |
1230
2cfb8c270cb5
more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents:
1224
diff
changeset
|
1252 |
^ SocketAddress knownClassFromCode:domain |
2cfb8c270cb5
more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents:
1224
diff
changeset
|
1253 |
|
2cfb8c270cb5
more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents:
1224
diff
changeset
|
1254 |
" |
2cfb8c270cb5
more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents:
1224
diff
changeset
|
1255 |
self socketAddressClassForDomain:#inet |
1260
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1256 |
self socketAddressClassForDomain:#unix |
1230
2cfb8c270cb5
more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents:
1224
diff
changeset
|
1257 |
" |
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1258 |
! |
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1259 |
|
532 | 1260 |
supportedProtocolFamilies |
1261 |
"return a collection of supported protocol families. |
|
1262 |
This list specifies what the Socket class supports - |
|
1263 |
socket creation may still fail, if your system was built |
|
1264 |
without it." |
|
1265 |
||
1280
b973855bfe74
moved all socket constant queries from SocketHandle
Claus Gittinger <cg@exept.de>
parents:
1278
diff
changeset
|
1266 |
^ OperatingSystem supportedProtocolFamilies |
532 | 1267 |
|
1268 |
" |
|
1269 |
Socket supportedProtocolFamilies |
|
1270 |
" |
|
559
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1271 |
! |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1272 |
|
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1273 |
typeOfProtocol:aProtocol |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1274 |
"given a protocols name (i.e. tcp, udp etc) return the connection type. |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1275 |
This method needs more ... - or is there a way to get this from the system ?" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1276 |
|
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1277 |
(aProtocol = 'tcp') ifTrue:[^ #stream]. |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1278 |
(aProtocol = 'udp') ifTrue:[^ #datagram]. |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1279 |
(aProtocol = 'ip') ifTrue:[^ #raw]. |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1280 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1281 |
unix domain |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1282 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1283 |
(aProtocol = 'ud') ifTrue:[^ #stream]. |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1284 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1285 |
add x25 stuff (if any) here ... |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1286 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1287 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1288 |
add appletalk stuff (if any) here ... |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1289 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1290 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1291 |
add other stuff (if any) here ... |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1292 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1293 |
^ nil |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1294 |
|
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1295 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1296 |
Socket typeOfProtocol:'tcp' |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1297 |
Socket typeOfProtocol:'ucp' |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1298 |
Socket typeOfProtocol:(Socket protocolOfService:'nntp') |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1299 |
Socket typeOfProtocol:(Socket protocolOfService:'echo') |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1300 |
" |
28 | 1301 |
! ! |
1302 |
||
1202 | 1303 |
!Socket methodsFor:'Compatibility-Dolphin'! |
1110 | 1304 |
|
1305 |
setReceiveTimeout: milliseconds |
|
1306 |
self receiveTimeout:(milliseconds / 1000) |
|
1307 |
! |
|
1308 |
||
1309 |
setSendTimeout: milliseconds |
|
1310 |
self sendTimeout:(milliseconds / 1000) |
|
1311 |
! ! |
|
1312 |
||
1202 | 1313 |
!Socket methodsFor:'Compatibility-ST80'! |
28 | 1314 |
|
887 | 1315 |
acceptNonBlock |
1316 |
^ self accept |
|
1317 |
! |
|
1318 |
||
870 | 1319 |
connectTo:aSocketAddress |
1320 |
^ self connectTo:aSocketAddress hostAddress port:aSocketAddress port |
|
1321 |
! |
|
1322 |
||
126 | 1323 |
errorReporter |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1324 |
"ST-80 mimicry." |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1325 |
|
126 | 1326 |
^ self |
28 | 1327 |
! |
1328 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1329 |
ioConnection |
887 | 1330 |
^ self |
1331 |
! |
|
1332 |
||
126 | 1333 |
notReadySignal |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1334 |
"ST-80 mimicry. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1335 |
for now - this is not yet raised" |
71 | 1336 |
|
126 | 1337 |
^ Signal new |
486 | 1338 |
! |
1339 |
||
576 | 1340 |
readAppendStream |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1341 |
"ST-80 mimicry. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1342 |
In ST-80, socket is not a stream, but referes to one. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1343 |
ST-80 code therefore uses 'Socket readWriteStream' to access |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1344 |
the actual stream. |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1345 |
In ST/X, sockets inherit from stream, so |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1346 |
this method returns the receiver, for transparency" |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1347 |
|
576 | 1348 |
^ self |
1349 |
! |
|
1350 |
||
486 | 1351 |
readStream |
1352 |
"ST-80 mimicry. |
|
1353 |
In ST-80, socket is not a stream, but referes to one. |
|
1354 |
ST-80 code therefore uses 'Socket readStream' to access |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1355 |
the actual stream. |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1356 |
In ST/X, sockets inherit from stream, so |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1357 |
this method returns the receiver, for transparency" |
486 | 1358 |
|
1359 |
^ self |
|
1360 |
||
1361 |
"Created: 24.1.1997 / 23:52:57 / cg" |
|
1362 |
! |
|
1363 |
||
1364 |
writeStream |
|
1365 |
"ST-80 mimicry. |
|
1366 |
In ST-80, socket is not a stream, but referes to one. |
|
1367 |
ST-80 code therefore uses 'Socket writeStream' to access |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1368 |
the actual stream. |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1369 |
In ST/X, sockets inherit from stream, so |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1370 |
this method returns the receiver, for transparency" |
486 | 1371 |
|
1372 |
^ self |
|
1373 |
||
1374 |
"Created: 24.1.1997 / 10:34:35 / cg" |
|
1375 |
"Modified: 24.1.1997 / 23:52:52 / cg" |
|
71 | 1376 |
! ! |
1377 |
||
1202 | 1378 |
!Socket methodsFor:'Compatibility-Squeak'! |
771 | 1379 |
|
1462 | 1380 |
address |
1381 |
^ self getSocketAddress |
|
1382 |
! |
|
1383 |
||
1384 |
dataAvailable |
|
1385 |
^ self canReadWithoutBlocking |
|
1386 |
! |
|
1387 |
||
1388 |
listenOn:aPortNr |
|
1389 |
self bindTo:aPortNr address:nil. |
|
1390 |
self listenWithBacklog:5 |
|
1391 |
! |
|
1392 |
||
771 | 1393 |
peerName |
1394 |
"return my peer (i.e. ipAddr + port); |
|
1395 |
May return nil if not yet setup completely." |
|
1396 |
||
1397 |
^ self getPeer |
|
1462 | 1398 |
! |
1399 |
||
1400 |
setOption: optionName value: optionValue |
|
1401 |
optionName = 'TCP_NODELAY' ifTrue:[ |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1402 |
^ self setTCPNoDelay:optionValue |
1462 | 1403 |
]. |
1404 |
self error:'unimplemented socketoption' mayProceed:true |
|
1405 |
! |
|
1406 |
||
1407 |
waitForConnectionUntil:aMillisecondClockValue |
|
1408 |
self halt. |
|
771 | 1409 |
! ! |
1410 |
||
126 | 1411 |
!Socket methodsFor:'datagram transmission'! |
28 | 1412 |
|
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1413 |
receiveBuffer:aDataBuffer start:startIndex for:nBytes |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1414 |
"receive data |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1415 |
Return the number of bytes received, or a negative number on error. |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1416 |
On error, the unix error code is left in the lastErrorNumber |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1417 |
instance variable. |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1418 |
The thread blocks until data arrives - you may want to wait before |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1419 |
receiving, using #readWait or #readWaitWithTimeout:." |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1420 |
|
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1421 |
|
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1422 |
|nReceived| |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1423 |
|
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1424 |
%{ |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1425 |
#ifndef NO_SOCKET |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1426 |
OBJ oClass, myDomain; |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1427 |
OBJ fp = __INST(filePointer); |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1428 |
int nInstVars, nInstBytes, objSize; |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1429 |
int sock; |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1430 |
int n; |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1431 |
char *cp; |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1432 |
int flags = 0; |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1433 |
|
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1434 |
if (fp != nil) { |
1343 | 1435 |
sock = fileno(__FILEVal(fp)); |
1436 |
||
1437 |
oClass = __Class(aDataBuffer); |
|
1438 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
1439 |
case BYTEARRAY: |
|
1440 |
case WORDARRAY: |
|
1441 |
case SWORDARRAY: |
|
1442 |
case LONGARRAY: |
|
1443 |
case SLONGARRAY: |
|
1444 |
case FLOATARRAY: |
|
1445 |
case DOUBLEARRAY: |
|
1446 |
break; |
|
1447 |
default: |
|
1448 |
goto bad; |
|
1449 |
} |
|
1450 |
||
1451 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
1452 |
nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ); |
|
1453 |
objSize = _Size(aDataBuffer) - nInstBytes; |
|
1454 |
cp = (char *)__InstPtr(aDataBuffer) + nInstBytes; |
|
1455 |
if (__isSmallInteger(startIndex)) { |
|
1456 |
cp += __intVal(startIndex); |
|
1457 |
objSize -= __intVal(startIndex); |
|
1458 |
} |
|
1459 |
if (__isSmallInteger(nBytes)) { |
|
1460 |
if (__intVal(nBytes) < objSize) { |
|
1461 |
objSize = __intVal(nBytes); |
|
1462 |
} |
|
1463 |
} |
|
1464 |
||
1465 |
__BEGIN_INTERRUPTABLE__ |
|
1466 |
do { |
|
1467 |
n = recv(sock, cp, objSize, flags); |
|
1468 |
} while ((n < 0) && (errno == EINTR)); |
|
1469 |
__END_INTERRUPTABLE__ |
|
1470 |
||
1471 |
if (n < 0) { |
|
1472 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1473 |
} |
|
1474 |
nReceived = __MKSMALLINT(n); |
|
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1475 |
} |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1476 |
#endif |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1477 |
bad: ; |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1478 |
%}. |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1479 |
nReceived notNil ifTrue:[ |
1343 | 1480 |
nReceived < 0 ifTrue:[ |
1481 |
'Socket [warning]: ' infoPrint. |
|
1482 |
(OperatingSystem errorTextForNumber:lastErrorNumber) infoPrintCR. |
|
1483 |
]. |
|
1484 |
^ nReceived |
|
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1485 |
]. |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1486 |
" |
1282 | 1487 |
arrive here if you try to receive into an invalid buffer (i.e. not ByteArray-like) |
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1488 |
" |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1489 |
self primitiveFailed |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1490 |
! |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1491 |
|
126 | 1492 |
receiveFrom:anAddressBuffer buffer:aDataBuffer |
1493 |
"receive datagramm data - put address of originating host into |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1494 |
anAddressBuffer, data into aBuffer. |
126 | 1495 |
Both must be ByteArray-like. The addressBuffer must |
1496 |
provide space for a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
1497 |
Return the number of bytes received, or a negative number on error. |
|
1498 |
On error, the unix error code is left in the lastErrorNumber |
|
1499 |
instance variable." |
|
28 | 1500 |
|
126 | 1501 |
^ self receiveFrom:anAddressBuffer buffer:aDataBuffer start:1 for:(aDataBuffer size) |
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
1502 |
! |
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
1503 |
|
126 | 1504 |
receiveFrom:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1505 |
"receive datagramm data |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1506 |
- put address of originating host into anAddressBuffer, data into aBuffer. |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1507 |
For backward compatibility, the addressBuffer may be a non-SocketAddress; |
1282 | 1508 |
then, it must be a byteArray with appropriate size for the addressBytes. |
1509 |
||
126 | 1510 |
Return the number of bytes received, or a negative number on error. |
1511 |
On error, the unix error code is left in the lastErrorNumber |
|
513
b8042ff0f82c
udp receive: translate received address according to the domain
Claus Gittinger <cg@exept.de>
parents:
511
diff
changeset
|
1512 |
instance variable. |
b8042ff0f82c
udp receive: translate received address according to the domain
Claus Gittinger <cg@exept.de>
parents:
511
diff
changeset
|
1513 |
The thread blocks until data arrives - you may want to wait before |
b8042ff0f82c
udp receive: translate received address according to the domain
Claus Gittinger <cg@exept.de>
parents:
511
diff
changeset
|
1514 |
receiving, using #readWait or #readWaitWithTimeout:." |
b8042ff0f82c
udp receive: translate received address according to the domain
Claus Gittinger <cg@exept.de>
parents:
511
diff
changeset
|
1515 |
|
1282 | 1516 |
|domainClass addr addrLen nReceived| |
1517 |
||
1518 |
domainClass := self class socketAddressClassForDomain:domain. |
|
1519 |
domainClass isNil ifTrue:[ |
|
1343 | 1520 |
^ self error:'invalid (unsupported) domain'. |
1282 | 1521 |
]. |
1522 |
(anAddressBuffer isKindOf:SocketAddress) ifTrue:[ |
|
1343 | 1523 |
anAddressBuffer class == domainClass ifFalse:[ |
1524 |
^ self error:'addressBuffer class mismatch (domain)'. |
|
1525 |
]. |
|
1526 |
addr := anAddressBuffer. |
|
1282 | 1527 |
] ifFalse:[ |
1343 | 1528 |
anAddressBuffer notNil ifTrue:[ |
1529 |
addr := domainClass new. |
|
1530 |
]. |
|
1282 | 1531 |
]. |
1532 |
||
126 | 1533 |
%{ |
418 | 1534 |
#ifndef NO_SOCKET |
513
b8042ff0f82c
udp receive: translate received address according to the domain
Claus Gittinger <cg@exept.de>
parents:
511
diff
changeset
|
1535 |
OBJ oClass, myDomain; |
217 | 1536 |
OBJ fp = __INST(filePointer); |
126 | 1537 |
int nInstVars, nInstBytes, objSize; |
1538 |
int sock; |
|
819 | 1539 |
union sockaddr_u sa; |
1009 | 1540 |
int alen = 0; |
1541 |
int n, offs, sIdx; |
|
126 | 1542 |
char *cp; |
1543 |
int flags = 0; |
|
1544 |
||
1545 |
if (fp != nil) { |
|
1343 | 1546 |
sock = fileno(__FILEVal(fp)); |
1547 |
||
1548 |
oClass = __Class(aDataBuffer); |
|
1549 |
offs = 0; |
|
1550 |
sIdx = 0; |
|
1551 |
if (__isSmallInteger(startIndex)) { |
|
1552 |
sIdx = __intVal(startIndex) - 1; |
|
1553 |
} |
|
1554 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
1555 |
case BYTEARRAY: |
|
1556 |
offs = sIdx; |
|
1557 |
break; |
|
1558 |
case WORDARRAY: |
|
1559 |
case SWORDARRAY: |
|
1560 |
offs = sIdx * 2; |
|
1561 |
break; |
|
1562 |
case LONGARRAY: |
|
1563 |
case SLONGARRAY: |
|
1564 |
offs = sIdx * 4; |
|
1565 |
break; |
|
1566 |
case LONGLONGARRAY: |
|
1567 |
case SLONGLONGARRAY: |
|
1568 |
offs = sIdx * 8; |
|
1003
0157b8af9a1e
oops - datagram receive was wrong (startIndex handling)
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
1569 |
# ifdef __NEED_LONGLONG_ALIGN |
1343 | 1570 |
offs += 4; |
1003
0157b8af9a1e
oops - datagram receive was wrong (startIndex handling)
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
1571 |
# endif |
1343 | 1572 |
break; |
1573 |
case FLOATARRAY: |
|
1574 |
offs = sIdx * sizeof(float); |
|
1575 |
break; |
|
1576 |
case DOUBLEARRAY: |
|
1577 |
offs = sIdx * sizeof(double); |
|
1003
0157b8af9a1e
oops - datagram receive was wrong (startIndex handling)
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
1578 |
# ifdef __NEED_DOUBLE_ALIGN |
1343 | 1579 |
offs += 4; |
1003
0157b8af9a1e
oops - datagram receive was wrong (startIndex handling)
Claus Gittinger <cg@exept.de>
parents:
969
diff
changeset
|
1580 |
# endif |
1343 | 1581 |
break; |
1582 |
default: |
|
1583 |
goto bad; |
|
1584 |
} |
|
1585 |
||
1586 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
1587 |
nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ); |
|
1588 |
objSize = _Size(aDataBuffer) - nInstBytes; |
|
1589 |
objSize -= offs; |
|
1590 |
if (__isSmallInteger(nBytes)) { |
|
1591 |
if (__intVal(nBytes) < objSize) { |
|
1592 |
objSize = __intVal(nBytes); |
|
1593 |
} |
|
1594 |
} |
|
1595 |
||
1596 |
__BEGIN_INTERRUPTABLE__ |
|
1597 |
do { |
|
1598 |
cp = (char *)__InstPtr(aDataBuffer) + nInstBytes + offs; |
|
1599 |
alen = sizeof(sa); |
|
1600 |
n = recvfrom(sock, cp, objSize, flags, (struct sockaddr *) &sa, &alen); |
|
1601 |
} while ((n < 0) && (errno == EINTR)); |
|
1602 |
__END_INTERRUPTABLE__ |
|
1603 |
||
1604 |
if (n >= 0) { |
|
1605 |
if (__isNonNilObject(addr)) { |
|
1606 |
oClass = __qClass(addr); |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1607 |
if (! __isBytes(addr) ) |
1343 | 1608 |
goto bad; |
1609 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
1610 |
nInstBytes = OHDR_SIZE + (nInstVars * sizeof(OBJ)); |
|
1611 |
objSize = __qSize(addr) - nInstBytes; |
|
1612 |
cp = (char *)__InstPtr(addr) + nInstBytes; |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1613 |
if (objSize < alen) |
1343 | 1614 |
goto bad; |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1615 |
|
1343 | 1616 |
/* |
1617 |
* extract the datagrams address |
|
1618 |
*/ |
|
1619 |
bcopy((char *)&sa, cp, alen); |
|
1620 |
addrLen = __MKSMALLINT(alen); |
|
1621 |
} |
|
1622 |
} |
|
1623 |
if (n < 0) { |
|
1624 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1625 |
} |
|
1626 |
nReceived = __MKSMALLINT(n); |
|
126 | 1627 |
} |
418 | 1628 |
#endif |
126 | 1629 |
bad: ; |
1630 |
%}. |
|
1631 |
nReceived notNil ifTrue:[ |
|
1343 | 1632 |
nReceived < 0 ifTrue:[ |
1633 |
'Socket [warning]: ' infoPrint. |
|
1634 |
(OperatingSystem errorTextForNumber:lastErrorNumber) infoPrintCR. |
|
1635 |
]. |
|
1636 |
addrLen notNil ifTrue:[ |
|
1637 |
(addr == anAddressBuffer) ifFalse:[ |
|
1638 |
self obsoleteFeatureWarning:'please use a socketAddress argument'. |
|
1639 |
||
1640 |
"/ can be a ByteArray for backward compatibility |
|
1641 |
anAddressBuffer replaceFrom:1 to:addrLen with:(addr hostAddress). |
|
1642 |
]. |
|
1643 |
]. |
|
1644 |
^ nReceived |
|
28 | 1645 |
]. |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1646 |
" |
126 | 1647 |
arrive here if you try to receive into an invalid buffer |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1648 |
(i.e. not ByteArray-like), |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1649 |
or if the addressBuffer is nonNil AND not a SocketAddress/ByteArray |
126 | 1650 |
or if the addressBuffer is nonNil AND too small. |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1651 |
" |
126 | 1652 |
self primitiveFailed |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1653 |
! |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1654 |
|
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1655 |
sendBuffer:aDataBuffer start:startIndex for:count flags:flags |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1656 |
"send data. |
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1657 |
Both must be ByteArray-like. The bytes in the addressBuffer must |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1658 |
be a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1659 |
Return the number of bytes transmitted, or a negative number on error. |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1660 |
On error, the unix error code is left in the lastErrorNumber |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1661 |
instance variable." |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1662 |
|
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1663 |
|nReceived portNo| |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1664 |
|
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1665 |
%{ |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1666 |
#ifndef NO_SOCKET |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1667 |
OBJ oClass; |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1668 |
OBJ fp = __INST(filePointer); |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1669 |
int nInstVars, nInstBytes, objSize; |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1670 |
int sock; |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1671 |
int n; |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1672 |
char *cp; |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1673 |
int _flags = 0; |
1009 | 1674 |
int offs, nBytes, sIdx; |
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1675 |
unsigned long norder; |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1676 |
|
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1677 |
_flags = __longIntVal(flags); |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1678 |
|
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1679 |
if ((fp != nil) |
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1680 |
&& __isSmallInteger(startIndex) |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1681 |
&& __isSmallInteger(count)) { |
1343 | 1682 |
sock = fileno(__FILEVal(fp)); |
1683 |
||
1684 |
oClass = __Class(aDataBuffer); |
|
1685 |
sIdx = __intVal(startIndex) - 1; |
|
1686 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
1687 |
case BYTEARRAY: |
|
1688 |
offs = sIdx; |
|
1689 |
break; |
|
1690 |
case WORDARRAY: |
|
1691 |
case SWORDARRAY: |
|
1692 |
offs = sIdx * 2; |
|
1693 |
break; |
|
1694 |
case LONGARRAY: |
|
1695 |
case SLONGARRAY: |
|
1696 |
offs = sIdx * 4; |
|
1697 |
break; |
|
1698 |
case LONGLONGARRAY: |
|
1699 |
case SLONGLONGARRAY: |
|
1700 |
offs = sIdx * 8; |
|
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1701 |
# ifdef __NEED_LONGLONG_ALIGN |
1343 | 1702 |
offs += 4; |
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1703 |
# endif |
1343 | 1704 |
break; |
1705 |
case FLOATARRAY: |
|
1706 |
offs = sIdx * sizeof(float); |
|
1707 |
break; |
|
1708 |
case DOUBLEARRAY: |
|
1709 |
offs = sIdx * sizeof(double); |
|
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1710 |
# ifdef __NEED_DOUBLE_ALIGN |
1343 | 1711 |
offs += 4; |
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1712 |
# endif |
1343 | 1713 |
break; |
1714 |
default: |
|
1715 |
goto bad; |
|
1716 |
} |
|
1717 |
nBytes = __intVal(count); |
|
1718 |
||
1719 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
1720 |
nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ); |
|
1721 |
objSize = __qSize(aDataBuffer) - nInstBytes; |
|
1722 |
cp = (char *)__InstPtr(aDataBuffer) + nInstBytes; |
|
1723 |
cp += offs; |
|
1724 |
if ((offs + nBytes) > objSize) { |
|
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1725 |
# ifdef DGRAM_DEBUG |
1343 | 1726 |
printf("cut off ...\n"); |
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1727 |
# endif |
1343 | 1728 |
nBytes = objSize - offs; |
1729 |
} |
|
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1730 |
|
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1731 |
# ifdef DGRAM_DEBUG |
1343 | 1732 |
printf("sending %d bytes ...\n", nBytes); |
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1733 |
# endif |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1734 |
|
1343 | 1735 |
__BEGIN_INTERRUPTABLE__ |
1736 |
do { |
|
1737 |
n = send(sock, cp, nBytes, _flags); |
|
1738 |
} while ((n < 0) && (errno == EINTR)); |
|
1739 |
__END_INTERRUPTABLE__ |
|
1740 |
||
1741 |
if (n < 0) { |
|
1742 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1743 |
} |
|
1744 |
RETURN (__MKSMALLINT(n)); |
|
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1745 |
} |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1746 |
#endif |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1747 |
bad: ; |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1748 |
%}. |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1749 |
" |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1750 |
arrive here if you try to send from an invalid buffer (i.e. not ByteArray-like), |
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1751 |
" |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1752 |
self primitiveFailed |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1753 |
! |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
1754 |
|
126 | 1755 |
sendTo:anAddressBuffer buffer:buffer |
1756 |
"send datagramm data - fetch address of destination host from |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1757 |
anAddressBuffer, data from aDataBuffer. |
126 | 1758 |
Both must be ByteArray-like. The bytes in the addressBuffer must |
1759 |
be a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
1760 |
Return the number of bytes transmitted, or a negative number on error. |
|
1761 |
On error, the unix error code is left in the lastErrorNumber |
|
1762 |
instance variable. |
|
1763 |
Flags is currently ignored; it is there for ST-80 compatibility." |
|
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1764 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1765 |
^ self sendTo:anAddressBuffer buffer:buffer start:1 for:buffer size flags:0 |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1766 |
! |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1767 |
|
817 | 1768 |
sendTo:anAddressBuffer buffer:buffer start:startIndex for:count |
1769 |
"send datagramm data - fetch address of destination host from |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1770 |
anAddressBuffer, data from aDataBuffer. |
817 | 1771 |
Both must be ByteArray-like. The bytes in the addressBuffer must |
1772 |
be a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
1773 |
Return the number of bytes transmitted, or a negative number on error. |
|
1774 |
On error, the unix error code is left in the lastErrorNumber |
|
1775 |
instance variable. |
|
1776 |
Flags is currently ignored; it is there for ST-80 compatibility." |
|
1777 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1778 |
^ self sendTo:anAddressBuffer buffer:buffer start:startIndex for:count flags:0 |
817 | 1779 |
! |
1780 |
||
126 | 1781 |
sendTo:anAddressBuffer buffer:aDataBuffer start:startIndex for:count flags:flags |
1782 |
"send datagramm data - fetch address of destination host from |
|
1783 |
anAddressBuffer, data from aDataBuffer starting at startIndex, |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1784 |
sending count bytes. |
126 | 1785 |
Both must be ByteArray-like. The bytes in the addressBuffer must |
1786 |
be a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
1787 |
Return the number of bytes transmitted, or a negative number on error. |
|
1788 |
On error, the unix error code is left in the lastErrorNumber |
|
1789 |
instance variable." |
|
0 | 1790 |
|
1282 | 1791 |
|domainClass addr| |
1792 |
||
1793 |
domainClass := self class socketAddressClassForDomain:domain. |
|
1794 |
domainClass isNil ifTrue:[ |
|
1343 | 1795 |
^ self error:'invalid (unsupported) domain'. |
1282 | 1796 |
]. |
1797 |
||
1798 |
(anAddressBuffer isKindOf:SocketAddress) ifTrue:[ |
|
1343 | 1799 |
addr := anAddressBuffer. |
1282 | 1800 |
] ifFalse:[ |
1343 | 1801 |
anAddressBuffer isByteArray ifFalse:[ |
1802 |
^ self error:'bad socketAddress argument' |
|
1803 |
]. |
|
1804 |
addr := domainClass hostAddress:anAddressBuffer. |
|
28 | 1805 |
]. |
0 | 1806 |
%{ |
418 | 1807 |
#ifndef NO_SOCKET |
126 | 1808 |
OBJ oClass; |
217 | 1809 |
OBJ fp = __INST(filePointer); |
126 | 1810 |
int nInstVars, nInstBytes, objSize; |
1282 | 1811 |
struct sockaddr *sockaddr_ptr; |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1812 |
int sockAddrOffs, sockaddr_size; |
126 | 1813 |
int sock; |
1814 |
int n; |
|
1815 |
char *cp; |
|
1816 |
int _flags = 0; |
|
1817 |
int offs, nBytes; |
|
1818 |
unsigned long norder; |
|
21 | 1819 |
|
126 | 1820 |
_flags = __longIntVal(flags); |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1821 |
|
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1822 |
if ((fp != nil) |
126 | 1823 |
&& __isSmallInteger(startIndex) |
1824 |
&& __isSmallInteger(count)) { |
|
1343 | 1825 |
sock = fileno(__FILEVal(fp)); |
1826 |
||
1827 |
if (! __isBytes(addr)) { |
|
1828 |
sockaddr_size = 0; |
|
1829 |
sockaddr_ptr = (struct sockaddr *)0; |
|
1830 |
} else { |
|
1831 |
int nIndex; |
|
1832 |
OBJ cls; |
|
1833 |
||
1834 |
sockAddrOffs = 0; |
|
1835 |
if ((cls = __qClass(addr)) != @global(ByteArray)) |
|
1836 |
sockAddrOffs += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1837 |
nIndex = __qSize(addr) - OHDR_SIZE; |
|
1838 |
sockaddr_size = nIndex - sockAddrOffs; |
|
1839 |
sockaddr_ptr = (struct sockaddr *)(__byteArrayVal(addr) + sockAddrOffs); |
|
1840 |
} |
|
1841 |
||
1842 |
oClass = __Class(aDataBuffer); |
|
1843 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
1844 |
case BYTEARRAY: |
|
1845 |
offs = __intVal(startIndex) - 1; |
|
1846 |
break; |
|
1847 |
case WORDARRAY: |
|
1848 |
case SWORDARRAY: |
|
1849 |
offs = (__intVal(startIndex) - 1) * 2; |
|
1850 |
break; |
|
1851 |
case LONGARRAY: |
|
1852 |
case SLONGARRAY: |
|
1853 |
offs = (__intVal(startIndex) - 1) * 4; |
|
1854 |
break; |
|
1855 |
case LONGLONGARRAY: |
|
1856 |
case SLONGLONGARRAY: |
|
1857 |
offs = (__intVal(startIndex) - 1) * 8; |
|
1282 | 1858 |
# ifdef __NEED_LONGLONG_ALIGN |
1343 | 1859 |
offs += 4; |
869 | 1860 |
# endif |
1343 | 1861 |
break; |
1862 |
case FLOATARRAY: |
|
1863 |
offs = (__intVal(startIndex) - 1) * sizeof(float); |
|
1864 |
break; |
|
1865 |
case DOUBLEARRAY: |
|
1866 |
offs = (__intVal(startIndex) - 1) * sizeof(double); |
|
790 | 1867 |
# ifdef __NEED_DOUBLE_ALIGN |
1343 | 1868 |
offs += 4; |
790 | 1869 |
# endif |
1343 | 1870 |
break; |
1871 |
default: |
|
1872 |
goto bad; |
|
1873 |
} |
|
1874 |
nBytes = __intVal(count); |
|
1875 |
||
1876 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
1877 |
nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ); |
|
1878 |
objSize = __qSize(aDataBuffer) - nInstBytes; |
|
1879 |
if ((offs + nBytes) > objSize) { |
|
790 | 1880 |
# ifdef DGRAM_DEBUG |
1343 | 1881 |
printf("cut off ...\n"); |
790 | 1882 |
# endif |
1343 | 1883 |
nBytes = objSize - offs; |
1884 |
} |
|
1885 |
||
1886 |
__BEGIN_INTERRUPTABLE__ |
|
1887 |
do { |
|
1888 |
sockaddr_ptr = (struct sockaddr *)(__byteArrayVal(addr) + sockAddrOffs); |
|
1889 |
cp = (char *)__InstPtr(aDataBuffer) + nInstBytes + offs; |
|
1890 |
n = sendto(sock, cp, nBytes, _flags, sockaddr_ptr, sockaddr_size); |
|
1891 |
} while ((n < 0) && (errno == EINTR)); |
|
1892 |
__END_INTERRUPTABLE__ |
|
1893 |
||
1894 |
if (n < 0) { |
|
1895 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1896 |
} |
|
1897 |
RETURN (__MKSMALLINT(n)); |
|
0 | 1898 |
} |
418 | 1899 |
#endif |
126 | 1900 |
bad: ; |
42 | 1901 |
%}. |
1902 |
" |
|
126 | 1903 |
arrive here if you try to send from an invalid buffer |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1904 |
(i.e. not ByteArray-like), |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1905 |
or if the addressBuffer is nonNil AND not a ByteArray/String |
126 | 1906 |
or if the addressBuffer is nonNil AND too small. |
42 | 1907 |
" |
126 | 1908 |
self primitiveFailed |
0 | 1909 |
! ! |
1910 |
||
1911 |
!Socket methodsFor:'low level'! |
|
1912 |
||
507 | 1913 |
bindAnonymously |
1914 |
^ self |
|
1915 |
bindTo:0 |
|
1916 |
address:nil |
|
509 | 1917 |
reuseAddress:false |
1918 |
! |
|
1919 |
||
1920 |
bindAnonymouslyToAddress:addressString |
|
1921 |
^ self |
|
1922 |
bindTo:0 |
|
1923 |
address:addressString |
|
1924 |
reuseAddress:false |
|
507 | 1925 |
! |
1926 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1927 |
bindTo:aSocketAddress |
425 | 1928 |
"ST80 compatible bind, expecting a socketAddress argument. |
1929 |
The socketAddress object (an instance of SocketAddress) |
|
1930 |
is supposed to respond to #portOrName and #address requests." |
|
71 | 1931 |
|
1932 |
^ self bindTo:(aSocketAddress portOrName) |
|
1933 |
address:(aSocketAddress address) |
|
425 | 1934 |
reuseAddress:true |
71 | 1935 |
! |
1936 |
||
425 | 1937 |
bindTo:portNrOrNameString address:addressString |
28 | 1938 |
"low level bind - returns true if ok, false otherwise. |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1939 |
Currently only non-address binding is supported; |
425 | 1940 |
i.e. the address must always be nil. |
71 | 1941 |
|
1942 |
The interpretation of portNrOrName depends on the domain: |
|
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1943 |
inet domain uses (4byte) byteArray like internet numbers, |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1944 |
unix domain uses pathname strings, |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1945 |
others use whatever will come up in the future |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1946 |
" |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1947 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1948 |
^ self |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1949 |
bindTo:portNrOrNameString |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1950 |
address:addressString |
425 | 1951 |
reuseAddress:true |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1952 |
! |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1953 |
|
1282 | 1954 |
bindTo:portNrOrNameOrNil address:hostOrPathNameOrSocketAddrOrNil reuseAddress:reuse |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1955 |
"low level bind - returns true if ok, false otherwise. |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1956 |
Currently only non-address binding is supported; |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1957 |
i.e. address must always be nil. |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1958 |
|
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1959 |
The interpretation of portNrOrName depends on the domain: |
1343 | 1960 |
inet domain uses (4byte) byteArray like internet numbers, |
1961 |
unix domain uses pathname strings, |
|
1962 |
others use whatever will come up in the future |
|
425 | 1963 |
|
1964 |
The reuse boolean argument controls if the SO_REUSEADDR socket option |
|
1965 |
is to be set (to avoid the 'bind: address in use' error). |
|
1966 |
" |
|
28 | 1967 |
|
1282 | 1968 |
|ok addr addrName domainClass| |
1969 |
||
28 | 1970 |
filePointer isNil ifTrue:[ |
1343 | 1971 |
^ self errorNotOpen |
1282 | 1972 |
]. |
1973 |
||
1974 |
domainClass := self class socketAddressClassForDomain:domain. |
|
1975 |
domainClass isNil ifTrue:[ |
|
1343 | 1976 |
^ self error:'invalid (unsupported) domain'. |
28 | 1977 |
]. |
1282 | 1978 |
|
1979 |
"/ backward compatibility: support for byteArray and string arg |
|
1980 |
||
1981 |
hostOrPathNameOrSocketAddrOrNil isNil ifTrue:[ |
|
1343 | 1982 |
addr := domainClass anyHost. |
1282 | 1983 |
] ifFalse:[ |
1343 | 1984 |
hostOrPathNameOrSocketAddrOrNil isString ifTrue:[ |
1985 |
addr := domainClass hostName:hostOrPathNameOrSocketAddrOrNil. |
|
1986 |
addrName := hostOrPathNameOrSocketAddrOrNil. |
|
1987 |
] ifFalse:[ |
|
1988 |
(hostOrPathNameOrSocketAddrOrNil isKindOf:SocketAddress) ifTrue:[ |
|
1989 |
addr := hostOrPathNameOrSocketAddrOrNil. |
|
1990 |
] ifFalse:[ |
|
1991 |
hostOrPathNameOrSocketAddrOrNil isByteArray ifFalse:[ |
|
1992 |
^ self error:'bad host (socketAddress) argument' |
|
1993 |
]. |
|
1994 |
addr := domainClass hostAddress:hostOrPathNameOrSocketAddrOrNil. |
|
1995 |
]. |
|
1996 |
]. |
|
1282 | 1997 |
]. |
1998 |
portNrOrNameOrNil notNil ifTrue:[ |
|
1343 | 1999 |
addr port:portNrOrNameOrNil. |
1282 | 2000 |
]. |
2001 |
||
1120 | 2002 |
%{ /* STACK: 100000 */ |
418 | 2003 |
#ifndef NO_SOCKET |
217 | 2004 |
OBJ t = __INST(filePointer); |
71 | 2005 |
OBJ myDomain; |
28 | 2006 |
int sock; |
819 | 2007 |
union sockaddr_u sa; |
1282 | 2008 |
struct sockaddr *sockaddr_ptr; |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2009 |
int sockaddr_size; |
6 | 2010 |
int ret; |
25 | 2011 |
int on = 1; |
1282 | 2012 |
int sockAddrOffs; |
2013 |
||
2014 |
if (! __isBytes(addr)) { |
|
1343 | 2015 |
addr = nil; |
2016 |
goto getOutOfHere; |
|
530
067797c15b77
added support for 6-byte internet addresses (untested)
Claus Gittinger <cg@exept.de>
parents:
529
diff
changeset
|
2017 |
} |
1282 | 2018 |
|
2019 |
{ |
|
1343 | 2020 |
int nIndex; |
2021 |
OBJ cls; |
|
2022 |
||
2023 |
sockAddrOffs = 0; |
|
2024 |
if ((cls = __qClass(addr)) != @global(ByteArray)) |
|
2025 |
sockAddrOffs += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
2026 |
nIndex = __qSize(addr) - OHDR_SIZE; |
|
2027 |
sockaddr_size = nIndex - sockAddrOffs; |
|
2028 |
sockaddr_ptr = (struct sockaddr *)(__byteArrayVal(addr) + sockAddrOffs); |
|
28 | 2029 |
} |
2030 |
||
110
7af19ae8693b
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
106
diff
changeset
|
2031 |
sock = fileno(__FILEVal(t)); |
28 | 2032 |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2033 |
# ifdef SO_REUSEADDR |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2034 |
if (reuse == true) { |
1343 | 2035 |
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0) { |
2036 |
DBGPRINTF(("SOCKET: setsockopt - SO_REUSEADDR failed\n")); |
|
2037 |
} |
|
28 | 2038 |
} |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2039 |
# endif /* SO_REUSEADDR */ |
28 | 2040 |
|
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
2041 |
__BEGIN_INTERRUPTABLE__ |
28 | 2042 |
do { |
1343 | 2043 |
sockaddr_ptr = (struct sockaddr *)(__byteArrayVal(addr) + sockAddrOffs); |
2044 |
ret = bind(sock, sockaddr_ptr, sockaddr_size); |
|
28 | 2045 |
} while ((ret < 0) && (errno == EINTR)); |
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
2046 |
__END_INTERRUPTABLE__ |
28 | 2047 |
|
2048 |
if (ret < 0) { |
|
1343 | 2049 |
DBGPRINTF(("SOCKET: bind failed errno=%d\n", errno)); |
2050 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2051 |
RETURN (false); |
|
1282 | 2052 |
} else { |
1343 | 2053 |
ok = true; |
2054 |
||
2055 |
if (! __isSmallInteger(portNrOrNameOrNil) |
|
2056 |
|| (portNrOrNameOrNil == __MKSMALLINT(0))) { |
|
2057 |
int p; |
|
2058 |
||
2059 |
/* |
|
2060 |
* anonymous port - get the actual portNr |
|
2061 |
*/ |
|
2062 |
if (getsockname(sock, sockaddr_ptr, &sockaddr_size) < 0) { |
|
2063 |
DBGPRINTF(("SOCKET: cannot get peername\n")); |
|
2064 |
} |
|
2065 |
} |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2066 |
} |
1282 | 2067 |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2068 |
#endif /* NO_SOCKET */ |
1282 | 2069 |
|
2070 |
getOutOfHere: ; |
|
42 | 2071 |
%}. |
1282 | 2072 |
ok ~~ true ifTrue:[ |
1343 | 2073 |
^ false |
1282 | 2074 |
]. |
2075 |
||
2076 |
peer := addr. |
|
2077 |
port := addr port. |
|
2078 |
peerName := addrName. |
|
2079 |
||
28 | 2080 |
^ true |
0 | 2081 |
|
28 | 2082 |
" |
2083 |
(Socket domain:#inet type:#stream) |
|
1343 | 2084 |
bindTo:9999 |
2085 |
address:nil |
|
28 | 2086 |
" |
1474 | 2087 |
! ! |
2088 |
||
2089 |
!Socket protectedMethodsFor:'low level'! |
|
0 | 2090 |
|
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2091 |
closeFile |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2092 |
"low level close" |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2093 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2094 |
%{ /* NOCONTEXT */ |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2095 |
#ifndef NO_SOCKET |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2096 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2097 |
OBJ t; |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2098 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2099 |
t = __INST(filePointer); |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2100 |
if (t != nil) { |
812 | 2101 |
FILE *fp; |
2102 |
||
2103 |
fp = __FILEVal(t); |
|
821
40b0b69bf4b5
more debugs; fixed at-addresses
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
2104 |
DBGFPRINTF((stderr, "socket fclose %x (%d)\n", fp, fileno(fp))); |
812 | 2105 |
fflush(fp); |
2106 |
/* shutdown(fileno(fp), 2); */ |
|
1035 | 2107 |
if (@global(FileOpenTrace) == true) { |
876 | 2108 |
fprintf(stderr, "fclose [Socket] %x\n", fp); |
2109 |
} |
|
812 | 2110 |
fclose(fp); |
2111 |
__INST(filePointer) = nil; |
|
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2112 |
} |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2113 |
#endif |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2114 |
%} |
1474 | 2115 |
! ! |
2116 |
||
2117 |
!Socket methodsFor:'low level'! |
|
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2118 |
|
896
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2119 |
getSocketError |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2120 |
"get the SO_ERROR form the socket, which indicates the |
896
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2121 |
result of an asynchronous operation" |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2122 |
|
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2123 |
%{ |
1282 | 2124 |
#ifndef NO_SOCKET |
896
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2125 |
OBJ t; |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2126 |
int err; |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2127 |
|
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2128 |
t = __INST(filePointer); |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2129 |
if (t == nil) |
1343 | 2130 |
err = EBADF; |
896
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2131 |
else { |
1343 | 2132 |
int sz, fd; |
2133 |
||
2134 |
fd = fileno(__FILEVal(t)); |
|
2135 |
sz = sizeof(err); |
|
2136 |
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) |
|
2137 |
err = errno; |
|
896
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2138 |
} |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2139 |
|
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2140 |
RETURN(__MKSMALLINT(err)); |
1282 | 2141 |
#endif |
896
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2142 |
%} |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2143 |
! |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2144 |
|
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2145 |
listenFor:aNumber |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2146 |
"same as listenWithBacklog: - for ST-80 compatibility" |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2147 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2148 |
^ self listenWithBacklog:aNumber |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2149 |
! |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2150 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2151 |
listenWithBacklog:aNumber |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2152 |
"start listening; return true if ok, false on error" |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2153 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2154 |
filePointer isNil ifTrue:[ |
789 | 2155 |
^ self errorNotOpen |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2156 |
]. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2157 |
%{ |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2158 |
#ifndef NO_SOCKET |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2159 |
OBJ fp = __INST(filePointer); |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2160 |
int sock; |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2161 |
int ret; |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2162 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2163 |
if (! __isSmallInteger(aNumber)) { |
789 | 2164 |
DBGPRINTF(("SOCKET: invalid arg\n")); |
2165 |
RETURN (false); |
|
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2166 |
} |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2167 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2168 |
sock = fileno(__FILEVal(fp)); |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2169 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2170 |
__BEGIN_INTERRUPTABLE__ |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2171 |
do { |
1009 | 2172 |
ret = listen(sock, __intVal(aNumber)); |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2173 |
} while ((ret < 0) && (errno == EINTR)); |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2174 |
__END_INTERRUPTABLE__ |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2175 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2176 |
if (ret < 0) { |
789 | 2177 |
DBGPRINTF(("SOCKET: listen call failed errno=%d\n", errno)); |
2178 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2179 |
RETURN (false); |
|
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2180 |
} |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2181 |
#else |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2182 |
RETURN (false); |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2183 |
#endif |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2184 |
%}. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2185 |
^ true |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2186 |
! |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2187 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2188 |
shutDown |
1339 | 2189 |
"shutDown and close the socket. |
2190 |
This does not wait for the data to be delivered." |
|
2191 |
||
2192 |
self shutdown:2. |
|
2193 |
self close |
|
2194 |
! |
|
2195 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2196 |
shutdown: howNum |
1339 | 2197 |
"shutDown the socket - inform it that no more I/O will be performed. |
1343 | 2198 |
0 - read side (no further reads) |
2199 |
1 - write side (no further writes) |
|
2200 |
2 - both (no further I/O at all) |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2201 |
shutDown:2 |
1343 | 2202 |
discards any pending data (as opposed to close, which waits until data is delivered)" |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2203 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2204 |
filePointer isNil ifTrue:[^ self]. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2205 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2206 |
%{ |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2207 |
#ifndef NO_SOCKET |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2208 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2209 |
OBJ fp; |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2210 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2211 |
fp = __INST(filePointer); |
1339 | 2212 |
if ((fp != nil) && __isSmallInteger(howNum)) { |
1343 | 2213 |
FILE *f; |
2214 |
int fd; |
|
2215 |
||
2216 |
__INST(filePointer) = nil; |
|
2217 |
f = __FILEVal(fp); |
|
2218 |
fd = fileno(f); |
|
2219 |
__BEGIN_INTERRUPTABLE__ |
|
2220 |
shutdown(fd, __intVal(howNum)); |
|
2221 |
DBGFPRINTF((stderr, "socket shutDown %x (%d)\n", fp, fd)); |
|
2222 |
__END_INTERRUPTABLE__ |
|
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2223 |
} |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2224 |
#endif |
1339 | 2225 |
%}. |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2226 |
! ! |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2227 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2228 |
!Socket methodsFor:'low level-accepting'! |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2229 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2230 |
accept |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2231 |
"create a new TCP socket from accepting on the receiver. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2232 |
This method will suspend the current process if no connection is waiting. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2233 |
For ST-80 compatibility" |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2234 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2235 |
|newSock| |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2236 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2237 |
newSock := self class new. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2238 |
(newSock acceptOn:self) ifFalse:[^ nil]. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2239 |
^ newSock |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2240 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2241 |
" |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2242 |
|sock newSock| |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2243 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2244 |
sock := Socket provide:8004. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2245 |
sock listenFor:5. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2246 |
newSock := sock accept. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2247 |
" |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2248 |
! |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2249 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2250 |
acceptOn:aSocket |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2251 |
"accept a connection on a server port (created with:'Socket>>onIPPort:') |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2252 |
usage is: (Socket basicNew acceptOn:(Socket onIPPort:9999)). |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2253 |
This method will suspend the current process if no connection is waiting. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2254 |
Return the true if ok; false if not." |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2255 |
|
896
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2256 |
aSocket readWait. |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2257 |
^ self primAcceptOn:aSocket |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2258 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2259 |
"Modified: / 11.3.1996 / 14:21:31 / stefan" |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2260 |
"Modified: / 1.8.1998 / 23:39:10 / cg" |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2261 |
! |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2262 |
|
896
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2263 |
primAcceptOn:aSocket |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2264 |
"accept a connection on a server port (created with:'Socket>>onIPPort:') |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2265 |
usage is: (Socket basicNew acceptOn:(Socket onIPPort:9999)). |
896
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2266 |
Return the true if ok; false if not." |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2267 |
|
1282 | 2268 |
|serverSocketFd addr addrLen domainClass| |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2269 |
|
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2270 |
filePointer notNil ifTrue:[ |
1343 | 2271 |
^ self errorAlreadyOpen |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2272 |
]. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2273 |
|
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2274 |
domain := aSocket domain. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2275 |
socketType := aSocket type. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2276 |
serverSocketFd := aSocket fileDescriptor. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2277 |
serverSocketFd isNil ifTrue:[ |
1343 | 2278 |
^ self error:'invalid server socket' |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2279 |
]. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2280 |
(serverSocketFd isMemberOf:SmallInteger) ifFalse:[ |
1343 | 2281 |
^ self error:'invalid server socket' |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2282 |
]. |
1282 | 2283 |
|
2284 |
domainClass := self class socketAddressClassForDomain:domain. |
|
2285 |
domainClass isNil ifTrue:[ |
|
1343 | 2286 |
^ self error:'invalid (unsupported) domain'. |
1282 | 2287 |
]. |
2288 |
addrLen := domainClass socketAddressSize. |
|
2289 |
addr := domainClass new. |
|
2290 |
||
1120 | 2291 |
%{ /* STACK: 100000 */ |
418 | 2292 |
#ifndef NO_SOCKET |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2293 |
FILE *fp; |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2294 |
int flags; |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2295 |
int sock, newSock; |
819 | 2296 |
union sockaddr_u sa; |
464 | 2297 |
int alen, alen0; |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2298 |
struct hostent *he ; |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2299 |
char dotted[20] ; |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2300 |
|
1282 | 2301 |
if (!__isSmallInteger(addrLen)) { |
1343 | 2302 |
DBGPRINTF(("SOCKET: bad addrLen\n")); |
2303 |
RETURN (false); |
|
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
2304 |
} |
1282 | 2305 |
alen0 = __intVal(addrLen); |
896
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2306 |
|
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2307 |
sock = __intVal(serverSocketFd); |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2308 |
|
1338
f81ec8de617f
Use fcntl instead of ioctl when setting O_NONBLOCK
Stefan Vogel <sv@exept.de>
parents:
1336
diff
changeset
|
2309 |
# if defined(O_NONBLOCK) && defined(SET_NDELAY) |
f81ec8de617f
Use fcntl instead of ioctl when setting O_NONBLOCK
Stefan Vogel <sv@exept.de>
parents:
1336
diff
changeset
|
2310 |
flags = fcntl(sock, F_GETFL); |
f81ec8de617f
Use fcntl instead of ioctl when setting O_NONBLOCK
Stefan Vogel <sv@exept.de>
parents:
1336
diff
changeset
|
2311 |
fcntl(sock, F_SETFL, flags | O_NONBLOCK); |
896
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2312 |
# endif |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2313 |
|
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2314 |
__BEGIN_INTERRUPTABLE__ |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2315 |
do { |
1343 | 2316 |
alen = alen0; |
2317 |
newSock = accept(sock, (struct sockaddr *) &sa, &alen); |
|
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2318 |
} while ((newSock < 0) && (errno == EINTR)); |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2319 |
__END_INTERRUPTABLE__ |
821
40b0b69bf4b5
more debugs; fixed at-addresses
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
2320 |
DBGFPRINTF((stderr, "socket accept newSock=%d\n", newSock)); |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2321 |
|
876 | 2322 |
# if defined(O_NDELAY) && defined(SET_NDELAY) |
1338
f81ec8de617f
Use fcntl instead of ioctl when setting O_NONBLOCK
Stefan Vogel <sv@exept.de>
parents:
1336
diff
changeset
|
2323 |
fcntl(sock, F_SETFL, flags); |
876 | 2324 |
# endif |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2325 |
|
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2326 |
if (newSock < 0) { |
1343 | 2327 |
DBGPRINTF(("SOCKET: accept call failed errno=%d\n", errno)); |
2328 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2329 |
RETURN (false); |
|
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2330 |
} |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2331 |
|
1282 | 2332 |
if (__isNonNilObject(addr)) { |
1343 | 2333 |
OBJ oClass; |
2334 |
int nInstVars, nInstBytes, objSize; |
|
2335 |
char *cp; |
|
2336 |
||
2337 |
oClass = __qClass(addr); |
|
2338 |
if (! __isBytes(addr) ) { |
|
2339 |
DBGPRINTF(("SOCKET: bad addr\n")); |
|
2340 |
close(newSock); |
|
2341 |
RETURN (false); |
|
2342 |
} |
|
2343 |
||
2344 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
2345 |
nInstBytes = OHDR_SIZE + (nInstVars * sizeof(OBJ)); |
|
2346 |
objSize = __qSize(addr) - nInstBytes; |
|
2347 |
cp = (char *)__InstPtr(addr) + nInstBytes; |
|
2348 |
if (objSize < alen) { |
|
2349 |
DBGPRINTF(("SOCKET: bad addr\n")); |
|
2350 |
close(newSock); |
|
2351 |
RETURN (false); |
|
2352 |
} |
|
2353 |
||
2354 |
/* |
|
2355 |
* extract the partners address |
|
2356 |
*/ |
|
2357 |
bcopy((char *)&sa, cp, alen); |
|
2358 |
addrLen = __MKSMALLINT(alen); |
|
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2359 |
} |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2360 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2361 |
/* |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2362 |
* make it a FILE * |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2363 |
*/ |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2364 |
fp = fdopen(newSock, "r+"); |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2365 |
if (! fp) { |
1343 | 2366 |
DBGPRINTF(("SOCKET: fdopen call failed\n")); |
2367 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2368 |
close(newSock); |
|
2369 |
DBGFPRINTF((stderr, "socket close (fdopen failed) (%d)\n", newSock)); |
|
2370 |
RETURN (false); |
|
876 | 2371 |
} |
1035 | 2372 |
if (@global(FileOpenTrace) == true) { |
1343 | 2373 |
fprintf(stderr, "fdopen [Socket] -> %x\n", fp); |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2374 |
} |
876 | 2375 |
|
2376 |
# ifdef BUGGY_STDIO_LIB |
|
2377 |
setbuf(fp, NULL); |
|
2378 |
__INST(buffered) = false; |
|
2379 |
# endif |
|
2380 |
||
1486 | 2381 |
# if 0 |
2382 |
// The original code was: |
|
2383 |
__INST(filePointer) = __MKOBJ((INT)fp); __STORESELF(filePointer); |
|
2384 |
// but for that, gcc generates wrong code, which loads self (volatile) into |
|
2385 |
// a register (bp), then calls __MKOBJ, then stores indirect bp. |
|
2386 |
// That is wrong if a scavenge occurs in MKOBJ, as bp is now still pointing to the old |
|
2387 |
// object. |
|
2388 |
# endif |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2389 |
{ |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2390 |
OBJ t; |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2391 |
|
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2392 |
t = __MKOBJ(fp); |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2393 |
__INST(filePointer) = t; |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2394 |
__STORE(self, t); |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2395 |
} |
876 | 2396 |
#endif /* not NO_SOCKET */ |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2397 |
%}. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2398 |
mode := #readwrite. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2399 |
Lobby register:self. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2400 |
binary := false. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2401 |
port := aSocket port. |
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
2402 |
|
1282 | 2403 |
addr notNil ifTrue:[ |
1343 | 2404 |
peer := addr. |
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
2405 |
]. |
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
2406 |
|
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2407 |
^ true |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2408 |
! |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
2409 |
|
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2410 |
waitForNewConnectionOrDataOnAny:otherConnections timeout:timeoutSeconds |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2411 |
"suspend the current process, until either a new connection comes |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2412 |
in at the receiver, or data arrives on any of the otherConnections. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2413 |
For a new connection, an accept is performed and the new socket is returned. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2414 |
For an old connection, that socket is returned. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2415 |
In any case, the caller gets a socket to operate on as return value, |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2416 |
or nil, if a timeout occured. |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2417 |
This method implements the inner wait-primitive of a multi-connection |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2418 |
server application." |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2419 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2420 |
"/ first, a quick check if data is already available |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2421 |
|
1035 | 2422 |
|wasBlocked sema| |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2423 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2424 |
self canReadWithoutBlocking ifTrue:[ |
1170 | 2425 |
^ self accept. |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2426 |
]. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2427 |
otherConnections do:[:aConnection | |
1170 | 2428 |
aConnection canReadWithoutBlocking ifTrue:[ |
2429 |
^ aConnection |
|
2430 |
] |
|
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2431 |
]. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2432 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2433 |
[ |
1170 | 2434 |
"/ check again - prevent incoming interrupts from disturbing our setup |
2435 |
wasBlocked := OperatingSystem blockInterrupts. |
|
2436 |
||
2437 |
self canReadWithoutBlocking ifTrue:[ |
|
2438 |
^ self accept. |
|
2439 |
]. |
|
2440 |
otherConnections do:[:aConnection | |
|
2441 |
aConnection canReadWithoutBlocking ifTrue:[ |
|
2442 |
^ aConnection |
|
2443 |
] |
|
2444 |
]. |
|
2445 |
||
2446 |
"/ nope - must wait. |
|
2447 |
||
2448 |
sema := Semaphore new name:'multiReadWait'. |
|
2449 |
otherConnections do:[:aConnection | |
|
2450 |
Processor signal:sema onInput:(aConnection fileDescriptor). |
|
2451 |
]. |
|
2452 |
Processor signal:sema onInput:(self fileDescriptor). |
|
2453 |
timeoutSeconds notNil ifTrue:[ |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2454 |
Processor signal:sema afterSeconds:timeoutSeconds |
1170 | 2455 |
]. |
2456 |
Processor activeProcess state:#ioWait. |
|
2457 |
sema wait. |
|
1136 | 2458 |
] ensure:[ |
1170 | 2459 |
sema notNil ifTrue:[Processor disableSemaphore:sema]. |
2460 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2461 |
]. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2462 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2463 |
"/ see who it was ... |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2464 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2465 |
self canReadWithoutBlocking ifTrue:[ |
1170 | 2466 |
^ self accept. |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2467 |
]. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2468 |
otherConnections do:[:aConnection | |
1170 | 2469 |
aConnection canReadWithoutBlocking ifTrue:[ |
2470 |
^ aConnection |
|
2471 |
] |
|
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2472 |
]. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2473 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2474 |
"/ none - a timeout |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2475 |
^ nil |
867 | 2476 |
! |
2477 |
||
2478 |
waitForNewConnectionWithTimeout:timeoutSeconds |
|
2479 |
"suspend the current process, until a new connection comes |
|
2480 |
in at the receiver or a timeout occurs. |
|
2481 |
For a new connection, an accept is performed and the new socket is returned. |
|
2482 |
Returns nil, if a timeout occured. |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2483 |
This method implements the inner wait-primitive of a single-connection |
867 | 2484 |
server application." |
2485 |
||
2486 |
(self readWaitWithTimeout:timeoutSeconds) ifTrue:[ |
|
2487 |
"/ a timeout occurred - no connection within timeout |
|
2488 |
^ nil |
|
2489 |
]. |
|
2490 |
^ self accept. |
|
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2491 |
! ! |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2492 |
|
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
2493 |
!Socket methodsFor:'low level-connecting'! |
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
2494 |
|
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2495 |
connectTo:hostOrPathName port:portNrOrName |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2496 |
"low level connect; connect to port, portNrOrNameOrNil on host, hostName. |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2497 |
For backward compatibility, host may be also a string or a byteArray, |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2498 |
but it is recommended to pass socketAddress instances. |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2499 |
|
71 | 2500 |
Return true if ok, false otherwise. |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2501 |
The current process will block (but not the whole Smalltalk) until the connection is established. |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2502 |
See also: #connectTo:port:withTimeout: for a somewhat nicer interface." |
28 | 2503 |
|
896
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2504 |
^ self connectTo:hostOrPathName port:portNrOrName withTimeout:nil |
649 | 2505 |
! |
2506 |
||
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2507 |
connectTo:hostOrPathNameOrSocketAddr port:portNrOrNameOrNil withTimeout:timeout |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2508 |
"low level connect; connect to port, portNrOrNameOrNil on host, hostName. |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2509 |
For backward compatibility, host may be also a string or a byteArray, |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2510 |
but it is recommended to pass socketAddress instances. |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2511 |
|
649 | 2512 |
Return true if ok, false otherwise. |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2513 |
The current process will block (but not the whole Smalltalk) until the connection is established, |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2514 |
or timeout millliseconds have passed." |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2515 |
|
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2516 |
|isAsync err domainClass addr addrName| |
430
12d736316f69
Fix #connectTo:port: Do not call connect() when we get EINPROGRESS
Stefan Vogel <sv@exept.de>
parents:
429
diff
changeset
|
2517 |
|
28 | 2518 |
filePointer isNil ifTrue:[ |
1343 | 2519 |
^ self errorNotOpen |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2520 |
]. |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2521 |
|
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2522 |
domainClass := self class socketAddressClassForDomain:domain. |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2523 |
domainClass isNil ifTrue:[ |
1343 | 2524 |
^ self error:'invalid (unsupported) domain'. |
28 | 2525 |
]. |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2526 |
|
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2527 |
"/ backward compatibility: support for byteArray and string arg |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2528 |
|
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2529 |
hostOrPathNameOrSocketAddr isString ifTrue:[ |
1343 | 2530 |
addr := domainClass hostName:hostOrPathNameOrSocketAddr port:portNrOrNameOrNil. |
2531 |
addrName := hostOrPathNameOrSocketAddr. |
|
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2532 |
] ifFalse:[ |
1343 | 2533 |
hostOrPathNameOrSocketAddr isByteArray ifFalse:[ |
2534 |
^ self error:'bad host (socketAddress) argument' |
|
2535 |
]. |
|
2536 |
(hostOrPathNameOrSocketAddr isKindOf:SocketAddress) ifTrue:[ |
|
2537 |
addr := hostOrPathNameOrSocketAddr. |
|
2538 |
portNrOrNameOrNil notNil ifTrue:[ |
|
2539 |
addr port:portNrOrNameOrNil. |
|
2540 |
]. |
|
2541 |
] ifFalse:[ |
|
2542 |
addr := domainClass hostAddress:hostOrPathNameOrSocketAddr port:portNrOrNameOrNil. |
|
2543 |
]. |
|
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2544 |
]. |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2545 |
|
1120 | 2546 |
%{ /* STACK: 100000 */ |
649 | 2547 |
|
418 | 2548 |
#ifndef NO_SOCKET |
217 | 2549 |
OBJ t = __INST(filePointer); |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2550 |
struct sockaddr *sockaddr_ptr; |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2551 |
int a, sock; |
28 | 2552 |
FILE *fp; |
649 | 2553 |
int ret, oldFlags; |
28 | 2554 |
int on = 1; |
115 | 2555 |
int sockaddr_size; |
28 | 2556 |
|
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2557 |
if (!__isNonNilObject(addr) || !__isBytes(addr)) { |
1343 | 2558 |
DBGPRINTF(("SOCKET: invalid addrBytes\n")); |
2559 |
RETURN (false); |
|
530
067797c15b77
added support for 6-byte internet addresses (untested)
Claus Gittinger <cg@exept.de>
parents:
529
diff
changeset
|
2560 |
} |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2561 |
|
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2562 |
{ |
1343 | 2563 |
int indx, nIndex; |
2564 |
OBJ cls; |
|
2565 |
||
2566 |
indx = 0; |
|
2567 |
if ((cls = __qClass(addr)) != @global(ByteArray)) |
|
2568 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
2569 |
nIndex = __qSize(addr) - OHDR_SIZE; |
|
2570 |
sockaddr_size = nIndex - indx; |
|
2571 |
sockaddr_ptr = (struct sockaddr *)(__byteArrayVal(addr) + indx); |
|
28 | 2572 |
} |
2573 |
||
110
7af19ae8693b
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
106
diff
changeset
|
2574 |
sock = fileno(__FILEVal(t)); |
28 | 2575 |
|
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2576 |
# if defined(O_NONBLOCK) |
896
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2577 |
/* |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2578 |
* set to non-blocking and wait later |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2579 |
*/ |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2580 |
oldFlags = fcntl(sock, F_GETFL, 0); |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2581 |
/* on SUNOS4.x, would use fcntl(osfd, F_SETFL, flags | FNDELAY); */ |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2582 |
fcntl(sock, F_SETFL, oldFlags | O_NONBLOCK); |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2583 |
# endif |
649 | 2584 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2585 |
/* |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2586 |
* connect |
28 | 2587 |
*/ |
81 | 2588 |
__BEGIN_INTERRUPTABLE__ |
28 | 2589 |
do { |
1343 | 2590 |
ret = connect(sock, sockaddr_ptr, sockaddr_size); |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2591 |
} while ((ret < 0) |
1343 | 2592 |
&& ((errno == EINTR) |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2593 |
# ifdef EAGAIN |
1343 | 2594 |
|| (errno == EAGAIN) |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2595 |
# endif |
1343 | 2596 |
)); |
81 | 2597 |
__END_INTERRUPTABLE__ |
28 | 2598 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2599 |
if (ret < 0) { |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2600 |
# if defined(EINPROGRESS) || defined(EALREADY) |
1343 | 2601 |
if (0 |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2602 |
# ifdef EINPROGRESS |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2603 |
|| (errno == EINPROGRESS) |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2604 |
# endif |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2605 |
# ifdef EALREADY |
1343 | 2606 |
|| (errno == EALREADY) |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2607 |
# endif |
1343 | 2608 |
) { |
2609 |
/* |
|
2610 |
* This was a nonblocking operation that will take some time. |
|
2611 |
* Do a select on read to get informed when the operation is ready. |
|
2612 |
*/ |
|
2613 |
||
2614 |
isAsync = true; |
|
2615 |
} else |
|
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2616 |
# endif /* EINPROGRESS or EALREADY */ |
1343 | 2617 |
{ |
2618 |
DBGPRINTF(("SOCKET: connect failed errno=%d\n", errno)); |
|
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2619 |
# ifdef DUMP_ADDRESS |
1343 | 2620 |
{ |
2621 |
char *cp = (char *)(&sa); |
|
2622 |
int i; |
|
2623 |
||
2624 |
printf("address data:\n"); |
|
2625 |
for (i=0; i<sockaddr_size; i++) { |
|
2626 |
printf(" %02x\n", *cp++); |
|
2627 |
} |
|
2628 |
} |
|
649 | 2629 |
# endif |
1343 | 2630 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
2631 |
RETURN (false); |
|
2632 |
} |
|
28 | 2633 |
} |
649 | 2634 |
|
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2635 |
# if defined(O_NONBLOCK) |
896
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2636 |
fcntl(sock, F_SETFL, oldFlags); |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2637 |
# endif |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2638 |
|
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2639 |
# else /* NO_SOCKET */ |
418 | 2640 |
RETURN (false); |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2641 |
# endif /* NO_SOCKET */ |
42 | 2642 |
%}. |
430
12d736316f69
Fix #connectTo:port: Do not call connect() when we get EINPROGRESS
Stefan Vogel <sv@exept.de>
parents:
429
diff
changeset
|
2643 |
isAsync == true ifTrue:[ |
1343 | 2644 |
(self writeWaitWithTimeoutMs:timeout) ifTrue:[ |
2645 |
"/ a timeout occured |
|
2646 |
"/ should cancel the connect? |
|
2647 |
^ false. |
|
2648 |
]. |
|
2649 |
err := self getSocketError. |
|
2650 |
err ~~ 0 ifTrue:[ |
|
2651 |
lastErrorNumber := err. |
|
2652 |
^ false. |
|
2653 |
]. |
|
430
12d736316f69
Fix #connectTo:port: Do not call connect() when we get EINPROGRESS
Stefan Vogel <sv@exept.de>
parents:
429
diff
changeset
|
2654 |
]. |
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2655 |
port := portNrOrNameOrNil. |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2656 |
peer := addr. |
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2657 |
peerName := addrName. |
28 | 2658 |
^ true |
896
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2659 |
|
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2660 |
" |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2661 |
|sock| |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2662 |
sock := Socket newTCP. |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2663 |
sock connectTo:'localhost' port:21 withTimeout:1000. |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2664 |
sock |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2665 |
|
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2666 |
|sock| |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2667 |
sock := Socket newTCP. |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2668 |
sock connectTo:'localhost' port:9876 withTimeout:2000. |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2669 |
sock |
2ca43149a20e
Ask for possible error after async connect.
Stefan Vogel <sv@exept.de>
parents:
888
diff
changeset
|
2670 |
" |
25 | 2671 |
! ! |
2672 |
||
1462 | 2673 |
!Socket methodsFor:'printing & storing'! |
2674 |
||
2675 |
printOn:aStream |
|
2676 |
aStream nextPutAll:'Socket(protocol='. |
|
2677 |
protocol printOn:aStream. |
|
2678 |
aStream nextPutAll:' port='. |
|
2679 |
port printOn:aStream. |
|
2680 |
aStream nextPutAll:' peer='. |
|
2681 |
peer printOn:aStream. |
|
2682 |
aStream nextPutAll:')'. |
|
2683 |
! ! |
|
2684 |
||
25 | 2685 |
!Socket methodsFor:'queries'! |
2686 |
||
126 | 2687 |
domain |
2688 |
"return the sockets addressing domain (i.e. #inet, #unix, #x25, #appletalk)" |
|
2689 |
||
2690 |
^ domain |
|
2691 |
! |
|
2692 |
||
2693 |
getName |
|
2694 |
"return the name; here, we return the ports name" |
|
2695 |
||
2696 |
^ port printString |
|
2697 |
! |
|
2698 |
||
102
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
2699 |
getPeer |
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
2700 |
"ST-80 compatibility: return an IPSocketAddress instance representing |
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
2701 |
my hostname/port combination. |
155
fcf846c707b7
interest is written with one 'r' (shame on me)
Claus Gittinger <cg@exept.de>
parents:
126
diff
changeset
|
2702 |
If you are interested in the hostname, use getPeerName directly." |
102
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
2703 |
|
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
2704 |
peer isNil ifTrue:[ |
1343 | 2705 |
peer := self class peerFromDomain:domain name:peerName port:port. |
819 | 2706 |
]. |
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
2707 |
^ peer |
102
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
2708 |
! |
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
2709 |
|
25 | 2710 |
getPeerName |
2711 |
"return the peer name; thats the hostname (or dotted name) of the |
|
2712 |
partners host after an accept." |
|
2713 |
||
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
2714 |
peerName isNil ifTrue:[ |
1343 | 2715 |
peerName := self class peerNameFromDomain:domain peer:peer. |
1187
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
2716 |
]. |
25 | 2717 |
^ peerName |
28 | 2718 |
! |
2719 |
||
1146 | 2720 |
getSocketAddress |
2721 |
"implemented for swazoo project primitive code cant load as extension |
|
1170 | 2722 |
answer my own address (I am bound to this address). |
1146 | 2723 |
Note that this address may change after connect or accept." |
1029
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2724 |
|
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2725 |
|socketAddress error| |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2726 |
|
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2727 |
filePointer isNil ifTrue:[ |
1170 | 2728 |
^ self errorNotOpen |
1029
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2729 |
]. |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2730 |
socketAddress := ByteArray new:16. |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2731 |
|
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2732 |
%{ |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2733 |
#ifndef NO_SOCKET |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2734 |
OBJ t = __INST(filePointer); |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2735 |
int sock; |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2736 |
int sockaddr_size; |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2737 |
int ret; |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2738 |
|
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2739 |
if (!__isNonNilObject(socketAddress) || |
1170 | 2740 |
(__intVal(__ClassInstPtr(__qClass(socketAddress))->c_flags) & ARRAYMASK) != BYTEARRAY) { |
2741 |
error = @symbol(badArgument1); |
|
2742 |
goto err; |
|
1029
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2743 |
} |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2744 |
sockaddr_size = __byteArraySize(socketAddress); |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2745 |
|
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2746 |
sock = fileno(__FILEVal(t)); |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2747 |
ret = getsockname(sock, (struct sockaddr *)__byteArrayVal(socketAddress), &sockaddr_size); |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2748 |
if (ret < 0) { |
1170 | 2749 |
DBGPRINTF(("SOCKET: getsocketname failed errno=%d\n", errno)); |
2750 |
error = __MKSMALLINT(errno); |
|
1029
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2751 |
} |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2752 |
err:; |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2753 |
#else /* NO_SOCKET */ |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2754 |
error = @symbol(notImplemented); |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2755 |
#endif /* NO_SOCKET */ |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2756 |
%}. |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2757 |
error notNil ifTrue:[ |
1170 | 2758 |
^ self errorReporter reportOn:error |
1029
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2759 |
]. |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2760 |
^ socketAddress copyFrom:5 to:8 |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2761 |
! |
5906977e62f0
implement getSocketName for swazoo as primitive cant load as extension
penk
parents:
1010
diff
changeset
|
2762 |
|
1146 | 2763 |
getSocketAdress |
2764 |
"implemented for swazoo project primitive code cant load as extension |
|
2765 |
answer the my own address (I am bound to this address). |
|
2766 |
Note that this address may change after connect or accept. |
|
2767 |
||
2768 |
BAD SPELLING, kept for compatibility with swazoo" |
|
2769 |
||
2770 |
||
2771 |
^ self getSocketAddress |
|
2772 |
! |
|
2773 |
||
126 | 2774 |
isActive |
2775 |
"return true, if the receiver has a connection" |
|
81 | 2776 |
|
126 | 2777 |
^ filePointer notNil |
81 | 2778 |
! |
2779 |
||
1110 | 2780 |
isConnected |
2781 |
"return true, if the receiver has a connection" |
|
2782 |
||
2783 |
^ self isActive |
|
2784 |
and:[port notNil |
|
1224
530a6f9723a1
use SocketAddress in #connect
Claus Gittinger <cg@exept.de>
parents:
1221
diff
changeset
|
2785 |
and:[peerName notNil or:[peer notNil]]] |
1110 | 2786 |
! |
2787 |
||
71 | 2788 |
port |
2789 |
"return the port number (or name for unix-sockets) to which the socket is bound" |
|
28 | 2790 |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2791 |
|p| |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2792 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2793 |
port == 0 ifTrue:[ |
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
2794 |
p := self getPort. |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2795 |
p notNil ifTrue:[ |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2796 |
port := p |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2797 |
] |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2798 |
]. |
71 | 2799 |
^ port |
28 | 2800 |
! |
2801 |
||
2802 |
type |
|
2803 |
"return the sockets connection type (i.e. #datagram, #stream etc)" |
|
2804 |
||
2805 |
^ socketType |
|
81 | 2806 |
! ! |
2807 |
||
126 | 2808 |
!Socket methodsFor:'socket setup'! |
2809 |
||
2810 |
domain:domainArg type:typeArg |
|
2811 |
"set up socket with domain and type. |
|
2812 |
This is a low level entry; no binding, listening or connect |
|
2813 |
is done. Both arguments must be symbols from one of |
|
2814 |
#inet,#unix, #appletalk, #x25 .. and #stream, #datagram, #raw resp." |
|
2815 |
||
649 | 2816 |
^ self domain:domainArg type:typeArg protocol:0 reuseAddress:true |
126 | 2817 |
! |
2818 |
||
2819 |
domain:domainArg type:typeArg protocol:protocolNumber |
|
2820 |
"set up socket with domain, type and protocol number. |
|
2821 |
This is a low level entry; no binding, listening or connect |
|
2822 |
is done. Both arguments must be symbols from one of |
|
2823 |
#inet,#unix, #appletalk, #x25 .. and #stream, #datagram, #raw resp." |
|
2824 |
||
649 | 2825 |
^ self domain:domainArg type:typeArg protocol:protocolNumber reuseAddress:true |
2826 |
||
2827 |
" |
|
2828 |
Socket new domain:#inet type:#stream |
|
2829 |
Socket new domain:#unix type:#stream |
|
2830 |
" |
|
2831 |
! |
|
2832 |
||
2833 |
domain:domainArg type:typeArg protocol:protocolNumber reuseAddress:reuse |
|
2834 |
"set up socket with domain, type and protocol number. |
|
2835 |
This is a low level entry; no binding, listening or connect |
|
2836 |
is done. Both arguments must be symbols from one of |
|
2837 |
#inet,#unix, #appletalk, #x25 .. and #stream, #datagram, #raw resp. |
|
2838 |
||
2839 |
The reuse boolean argument controls if the SO_REUSEADDR socket option |
|
2840 |
is to be set (to avoid the 'bind: address in use' error)." |
|
2841 |
||
1280
b973855bfe74
moved all socket constant queries from SocketHandle
Claus Gittinger <cg@exept.de>
parents:
1278
diff
changeset
|
2842 |
|domainName domainCode typeCode| |
126 | 2843 |
|
2844 |
filePointer notNil ifTrue:[ |
|
1343 | 2845 |
^ self errorAlreadyOpen |
126 | 2846 |
]. |
1280
b973855bfe74
moved all socket constant queries from SocketHandle
Claus Gittinger <cg@exept.de>
parents:
1278
diff
changeset
|
2847 |
domainName := SocketAddress domainCodeFromName:domainArg. |
b973855bfe74
moved all socket constant queries from SocketHandle
Claus Gittinger <cg@exept.de>
parents:
1278
diff
changeset
|
2848 |
domainCode := OperatingSystem domainCodeOf:domainName. |
b973855bfe74
moved all socket constant queries from SocketHandle
Claus Gittinger <cg@exept.de>
parents:
1278
diff
changeset
|
2849 |
typeCode := OperatingSystem socketTypeCodeOf:typeArg. |
126 | 2850 |
%{ |
649 | 2851 |
|
418 | 2852 |
#ifndef NO_SOCKET |
126 | 2853 |
FILE *fp; |
2854 |
int dom, typ, pf, proto = 0, sock; |
|
649 | 2855 |
int on = 1; |
126 | 2856 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2857 |
if (! __isSmallInteger(domainCode)) { |
1343 | 2858 |
DBGPRINTF(("SOCKET: bad domain\n")); |
2859 |
RETURN ( nil ); |
|
126 | 2860 |
} |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2861 |
if (! __isSmallInteger(typeCode)) { |
1343 | 2862 |
DBGPRINTF(("SOCKET: bad type\n")); |
2863 |
RETURN ( nil ); |
|
126 | 2864 |
} |
2865 |
if (protocolNumber != nil) { |
|
1343 | 2866 |
if (!__isSmallInteger(protocolNumber)) { |
2867 |
DBGPRINTF(("SOCKET: bad protocol\n")); |
|
2868 |
RETURN ( nil ); |
|
2869 |
} |
|
2870 |
proto = __intVal(protocolNumber); |
|
126 | 2871 |
} |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2872 |
|
81 | 2873 |
|
126 | 2874 |
/* |
2875 |
* get address and protocol-family |
|
2876 |
*/ |
|
1280
b973855bfe74
moved all socket constant queries from SocketHandle
Claus Gittinger <cg@exept.de>
parents:
1278
diff
changeset
|
2877 |
dom = __intVal(domainCode); |
b973855bfe74
moved all socket constant queries from SocketHandle
Claus Gittinger <cg@exept.de>
parents:
1278
diff
changeset
|
2878 |
typ = __intVal(typeCode); |
126 | 2879 |
|
2880 |
__BEGIN_INTERRUPTABLE__ |
|
2881 |
do { |
|
1343 | 2882 |
DBGPRINTF(("SOCKET: opening socket domain=%d type=%d proto=%d\n", dom, typ, proto)); |
2883 |
sock = socket(dom, typ, proto); |
|
126 | 2884 |
#if defined(EPROTONOSUPPORT) /* for SGI */ |
1343 | 2885 |
if ((proto != 0) && (sock < 0) && (errno == EPROTONOSUPPORT)) { |
2886 |
DBGPRINTF(("SOCKET: retry with UNSPEC protocol\n")); |
|
2887 |
proto = 0; |
|
2888 |
sock = socket(dom, typ, 0); |
|
2889 |
} |
|
126 | 2890 |
#endif |
2891 |
} while ((sock < 0) && (errno == EINTR)); |
|
2892 |
__END_INTERRUPTABLE__ |
|
821
40b0b69bf4b5
more debugs; fixed at-addresses
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
2893 |
DBGFPRINTF((stderr, "socket create newSock=%d\n", sock)); |
126 | 2894 |
|
2895 |
if (sock < 0) { |
|
1343 | 2896 |
DBGPRINTF(("SOCKET: socket(dom=%d typ=%d proto=%d) call failed errno=%d\n", dom, typ, proto, errno)); |
2897 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
126 | 2898 |
} else { |
649 | 2899 |
# ifdef SO_REUSEADDR |
1343 | 2900 |
if (reuse == true) { |
2901 |
DBGPRINTF(("SOCKET: setsockopt - SO_REUSEADDR\n")); |
|
2902 |
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0) { |
|
2903 |
DBGPRINTF(("SOCKET: setsockopt - SO_REUSEADDR failed\n")); |
|
2904 |
} |
|
2905 |
} |
|
649 | 2906 |
# endif /* SO_REUSEADDR */ |
711 | 2907 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2908 |
/* |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2909 |
* make it a FILE * |
1343 | 2910 |
*/ |
2911 |
fp = fdopen(sock, "r+"); |
|
2912 |
if (! fp) { |
|
2913 |
DBGPRINTF(("SOCKET: fdopen call failed\n")); |
|
2914 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2915 |
__BEGIN_INTERRUPTABLE__ |
|
2916 |
close(sock); |
|
2917 |
DBGFPRINTF((stderr, "socket close (fdopen failed) (%d)\n", sock)); |
|
2918 |
__END_INTERRUPTABLE__ |
|
2919 |
} else { |
|
2920 |
if (@global(FileOpenTrace) == true) { |
|
2921 |
fprintf(stderr, "fdopen [Socket] -> %x\n", fp); |
|
2922 |
} |
|
2923 |
||
1486 | 2924 |
# if 0 |
2925 |
// The original code was: |
|
2926 |
__INST(filePointer) = __MKOBJ((INT)fp); __STORESELF(filePointer); |
|
2927 |
// but for that, gcc generates wrong code, which loads self (volatile) into |
|
2928 |
// a register (bp), then calls __MKOBJ, then stores indirect bp. |
|
2929 |
// That is wrong if a scavenge occurs in MKOBJ, as bp is now still pointing to the old |
|
2930 |
// object. |
|
2931 |
# endif |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2932 |
{ |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2933 |
OBJ t; |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2934 |
|
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2935 |
t = __MKOBJ(fp); |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2936 |
__INST(filePointer) = t; |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2937 |
__STORE(self, t); |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2938 |
} |
1343 | 2939 |
} |
126 | 2940 |
} |
418 | 2941 |
#endif |
126 | 2942 |
%}. |
2943 |
||
2944 |
"all ok?" |
|
2945 |
filePointer notNil ifTrue:[ |
|
1343 | 2946 |
domain := domainArg. |
2947 |
socketType := typeArg. |
|
2948 |
Lobby register:self. |
|
126 | 2949 |
] ifFalse:[ |
1343 | 2950 |
^ nil |
126 | 2951 |
]. |
2952 |
||
2953 |
" |
|
2954 |
Socket new domain:#inet type:#stream |
|
2955 |
Socket new domain:#unix type:#stream |
|
2956 |
" |
|
28 | 2957 |
! ! |
2958 |
||
2959 |
!Socket methodsFor:'specials'! |
|
2960 |
||
1335 | 2961 |
receiveBufferSize |
2962 |
"get the send buffer size - for special applications only. |
|
2963 |
Not all operatingSystems offer this functionality |
|
2964 |
(returns nil, if unsupported)" |
|
2965 |
||
2966 |
filePointer isNil ifTrue:[ |
|
1343 | 2967 |
^ self errorNotOpen |
1335 | 2968 |
]. |
2969 |
%{ |
|
2970 |
#if defined(SO_RCVBUF) && defined(SOL_SOCKET) |
|
2971 |
{ |
|
1343 | 2972 |
OBJ fp = __INST(filePointer); |
2973 |
int sock; |
|
2974 |
int opt; |
|
2975 |
int size; |
|
2976 |
||
2977 |
sock = fileno(__FILEVal(fp)); |
|
2978 |
if (getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&opt, &size) >= 0) { |
|
2979 |
RETURN( __MKSMALLINT(opt) ); |
|
2980 |
} |
|
1335 | 2981 |
} |
2982 |
#endif |
|
2983 |
%}. |
|
2984 |
^ nil |
|
2985 |
! |
|
2986 |
||
2987 |
receiveBufferSize:size |
|
2988 |
"set the receive buffer size - for special applications only. |
|
2989 |
Not all operatingSystems offer this functionality |
|
2990 |
(returns false, if unsupported)" |
|
2991 |
||
2992 |
filePointer isNil ifTrue:[ |
|
1343 | 2993 |
^ self errorNotOpen |
1335 | 2994 |
]. |
2995 |
%{ |
|
2996 |
#if defined(SO_RCVBUF) && defined(SOL_SOCKET) |
|
2997 |
if (__isSmallInteger(size)) { |
|
1343 | 2998 |
OBJ fp = __INST(filePointer); |
2999 |
int sock; |
|
3000 |
int opt; |
|
3001 |
||
3002 |
sock = fileno(__FILEVal(fp)); |
|
3003 |
opt = __intVal(size); |
|
3004 |
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&opt, sizeof(int)); |
|
3005 |
RETURN(true); |
|
1335 | 3006 |
} |
3007 |
#endif |
|
3008 |
%}. |
|
3009 |
^ false |
|
3010 |
! |
|
3011 |
||
28 | 3012 |
receiveTimeout:seconds |
71 | 3013 |
"set the receive timeout - for special applications only. |
3014 |
Not all operatingSystems offer this functionality |
|
3015 |
(returns false, if unsupported)" |
|
28 | 3016 |
|
3017 |
|millis| |
|
3018 |
||
3019 |
filePointer isNil ifTrue:[ |
|
789 | 3020 |
^ self errorNotOpen |
28 | 3021 |
]. |
3022 |
millis := (seconds * 1000) rounded. |
|
3023 |
%{ |
|
3024 |
#if defined(SO_RCVTIMEO) && defined(SOL_SOCKET) && defined(HZ) |
|
58 | 3025 |
if (__isSmallInteger(millis)) { |
789 | 3026 |
OBJ fp = __INST(filePointer); |
3027 |
int sock; |
|
3028 |
int opt; |
|
3029 |
||
3030 |
sock = fileno(__FILEVal(fp)); |
|
1009 | 3031 |
opt = __intVal(millis) / (1000 / HZ); |
789 | 3032 |
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&opt, sizeof(int)); |
3033 |
RETURN(true); |
|
28 | 3034 |
} |
3035 |
#endif |
|
71 | 3036 |
%}. |
28 | 3037 |
^ false |
81 | 3038 |
! |
3039 |
||
1335 | 3040 |
sendBufferSize |
3041 |
"get the send buffer size - for special applications only. |
|
3042 |
Not all operatingSystems offer this functionality |
|
3043 |
(returns nil, if unsupported)" |
|
3044 |
||
3045 |
filePointer isNil ifTrue:[ |
|
1343 | 3046 |
^ self errorNotOpen |
1335 | 3047 |
]. |
3048 |
%{ |
|
3049 |
#if defined(SO_SNDBUF) && defined(SOL_SOCKET) |
|
3050 |
{ |
|
1343 | 3051 |
OBJ fp = __INST(filePointer); |
3052 |
int sock; |
|
3053 |
int opt; |
|
3054 |
int size; |
|
3055 |
||
3056 |
sock = fileno(__FILEVal(fp)); |
|
3057 |
if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&opt, &size) >= 0) { |
|
3058 |
RETURN( __MKSMALLINT(opt) ); |
|
3059 |
} |
|
1335 | 3060 |
} |
3061 |
#endif |
|
3062 |
%}. |
|
3063 |
^ nil |
|
3064 |
! |
|
3065 |
||
3066 |
sendBufferSize:size |
|
3067 |
"set the send buffer size - for special applications only. |
|
3068 |
Not all operatingSystems offer this functionality |
|
3069 |
(returns false, if unsupported)" |
|
3070 |
||
3071 |
filePointer isNil ifTrue:[ |
|
1343 | 3072 |
^ self errorNotOpen |
1335 | 3073 |
]. |
3074 |
%{ |
|
3075 |
#if defined(SO_SNDBUF) && defined(SOL_SOCKET) |
|
3076 |
if (__isSmallInteger(size)) { |
|
1343 | 3077 |
OBJ fp = __INST(filePointer); |
3078 |
int sock; |
|
3079 |
int opt; |
|
3080 |
||
3081 |
sock = fileno(__FILEVal(fp)); |
|
3082 |
opt = __intVal(size); |
|
3083 |
setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&opt, sizeof(int)); |
|
3084 |
RETURN(true); |
|
1335 | 3085 |
} |
3086 |
#endif |
|
3087 |
%}. |
|
3088 |
^ false |
|
3089 |
! |
|
3090 |
||
126 | 3091 |
sendTimeout:seconds |
3092 |
"set the send timeout - for special applications only. |
|
3093 |
Not all operatingSystems offer this functionality |
|
3094 |
(returns false, if unsupported)" |
|
81 | 3095 |
|
126 | 3096 |
|millis| |
25 | 3097 |
|
126 | 3098 |
filePointer isNil ifTrue:[ |
789 | 3099 |
^ self errorNotOpen |
126 | 3100 |
]. |
3101 |
millis := (seconds * 1000) rounded. |
|
3102 |
%{ |
|
3103 |
#if defined(SO_SNDTIMEO) && defined(SOL_SOCKET) && defined(HZ) |
|
3104 |
if (__isSmallInteger(millis)) { |
|
789 | 3105 |
OBJ fp = __INST(filePointer); |
3106 |
int sock; |
|
3107 |
int opt; |
|
3108 |
||
3109 |
sock = fileno(__FILEVal(fp)); |
|
1009 | 3110 |
opt = __intVal(millis) / (1000 / HZ); |
789 | 3111 |
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&opt, sizeof(int)); |
3112 |
RETURN(true); |
|
126 | 3113 |
} |
3114 |
#endif |
|
3115 |
%}. |
|
3116 |
^ false |
|
1340 | 3117 |
! |
3118 |
||
1342 | 3119 |
setTCPCork:aBoolean |
3120 |
"enable/disable TCP_CORK (do-not-send-partial-frames) |
|
3121 |
For special applications only. |
|
3122 |
Not all OperatingSystems offer this functionality |
|
3123 |
(returns false, if unsupported)" |
|
3124 |
||
3125 |
filePointer isNil ifTrue:[ |
|
1343 | 3126 |
^ self errorNotOpen |
1342 | 3127 |
]. |
3128 |
%{ |
|
3129 |
#if defined(IPPROTO_TCP) && defined(TCP_CORK) |
|
3130 |
int onOff = (aBoolean == true); |
|
3131 |
||
3132 |
if ((aBoolean == true) || (aBoolean == false)) { |
|
1343 | 3133 |
OBJ fp = __INST(filePointer); |
3134 |
int sock; |
|
3135 |
int onOff = (aBoolean == true); |
|
3136 |
||
3137 |
sock = fileno(__FILEVal(fp)); |
|
3138 |
setsockopt(sock, IPPROTO_TCP, TCP_CORK, (char *)&onOff, sizeof(int)); |
|
3139 |
RETURN(true); |
|
1342 | 3140 |
} |
3141 |
#endif |
|
3142 |
%}. |
|
3143 |
^ false |
|
3144 |
! |
|
3145 |
||
1341 | 3146 |
setTCPNoDelay:aBoolean |
1340 | 3147 |
"enable/disable TCP_NODELAY (i.e. disable/enable the Nagle algorithm) |
3148 |
For special applications only. |
|
3149 |
Not all OperatingSystems offer this functionality |
|
3150 |
(returns false, if unsupported)" |
|
3151 |
||
3152 |
filePointer isNil ifTrue:[ |
|
1343 | 3153 |
^ self errorNotOpen |
1340 | 3154 |
]. |
3155 |
%{ |
|
1342 | 3156 |
#if defined(IPPROTO_TCP) && defined(TCP_NODELAY) |
1340 | 3157 |
int onOff = (aBoolean == true); |
3158 |
||
3159 |
if ((aBoolean == true) || (aBoolean == false)) { |
|
1343 | 3160 |
OBJ fp = __INST(filePointer); |
3161 |
int sock; |
|
3162 |
int onOff = (aBoolean == true); |
|
3163 |
||
3164 |
sock = fileno(__FILEVal(fp)); |
|
3165 |
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&onOff, sizeof(int)); |
|
3166 |
RETURN(true); |
|
1340 | 3167 |
} |
3168 |
#endif |
|
3169 |
%}. |
|
3170 |
^ false |
|
126 | 3171 |
! ! |
82 | 3172 |
|
464 | 3173 |
!Socket class methodsFor:'documentation'! |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
3174 |
|
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
3175 |
version |
1486 | 3176 |
^ '$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.202 2004-11-09 17:30:22 cg Exp $' |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
3177 |
! ! |