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