author | Claus Gittinger <cg@exept.de> |
Wed, 09 Aug 2017 14:12:34 +0200 | |
changeset 4497 | bf5e9d5ec532 |
parent 4414 | 42edc97b2323 |
child 4506 | 9ea05177d1f7 |
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 |
" |
|
888 | 12 |
"{ Package: 'stx:libbasic2' }" |
13 |
||
3462 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
63 | 16 |
NonPositionableExternalStream subclass:#Socket |
4234 | 17 |
instanceVariableNames:'domain socketType protocol port peer peerName listening' |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
18 |
classVariableNames:'' |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
19 |
poolDictionaries:'' |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
20 |
category:'Streams-External' |
0 | 21 |
! |
22 |
||
126 | 23 |
!Socket primitiveDefinitions! |
24 |
%{ |
|
1009 | 25 |
/* #define DGRAM_DEBUG /* */ |
26 |
||
3590 | 27 |
#ifdef __MINGW__ |
28 |
# include <stdio.h> |
|
29 |
#endif |
|
1343 | 30 |
#include "stxOSDefs.h" |
126 | 31 |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
32 |
#ifdef __win32__ |
3123 | 33 |
//#define DEBUG 1 |
2818 | 34 |
/* this is to catch uses of those - there should be none */ |
1503 | 35 |
# undef __BEGIN_INTERRUPTABLE__ |
36 |
# undef __END_INTERRUPTABLE__ |
|
2818 | 37 |
# ifdef __TCC__ |
38 |
# define __BEGIN_INTERRUPTABLE__ xxxx |
|
39 |
# define __END_INTERRUPTABLE__ yyyy |
|
40 |
# else |
|
41 |
# define __BEGIN_INTERRUPTABLE__ .. |
|
42 |
# define __END_INTERRUPTABLE__ .. |
|
43 |
# endif |
|
1503 | 44 |
|
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
45 |
# define WRAP_STDIO |
2823
10179ccfae5d
Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents:
2818
diff
changeset
|
46 |
// The default is linger until all data is sent in background |
10179ccfae5d
Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents:
2818
diff
changeset
|
47 |
// so the close on the socket is non-blocking |
10179ccfae5d
Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents:
2818
diff
changeset
|
48 |
// linger.onoff=off linger.time= *irrelevant* |
10179ccfae5d
Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents:
2818
diff
changeset
|
49 |
//# define SET_LINGER_WHEN_CREATING_SOCKET |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
50 |
|
3742 | 51 |
# ifdef __xxMINGW__ |
2909 | 52 |
extern HANDLE _get_osfhandle(); |
1845 | 53 |
# endif |
54 |
||
3620 | 55 |
# if defined(__BORLANDC__) |
56 |
// not defined in borland headers |
|
57 |
typedef unsigned int socklen_t; |
|
58 |
# endif |
|
59 |
||
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
60 |
# if 0 && (defined( __BORLANDC__ ) || defined( __MINGW__ )) |
2915 | 61 |
# define SOCKET_FROM_FD(fd) (SOCKET)(_get_osfhandle(fd)) |
2909 | 62 |
# else |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
63 |
# define SOCKET_FROM_FD(fd) (SOCKET)(fd) |
2909 | 64 |
# endif |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
65 |
#else /* not __win32__ */ |
3215 | 66 |
# define SOCKET_FROM_FD(fd) (fd) |
67 |
# define closesocket(sock) close(sock) |
|
68 |
# define SOCKET int |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
69 |
#endif /* ! __win32__ */ |
1503 | 70 |
|
1843
8ee90d243b49
closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
71 |
#define SOCKET_FROM_FILE(f) (SOCKET_FROM_FD(fileno(f))) |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
72 |
#define SOCKET_FROM_FILE_OBJECT(f) (__INST(handleType) == @symbol(socketHandle) ? SOCKET_FROM_FD(__FILEVal(f)) : SOCKET_FROM_FILE(__FILEVal(f))) |
1539
c0e89dcb58b3
non blocking stuff done right here;
Claus Gittinger <cg@exept.de>
parents:
1538
diff
changeset
|
73 |
|
3590 | 74 |
#ifndef __MINGW__ |
75 |
# include <stdio.h> |
|
76 |
#endif |
|
126 | 77 |
#include <errno.h> |
78 |
||
1343 | 79 |
#ifdef WANT__TCP_DOT_H |
80 |
# include <netinet/tcp.h> |
|
81 |
#endif |
|
82 |
||
1344 | 83 |
#ifdef WANT__NETDB_DOT_H |
84 |
# include <netdb.h> |
|
1343 | 85 |
#endif |
86 |
||
739
eb3878300c3b
removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents:
726
diff
changeset
|
87 |
#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
|
88 |
# define USE_H_ERRNO |
eb3878300c3b
removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents:
726
diff
changeset
|
89 |
#endif |
eb3878300c3b
removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents:
726
diff
changeset
|
90 |
|
126 | 91 |
/* |
92 |
* on some systems errno is a macro ... check for it here |
|
93 |
*/ |
|
94 |
#ifndef errno |
|
95 |
extern errno; |
|
96 |
#endif |
|
97 |
||
648
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
98 |
#ifdef USE_H_ERRNO |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
99 |
# ifndef h_errno |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
100 |
extern h_errno; |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
101 |
# endif |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
102 |
#endif |
58e11a70ddaf
gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents:
617
diff
changeset
|
103 |
|
126 | 104 |
#ifdef DEBUG |
1839 | 105 |
# define DBGPRINTF(x) { if (__debugging__) console_printf x; } |
106 |
# define DBGFPRINTF(x) { if (__debugging__) console_fprintf x; } |
|
126 | 107 |
#else |
108 |
# define DBGPRINTF(x) /* as nothing */ |
|
821
40b0b69bf4b5
more debugs; fixed at-addresses
Claus Gittinger <cg@exept.de>
parents:
820
diff
changeset
|
109 |
# define DBGFPRINTF(x) /* as nothing */ |
126 | 110 |
#endif |
111 |
||
1531 | 112 |
#ifndef TRUE |
1535 | 113 |
# define TRUE 1 |
1531 | 114 |
#endif |
115 |
#ifndef FALSE |
|
1535 | 116 |
# define FALSE 0 |
1531 | 117 |
#endif |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
118 |
#ifndef __win32__ |
1531 | 119 |
typedef int BOOL; |
120 |
#endif |
|
121 |
||
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
122 |
#ifdef __win32__ |
1843
8ee90d243b49
closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
123 |
# undef stdout |
8ee90d243b49
closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
124 |
# undef stderr |
8ee90d243b49
closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
125 |
# define stdout __win32_stdout() |
8ee90d243b49
closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
126 |
# define stderr __win32_stderr() |
8ee90d243b49
closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
127 |
#endif |
126 | 128 |
%} |
129 |
! ! |
|
130 |
||
511 | 131 |
!Socket primitiveVariables! |
132 |
%{ |
|
1844 | 133 |
static int __debugging__ = 0; |
511 | 134 |
%} |
135 |
! ! |
|
136 |
||
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
137 |
!Socket primitiveFunctions! |
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
138 |
%{ |
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
139 |
|
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
140 |
static int |
3616 | 141 |
setupBufferParameters(OBJ aDataBuffer, OBJ startIndex, char **p_extPtr, INT *p_offs, size_t *p_objSize) |
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
142 |
{ |
3657 | 143 |
char *extPtr = 0; |
144 |
int sIdx = 0, objSize = 0, offs = 0; |
|
145 |
||
146 |
sIdx = 0; |
|
147 |
if (__isSmallInteger(startIndex)) { |
|
148 |
sIdx = __intVal(startIndex) - 1; |
|
149 |
} |
|
150 |
||
151 |
if (__isExternalBytesLike(aDataBuffer)) { |
|
152 |
OBJ sz; |
|
153 |
||
154 |
extPtr = (char *)(__externalBytesAddress(aDataBuffer)); |
|
155 |
sz = __externalBytesSize(aDataBuffer); |
|
156 |
if (__isSmallInteger(sz)) { |
|
157 |
objSize = __intVal(sz); |
|
158 |
} else { |
|
159 |
objSize = 0; /* unknown */ |
|
160 |
} |
|
161 |
offs = sIdx; |
|
162 |
} else { |
|
163 |
OBJ oClass; |
|
164 |
int nInstVars, nInstBytes; |
|
165 |
||
166 |
extPtr = (char *)0; |
|
167 |
oClass = __Class(aDataBuffer); |
|
168 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
169 |
case BYTEARRAY: |
|
170 |
offs = sIdx; |
|
171 |
break; |
|
172 |
case WORDARRAY: |
|
173 |
case SWORDARRAY: |
|
174 |
offs = sIdx * 2; |
|
175 |
break; |
|
176 |
case LONGARRAY: |
|
177 |
case SLONGARRAY: |
|
178 |
offs = sIdx * 4; |
|
179 |
break; |
|
180 |
case LONGLONGARRAY: |
|
181 |
case SLONGLONGARRAY: |
|
182 |
offs = sIdx * 8; |
|
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
183 |
# ifdef __NEED_LONGLONG_ALIGN |
3657 | 184 |
offs += 4; |
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
185 |
# endif |
3657 | 186 |
break; |
187 |
case FLOATARRAY: |
|
188 |
offs = sIdx * sizeof(float); |
|
189 |
break; |
|
190 |
case DOUBLEARRAY: |
|
191 |
offs = sIdx * sizeof(double); |
|
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
192 |
# ifdef __NEED_DOUBLE_ALIGN |
3657 | 193 |
offs += 4; |
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
194 |
# endif |
3657 | 195 |
break; |
196 |
default: |
|
197 |
*p_objSize = -1; |
|
198 |
return 0; |
|
199 |
} |
|
200 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
201 |
nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ); |
|
202 |
offs = offs + nInstBytes; |
|
203 |
objSize = __Size(aDataBuffer) - offs; |
|
204 |
} |
|
205 |
*p_extPtr = extPtr; |
|
206 |
*p_objSize = objSize; |
|
207 |
*p_offs = offs; |
|
208 |
return 1; |
|
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
209 |
} |
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
210 |
%} |
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
211 |
! ! |
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
212 |
|
464 | 213 |
!Socket class methodsFor:'documentation'! |
25 | 214 |
|
31 | 215 |
copyright |
216 |
" |
|
217 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
42 | 218 |
All Rights Reserved |
31 | 219 |
|
220 |
This software is furnished under a license and may be used |
|
221 |
only in accordance with the terms of that license and with the |
|
222 |
inclusion of the above copyright notice. This software may not |
|
223 |
be provided or otherwise made available to, or used by, any |
|
224 |
other person. No title to or ownership of the software is |
|
225 |
hereby transferred. |
|
226 |
" |
|
227 |
! |
|
228 |
||
25 | 229 |
documentation |
230 |
" |
|
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
231 |
This class provides access to sockets for interprocess communication. |
25 | 232 |
|
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
233 |
Currently, only IP and UNIX domain sockets are really fully |
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
234 |
tested and supported. |
525 | 235 |
Code for appletalk is included, but was never tested ... |
236 |
More may be added in the future. |
|
237 |
(the code is prepared for things like SNA or decNet; |
|
238 |
however, right now, this code is empty and needs a little work. |
|
3989 | 239 |
Implementing those is pretty straightforward, once the address |
525 | 240 |
data structures are known.) |
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
241 |
|
28 | 242 |
Due to historic reasons (I started this class, before I got hold of some |
243 |
code using ST-80 Sockets i.e. RemoteInvocation), there is some old interface |
|
3481 | 244 |
which is still supported. |
245 |
This may vanish; use the #family:type: or #newTCPxxx and #newUDPxxx interfaces, |
|
71 | 246 |
together with the bind/listen and accept calls, |
247 |
which are meant to be compatible to ST-80's UnixSocketAccessor interface. |
|
248 |
||
3481 | 249 |
TODO: cleanup historic leftovers, |
4414 | 250 |
change to raise more signals on errors. |
259 | 251 |
|
252 |
[author:] |
|
4414 | 253 |
Claus Gittinger |
48 | 254 |
" |
255 |
! |
|
28 | 256 |
|
48 | 257 |
examples |
258 |
" |
|
28 | 259 |
example (get help info from an nntp server): |
4414 | 260 |
[exBegin] |
261 |
|sock| |
|
262 |
||
263 |
sock := Socket newTCPclientToHost:'smtp.exept.de' port:'smtp'. |
|
264 |
sock isNil ifTrue:[ |
|
265 |
self warn:'no smtp daemon is running'. |
|
266 |
^ self |
|
267 |
]. |
|
268 |
Transcript showCR:sock nextLine. |
|
269 |
||
270 |
sock nextPutAll:'HELO STX socket test'; cr. |
|
271 |
Transcript showCR:sock nextLine. |
|
272 |
sock close |
|
273 |
[exEnd] |
|
209 | 274 |
|
28 | 275 |
|
209 | 276 |
example (connect to finger daemon, get users entry): |
4414 | 277 |
[exBegin] |
278 |
|sock entry| |
|
279 |
||
280 |
sock := Socket newTCPclientToHost:'localhost' port:'finger'. |
|
281 |
sock isNil ifTrue:[ |
|
282 |
self warn:'no finger daemon is running'. |
|
283 |
^ self |
|
284 |
]. |
|
285 |
sock useCRLF:true. |
|
286 |
sock buffered:false. |
|
287 |
sock isNil ifTrue:[ |
|
288 |
Transcript showCR:'cannot connect to local finger daemon' |
|
289 |
] ifFalse:[ |
|
290 |
sock nextPutAll:(OperatingSystem getLoginName). |
|
291 |
sock cr. |
|
292 |
||
293 |
entry := sock nextLine. |
|
294 |
Transcript showCR:entry. |
|
295 |
||
296 |
sock close |
|
297 |
] |
|
298 |
[exEnd] |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
299 |
|
28 | 300 |
example (connect to an ftp server): |
4414 | 301 |
[exBegin] |
302 |
|sock| |
|
303 |
||
304 |
sock := Socket newTCPclientToHost:'www.exept.de' port:'ftp'. |
|
305 |
||
306 |
sock buffered:false. |
|
307 |
Transcript showCR:sock nextLine. |
|
308 |
sock nextPutAll:('USER ' , 'anonymous'); cr. |
|
309 |
Transcript showCR:sock nextLine. |
|
310 |
sock nextPutAll:('PASS ' , 'fooBar'); cr. |
|
311 |
Transcript showCR:sock nextLine. |
|
312 |
sock nextPutAll:'HELP'; cr. |
|
313 |
[ |
|
314 |
|line| |
|
315 |
line := sock nextLine. |
|
316 |
Transcript showCR:line. |
|
317 |
(line at:4) = $- |
|
318 |
] whileTrue. |
|
319 |
sock close. |
|
320 |
||
321 |
'don't know enough of the ftp protocol to continue here ...' |
|
322 |
[exEnd] |
|
28 | 323 |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
324 |
|
93 | 325 |
example (connect to an snmp server [UDP]): |
3985 | 326 |
Note: this is not a real connection, only the destination address is |
4414 | 327 |
being fixed. |
328 |
[exBegin] |
|
329 |
|sock port| |
|
330 |
||
331 |
sock := Socket newUDP. |
|
332 |
port := Socket portOfService:'snmp'. |
|
333 |
sock connectTo:'localhost' port:port. |
|
334 |
sock buffered:false. |
|
335 |
Transcript showCR:'got it'. |
|
336 |
sock close. |
|
337 |
[exEnd] |
|
42 | 338 |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
339 |
|
42 | 340 |
example (await connection from a client and read some data): |
4414 | 341 |
[exBegin] |
342 |
|connectSock sock| |
|
343 |
||
344 |
connectSock := Socket newTCPserverAtPort:9998. |
|
345 |
connectSock isNil ifTrue:[ |
|
346 |
Transcript showCR:'socket setup failed.'. |
|
347 |
] ifFalse:[ |
|
348 |
Transcript showCR:'listen ..'. |
|
349 |
(connectSock listenFor:5) ifFalse:[ |
|
350 |
Transcript showCR:'listen failed.'. |
|
351 |
] ifTrue:[ |
|
352 |
Transcript showCR:'wait'. |
|
353 |
connectSock readWait. |
|
354 |
Transcript showCR:'accept'. |
|
355 |
sock := connectSock accept. |
|
356 |
sock isNil ifTrue:[ |
|
357 |
Transcript showCR:'accept failed.'. |
|
358 |
] ifFalse:[ |
|
359 |
sock buffered:false. |
|
360 |
Transcript showCR:'server: got it'. |
|
361 |
'can now do transfer via sock'. |
|
362 |
Transcript showCR:'read'. |
|
363 |
Transcript showCR:('got: ' , sock nextLine). |
|
364 |
||
365 |
Transcript showCR:'close'. |
|
366 |
sock close |
|
367 |
]. |
|
368 |
connectSock close. |
|
369 |
] |
|
370 |
] |
|
371 |
[exEnd] |
|
42 | 372 |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
373 |
|
42 | 374 |
example (connect to above server and send some data): |
4414 | 375 |
[exBegin] |
376 |
|sock| |
|
377 |
||
378 |
sock := Socket newTCPclientToHost:'localhost' port:9998. |
|
379 |
sock isNil ifTrue:[ |
|
380 |
Transcript showCR:'nope' |
|
381 |
] ifFalse:[ |
|
382 |
sock buffered:false. |
|
383 |
Transcript showCR:'client: got it'. |
|
384 |
'can now do transfer via sock'. |
|
385 |
Transcript showCR:'sending <hello>'. |
|
386 |
sock nextPutLine:'hello'. |
|
387 |
sock close |
|
388 |
] |
|
389 |
[exEnd] |
|
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
390 |
|
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
391 |
example: UNIX domain socket (await connection from a client and read some data): |
1009 | 392 |
|
4414 | 393 |
|connectSock sock| |
394 |
||
395 |
'/tmp/ud_socket' asFilename remove. |
|
396 |
connectSock := Socket newUNIXserverAt:'/tmp/ud_socket'. |
|
397 |
connectSock isNil ifTrue:[ |
|
398 |
Transcript showCR:'socket setup failed.'. |
|
399 |
] ifFalse:[ |
|
400 |
Transcript showCR:'listen ..'. |
|
401 |
(connectSock listenFor:5) ifFalse:[ |
|
402 |
Transcript showCR:'listen failed.'. |
|
403 |
] ifTrue:[ |
|
404 |
Transcript showCR:'wait'. |
|
405 |
connectSock buffered:false. |
|
406 |
connectSock readWait. |
|
407 |
Transcript showCR:'accept'. |
|
408 |
sock := connectSock accept. |
|
409 |
sock isNil ifTrue:[ |
|
410 |
Transcript showCR:'accept failed.'. |
|
411 |
] ifFalse:[ |
|
412 |
sock buffered:false. |
|
413 |
Transcript showCR:'server: got it'. |
|
414 |
'can now do transfer via sock'. |
|
415 |
Transcript showCR:'read'. |
|
416 |
Transcript showCR:('got: ' , sock nextLine). |
|
417 |
||
418 |
Transcript showCR:'close'. |
|
419 |
sock close |
|
420 |
]. |
|
421 |
connectSock close. |
|
422 |
] |
|
423 |
] |
|
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
424 |
|
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
425 |
|
118 | 426 |
example (connect to above server and send some data; |
4414 | 427 |
Notice, this fails, if above server code is executed in the same ST/X image |
428 |
(at least on LINUX), since the OS does not correctly handle |
|
429 |
a connect from within an interrupted accept system call |
|
430 |
On SGI's SVR4, this works ok |
|
431 |
[exBegin] |
|
432 |
|sock| |
|
433 |
||
434 |
sock := Socket newUNIXclientTo:'/tmp/ud_socket'. |
|
435 |
sock isNil ifTrue:[ |
|
436 |
Transcript showCR:'nope' |
|
437 |
] ifFalse:[ |
|
438 |
sock buffered:false. |
|
439 |
Transcript showCR:'client: got it'. |
|
440 |
'can now do transfer via sock'. |
|
441 |
Transcript showCR:'sending <hello>'. |
|
442 |
sock nextPutLine:'hello'. |
|
443 |
sock close |
|
444 |
] |
|
445 |
[exEnd] |
|
1007 | 446 |
|
447 |
||
448 |
example (UDP await packet from a client and read some data): |
|
4414 | 449 |
[exBegin] |
450 |
|udpSock sock addr n dataBuffer| |
|
451 |
||
452 |
udpSock := Socket newUDPserverAtPort:9999. |
|
453 |
udpSock isNil ifTrue:[ |
|
454 |
Transcript showCR:'socket setup failed.'. |
|
455 |
] ifFalse:[ |
|
456 |
Transcript showCR:'wait'. |
|
457 |
udpSock readWait. |
|
458 |
||
459 |
addr := IPSocketAddress new. |
|
460 |
dataBuffer := ByteArray new:1000. |
|
461 |
n := udpSock receiveFrom:addr buffer:dataBuffer start:1 for:dataBuffer size. |
|
462 |
n > 0 ifTrue:[ |
|
463 |
Transcript showCR:('got: ' , n printString , 'bytes from ' , addr printString). |
|
464 |
Transcript showCR:('data: ' , (dataBuffer copyTo:n) printString). |
|
465 |
] ifFalse:[ |
|
466 |
Transcript showCR:'read failed'. |
|
467 |
]. |
|
468 |
||
469 |
Transcript showCR:'close'. |
|
470 |
udpSock close |
|
471 |
] |
|
472 |
[exEnd] |
|
1007 | 473 |
example (connect to above UDP server and send some data; |
4414 | 474 |
[exBegin] |
475 |
|sock| |
|
476 |
||
477 |
sock := Socket newUDP. |
|
478 |
sock isNil ifTrue:[ |
|
479 |
Transcript showCR:'nope' |
|
480 |
] ifFalse:[ |
|
481 |
sock sendTo:(IPSocketAddress new hostName:'localhost' port:9999) buffer:'hello world'. |
|
482 |
sock close |
|
483 |
] |
|
484 |
[exEnd] |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
485 |
|
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
486 |
example: pingWalk (try to ping hosts on the local network) |
2824 | 487 |
Note: it dosen't use ICMP ping, but tries to reache the echo service, |
4414 | 488 |
which is disabled on most OS. |
489 |
[exBegin] |
|
490 |
|myAddress list top hosts walkProcess port| |
|
491 |
||
492 |
myAddress := OperatingSystem getNetworkAddresses |
|
493 |
keysAndValuesSelect:[:eachIFName :eachAddress| |
|
494 |
eachAddress isLocal not |
|
495 |
and:[eachIFName = 'wlan0'] |
|
496 |
]. |
|
497 |
myAddress := myAddress first hostAddress. |
|
498 |
||
499 |
port := Socket portOfService:'echo'. |
|
500 |
port isNil ifTrue:[ |
|
501 |
self error:'dont know echo port'. |
|
502 |
^ self |
|
503 |
]. |
|
504 |
||
505 |
top := StandardSystemView new. |
|
506 |
top label:'PING net walk'. |
|
507 |
||
508 |
list := ScrollableView for:ListView in:top. |
|
509 |
list origin:0.0@0.0 corner:1.0@1.0. |
|
510 |
||
511 |
top openAndWait. |
|
512 |
||
513 |
walkProcess := [ |
|
514 |
|l low hi direction tryHostID dottedName hostName conn addr| |
|
515 |
||
516 |
l := SortedCollection new. |
|
517 |
||
518 |
' only works with type C-net |
|
519 |
the code below could simply do 1 to:254 do:[:hostID } |
|
520 |
but, to probe likely hosts earlier, the probing is done |
|
521 |
ping-pong like around my ip-address (assuming, that other machines |
|
522 |
have numbers around my own)'. |
|
523 |
||
524 |
low := hi := (myAddress at:4). |
|
525 |
direction := 1. |
|
526 |
||
527 |
[low > 0 or:[hi < 255]] whileTrue:[ |
|
528 |
direction > 0 ifTrue:[ |
|
529 |
hi := hi + 1. |
|
530 |
tryHostID := hi. |
|
531 |
direction := -1. |
|
532 |
] ifFalse:[ |
|
533 |
low := low - 1. |
|
534 |
tryHostID := low. |
|
535 |
direction := 1. |
|
536 |
]. |
|
537 |
(tryHostID between:1 and:254) ifTrue:[ |
|
538 |
dottedName := (myAddress at:1) printString |
|
539 |
, '.' , (myAddress at:2) printString |
|
540 |
, '.' , (myAddress at:3) printString |
|
541 |
, '.' , tryHostID printString. |
|
542 |
||
543 |
top label:'PING net walk - trying ' , dottedName. |
|
544 |
||
545 |
top windowGroup withCursor:Cursor wait do:[ |
|
546 |
conn := Socket newTCPclientToHost:dottedName port:port withTimeout:1000. |
|
547 |
conn notNil ifTrue:[ |
|
548 |
addr := Socket ipAddressOfHost:dottedName. |
|
549 |
hostName := Socket hostWithIpAddress:addr. |
|
550 |
hostName isNil ifTrue:[ |
|
551 |
hostName :='?' |
|
552 |
]. |
|
553 |
l add:(dottedName paddedTo:15 with:Character space) |
|
554 |
, ' ' |
|
555 |
, (hostName paddedTo:15 with:Character space) |
|
556 |
, ' up & reachable'. |
|
557 |
list list:l. |
|
558 |
conn close. |
|
559 |
] |
|
560 |
]. |
|
561 |
]. |
|
562 |
]. |
|
563 |
top label:'PING reachable hosts'. |
|
564 |
] forkAt:(Processor userBackgroundPriority). |
|
565 |
walkProcess name:'ping net walker'. |
|
566 |
[exEnd] |
|
567 |
||
568 |
||
569 |
This example creates a simple UDP server that accepts |
|
570 |
single packets from anybody and broadcasts them to all |
|
571 |
clients that have connected so far. |
|
572 |
||
573 |
[exBegin] |
|
574 |
| socket address buffer msgSize clients | |
|
575 |
clients := Set new. |
|
576 |
address := IPSocketAddress new. |
|
577 |
buffer := String new: 1024. |
|
578 |
||
579 |
socket := self newUDPserverAtPort: 6666. |
|
580 |
||
581 |
Transcript showCR: 'server starting'. |
|
582 |
||
583 |
[ |
|
584 |
[true] whileTrue: [ |
|
585 |
(socket readWaitWithTimeoutMs: 200) ifFalse: [ |
|
586 |
msgSize := socket |
|
587 |
receiveFrom: address |
|
588 |
buffer: buffer |
|
589 |
start: 1 |
|
590 |
for: buffer size. |
|
591 |
||
592 |
clients add: address copy. |
|
593 |
clients do: [ :clientAddress | |
|
594 |
socket |
|
595 |
sendTo: clientAddress |
|
596 |
buffer: buffer |
|
597 |
start: 1 |
|
598 |
for: msgSize]] |
|
599 |
] |
|
600 |
] ensure:[ |
|
601 |
Transcript showCR: 'server shutting down'. |
|
602 |
socket close |
|
603 |
] |
|
604 |
[exEnd] |
|
817 | 605 |
|
606 |
send a datagram to above server: |
|
4414 | 607 |
[exBegin] |
608 |
||
609 |
| socket address buffer host msg | |
|
610 |
||
611 |
host := Dialog |
|
612 |
request: 'What is the name of the server''s host?' |
|
613 |
initialAnswer: 'localhost'. |
|
614 |
||
615 |
socket := self newUDP. |
|
616 |
||
617 |
address := IPSocketAddress hostName: host port: 6666. |
|
618 |
||
619 |
buffer := ByteArray new: 1000. |
|
620 |
[ |
|
621 |
[(msg := Dialog request: 'Say something') isEmpty] whileFalse:[ |
|
622 |
| replySize stream | |
|
623 |
||
624 |
socket writeWait. |
|
625 |
stream := buffer writeStream. |
|
626 |
stream nextPutAll: msg. |
|
627 |
socket sendTo:address buffer:buffer start:1 for:stream position. |
|
628 |
socket readWait. |
|
629 |
||
630 |
replySize := socket receiveFrom:address buffer:buffer. |
|
631 |
replySize > 0 ifTrue: [ |
|
632 |
Transcript cr; nextPutAll: 'Server acknowledged: '. |
|
633 |
Transcript show: ((buffer copyFrom: 1 to: replySize) asString) |
|
634 |
] |
|
635 |
] |
|
636 |
] ensure: [socket close]. |
|
637 |
Transcript cr |
|
638 |
[exEnd] |
|
1316 | 639 |
|
640 |
||
641 |
loopBack: |
|
4414 | 642 |
[exBegin] |
643 |
||
644 |
|readerTask readingSocket writingSocket| |
|
645 |
||
646 |
readingSocket := self newTCPserverAtPort:9999. |
|
647 |
readerTask := |
|
648 |
[ |
|
649 |
|connection| |
|
650 |
||
651 |
readingSocket listenFor:1. |
|
652 |
connection := readingSocket accept. |
|
653 |
readingSocket close. |
|
654 |
[connection atEnd] whileFalse:[ |
|
655 |
Transcript showCR:(connection nextLine). |
|
656 |
]. |
|
657 |
connection close. |
|
658 |
] fork. |
|
659 |
||
660 |
Delay waitForSeconds:1. |
|
661 |
writingSocket := self newTCPclientToHost:'localhost' port:9999. |
|
662 |
writingSocket nextPutLine:'Hello'. |
|
663 |
writingSocket nextPutLine:'World'. |
|
664 |
writingSocket close. |
|
665 |
[exEnd] |
|
25 | 666 |
" |
667 |
! ! |
|
668 |
||
1462 | 669 |
!Socket class methodsFor:'instance creation'! |
126 | 670 |
|
3059 | 671 |
bindTo:aSocketAddress type:aTypeSymbol |
672 |
"create a socket for a specific type |
|
673 |
and bind it to aSocketAddress. |
|
674 |
Type must be: |
|
3217 | 675 |
#stream, #datagram or #raw |
3059 | 676 |
|
677 |
Neither connect nor connect-wait is done." |
|
678 |
||
679 |
|newSock| |
|
680 |
||
681 |
newSock := self domain:aSocketAddress domain type:aTypeSymbol. |
|
682 |
[ |
|
3217 | 683 |
newSock bindTo:aSocketAddress reuseAddress:true. |
3059 | 684 |
] ifCurtailed:[ |
3217 | 685 |
newSock close. |
3059 | 686 |
]. |
687 |
^ newSock |
|
688 |
||
689 |
||
690 |
" |
|
3217 | 691 |
Socket bindTo:(IPSocketAddress anyHost port:8081) type:#stream. |
692 |
Socket bindTo:(IPv6SocketAddress anyHost port:8081) type:#datagram. |
|
3059 | 693 |
" |
694 |
! |
|
695 |
||
126 | 696 |
domain:domainSymbol type:type |
697 |
"create a socket for domain and type - |
|
698 |
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
|
699 |
Domain must be one of the symbols: |
3217 | 700 |
#inet, #unix, #appletalk, #decnet, #xns, ...; |
819 | 701 |
Type must be: |
3217 | 702 |
#stream, #datagram or #raw |
3059 | 703 |
|
704 |
XXX: currently only the #AF_INET and #AF_INET6 and #AF_UNIX domains are supported" |
|
126 | 705 |
|
706 |
^ self new domain:domainSymbol type:type |
|
707 |
||
708 |
" |
|
3059 | 709 |
Socket domain:#AF_INET type:#stream |
710 |
Socket domain:#AF_INET type:#datagram |
|
711 |
Socket domain:#AF_INET6 type:#stream |
|
712 |
Socket domain:#AF_INET6 type:#datagram |
|
713 |
Socket domain:#AF_UNIX type:#stream |
|
126 | 714 |
" |
715 |
! |
|
716 |
||
717 |
newTCP |
|
718 |
"create a TCP socket - no binding or other setup is done, |
|
719 |
neither connect nor connect-wait is done." |
|
720 |
||
3059 | 721 |
^ self new domain:#'AF_INET' type:#stream |
722 |
||
723 |
" |
|
724 |
Socket newTCP |
|
725 |
" |
|
126 | 726 |
! |
727 |
||
728 |
newTCP:aServiceOrNil |
|
729 |
"create a TCP socket for a service - |
|
730 |
neither connect nor connect-wait is done." |
|
731 |
||
3059 | 732 |
|newSock socketAddress socketAddressClass| |
126 | 733 |
|
734 |
newSock := self newTCP. |
|
735 |
(newSock notNil and:[aServiceOrNil notNil]) ifTrue:[ |
|
3217 | 736 |
[ |
737 |
socketAddressClass := newSock socketAddressClass. |
|
738 |
socketAddress := socketAddressClass hostName:nil serviceName:aServiceOrNil type:#stream. |
|
739 |
socketAddress hostAddress:socketAddressClass anyAddress. |
|
740 |
newSock bindTo:socketAddress reuseAddress:true. |
|
741 |
] ifCurtailed:[ |
|
742 |
newSock close. |
|
743 |
] |
|
126 | 744 |
]. |
745 |
^ newSock |
|
746 |
||
747 |
||
1911 | 748 |
" |
3217 | 749 |
Socket newTCP:'http-alt'. |
750 |
Socket newTCP:9996. |
|
1911 | 751 |
" |
126 | 752 |
! |
753 |
||
576 | 754 |
newTCPclientToAddress:aHostAddress port:aService |
755 |
"create a new TCP client socket connecting to a service. |
|
756 |
Return a socket instance if ok, nil on failure. |
|
757 |
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
|
758 |
not the whole smalltalk). |
576 | 759 |
See also: #newTCPclientToAddress:port:withTimeout:" |
760 |
||
761 |
^ self newTCPclientToAddress:aHostAddress port:aService withTimeout:nil |
|
762 |
! |
|
763 |
||
3059 | 764 |
newTCPclientToAddress:aSocketAddressOrByteArray port:aService withTimeout:millis |
576 | 765 |
"create a new TCP client socket connecting to a service. |
766 |
Return a socket instance if ok, nil on failure. |
|
767 |
If the millis arg is nonNil, stop trying to connect after that many milliseconds |
|
811 | 768 |
and return nil." |
576 | 769 |
|
3059 | 770 |
|port socketAddress| |
771 |
||
772 |
aService isString ifTrue:[ |
|
3217 | 773 |
port := self portOfService:aService protocol:#tcp. |
3059 | 774 |
] ifFalse:[ |
3217 | 775 |
port := aService. |
3059 | 776 |
]. |
3217 | 777 |
socketAddress := aSocketAddressOrByteArray isSocketAddress |
778 |
ifTrue:[aSocketAddressOrByteArray] |
|
779 |
ifFalse:[ |
|
780 |
"Passing ByteArrays is obsolete and only supported for IPv4" |
|
781 |
IPSocketAddress hostAddress:aSocketAddressOrByteArray |
|
782 |
]. |
|
3059 | 783 |
port notNil ifTrue:[ |
3217 | 784 |
socketAddress port:port. |
3059 | 785 |
]. |
786 |
^ self newTCPclientToAddress:socketAddress withTimeout:millis |
|
576 | 787 |
! |
788 |
||
3059 | 789 |
newTCPclientToAddress:aSocketAddress withTimeout:millis |
126 | 790 |
"create a new TCP client socket connecting to a service. |
791 |
Return a socket instance if ok, nil on failure. |
|
3059 | 792 |
If the millis arg is nonNil, stop trying to connect after that many milliseconds |
793 |
and return nil." |
|
794 |
||
795 |
|socket| |
|
796 |
||
797 |
socket := self domain:aSocketAddress domain type:#stream. |
|
798 |
(socket connectTo:aSocketAddress withTimeout:millis) ifFalse:[ |
|
3217 | 799 |
socket close. |
800 |
^ nil |
|
3059 | 801 |
]. |
802 |
^ socket. |
|
803 |
||
804 |
" |
|
3217 | 805 |
self newTCPclientToAddress:(IPv6SocketAddress hostName:'www.exept.de' port:80) withTimeout:nil |
3059 | 806 |
" |
807 |
! |
|
808 |
||
809 |
newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName |
|
810 |
"create a new TCP client socket connecting to a service. |
|
3985 | 811 |
If hostNameOrAddress is a string, try all the resolved addresses regardless |
3988 | 812 |
whether for IPv4 or IPv6. |
3059 | 813 |
Return a socket instance if ok, nil on failure. |
126 | 814 |
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
|
815 |
not the whole smalltalk). |
126 | 816 |
See also: #newTCPclientToHost:port:withTimeout:" |
817 |
||
3590 | 818 |
^ self |
4414 | 819 |
newTCPclientToHost:hostNameOrAddress |
820 |
port:aPortOrServiceName |
|
821 |
domain:self defaultIpDomainForConnect |
|
822 |
withTimeout:nil |
|
126 | 823 |
|
824 |
" |
|
4239 | 825 |
Socket newTCPclientToHost:'www.exept.de' port:'https' |
126 | 826 |
" |
827 |
||
828 |
"Created: 31.10.1995 / 18:54:11 / cg" |
|
829 |
! |
|
830 |
||
3059 | 831 |
newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName domain:aDomainSymbolOrNil withTimeout:millis |
832 |
"create a new TCP client socket connecting to a service on hostNameOrAddress. |
|
3985 | 833 |
If hostNameOrAddress is a string, try all the resolved addresses. |
126 | 834 |
Return a socket instance if ok, nil on failure. |
3059 | 835 |
Set aDomainSymbolOrNil to #AF_INET of #AF_INET6 to connect via a defined protocol. |
836 |
Set aDomainSymbolOrNil to nil, to try all protocols. |
|
126 | 837 |
If the millis arg is nonNil, stop trying to connect after that many milliseconds |
3059 | 838 |
and return nil." |
839 |
||
4341 | 840 |
|socket addressList lastDomainSymbol| |
3059 | 841 |
|
842 |
hostNameOrAddress isString ifFalse:[ |
|
4414 | 843 |
^ self newTCPclientToAddress:hostNameOrAddress port:aPortOrServiceName withTimeout:millis. |
126 | 844 |
]. |
3059 | 845 |
|
4341 | 846 |
addressList := SocketAddress |
4414 | 847 |
allForHostName:hostNameOrAddress |
848 |
serviceName:aPortOrServiceName |
|
849 |
domain:aDomainSymbolOrNil |
|
850 |
type:#stream. |
|
4341 | 851 |
|
852 |
addressList do:[:eachAddress| |
|
4414 | 853 |
|domainSymbol| |
854 |
||
855 |
domainSymbol := eachAddress domain. |
|
856 |
domainSymbol ~~ lastDomainSymbol ifTrue:[ |
|
857 |
socket notNil ifTrue:[ |
|
858 |
socket close. |
|
859 |
]. |
|
860 |
socket := self new domain:domainSymbol type:#stream. |
|
861 |
lastDomainSymbol := domainSymbol. |
|
862 |
]. |
|
863 |
(socket connectTo:eachAddress withTimeout:millis) ifTrue:[ |
|
864 |
^ socket. |
|
865 |
]. |
|
3059 | 866 |
]. |
867 |
socket notNil ifTrue:[ |
|
4414 | 868 |
socket close. |
3059 | 869 |
]. |
870 |
^ nil. |
|
871 |
||
126 | 872 |
" |
3059 | 873 |
Socket newTCPclientToHost:'www.exept.de' port:80 domain:#'AF_INET' withTimeout:1000. |
874 |
Socket newTCPclientToHost:'www.exept.de' port:80 domain:#'AF_INET6' withTimeout:1000. |
|
875 |
Socket newTCPclientToHost:'www.exept.de' port:80 domain:nil withTimeout:1000. |
|
126 | 876 |
Socket newTCPclientToHost:'localhost' port:'nntp' withTimeout:1000 |
877 |
" |
|
604 | 878 |
|
4341 | 879 |
"Modified: / 21-02-2017 / 21:36:16 / stefan" |
126 | 880 |
! |
881 |
||
3217 | 882 |
newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName withTimeout:millis |
3059 | 883 |
"create a new TCP client socket connecting to a service on hostNameOrAddress. |
3985 | 884 |
If hostNameOrAddress is a string, try all the resolved addresses regardless |
3988 | 885 |
whether for IPv4 or IPv6. |
3059 | 886 |
Return a socket instance if ok, nil on failure. |
887 |
If the millis arg is nonNil, stop trying to connect after that many milliseconds |
|
888 |
and return nil." |
|
3217 | 889 |
|
890 |
^ self |
|
4414 | 891 |
newTCPclientToHost:hostNameOrAddress |
892 |
port:aPortOrServiceName |
|
893 |
domain:self defaultIpDomainForConnect |
|
894 |
withTimeout:millis |
|
3059 | 895 |
! |
896 |
||
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
897 |
newTCPserverAtAnonymousPort |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
898 |
"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
|
899 |
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
|
900 |
|
811 | 901 |
^ self newTCPserverAtPort:0 |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
902 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
903 |
! |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
904 |
|
126 | 905 |
newTCPserverAtPort:aService |
906 |
"create a new TCP server socket providing service." |
|
907 |
||
1911 | 908 |
^ self newTCP:aService |
126 | 909 |
! |
910 |
||
911 |
newUDP |
|
912 |
"create a UDP socket - no binding or other setup is done, |
|
913 |
neither connect nor connect-wait is done." |
|
914 |
||
3059 | 915 |
^ self new domain:#'AF_INET' type:#datagram |
126 | 916 |
|
917 |
"Socket newUDP" |
|
918 |
! |
|
919 |
||
920 |
newUDP:aServiceOrNil |
|
921 |
"create a UDP socket for a service - |
|
922 |
neither connect nor connect-wait is done." |
|
923 |
||
3059 | 924 |
|newSock socketAddressClass socketAddress| |
126 | 925 |
|
926 |
newSock := self newUDP. |
|
927 |
(newSock notNil and:[aServiceOrNil notNil]) ifTrue:[ |
|
3217 | 928 |
[ |
929 |
socketAddressClass := newSock socketAddressClass. |
|
930 |
socketAddress := socketAddressClass hostName:nil serviceName:aServiceOrNil type:#stream. |
|
931 |
socketAddress hostAddress:socketAddressClass anyAddress. |
|
932 |
newSock bindTo:socketAddress reuseAddress:true. |
|
933 |
] ifCurtailed:[ |
|
934 |
newSock close. |
|
935 |
] |
|
126 | 936 |
]. |
937 |
^ newSock |
|
938 |
||
1911 | 939 |
" |
3217 | 940 |
Socket newUDP:4444. |
941 |
Socket newUDP:'activesync'. |
|
1911 | 942 |
" |
126 | 943 |
! |
944 |
||
945 |
newUDPserverAtPort:aService |
|
946 |
"create a new UDP server socket providing service." |
|
947 |
||
1911 | 948 |
^ self newUDP:aService |
126 | 949 |
! |
950 |
||
951 |
newUNIX |
|
952 |
"create a UNIX domain socket - no binding or other setup is done, |
|
811 | 953 |
neither connect nor connect-wait is done. |
954 |
If the system does not support unix domain sockets (i.e. VMS or MSDOS), |
|
955 |
return nil." |
|
126 | 956 |
|
3059 | 957 |
^ self new domain:#'AF_UNIX' type:#stream |
126 | 958 |
|
959 |
" |
|
960 |
Socket newUNIX |
|
961 |
" |
|
962 |
! |
|
963 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
964 |
newUNIXclientTo:pathName |
126 | 965 |
"create a new UNIX client socket connecting to a pathname. |
966 |
Return a socket instance if ok, nil on failure. |
|
967 |
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
|
968 |
not the whole smalltalk). |
811 | 969 |
If the system does not support unix domain sockets (i.e. VMS or MSDOS), |
970 |
return nil. |
|
126 | 971 |
See also: #newUNIXclientTo:withTimeout:" |
972 |
||
973 |
^ self newUNIXclientTo:pathName withTimeout:nil |
|
974 |
||
975 |
! |
|
976 |
||
977 |
newUNIXclientTo:pathName withTimeout:millis |
|
978 |
"create a new UNIX client socket connecting to a pathname. |
|
979 |
Return a socket instance if ok, nil on failure. |
|
980 |
If the millis arg is nonNil, stop trying to connect after that many milliseconds |
|
811 | 981 |
and return nil. |
982 |
If the system does not support unix domain sockets (i.e. VMS or MSDOS), |
|
983 |
return nil." |
|
126 | 984 |
|
985 |
|newSock| |
|
986 |
||
987 |
newSock := self newUNIX. |
|
988 |
newSock notNil ifTrue:[ |
|
3217 | 989 |
(newSock connectTo:(UDSocketAddress name:pathName) withTimeout:millis) ifFalse:[ |
990 |
newSock close. |
|
991 |
^ nil |
|
992 |
] |
|
126 | 993 |
]. |
994 |
^ newSock |
|
995 |
||
996 |
" |
|
3217 | 997 |
Socket newUNIXclientTo:'/tmp/foo' |
126 | 998 |
" |
999 |
! |
|
1000 |
||
1001 |
newUNIXserverAt:pathName |
|
811 | 1002 |
"create a new UNIX server socket providing service at a pathname. |
1003 |
If the system does not support unix domain sockets (i.e. VMS or MSDOS), |
|
1004 |
return nil." |
|
126 | 1005 |
|
3059 | 1006 |
^ self bindTo:(UDSocketAddress name:pathName) type:#stream. |
126 | 1007 |
|
1008 |
" |
|
1009 |
|s s2| |
|
1010 |
||
3059 | 1011 |
'/tmp/foo' asFilename remove. |
126 | 1012 |
s := Socket newUNIXserverAt:'/tmp/foo'. |
1013 |
s listenFor:5. |
|
1014 |
s2 := s accept. |
|
1015 |
" |
|
1016 |
! ! |
|
1017 |
||
1462 | 1018 |
!Socket class methodsFor:'Compatibility-ST80'! |
1019 |
||
1020 |
family:domainSymbol type:typeSymbol |
|
1021 |
"create a socket for domain and type - ST80 simply uses a different name. |
|
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
1022 |
Domain must be one of the symbols: #inet, #unix, #appletalk or #ns; |
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
1023 |
Type must be #stream, #datagram or #raw." |
1462 | 1024 |
|
1025 |
^ self domain:domainSymbol type:typeSymbol |
|
1026 |
||
1027 |
" |
|
3059 | 1028 |
Socket family:#AF_INET type:#stream |
1029 |
Socket family:#AF_INET type:#datagram |
|
1030 |
Socket family:#AF_UNIX type:#stream |
|
1462 | 1031 |
" |
1032 |
! |
|
1033 |
||
1034 |
getHostname |
|
3462 | 1035 |
"return the computer's hostname string" |
1462 | 1036 |
|
1037 |
^ OperatingSystem getHostName |
|
1038 |
||
1039 |
"Created: / 27.2.1998 / 02:32:17 / cg" |
|
1040 |
! |
|
1041 |
||
1042 |
sockStream |
|
1043 |
"return the type code for stream sockets" |
|
1044 |
||
1045 |
^ #stream |
|
1046 |
! ! |
|
1047 |
||
1600 | 1048 |
!Socket class methodsFor:'Compatibility-Squeak'! |
1049 |
||
2900 | 1050 |
deadlineSecs:seconds |
1051 |
^ Timestamp now addSeconds:seconds |
|
1052 |
! |
|
1053 |
||
1600 | 1054 |
initializeNetwork |
1055 |
"/ intentionally left blank here |
|
1056 |
! |
|
1057 |
||
3485 | 1058 |
standardDeadline |
1059 |
"a standard timeout in seconds for connection setup; |
|
1060 |
not really used in ST/X code (but by some code ported from squeak)" |
|
1061 |
||
1062 |
^ 30 |
|
1063 |
! |
|
1064 |
||
2292 | 1065 |
standardTimeout |
3485 | 1066 |
"a standard timeout in seconds for transfers; |
1067 |
not really used in ST/X code (but by some code ported from squeak)" |
|
1068 |
||
2292 | 1069 |
^ 30 |
1070 |
! |
|
1071 |
||
1600 | 1072 |
wildcardPort |
1073 |
^ nil |
|
1074 |
! ! |
|
1075 |
||
1462 | 1076 |
!Socket class methodsFor:'Compatibility-VW'! |
1077 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1078 |
AF_INET |
1462 | 1079 |
^ #AF_INET |
1080 |
! |
|
1081 |
||
1082 |
SOCK_STREAM |
|
1083 |
^ #SOCK_STREAM |
|
1084 |
! ! |
|
1085 |
||
1086 |
!Socket class methodsFor:'Signal constants'! |
|
1087 |
||
1088 |
brokenConnectionSignal |
|
1089 |
"return the signal used to tell broken connections. |
|
1090 |
Since in unix, this is the same as the broken pipe signal, |
|
1091 |
return that one. |
|
1092 |
(for other Operatingsystems, this may change ..)" |
|
1093 |
||
1094 |
^ PipeStream brokenPipeSignal |
|
1095 |
! |
|
1096 |
||
1097 |
invalidArgumentsSignal |
|
1098 |
"dummy for compatibility" |
|
1099 |
||
1100 |
^ self errorSignal |
|
1101 |
! ! |
|
1102 |
||
1103 |
!Socket class methodsFor:'debugging'! |
|
1104 |
||
1105 |
debug:aBoolean |
|
1106 |
"turn on/off internal debugprints. |
|
1107 |
This method is for ST/X debugging only and |
|
1108 |
may be removed in later versions" |
|
1109 |
||
1110 |
%{ /* NOCONTEXT */ |
|
1111 |
||
1112 |
__debugging__ = (aBoolean == true); |
|
1113 |
%} |
|
1114 |
" |
|
1115 |
Socket debug:true |
|
1116 |
Socket debug:false |
|
1117 |
" |
|
1118 |
! ! |
|
1119 |
||
3059 | 1120 |
!Socket class methodsFor:'defaults'! |
1121 |
||
1122 |
defaultIpDomainForConnect |
|
1123 |
"answer the domain used to look up host names for connect: |
|
4414 | 1124 |
#AF_INET use only IPv4 |
1125 |
#AF_INET6 use only IPv6 |
|
1126 |
nil use both IPv4 and IPv6" |
|
4239 | 1127 |
|
1128 |
^ nil |
|
3059 | 1129 |
! ! |
1130 |
||
1911 | 1131 |
!Socket class methodsFor:'obsolete'! |
1132 |
||
1133 |
connectTo:service on:host |
|
1134 |
<resource: #obsolete> |
|
1135 |
"standard & easy client setup: |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1136 |
create new client tcp socket, bind and connect; |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1137 |
return the socket. |
1911 | 1138 |
The thread blocks (interruptable), until the connection is established." |
1139 |
||
1140 |
^ self newTCPclientToHost:host port:(self portOfService:service). |
|
1141 |
||
1142 |
" |
|
1143 |
Socket connectTo:9995 on:'clam' |
|
1144 |
Socket connectTo:4711 on:'exept' |
|
1145 |
Socket connectTo:'finger' on:'clam' |
|
1146 |
Socket connectTo:'ftp' on:'exept' |
|
1147 |
Socket connectTo:'nntp' on:(OperatingSystem getEnvironment:'NNTPSERVER') |
|
1148 |
" |
|
1149 |
! |
|
1150 |
||
1151 |
networkLongOrderIsMSB |
|
1152 |
"return the well known fact, that network byte order is most significant byte first" |
|
1153 |
||
1154 |
<resource: #obsolete> |
|
1155 |
||
1156 |
^ true |
|
1157 |
! |
|
1158 |
||
1159 |
networkShortOrderIsMSB |
|
1160 |
"return the well known fact, that network byte order is most significant byte first" |
|
1161 |
||
1162 |
<resource: #obsolete> |
|
1163 |
||
1164 |
^ true |
|
1165 |
! |
|
1166 |
||
1167 |
provide:aService |
|
1168 |
"standard & easy server setup: |
|
1169 |
create a new TCP server socket providing a service." |
|
1170 |
||
1171 |
<resource:#obsolete> |
|
1172 |
||
1173 |
|newSock| |
|
1174 |
||
1175 |
self obsoleteMethodWarning:'use #newTCPserverAtPort: / #listen'. |
|
1176 |
||
1177 |
newSock := self newTCPserverAtPort:(self portOfService:aService). |
|
1178 |
newSock notNil ifTrue:[ |
|
1179 |
newSock listenFor:5. |
|
1180 |
]. |
|
1181 |
^ newSock |
|
1182 |
||
1183 |
" |
|
1184 |
Socket provide:9995 |
|
1185 |
(Socket provide:9996) accept |
|
1186 |
Socket provide:'nntp' |
|
1187 |
" |
|
1188 |
! ! |
|
1189 |
||
1190 |
!Socket class methodsFor:'obsolete host queries'! |
|
525 | 1191 |
|
1192 |
appletalkAddressOfHost:aHostName |
|
1911 | 1193 |
<resource: #obsolete> |
525 | 1194 |
"return the APPLETALK address for a hostname as a byteArray, |
819 | 1195 |
where the network bytes come first (no matter what the local byteorder is) |
1196 |
followed by the node byte. |
|
525 | 1197 |
If the host is unknown, return nil. |
812 | 1198 |
This is the reverse operation to #hostWithAppletalkAddress:. |
1199 |
WARNING: untested code - I have no appletalk to test this." |
|
525 | 1200 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1201 |
NameLookupError |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1202 |
handle:[:ex | |
1343 | 1203 |
^ nil |
1204 |
] |
|
1205 |
do:[ |
|
1206 |
^ (AppletalkSocketAddress hostName:aHostName) address |
|
1207 |
] |
|
525 | 1208 |
! |
1209 |
||
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
1210 |
hostWithAppletalkAddress:addrByteArray |
1911 | 1211 |
<resource: #obsolete> |
525 | 1212 |
"return the hostname for an APPLETALK address. |
1213 |
The address is supposed to be a byteArray consisting of 3 bytes, |
|
1214 |
the network bytes come first (no matter what the local byteorder is). |
|
1215 |
The last byte is the node number. |
|
4320 | 1216 |
Nil is returned for an unknown host or if it's not an appletalk host. |
812 | 1217 |
This is is the reverse operation to #appletalkAddressOfHost:. |
1218 |
WARNING: untested code - I have no appletalk to test this." |
|
525 | 1219 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1220 |
NameLookupError |
4414 | 1221 |
handle:[:ex | |
1222 |
^ nil |
|
1223 |
] |
|
1224 |
do:[ |
|
1225 |
^ (AppletalkSocketAddress hostAddress:addrByteArray) hostName |
|
1226 |
] |
|
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
1227 |
|
525 | 1228 |
" |
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
1229 |
Socket appletalkAddressOfHost:'yourAppleHere' |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1230 |
Socket hostWithAppletalkAddress:#[1 2 3] |
525 | 1231 |
" |
4320 | 1232 |
|
1233 |
"Modified (comment): / 13-02-2017 / 20:30:56 / cg" |
|
126 | 1234 |
! |
1235 |
||
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
1236 |
hostWithIpAddress:addrByteArray |
1911 | 1237 |
<resource: #obsolete> |
126 | 1238 |
"return the hostname for an IP (internet-) address. |
1239 |
The address is supposed to be a byteArray consisting of 4 bytes, |
|
1240 |
the network bytes come first (no matter what the local byteorder is). |
|
4320 | 1241 |
Nil is returned for an unknown host or if it's not an internet host. |
126 | 1242 |
This is the reverse operation to #ipAddressOfHost:." |
1243 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1244 |
NameLookupError |
4414 | 1245 |
handle:[:ex | |
1246 |
^ nil |
|
1247 |
] |
|
1248 |
do:[ |
|
1249 |
^ (IPSocketAddress hostAddress:addrByteArray) hostName |
|
1250 |
] |
|
126 | 1251 |
|
1252 |
" |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1253 |
Socket ipAddressOfHost:'clam' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1254 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'clam') |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1255 |
Socket ipAddressOfHost:'porty' |
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
1256 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'porty') |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1257 |
Socket hostWithIpAddress:#[1 2 3 4] |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1258 |
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
|
1259 |
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
|
1260 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'www.altavista.com') |
126 | 1261 |
" |
4320 | 1262 |
|
1263 |
"Modified (comment): / 13-02-2017 / 20:31:01 / cg" |
|
126 | 1264 |
! |
1265 |
||
1278
7251c65b8d38
use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents:
1274
diff
changeset
|
1266 |
hostWithIpV6Address:addrByteArray |
1911 | 1267 |
<resource: #obsolete> |
820
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1268 |
"return the hostname for an IPv6 (internet-) address. |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1269 |
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
|
1270 |
the network bytes come first (no matter what the local byteorder is). |
4320 | 1271 |
Nil is returned for an unknown host or if it's not an internet host. |
820
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1272 |
This is the reverse operation to #ipV6AddressOfHost:." |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1273 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1274 |
NameLookupError |
4414 | 1275 |
handle:[:ex | |
1276 |
^ nil |
|
1277 |
] |
|
1278 |
do:[ |
|
1279 |
^ (IPv6SocketAddress hostAddress:addrByteArray) hostName |
|
1280 |
] |
|
820
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1281 |
|
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1282 |
" |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1283 |
Socket ipV6AddressOfHost:'clam' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1284 |
Socket hostWithIpV6Address:(Socket ipAddressOfHost:'clam') |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1285 |
Socket ipV6AddressOfHost:'porty' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1286 |
Socket hostWithIpV6Address:(Socket ipAddressOfHost:'porty') |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1287 |
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
|
1288 |
Socket ipV6AddressOfHost:'www.exept.de' |
820
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1289 |
" |
4320 | 1290 |
|
1291 |
"Modified (comment): / 13-02-2017 / 20:31:08 / cg" |
|
820
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1292 |
! |
b6860c3660c8
Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents:
819
diff
changeset
|
1293 |
|
6 | 1294 |
ipAddressOfHost:aHostName |
1911 | 1295 |
<resource: #obsolete> |
98
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
1296 |
"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
|
1297 |
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
|
1298 |
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
|
1299 |
This is the reverse operation to #hostWithIpAddress:." |
6 | 1300 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1301 |
NameLookupError |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1302 |
handle:[:ex | |
1343 | 1303 |
^ nil |
1304 |
] |
|
1305 |
do:[ |
|
1306 |
^ (IPSocketAddress hostName:aHostName) address |
|
1307 |
] |
|
21 | 1308 |
|
71 | 1309 |
" |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1310 |
Socket ipAddressOfHost:'localhost' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1311 |
Socket ipAddressOfHost:'exept' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1312 |
Socket ipAddressOfHost:'1.2.3.4' |
1274
f257a1b16b4d
use naemLookup from SocketAddress for ipv6
Claus Gittinger <cg@exept.de>
parents:
1263
diff
changeset
|
1313 |
Socket ipAddressOfHost:'193.15.16.17' |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1314 |
Socket ipAddressOfHost:'josef' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1315 |
Socket ipAddressOfHost:'styx.com' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1316 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'localhost') |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1317 |
Socket ipAddressOfHost:(Socket hostWithIpAddress:'127.0.0.1') |
71 | 1318 |
" |
819 | 1319 |
! |
1320 |
||
1321 |
ipV6AddressOfHost:aHostName |
|
1911 | 1322 |
<resource: #obsolete> |
819 | 1323 |
"return the IPv6 (internet-) number for a hostname as a byteArray, |
1324 |
where the network bytes come first (no matter what the cpus byteOrder is). |
|
1325 |
If the host is unknown, return nil. |
|
1326 |
This is the reverse operation to #hostWithIpV6Address:." |
|
1327 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1328 |
NameLookupError |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1329 |
handle:[:ex | |
1343 | 1330 |
^ nil |
1331 |
] |
|
1332 |
do:[ |
|
1333 |
^ (IPv6SocketAddress hostName:aHostName) address |
|
1334 |
] |
|
819 | 1335 |
|
1336 |
" |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1337 |
Socket ipV6AddressOfHost:'localhost' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1338 |
Socket ipV6AddressOfHost:'exept' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1339 |
Socket ipV6AddressOfHost:'exept.exept.de' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1340 |
Socket ipV6AddressOfHost:'www.google.de' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1341 |
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
|
1342 |
Socket ipV6AddressOfHost:'josef' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1343 |
Socket ipV6AddressOfHost:'styx.com' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1344 |
Socket hostWithIpV6Address:(Socket ipV6AddressOfHost:'localhost') |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1345 |
Socket ipV6AddressOfHost:(Socket hostV6WithIpAddress:'127.0.0.1') |
819 | 1346 |
" |
531 | 1347 |
! ! |
1348 |
||
1349 |
!Socket class methodsFor:'queries'! |
|
1350 |
||
1260
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1351 |
peerFromDomain:domain name:peerName port:port |
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1352 |
|addrClass| |
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1353 |
|
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1354 |
addrClass := self socketAddressClassForDomain:domain. |
1263
23161346a162
no longer callBack into sockets getHostName/getAddress code.
Claus Gittinger <cg@exept.de>
parents:
1260
diff
changeset
|
1355 |
^ 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
|
1356 |
! |
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1357 |
|
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1358 |
peerNameFromDomain:domain peer:peer |
1260
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1359 |
|addrClass| |
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1360 |
|
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1361 |
addrClass := self socketAddressClassForDomain:domain. |
9a8fb2391174
instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents:
1235
diff
changeset
|
1362 |
^ 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
|
1363 |
! |
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1364 |
|
0 | 1365 |
portOfService:aNameOrNumber |
819 | 1366 |
"returns the port-number for a given IP-service |
0 | 1367 |
or nil if no such service exists; |
1368 |
- used to convert service names to portNumbers" |
|
1369 |
||
91 | 1370 |
^ self portOfService:aNameOrNumber protocol:nil |
1371 |
||
28 | 1372 |
" |
1373 |
Socket portOfService:'finger' |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1374 |
Socket portOfService:'nntp' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1375 |
Socket portOfService:'echo' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1376 |
Socket portOfService:'snmp' |
28 | 1377 |
" |
0 | 1378 |
! |
1379 |
||
91 | 1380 |
portOfService:aNameOrNumber protocol:aProtocol |
819 | 1381 |
"returns the port-number for a given IP-service |
91 | 1382 |
or nil if no such service exists; |
1383 |
- used to convert service names to portNumbers" |
|
1384 |
||
2909 | 1385 |
%{ /* UNLIMITEDSTACK(noWIN32) */ |
418 | 1386 |
#ifndef NO_SOCKET |
91 | 1387 |
struct servent *servent = NULL; |
1388 |
char *protocol; |
|
1389 |
int tryBoth = 0; |
|
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
1390 |
short portNo; |
91 | 1391 |
|
604 | 1392 |
if (__isSmallInteger(aNameOrNumber)) { |
2340 | 1393 |
RETURN ( aNameOrNumber ); |
92 | 1394 |
} |
1395 |
||
2331 | 1396 |
if (__isStringLike(aProtocol)) { |
2340 | 1397 |
protocol = (char *)__stringVal(aProtocol); |
604 | 1398 |
} else { |
2340 | 1399 |
protocol = "tcp"; |
1400 |
tryBoth = 1; |
|
91 | 1401 |
} |
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
1402 |
|
2331 | 1403 |
if (__isStringLike(aNameOrNumber)) { |
2340 | 1404 |
servent = getservbyname((char *) __stringVal(aNameOrNumber), protocol); |
1405 |
if (servent != NULL) { |
|
1406 |
RETURN ( __MKSMALLINT(ntohs(servent->s_port)) ); |
|
1407 |
} |
|
1408 |
if (tryBoth) { |
|
1409 |
servent = getservbyname((char *) __stringVal(aNameOrNumber), "udp"); |
|
1410 |
if (servent != NULL) { |
|
1411 |
RETURN ( __MKSMALLINT(ntohs(servent->s_port)) ); |
|
1412 |
} |
|
1413 |
} |
|
1414 |
RETURN ( nil ); |
|
91 | 1415 |
} |
418 | 1416 |
#endif |
91 | 1417 |
RETURN ( nil ); |
1418 |
%} |
|
1419 |
" |
|
1420 |
Socket portOfService:'echo' protocol:'udp' |
|
1421 |
Socket portOfService:'echo' protocol:'tcp' |
|
1422 |
" |
|
1423 |
! |
|
1424 |
||
0 | 1425 |
protocolOfService:aNameOrNumber |
819 | 1426 |
"returns the protocol (as string) for a given IP-service |
28 | 1427 |
or nil if no such service exists." |
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
1428 |
|
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
1429 |
%{ /* UNLIMITEDSTACK(noWIN32) */ |
418 | 1430 |
#ifndef NO_SOCKET |
0 | 1431 |
struct servent *servent = NULL; |
524
8287da54eccf
more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents:
513
diff
changeset
|
1432 |
short portNo; |
0 | 1433 |
|
58 | 1434 |
if (__isSmallInteger(aNameOrNumber)) { |
2340 | 1435 |
portNo = __intVal(aNameOrNumber); |
1436 |
servent = getservbyport(htons(portNo), "tcp") ; |
|
1437 |
if (servent == NULL) { |
|
1438 |
servent = getservbyport(htons(portNo), "udp") ; |
|
1439 |
if (servent == NULL) { |
|
1440 |
RETURN ( nil ); |
|
1441 |
} |
|
1442 |
} |
|
0 | 1443 |
} else { |
2340 | 1444 |
if (__isStringLike(aNameOrNumber)) { |
1445 |
servent = getservbyname((char *) __stringVal(aNameOrNumber), "tcp"); |
|
1446 |
if (servent == NULL) { |
|
1447 |
servent = getservbyname((char *) __stringVal(aNameOrNumber), "udp"); |
|
1448 |
if (servent == NULL) { |
|
1449 |
RETURN ( nil ); |
|
1450 |
} |
|
1451 |
} |
|
1452 |
} |
|
0 | 1453 |
} |
1454 |
if (servent) { |
|
2340 | 1455 |
RETURN ( __MKSTRING(servent->s_proto) ); |
0 | 1456 |
} |
1504
92005eec5c9d
fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents:
1503
diff
changeset
|
1457 |
#endif /* !NO_SOCKET */ |
0 | 1458 |
RETURN ( nil ); |
1459 |
%} |
|
28 | 1460 |
" |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1461 |
Socket protocolOfService:'finger' |
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1462 |
Socket protocolOfService:'nntp' |
28 | 1463 |
Socket protocolOfService:'xxx' |
1464 |
Socket protocolOfService:79 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1465 |
Socket protocolOfService:'snmp' |
28 | 1466 |
" |
1467 |
! |
|
1468 |
||
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
|
1469 |
socketAddressClassForDomain:domain |
1230
2cfb8c270cb5
more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents:
1224
diff
changeset
|
1470 |
^ SocketAddress knownClassFromCode:domain |
2cfb8c270cb5
more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents:
1224
diff
changeset
|
1471 |
|
2cfb8c270cb5
more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents:
1224
diff
changeset
|
1472 |
" |
3059 | 1473 |
self socketAddressClassForDomain:#AF_INET |
1474 |
self socketAddressClassForDomain:#AF_UNIX |
|
1230
2cfb8c270cb5
more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents:
1224
diff
changeset
|
1475 |
" |
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
|
1476 |
! |
7e5f2f6b177d
getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents:
1186
diff
changeset
|
1477 |
|
532 | 1478 |
supportedProtocolFamilies |
1479 |
"return a collection of supported protocol families. |
|
1480 |
This list specifies what the Socket class supports - |
|
1481 |
socket creation may still fail, if your system was built |
|
1482 |
without it." |
|
1483 |
||
1280
b973855bfe74
moved all socket constant queries from SocketHandle
Claus Gittinger <cg@exept.de>
parents:
1278
diff
changeset
|
1484 |
^ OperatingSystem supportedProtocolFamilies |
532 | 1485 |
|
1486 |
" |
|
1487 |
Socket supportedProtocolFamilies |
|
1488 |
" |
|
559
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1489 |
! |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1490 |
|
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1491 |
typeOfProtocol:aProtocol |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1492 |
"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
|
1493 |
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
|
1494 |
|
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1495 |
(aProtocol = 'tcp') ifTrue:[^ #stream]. |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1496 |
(aProtocol = 'udp') ifTrue:[^ #datagram]. |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1497 |
(aProtocol = 'ip') ifTrue:[^ #raw]. |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1498 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1499 |
unix domain |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1500 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1501 |
(aProtocol = 'ud') ifTrue:[^ #stream]. |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1502 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1503 |
add x25 stuff (if any) here ... |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1504 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1505 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1506 |
add appletalk stuff (if any) here ... |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1507 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1508 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1509 |
add other stuff (if any) here ... |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1510 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1511 |
^ nil |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1512 |
|
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1513 |
" |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1514 |
Socket typeOfProtocol:'tcp' |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1515 |
Socket typeOfProtocol:'ucp' |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1516 |
Socket typeOfProtocol:(Socket protocolOfService:'nntp') |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1517 |
Socket typeOfProtocol:(Socket protocolOfService:'echo') |
61012867ebed
cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
1518 |
" |
28 | 1519 |
! ! |
1520 |
||
1202 | 1521 |
!Socket methodsFor:'Compatibility-Dolphin'! |
1110 | 1522 |
|
1523 |
setReceiveTimeout: milliseconds |
|
1524 |
self receiveTimeout:(milliseconds / 1000) |
|
1525 |
! |
|
1526 |
||
1527 |
setSendTimeout: milliseconds |
|
1528 |
self sendTimeout:(milliseconds / 1000) |
|
1529 |
! ! |
|
1530 |
||
1202 | 1531 |
!Socket methodsFor:'Compatibility-ST80'! |
28 | 1532 |
|
887 | 1533 |
acceptNonBlock |
1534 |
^ self accept |
|
1535 |
! |
|
1536 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
1537 |
ioConnection |
887 | 1538 |
^ self |
1539 |
! |
|
1540 |
||
126 | 1541 |
notReadySignal |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1542 |
"ST-80 mimicry. |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1543 |
for now - this is not yet raised" |
71 | 1544 |
|
126 | 1545 |
^ Signal new |
486 | 1546 |
! |
1547 |
||
576 | 1548 |
readAppendStream |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1549 |
"ST-80 mimicry. |
4293 | 1550 |
In ST-80, socket is not a stream, but refers to one. |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1551 |
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
|
1552 |
the actual stream. |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1553 |
In ST/X, sockets inherit from stream, so |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1554 |
this method returns the receiver, for transparency" |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1555 |
|
576 | 1556 |
^ self |
1557 |
! |
|
1558 |
||
486 | 1559 |
readStream |
1560 |
"ST-80 mimicry. |
|
4293 | 1561 |
In ST-80, socket is not a stream, but refers to one. |
486 | 1562 |
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
|
1563 |
the actual stream. |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1564 |
In ST/X, sockets inherit from stream, so |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1565 |
this method returns the receiver, for transparency" |
486 | 1566 |
|
1567 |
^ self |
|
1568 |
||
1569 |
"Created: 24.1.1997 / 23:52:57 / cg" |
|
1570 |
! |
|
1571 |
||
1572 |
writeStream |
|
1573 |
"ST-80 mimicry. |
|
4293 | 1574 |
In ST-80, socket is not a stream, but refers to one. |
486 | 1575 |
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
|
1576 |
the actual stream. |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1577 |
In ST/X, sockets inherit from stream, so |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
1578 |
this method returns the receiver, for transparency" |
486 | 1579 |
|
1580 |
^ self |
|
1581 |
||
1582 |
"Created: 24.1.1997 / 10:34:35 / cg" |
|
1583 |
"Modified: 24.1.1997 / 23:52:52 / cg" |
|
71 | 1584 |
! ! |
1585 |
||
1202 | 1586 |
!Socket methodsFor:'Compatibility-Squeak'! |
771 | 1587 |
|
1462 | 1588 |
address |
1589 |
^ self getSocketAddress |
|
1590 |
! |
|
1591 |
||
2308
d8b410a4f38b
added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents:
2292
diff
changeset
|
1592 |
connectToHostNamed:hostName port:portNr |
d8b410a4f38b
added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents:
2292
diff
changeset
|
1593 |
self connectTo:hostName port:portNr |
d8b410a4f38b
added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents:
2292
diff
changeset
|
1594 |
! |
d8b410a4f38b
added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents:
2292
diff
changeset
|
1595 |
|
1462 | 1596 |
dataAvailable |
1597 |
^ self canReadWithoutBlocking |
|
1598 |
! |
|
1599 |
||
1886 | 1600 |
destroy |
1601 |
self close |
|
1602 |
||
1603 |
"Created: / 04-06-2007 / 21:29:03 / cg" |
|
1604 |
! |
|
1605 |
||
1462 | 1606 |
listenOn:aPortNr |
1882 | 1607 |
self listenOn:aPortNr backlogSize:5 |
1608 |
||
1609 |
"Modified: / 31-05-2007 / 17:59:53 / cg" |
|
1610 |
! |
|
1611 |
||
1612 |
listenOn:aPortNr backlogSize:aNumber |
|
1462 | 1613 |
self bindTo:aPortNr address:nil. |
1935 | 1614 |
self listenFor:aNumber |
1882 | 1615 |
|
1616 |
"Created: / 31-05-2007 / 17:59:47 / cg" |
|
1462 | 1617 |
! |
1618 |
||
1885 | 1619 |
peerName |
1620 |
"return my peer (i.e. ipAddr + port); |
|
1621 |
May return nil if not yet setup completely." |
|
1622 |
||
1623 |
^ self getPeer |
|
1624 |
! |
|
1625 |
||
1626 |
sendData: aStringOrByteArray |
|
3155
d1884a37375c
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
3152
diff
changeset
|
1627 |
"Send all of the data in the given array, even if it requires multiple calls to send it all. |
d1884a37375c
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
3152
diff
changeset
|
1628 |
Return the number of bytes sent." |
d1884a37375c
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
3152
diff
changeset
|
1629 |
|
d1884a37375c
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
3152
diff
changeset
|
1630 |
^ self nextPutBytes:aStringOrByteArray size from:aStringOrByteArray startingAt:1. |
1600 | 1631 |
! |
1632 |
||
1462 | 1633 |
setOption: optionName value: optionValue |
2899 | 1634 |
^ self setSocketOption:(optionName asSymbol) argument:optionValue argument:nil |
1887 | 1635 |
|
1636 |
"Modified: / 04-06-2007 / 21:23:19 / cg" |
|
1600 | 1637 |
! |
1638 |
||
2904 | 1639 |
waitForConnectionUntil:aTimestamp |
1640 |
"return true if connected" |
|
1641 |
||
1642 |
self readWaitWithTimeoutMs: (aTimestamp millisecondDeltaFrom:Timestamp now). |
|
1643 |
^ self isConnected |
|
771 | 1644 |
! ! |
1645 |
||
1935 | 1646 |
!Socket methodsFor:'accepting connections'! |
1647 |
||
1648 |
accept |
|
1649 |
"create a new TCP socket from accepting on the receiver. |
|
1650 |
This method will suspend the current process if no connection is waiting. |
|
1651 |
For ST-80 compatibility" |
|
1652 |
||
3633 | 1653 |
^ self waitForNewConnectionWithTimeout:nil |
1935 | 1654 |
|
1655 |
" |
|
1656 |
|sock newSock| |
|
1657 |
||
1658 |
sock := Socket provide:8004. |
|
1659 |
sock listenFor:5. |
|
1660 |
newSock := sock accept. |
|
1661 |
" |
|
1662 |
! |
|
1663 |
||
1664 |
blockingAccept |
|
1665 |
"create a new TCP socket from accepting on the receiver. |
|
1666 |
This method will suspend the smalltalk image with all smalltalk processes if no connection is waiting. |
|
1667 |
For ST-80 compatibility" |
|
1668 |
||
1669 |
|newSock| |
|
1670 |
||
1671 |
newSock := self class new. |
|
1672 |
(newSock primAcceptOn:self blocking:true) ifFalse:[ |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1673 |
"should raise an error here" |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1674 |
^ nil |
1935 | 1675 |
]. |
1676 |
^ newSock |
|
1677 |
! ! |
|
1678 |
||
1679 |
!Socket methodsFor:'binding'! |
|
1680 |
||
1681 |
bindAnonymously |
|
1682 |
"bind to any address. A free port will be allocated. |
|
1683 |
Our own socket address will be determined after conection set up. |
|
1684 |
This is the default after the socket has been created" |
|
1685 |
||
3059 | 1686 |
^ self bindTo:nil reuseAddress:false |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1687 |
|
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1688 |
" |
1938 | 1689 |
self newTCP bindAnonymously; listenFor:1; yourself |
4206 | 1690 |
self newTCP bindAnonymously; port |
1938 | 1691 |
" |
1935 | 1692 |
! |
1693 |
||
3059 | 1694 |
bindAnonymouslyToAddress:aSocketAddress |
1935 | 1695 |
"bind to address addressString. |
1696 |
A free port will be allocated" |
|
1697 |
||
1698 |
^ self |
|
3217 | 1699 |
bindTo:0 |
1700 |
address:aSocketAddress |
|
1701 |
reuseAddress:false |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1702 |
|
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1703 |
" |
3059 | 1704 |
self newTCP bindAnonymouslyToAddress:IPSocketAddress localHost; listenFor:1; yourself |
1938 | 1705 |
" |
1935 | 1706 |
! |
1707 |
||
1708 |
bindTo:aSocketAddress |
|
1709 |
"ST80 compatible bind, expecting a socketAddress argument. |
|
1710 |
The socketAddress object (an instance of SocketAddress) |
|
1711 |
is supposed to respond to #portOrName and #address requests." |
|
1712 |
||
3059 | 1713 |
^ self bindTo:aSocketAddress reuseAddress:true |
1935 | 1714 |
! |
1715 |
||
1716 |
bindTo:portNrOrNameString address:addressString |
|
3059 | 1717 |
"Old interface: bind the socket to an address |
1718 |
- returns true if ok, false otherwise. |
|
1719 |
||
1720 |
The interpretation of hostOrPathNameOrSocketAddrOrNil portNrOrName depends on the domain: |
|
3217 | 1721 |
Best use a SocketAddress |
1722 |
For backward compatibility: |
|
1723 |
AF_INET domain can also use (4byte) byteArray like internet numbers, |
|
1724 |
AF_UNIX domain cab use pathname strings." |
|
1935 | 1725 |
|
1726 |
^ self |
|
3217 | 1727 |
bindTo:portNrOrNameString |
1728 |
address:addressString |
|
1729 |
reuseAddress:true |
|
1935 | 1730 |
! |
1731 |
||
1732 |
bindTo:portNrOrNameOrNil address:hostOrPathNameOrSocketAddrOrNil reuseAddress:reuse |
|
3059 | 1733 |
"Old interface: bind the socket to an address |
1734 |
- returns true if ok, false otherwise. |
|
1735 |
||
1736 |
The interpretation of hostOrPathNameOrSocketAddrOrNil portNrOrName depends on the domain: |
|
3217 | 1737 |
Best use a SocketAddress |
1738 |
For backward compatibility: |
|
1739 |
AF_INET domain can also use (4byte) byteArray like internet numbers, |
|
1740 |
AF_UNIX domain can use pathname strings. |
|
1935 | 1741 |
|
1742 |
The reuse boolean argument controls if the SO_REUSEADDR socket option |
|
1743 |
is to be set (to avoid the 'bind: address in use' error). |
|
1744 |
" |
|
1745 |
||
3059 | 1746 |
|socketAddress| |
1935 | 1747 |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1748 |
handle isNil ifTrue:[ |
3217 | 1749 |
^ self errorNotOpen |
1935 | 1750 |
]. |
1751 |
||
1752 |
hostOrPathNameOrSocketAddrOrNil isNil ifTrue:[ |
|
3217 | 1753 |
socketAddress := self socketAddressClass anyHost. |
1935 | 1754 |
] ifFalse:[ |
3217 | 1755 |
(hostOrPathNameOrSocketAddrOrNil isSocketAddress) ifTrue:[ |
1756 |
socketAddress := hostOrPathNameOrSocketAddrOrNil. |
|
1757 |
] ifFalse:[ |
|
1758 |
"backward compatibility: support for byteArray and string arg" |
|
1759 |
hostOrPathNameOrSocketAddrOrNil isString ifTrue:[ |
|
1760 |
socketAddress := self socketAddressClass hostName:hostOrPathNameOrSocketAddrOrNil. |
|
1761 |
] ifFalse:[ |
|
1762 |
hostOrPathNameOrSocketAddrOrNil isByteCollection ifFalse:[ |
|
1763 |
^ self error:'bindTo: bad host (socketAddress) argument' |
|
1764 |
]. |
|
1765 |
socketAddress := self socketAddressClass hostAddress:hostOrPathNameOrSocketAddrOrNil. |
|
1766 |
]. |
|
1767 |
]. |
|
1935 | 1768 |
]. |
1769 |
portNrOrNameOrNil notNil ifTrue:[ |
|
3217 | 1770 |
socketAddress port:portNrOrNameOrNil. |
1935 | 1771 |
]. |
3059 | 1772 |
|
1773 |
^ self bindTo:socketAddress reuseAddress:reuse. |
|
1774 |
||
1775 |
" |
|
1776 |
(Socket domain:#'AF_INET' type:#stream) |
|
3217 | 1777 |
bindTo:2144 address:nil; yourself |
3059 | 1778 |
" |
1779 |
! |
|
1780 |
||
1781 |
bindTo:aSocketAddress reuseAddress:reuse |
|
1782 |
"Bind the socket to aSocketAddress - returns true if ok, false otherwise. |
|
1783 |
||
1784 |
The reuse boolean argument controls if the SO_REUSEADDR socket option |
|
1785 |
is to be set (to avoid the 'bind: address in use' error). |
|
1786 |
||
3137 | 1787 |
You can bind to nil to bind to anyHost and assign a random port." |
3059 | 1788 |
|
1789 |
|ok error socketAddress| |
|
1790 |
||
1791 |
handle isNil ifTrue:[ |
|
4414 | 1792 |
^ self errorNotOpen |
3059 | 1793 |
]. |
1794 |
socketAddress := aSocketAddress. |
|
1795 |
socketAddress isNil ifTrue:[ |
|
4414 | 1796 |
"ok, get a all zero socket address, so it is for anyHost |
1797 |
and the port will be assigned" |
|
1798 |
socketAddress := self socketAddressClass new. |
|
3513 | 1799 |
]. |
1800 |
domain == #'AF_INET6' ifTrue:[ |
|
4414 | 1801 |
"accept also IPv4 connections on IPv6 sockets (this is off by default for windows" |
1802 |
self setSocketOption:#'IPV6_V6ONLY' argument:false argument:nil. |
|
1935 | 1803 |
]. |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1804 |
ok := false. |
1935 | 1805 |
|
1806 |
%{ /* STACK: 100000 */ |
|
1807 |
#ifndef NO_SOCKET |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1808 |
OBJ fp = __INST(handle); |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1809 |
SOCKET sock; |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1810 |
union sockaddr_u sa; |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1811 |
int sockaddr_size; |
2909 | 1812 |
INT ret; |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1813 |
int sockAddrOffs; |
1935 | 1814 |
|
3059 | 1815 |
if (fp == nil) { |
4414 | 1816 |
goto getOutOfHere; |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1817 |
} |
3059 | 1818 |
|
1819 |
if (! __isBytes(socketAddress)) { |
|
4414 | 1820 |
error = __mkSmallInteger(-1); |
1821 |
goto getOutOfHere; |
|
1935 | 1822 |
} |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1823 |
/* get the socket-address */ |
3059 | 1824 |
if (__isNonNilObject(socketAddress)){ |
4414 | 1825 |
int nIndex; |
1826 |
OBJ cls = __qClass(socketAddress); |
|
1827 |
||
1828 |
sockAddrOffs = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1829 |
nIndex = __qSize(socketAddress) - OHDR_SIZE; |
|
1830 |
sockaddr_size = nIndex - sockAddrOffs; |
|
1831 |
if (sockaddr_size > sizeof(sa)) { |
|
1832 |
error=__mkSmallInteger(-2); |
|
1833 |
goto getOutOfHere; |
|
1834 |
} |
|
1835 |
memcpy(&sa, __byteArrayVal(socketAddress) + sockAddrOffs, sockaddr_size); |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1836 |
} |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1837 |
|
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1838 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
1935 | 1839 |
|
1840 |
# ifdef SO_REUSEADDR |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1841 |
if (reuse == true) { |
4414 | 1842 |
int on = 1; |
1843 |
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0) { |
|
1844 |
DBGPRINTF(("SOCKET: setsockopt - SO_REUSEADDR failed\n")); |
|
1845 |
} |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1846 |
} |
1935 | 1847 |
# endif /* SO_REUSEADDR */ |
1848 |
||
1849 |
# ifdef BIND_BLOCKS |
|
1850 |
# ifdef DO_WRAP_CALLS |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1851 |
do { |
4414 | 1852 |
__threadErrno = 0; |
1853 |
ret = STX_WSA_NOINT_CALL3("bind", bind, sock, &sa, sockaddr_size); |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1854 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
3126 | 1855 |
if (ret < 0) { |
4414 | 1856 |
errno = __threadErrno; |
3126 | 1857 |
} |
1935 | 1858 |
# else |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1859 |
__BEGIN_INTERRUPTABLE__ |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1860 |
do { |
4414 | 1861 |
ret = bind(sock, (struct sockaddr *)&sa, sockaddr_size); |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1862 |
} while ((ret < 0) && (errno == EINTR)); |
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1863 |
__END_INTERRUPTABLE__ |
1935 | 1864 |
# endif |
1865 |
# else |
|
3126 | 1866 |
errno = 0; |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1867 |
ret = bind(sock, (struct sockaddr *)&sa, sockaddr_size); |
1935 | 1868 |
# endif |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1869 |
if (ret < 0) { |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
1870 |
# ifdef __win32__ |
4414 | 1871 |
if (errno == 0) { |
1872 |
errno = WSAGetLastError(); |
|
1873 |
} |
|
3126 | 1874 |
# endif |
4414 | 1875 |
DBGPRINTF(("SOCKET: bind failed errno=%d\n", errno)); |
1876 |
error = __INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1877 |
goto getOutOfHere; |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1878 |
} else { |
4414 | 1879 |
ok = true; |
1935 | 1880 |
} |
1881 |
#endif /* NO_SOCKET */ |
|
1882 |
||
1883 |
getOutOfHere: ; |
|
1884 |
%}. |
|
4234 | 1885 |
ok ifFalse:[ |
4414 | 1886 |
|errorHolder errorString| |
1887 |
||
1888 |
error isInteger ifTrue:[ |
|
1889 |
errorHolder := OperatingSystem errorHolderForNumber:error. |
|
1890 |
errorString := errorHolder errorString. |
|
1891 |
] ifFalse:[ |
|
1892 |
errorString := error. |
|
1893 |
]. |
|
1894 |
OpenError newException |
|
1895 |
errorString:('cannot bind socket to address: %1 (%2)' |
|
1896 |
bindWith:socketAddress |
|
1897 |
with:errorString); |
|
1898 |
errorCode:error; |
|
1899 |
osErrorHolder:errorHolder; |
|
1900 |
parameter:self; |
|
1901 |
raiseRequest. |
|
1902 |
"maybe someone catches the error and binds to some other port..." |
|
1903 |
^ true. |
|
1935 | 1904 |
]. |
1905 |
||
3059 | 1906 |
port := socketAddress port. |
1907 |
port == 0 ifTrue:[ |
|
4414 | 1908 |
"this is a bind to a random port, now we can get the real port" |
1909 |
port := self getFullSocketAddress port. |
|
3059 | 1910 |
]. |
1935 | 1911 |
^ true |
1912 |
||
1913 |
" |
|
3059 | 1914 |
(Socket domain:#'AF_INET' type:#stream) |
4414 | 1915 |
bindTo:(IPSocketAddress anyHost port:445) reuseAddress:false; |
1916 |
yourself. |
|
3059 | 1917 |
|
1918 |
(Socket domain:#'AF_INET' type:#stream) |
|
4414 | 1919 |
bindTo:139 reuseAddress:false; |
1920 |
yourself. |
|
3059 | 1921 |
|
1922 |
(Socket domain:#'AF_INET6' type:#stream) |
|
4414 | 1923 |
bindTo:nil reuseAddress:false; |
1924 |
yourself. |
|
3059 | 1925 |
|
1926 |
(Socket domain:#'AF_INET' type:#stream) |
|
4414 | 1927 |
bindTo:(IPSocketAddress localHost port:2122) reuseAddress:false; |
1928 |
yourself. |
|
3059 | 1929 |
|
1930 |
(Socket domain:#'AF_UNIX' type:#stream) |
|
4414 | 1931 |
bindTo:nil reuseAddress:false; |
1932 |
yourself. |
|
1935 | 1933 |
" |
1934 |
! |
|
1935 |
||
1936 |
listenFor:aNumber |
|
1937 |
"start listening; return true if ok, false on error |
|
1938 |
aNumber is the number of connect requests, that may be queued on the socket" |
|
1939 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1940 |
handle isNil ifTrue:[ |
3217 | 1941 |
^ self errorNotOpen |
1935 | 1942 |
]. |
1943 |
%{ |
|
1944 |
#ifndef NO_SOCKET |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
1945 |
OBJ fp = __INST(handle); |
1935 | 1946 |
SOCKET sock; |
1947 |
int ret; |
|
1948 |
||
1949 |
if (! __isSmallInteger(aNumber)) { |
|
3217 | 1950 |
DBGPRINTF(("SOCKET: invalid arg\n")); |
1951 |
RETURN (false); |
|
1935 | 1952 |
} |
1953 |
||
1954 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
1955 |
||
1956 |
#ifdef LISTEN_BLOCKS |
|
1957 |
# ifdef DO_WRAP_CALLS |
|
1958 |
do { |
|
3217 | 1959 |
__threadErrno = 0; |
1960 |
ret = STX_WSA_NOINT_CALL2("listen", listen, sock, __intVal(aNumber)); |
|
1935 | 1961 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
3126 | 1962 |
if (ret < 0) { |
3217 | 1963 |
errno = __threadErrno; |
3126 | 1964 |
} |
1935 | 1965 |
# else |
1966 |
__BEGIN_INTERRUPTABLE__ |
|
1967 |
do { |
|
3217 | 1968 |
ret = listen(sock, __intVal(aNumber)); |
1935 | 1969 |
} while ((ret < 0) && (errno == EINTR)); |
1970 |
__END_INTERRUPTABLE__ |
|
1971 |
# endif |
|
1972 |
#else |
|
3126 | 1973 |
errno = 0; |
1935 | 1974 |
ret = listen(sock, __intVal(aNumber)); |
1975 |
#endif |
|
1976 |
||
1977 |
if (ret < 0) { |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
1978 |
# ifdef __win32__ |
3217 | 1979 |
if (errno == 0) { |
1980 |
errno = WSAGetLastError(); |
|
1981 |
} |
|
3126 | 1982 |
# endif |
3217 | 1983 |
DBGPRINTF(("SOCKET: listen call failed errno=%d\n", errno)); |
1984 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
1985 |
RETURN (false); |
|
1935 | 1986 |
} |
1987 |
#else |
|
1988 |
RETURN (false); |
|
1989 |
#endif |
|
1990 |
%}. |
|
3157 | 1991 |
listening := aNumber. |
1935 | 1992 |
^ true |
1993 |
! ! |
|
1994 |
||
1576
a2ffb22c5f5e
New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents:
1568
diff
changeset
|
1995 |
!Socket methodsFor:'closing'! |
a2ffb22c5f5e
New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents:
1568
diff
changeset
|
1996 |
|
4172 | 1997 |
abortAndClose |
1998 |
"immediately abort the connection: |
|
4414 | 1999 |
discard buffered data and close the stream" |
2000 |
||
2001 |
self linger:0. |
|
2002 |
self close. |
|
3224 | 2003 |
! |
2004 |
||
1885 | 2005 |
shutDown |
4414 | 2006 |
"shutDown (initiate a graceful close) |
4172 | 2007 |
and close (free the filedescriptor) the socket. |
4414 | 2008 |
The close will return immediately and buffered data will be sent in the |
4172 | 2009 |
background, unless you set linger" |
1885 | 2010 |
|
2011 |
self shutdown:2. |
|
2012 |
self close |
|
1576
a2ffb22c5f5e
New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents:
1568
diff
changeset
|
2013 |
! |
a2ffb22c5f5e
New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents:
1568
diff
changeset
|
2014 |
|
1885 | 2015 |
shutDownInput |
2016 |
"shutDown the input side of the socket. |
|
2017 |
Any read on the socket will signal end-of-file from now on. |
|
1839 | 2018 |
The other side MAY be informed, that no more data will be accepted |
1576
a2ffb22c5f5e
New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents:
1568
diff
changeset
|
2019 |
(e.g. setting the TCP-Windowsize to 0)" |
a2ffb22c5f5e
New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents:
1568
diff
changeset
|
2020 |
|
a2ffb22c5f5e
New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents:
1568
diff
changeset
|
2021 |
self shutdown:0. |
1885 | 2022 |
! |
2023 |
||
2024 |
shutDownOutput |
|
2025 |
"shutDown the output side of the socket. |
|
2026 |
Any write to the socket will signal end-of-file from now on. |
|
4172 | 2027 |
An orderly release (TCP FIN) will be initiated after the last buffered data |
2028 |
has been sent, so the other side will get a end-of-file condition eventually. |
|
4414 | 2029 |
If you set linger > 0, the operation will wait until buffered data |
4172 | 2030 |
has been delivered to the peer. |
2031 |
Otherwise the operation returns immediately." |
|
1343 | 2032 |
|
1884 | 2033 |
self shutdown:1. |
1885 | 2034 |
! ! |
2035 |
||
1935 | 2036 |
!Socket methodsFor:'connecting'! |
2037 |
||
3059 | 2038 |
connectTo:aSocketAddress |
2039 |
^ self connectTo:aSocketAddress withTimeout:nil |
|
2040 |
! |
|
2041 |
||
1935 | 2042 |
connectTo:hostOrPathName port:portNrOrName |
3655 | 2043 |
<resource: #obsolete> |
3059 | 2044 |
"Backward compatibility connect; connect to port, portNrOrNameOrNil on host, hostOrPathNameOrSocketAddr. |
2045 |
For backward compatibility, hostOrPathNameOrSocketAddr may be also a string or a byteArray, |
|
2046 |
but it is recommended to pass SocketAddress instances. |
|
1935 | 2047 |
|
2048 |
Return true if ok, false otherwise. |
|
2049 |
The current process will block (but not the whole Smalltalk) until the connection is established. |
|
2050 |
See also: #connectTo:port:withTimeout: for a somewhat nicer interface." |
|
2051 |
||
2052 |
^ self connectTo:hostOrPathName port:portNrOrName withTimeout:nil |
|
2053 |
! |
|
2054 |
||
2055 |
connectTo:hostOrPathNameOrSocketAddr port:portNrOrNameOrNil withTimeout:timeout |
|
3655 | 2056 |
<resource: #obsolete> |
3059 | 2057 |
"Backward compatibility connect; connect to port, portNrOrNameOrNil on host, hostOrPathNameOrSocketAddr. |
2058 |
For backward compatibility, hostOrPathNameOrSocketAddr may be also a string or a byteArray, |
|
2059 |
but it is recommended to pass SocketAddress instances. |
|
1935 | 2060 |
|
2061 |
Return true if ok, false otherwise. |
|
2062 |
The current process will block (but not the whole Smalltalk) until the connection is established, |
|
4038 | 2063 |
or timeout milliseconds have passed." |
1935 | 2064 |
|
3059 | 2065 |
|domainClass socketAddress| |
2066 |
||
2067 |
(hostOrPathNameOrSocketAddr isSocketAddress) ifTrue:[ |
|
4414 | 2068 |
socketAddress := hostOrPathNameOrSocketAddr. |
2069 |
portNrOrNameOrNil notNil ifTrue:[ |
|
2070 |
socketAddress port:portNrOrNameOrNil. |
|
2071 |
]. |
|
3059 | 2072 |
] ifFalse:[ |
4414 | 2073 |
"backward compatibility: support for byteArray and string arg" |
2074 |
domainClass := self class socketAddressClassForDomain:domain. |
|
2075 |
domainClass isNil ifTrue:[ |
|
2076 |
^ self error:'invalid (unsupported) domain'. |
|
2077 |
]. |
|
2078 |
||
2079 |
hostOrPathNameOrSocketAddr isString ifTrue:[ |
|
2080 |
socketAddress := domainClass hostName:hostOrPathNameOrSocketAddr serviceName:portNrOrNameOrNil type:#SOCK_STREAM. |
|
2081 |
peerName := hostOrPathNameOrSocketAddr. |
|
2082 |
] ifFalse:[ |
|
2083 |
hostOrPathNameOrSocketAddr isByteCollection ifFalse:[ |
|
2084 |
^ self error:'connectTo: bad host (socketAddress) argument' |
|
2085 |
]. |
|
2086 |
socketAddress := domainClass hostAddress:hostOrPathNameOrSocketAddr port:portNrOrNameOrNil. |
|
2087 |
]. |
|
3059 | 2088 |
]. |
2089 |
||
2090 |
^ self connectTo:socketAddress withTimeout:timeout. |
|
2091 |
||
2092 |
" |
|
2093 |
|sock| |
|
2094 |
sock := Socket newTCP. |
|
2095 |
sock connectTo:'localhost' port:21 withTimeout:1000. |
|
2096 |
sock |
|
2097 |
||
2098 |
|sock| |
|
2099 |
sock := Socket newTCP. |
|
2100 |
sock connectTo:'localhost' port:9876 withTimeout:2000. |
|
2101 |
sock |
|
2102 |
" |
|
2103 |
! |
|
2104 |
||
2105 |
connectTo:aSocketAddress withTimeout:timeout |
|
2106 |
"Connect to a SocketAddress. |
|
2107 |
||
2108 |
Return true if ok, false otherwise. |
|
2109 |
The current process will block (but not the whole Smalltalk) until the connection is established, |
|
4038 | 2110 |
or timeout milliseconds have passed." |
3059 | 2111 |
|
2112 |
|isAsync err| |
|
1935 | 2113 |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
2114 |
handle isNil ifTrue:[ |
4414 | 2115 |
^ self errorNotOpen |
1935 | 2116 |
]. |
4234 | 2117 |
isAsync := false. |
1935 | 2118 |
|
2119 |
%{ /* STACK: 100000 */ |
|
2120 |
#ifndef NO_SOCKET |
|
3123 | 2121 |
//#define DUMP_ADDRESS 1 |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
2122 |
OBJ fp = __INST(handle); |
1935 | 2123 |
union sockaddr_u sa; |
2124 |
SOCKET sock; |
|
2125 |
int a; |
|
2909 | 2126 |
INT ret; |
2127 |
int oldFlags; |
|
3741 | 2128 |
# ifdef __MINGW64__ |
2129 |
unsigned long on = 1; |
|
2130 |
# else |
|
1935 | 2131 |
int on = 1; |
3741 | 2132 |
#endif |
1935 | 2133 |
int sockaddr_size; |
2134 |
||
3059 | 2135 |
if (!__isNonNilObject(aSocketAddress) || !__isBytes(aSocketAddress)) { |
4414 | 2136 |
DBGPRINTF(("SOCKET: invalid socketAddress\n")); |
2137 |
err = @symbol(argumentError); |
|
2138 |
goto out; |
|
1935 | 2139 |
} |
2140 |
||
2141 |
{ |
|
4414 | 2142 |
int sockAddrOffs = 0; |
2143 |
int nIndex =__byteArraySize(aSocketAddress); |
|
2144 |
OBJ cls = __qClass(aSocketAddress); |
|
2145 |
||
2146 |
//if (cls != @global(ByteArray)) |
|
2147 |
// sockAddrOffs = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
2148 |
sockaddr_size = nIndex - sockAddrOffs; |
|
2149 |
if (sockaddr_size > sizeof(sa)) { |
|
2150 |
DBGPRINTF(("SOCKET: invalid (short) socketAddress\n")); |
|
2151 |
err = @symbol(argumentError); |
|
2152 |
goto out; |
|
2153 |
} |
|
2154 |
memcpy(&sa, __byteArrayVal(aSocketAddress) + sockAddrOffs, sockaddr_size); |
|
1935 | 2155 |
} |
2156 |
||
2157 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
2158 |
||
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2159 |
#ifdef __win32__ |
3657 | 2160 |
ioctlsocket(sock, FIONBIO, &on); |
2161 |
#elif defined(O_NONBLOCK) |
|
1935 | 2162 |
/* |
2163 |
* set to non-blocking and wait later |
|
2164 |
*/ |
|
2165 |
oldFlags = fcntl(sock, F_GETFL, 0); |
|
2166 |
/* on SUNOS4.x, would use fcntl(osfd, F_SETFL, flags | FNDELAY); */ |
|
2167 |
fcntl(sock, F_SETFL, oldFlags | O_NONBLOCK); |
|
2168 |
# endif |
|
2169 |
||
2170 |
/* |
|
2171 |
* connect |
|
2172 |
*/ |
|
3123 | 2173 |
|
4039 | 2174 |
// we do not use wrap calls any longer, because they have problems with aborting. |
3657 | 2175 |
// we use nonblocking accept() instead. |
2176 |
# if 0 && defined(DO_WRAP_CALLS) |
|
3217 | 2177 |
// __setWrapCallDebugging(1,1); |
2414 | 2178 |
|
3123 | 2179 |
|
1935 | 2180 |
do { |
4414 | 2181 |
DBGFPRINTF((stderr, "SOCKET: (sock=%d) connect...\n", sock)); |
2182 |
ret = STX_WSA_NOINT_CALL3("connect", connect, sock, &sa, (INT)sockaddr_size); |
|
2183 |
DBGFPRINTF((stderr, "SOCKET: connect(%d) -> %"_ld_" (%d)\n", sock, (INT)ret, __threadErrno)); |
|
1935 | 2184 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
3123 | 2185 |
|
3077 | 2186 |
if (ret < 0) { |
4414 | 2187 |
int optLen = sizeof(errno); |
2188 |
errno = __threadErrno; |
|
3123 | 2189 |
#if 0 |
4414 | 2190 |
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &errno, &optLen) == SOCKET_ERROR) { |
2191 |
DBGFPRINTF((stderr, "SOCKET: getsockopt(SO_ERROR) failed: %d\n", WSAGetLastError())); |
|
2192 |
} |
|
3657 | 2193 |
#endif // 0 |
3077 | 2194 |
} |
2195 |
||
2196 |
// __setWrapCallDebugging(1,0); |
|
3657 | 2197 |
# else // !DO_WRAP_CALLS |
2198 |
||
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2199 |
# if !defined(__win32__) && !defined(O_NONBLOCK) |
1935 | 2200 |
__BEGIN_INTERRUPTABLE__ |
3657 | 2201 |
# endif |
1935 | 2202 |
do { |
4414 | 2203 |
ret = connect(sock, (struct sockaddr *)&sa, sockaddr_size); |
1935 | 2204 |
} while ((ret < 0) |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2205 |
# ifdef __win32__ |
4414 | 2206 |
&& (errno = WSAGetLastError()) |
3657 | 2207 |
# endif |
4414 | 2208 |
&& ((errno == EINTR) |
3657 | 2209 |
# ifdef EAGAIN |
4414 | 2210 |
|| (errno == EAGAIN) |
3657 | 2211 |
# endif |
4414 | 2212 |
)); |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2213 |
# if !defined(__win32__) && !defined(O_NONBLOCK) |
1935 | 2214 |
__END_INTERRUPTABLE__ |
3657 | 2215 |
# endif |
2216 |
#endif |
|
2217 |
||
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2218 |
#if defined(__win32__) && !defined(EINPROGRESS) |
3657 | 2219 |
# define EINPROGRESSS WSAEINPROGRESSS |
2220 |
# define EALREADY WSAEALREADY |
|
1935 | 2221 |
#endif |
2222 |
||
2223 |
if (ret < 0) { |
|
2224 |
# if defined(EINPROGRESS) || defined(EALREADY) |
|
4414 | 2225 |
if (0 |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2226 |
# ifdef __win32__ |
4414 | 2227 |
|| (errno == WSAEWOULDBLOCK) |
3657 | 2228 |
# endif |
1935 | 2229 |
# ifdef EINPROGRESS |
4414 | 2230 |
|| (errno == EINPROGRESS) |
1935 | 2231 |
# endif |
2232 |
# ifdef EALREADY |
|
4414 | 2233 |
|| (errno == EALREADY) |
1935 | 2234 |
# endif |
4414 | 2235 |
) { |
2236 |
/* |
|
2237 |
* This was a nonblocking operation that will take some time. |
|
2238 |
* Do a select on read to get informed when the operation is ready. |
|
2239 |
*/ |
|
2240 |
DBGFPRINTF((stderr, "SOCKET: isAsync is true\n")); |
|
2241 |
isAsync = true; |
|
2242 |
} else |
|
1935 | 2243 |
# endif /* EINPROGRESS or EALREADY */ |
4414 | 2244 |
{ |
2245 |
DBGFPRINTF((stderr, "SOCKET: connect failed ret=%"_ld_" errno=%d __threadErrno=%d\n", |
|
2246 |
(INT)ret, errno, __threadErrno )); |
|
1935 | 2247 |
# ifdef DUMP_ADDRESS |
4414 | 2248 |
{ |
2249 |
unsigned char *cp = (unsigned char *)(&sa); |
|
2250 |
int i; |
|
2251 |
||
2252 |
console_printf("address data:\n"); |
|
2253 |
for (i=0; i<sockaddr_size; i++) { |
|
2254 |
console_printf(" %02x\n", *cp++); |
|
2255 |
} |
|
2256 |
} |
|
1935 | 2257 |
# endif |
4414 | 2258 |
err = __MKSMALLINT(errno); |
2259 |
} |
|
1935 | 2260 |
} |
2261 |
||
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2262 |
# ifdef __win32__ |
3657 | 2263 |
{ |
4414 | 2264 |
int off = 0; |
2265 |
ioctlsocket(sock, FIONBIO, &off); |
|
3657 | 2266 |
} |
2267 |
# elif defined(O_NONBLOCK) // Linux / Unix |
|
1935 | 2268 |
fcntl(sock, F_SETFL, oldFlags); |
2269 |
# endif |
|
2270 |
||
2271 |
# else /* NO_SOCKET */ |
|
3077 | 2272 |
err = @symbol(notSupported); |
1935 | 2273 |
# endif /* NO_SOCKET */ |
3077 | 2274 |
out:; |
1935 | 2275 |
%}. |
3217 | 2276 |
|
3077 | 2277 |
err notNil ifTrue:[ |
4414 | 2278 |
err isSymbol ifTrue:[ |
2279 |
self primitiveFailed:err. |
|
2280 |
]. |
|
2281 |
lastErrorNumber := err. |
|
2282 |
^ false. |
|
2283 |
"/ Once we will raise an exception instead of returning false (and have to change some code above): |
|
3077 | 2284 |
"/ (OperatingSystem errorHolderForNumber:err) reportError. |
2285 |
]. |
|
4234 | 2286 |
isAsync ifTrue:[ |
4414 | 2287 |
(self writeExceptionWaitWithTimeoutMs:timeout) ifTrue:[ |
2288 |
"/ a timeout occurred |
|
2289 |
"/ should I cancel the connect? |
|
2290 |
lastErrorNumber := OperatingSystem errorNumberFor:#ETIMEDOUT. |
|
2291 |
^ false. |
|
2292 |
]. |
|
2293 |
err := self getSocketError. |
|
2294 |
err ~~ 0 ifTrue:[ |
|
2295 |
lastErrorNumber := err. |
|
2296 |
^ false. |
|
2297 |
]. |
|
1935 | 2298 |
]. |
2299 |
||
1940
2ea7d2c1a2e6
changed #connectTo:port:withTimeout: - add commented code tah may be used in the future
Stefan Vogel <sv@exept.de>
parents:
1939
diff
changeset
|
2300 |
|
3059 | 2301 |
peer := aSocketAddress. |
1935 | 2302 |
port isNil ifTrue:[ |
4414 | 2303 |
"socket has not been explicitly bound, |
2304 |
after connect it has been bound implicitly - fetch the port" |
|
2305 |
port := self getFullSocketAddress port. |
|
1935 | 2306 |
]. |
2307 |
^ true |
|
2308 |
||
2309 |
" |
|
2310 |
|sock| |
|
2311 |
sock := Socket newTCP. |
|
3059 | 2312 |
sock connectTo:(IPSocketAddress localHost port:21) withTimeout:1000. |
1935 | 2313 |
sock |
2314 |
||
2315 |
|sock| |
|
2316 |
sock := Socket newTCP. |
|
3059 | 2317 |
sock connectTo:(IPSocketAddress localHost port:9876) withTimeout:2000. |
1935 | 2318 |
sock |
2319 |
" |
|
2320 |
! ! |
|
2321 |
||
1885 | 2322 |
!Socket methodsFor:'datagram transmission'! |
2323 |
||
2324 |
receiveBuffer:aDataBuffer start:startIndex for:nBytes |
|
2325 |
"receive data |
|
2326 |
Return the number of bytes received, or a negative number on error. |
|
2327 |
On error, the unix error code is left in the lastErrorNumber |
|
2328 |
instance variable. |
|
2329 |
The thread blocks until data arrives - you may want to wait before |
|
2330 |
receiving, using #readWait or #readWaitWithTimeout:." |
|
2331 |
||
3126 | 2332 |
|nReceived error| |
1885 | 2333 |
|
2334 |
%{ |
|
2335 |
#ifndef NO_SOCKET |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
2336 |
OBJ fp = __INST(handle); |
1885 | 2337 |
|
2338 |
if (fp != nil) { |
|
3657 | 2339 |
SOCKET sock; |
2340 |
INT objSize, offs; |
|
2341 |
INT n; |
|
2342 |
char *extPtr; |
|
2343 |
unsigned char *buffer; |
|
2344 |
unsigned char *allocatedBuffer = NULL; |
|
2345 |
INT flags = 0; |
|
2346 |
||
2347 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
2348 |
||
2349 |
if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad; |
|
2350 |
if (__isSmallInteger(nBytes)) { |
|
2351 |
if (__intVal(nBytes) < objSize) { |
|
2352 |
objSize = __intVal(nBytes); |
|
2353 |
} |
|
2354 |
} |
|
1885 | 2355 |
|
2356 |
# ifdef DO_WRAP_CALLS |
|
3657 | 2357 |
if (extPtr) { |
2358 |
buffer = extPtr + offs; |
|
2359 |
} else { |
|
2360 |
allocatedBuffer = buffer = (char *)malloc(objSize); |
|
2361 |
} |
|
2362 |
||
2363 |
do { |
|
2364 |
__threadErrno = 0; |
|
2365 |
n = (INT)STX_WSA_NOINT_CALL4("recv", recv, sock, buffer, objSize, flags); |
|
2366 |
} while ((n < 0) && (__threadErrno == EINTR)); |
|
2367 |
if (n < 0) { |
|
2368 |
errno = __threadErrno; |
|
2369 |
} |
|
2370 |
||
2371 |
if (allocatedBuffer) { |
|
2372 |
if (n > 0) { |
|
4414 | 2373 |
memcpy((char *)__InstPtr(aDataBuffer) + offs, allocatedBuffer, n); |
3657 | 2374 |
} |
2375 |
free(allocatedBuffer); |
|
2376 |
} |
|
1885 | 2377 |
# else |
3657 | 2378 |
__BEGIN_INTERRUPTABLE__ |
2379 |
do { |
|
2380 |
if (extPtr) { |
|
2381 |
n = recv(sock, extPtr + offs, objSize, flags); |
|
2382 |
} else { |
|
2383 |
n = recv(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, flags); |
|
2384 |
} |
|
2385 |
} while ((n < 0) && (errno == EINTR)); |
|
2386 |
__END_INTERRUPTABLE__ |
|
1885 | 2387 |
# endif |
2388 |
||
3657 | 2389 |
if (n < 0) { |
2390 |
error = __INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2391 |
} else { |
|
2392 |
RETURN(__MKSMALLINT(n)); |
|
2393 |
} |
|
1885 | 2394 |
} |
2395 |
#endif |
|
2396 |
bad: ; |
|
2397 |
%}. |
|
3126 | 2398 |
error notNil ifTrue:[ |
3657 | 2399 |
^ self readError:error. |
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2400 |
]. |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2401 |
" |
1282 | 2402 |
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
|
2403 |
" |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2404 |
self primitiveFailed |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2405 |
! |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2406 |
|
1885 | 2407 |
receiveFrom:anAddressBuffer buffer:aDataBuffer |
2408 |
"receive datagramm data - put address of originating host into |
|
2409 |
anAddressBuffer, data into aBuffer. |
|
2410 |
Both must be ByteArray-like. The addressBuffer must |
|
2411 |
provide space for a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
2412 |
Return the number of bytes received, or a negative number on error. |
|
2413 |
On error, the unix error code is left in the lastErrorNumber |
|
126 | 2414 |
instance variable." |
28 | 2415 |
|
3615 | 2416 |
^ self receiveFrom:anAddressBuffer buffer:aDataBuffer start:1 for:(aDataBuffer size) flags:0 |
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
2417 |
! |
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
2418 |
|
1885 | 2419 |
receiveFrom:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes |
3615 | 2420 |
^ self receiveFrom:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes flags:0 |
2421 |
! |
|
2422 |
||
2423 |
receiveFrom:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes flags:flags |
|
1885 | 2424 |
"receive datagramm data |
2425 |
- put address of originating host into anAddressBuffer, data into aBuffer. |
|
2426 |
For backward compatibility, the addressBuffer may be a non-SocketAddress; |
|
2427 |
then, it must be a byteArray with appropriate size for the addressBytes. |
|
2428 |
||
2429 |
Return the number of bytes received, or a negative number on error. |
|
2430 |
On error, the unix error code is left in the lastErrorNumber |
|
2431 |
instance variable. |
|
2432 |
The thread blocks until data arrives - you may want to wait before |
|
2433 |
receiving, using #readWait or #readWaitWithTimeout:." |
|
2434 |
||
3126 | 2435 |
|domainClass addr addrLen nReceived error| |
1885 | 2436 |
|
2437 |
domainClass := self class socketAddressClassForDomain:domain. |
|
2438 |
domainClass isNil ifTrue:[ |
|
4414 | 2439 |
^ self error:'invalid (unsupported) domain'. |
1885 | 2440 |
]. |
4234 | 2441 |
anAddressBuffer isSocketAddress ifTrue:[ |
4414 | 2442 |
anAddressBuffer class == domainClass ifFalse:[ |
2443 |
^ self error:'addressBuffer class mismatch (domain)'. |
|
2444 |
]. |
|
2445 |
addr := anAddressBuffer. |
|
1885 | 2446 |
] ifFalse:[ |
4414 | 2447 |
anAddressBuffer notNil ifTrue:[ |
2448 |
addr := domainClass new. |
|
2449 |
]. |
|
1885 | 2450 |
]. |
2451 |
||
2452 |
%{ |
|
2453 |
#ifndef NO_SOCKET |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
2454 |
OBJ fp = __INST(handle); |
1885 | 2455 |
|
2456 |
if (fp != nil) { |
|
4414 | 2457 |
SOCKET sock; |
2458 |
size_t objSize; |
|
2459 |
union sockaddr_u sa; |
|
2460 |
socklen_t alen = 0; |
|
2461 |
INT n, offs; |
|
2462 |
int _flags = __longIntVal(flags); |
|
2463 |
char *extPtr; |
|
2464 |
unsigned char *allocatedBuffer = NULL, *buffer = NULL; |
|
2465 |
||
2466 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
2467 |
||
2468 |
if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad; |
|
2469 |
if (__isSmallInteger(nBytes)) { |
|
2470 |
if (__intVal(nBytes) < objSize) { |
|
2471 |
objSize = __intVal(nBytes); |
|
2472 |
} |
|
2473 |
} |
|
1885 | 2474 |
# ifdef DO_WRAP_CALLS |
4414 | 2475 |
if (extPtr) { |
2476 |
buffer = extPtr + offs; |
|
2477 |
} else { |
|
2478 |
allocatedBuffer = buffer = (char *)malloc(objSize); |
|
2479 |
} |
|
2480 |
||
2481 |
do { |
|
2482 |
__threadErrno = 0; |
|
2483 |
alen = sizeof(sa); |
|
2484 |
n = (INT)STX_WSA_NOINT_CALL6("recvfrom", recvfrom, sock, buffer, objSize, _flags, (struct sockaddr *)&sa, &alen); |
|
2485 |
} while ((n < 0) && (__threadErrno == EINTR)); |
|
2486 |
if (n < 0) { |
|
2487 |
errno = __threadErrno; |
|
2488 |
} |
|
2489 |
||
2490 |
if (allocatedBuffer) { |
|
2491 |
if (n > 0) { |
|
2492 |
memcpy((char *)__InstPtr(aDataBuffer) + offs, allocatedBuffer, n); |
|
2493 |
} |
|
2494 |
free(allocatedBuffer); |
|
2495 |
} |
|
1885 | 2496 |
# else |
4414 | 2497 |
__BEGIN_INTERRUPTABLE__ |
2498 |
do { |
|
2499 |
alen = sizeof(sa); |
|
2500 |
if (extPtr) { |
|
2501 |
n = recvfrom(sock, extPtr + offs, objSize, _flags, (struct sockaddr *) &sa, &alen); |
|
2502 |
} else { |
|
2503 |
n = recvfrom(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, _flags, (struct sockaddr *) &sa, &alen); |
|
2504 |
} |
|
2505 |
} while ((n < 0) && (errno == EINTR)); |
|
2506 |
__END_INTERRUPTABLE__ |
|
1885 | 2507 |
# endif |
2508 |
||
4414 | 2509 |
if (n >= 0) { |
2510 |
if (__isNonNilObject(addr)) { |
|
2511 |
char *addrPtr; |
|
2512 |
OBJ oClass; |
|
2513 |
int nInstVars, nInstBytes, objSize; |
|
2514 |
||
2515 |
oClass = __qClass(addr); |
|
2516 |
if (! __isBytes(addr) ) |
|
2517 |
goto bad; |
|
2518 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
2519 |
nInstBytes = OHDR_SIZE + (nInstVars * sizeof(OBJ)); |
|
2520 |
objSize = __qSize(addr) - nInstBytes; |
|
2521 |
addrPtr = (char *)__InstPtr(addr) + nInstBytes; |
|
2522 |
if (objSize < alen) |
|
2523 |
goto bad; |
|
2524 |
||
2525 |
/* |
|
2526 |
* extract the datagrams address |
|
2527 |
*/ |
|
2528 |
memcpy(addrPtr, (char *)&sa, alen); |
|
2529 |
addrLen = __MKSMALLINT(alen); |
|
2530 |
} |
|
2531 |
} |
|
2532 |
if (n < 0) { |
|
2533 |
error = __INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2534 |
} |
|
2535 |
nReceived = __MKSMALLINT(n); |
|
1885 | 2536 |
} |
2537 |
#endif |
|
2538 |
bad: ; |
|
2539 |
%}. |
|
3126 | 2540 |
error notNil ifTrue:[ |
4414 | 2541 |
^ self readError:error. |
3126 | 2542 |
]. |
2543 |
||
1885 | 2544 |
nReceived notNil ifTrue:[ |
4414 | 2545 |
addrLen notNil ifTrue:[ |
2546 |
(addr == anAddressBuffer) ifFalse:[ |
|
2547 |
self obsoleteFeatureWarning:'please use a socketAddress argument'. |
|
2548 |
||
2549 |
"can be a ByteArray for backward compatibility" |
|
2550 |
anAddressBuffer replaceFrom:1 to:addrLen with:(addr hostAddress). |
|
2551 |
]. |
|
2552 |
]. |
|
2553 |
^ nReceived |
|
1885 | 2554 |
]. |
2555 |
" |
|
2556 |
arrive here if you try to receive into an invalid buffer |
|
2557 |
(i.e. not ByteArray-like), |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2558 |
or if the addressBuffer is nonNil AND not a SocketAddress/ByteArray |
126 | 2559 |
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
|
2560 |
" |
126 | 2561 |
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
|
2562 |
! |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2563 |
|
1885 | 2564 |
sendBuffer:aDataBuffer start:startIndex for:nBytes flags:flags |
2565 |
"send data. |
|
2566 |
Both must be ByteArray-like. The bytes in the addressBuffer must |
|
2567 |
be a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
2568 |
Return the number of bytes transmitted, or a negative number on error. |
|
2569 |
On error, the unix error code is left in the lastErrorNumber |
|
2570 |
instance variable." |
|
2571 |
||
3126 | 2572 |
|error| |
1885 | 2573 |
|
2574 |
%{ |
|
2575 |
#ifndef NO_SOCKET |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
2576 |
OBJ fp = __INST(handle); |
1885 | 2577 |
|
2578 |
if ((fp != nil) |
|
2579 |
&& __isSmallInteger(startIndex) |
|
2580 |
&& __isSmallInteger(nBytes)) { |
|
3657 | 2581 |
SOCKET sock; |
2582 |
INT objSize, n, offs; |
|
2583 |
char *extPtr; |
|
2584 |
int _flags = __longIntVal(flags); |
|
2585 |
unsigned long norder; |
|
2586 |
unsigned char *buffer, *allocatedBuffer = NULL; |
|
2587 |
||
2588 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
2589 |
||
2590 |
if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad; |
|
2591 |
if (__isSmallInteger(nBytes)) { |
|
2592 |
if (__intVal(nBytes) < objSize) { |
|
2593 |
objSize = __intVal(nBytes); |
|
2594 |
} |
|
2595 |
} |
|
1885 | 2596 |
|
2597 |
# ifdef DGRAM_DEBUG |
|
3657 | 2598 |
console_printf("sending %d bytes ...\n", nBytes); |
1885 | 2599 |
# endif |
2600 |
||
2601 |
#ifdef DO_WRAP_CALLS |
|
3657 | 2602 |
if (extPtr) { |
2603 |
buffer = extPtr + offs; |
|
2604 |
} else { |
|
2605 |
allocatedBuffer = buffer = (char *)malloc(objSize); |
|
4414 | 2606 |
memcpy(allocatedBuffer, (char *)__InstPtr(aDataBuffer) + offs, objSize); |
3657 | 2607 |
} |
2608 |
||
2609 |
do { |
|
2610 |
__threadErrno = 0; |
|
2611 |
n = (INT)STX_WSA_NOINT_CALL4("send", send, sock, buffer, objSize, _flags); |
|
2612 |
} while ((n < 0) && (__threadErrno == EINTR)); |
|
2613 |
if (n < 0) { |
|
2614 |
errno = __threadErrno; |
|
2615 |
} |
|
2616 |
||
2617 |
if (allocatedBuffer) { |
|
2618 |
free(allocatedBuffer); |
|
2619 |
} |
|
1885 | 2620 |
#else |
3657 | 2621 |
__BEGIN_INTERRUPTABLE__ |
2622 |
do { |
|
2623 |
if (extPtr) { |
|
2624 |
n = send(sock, extPtr + offs, objSize, _flags); |
|
2625 |
} else { |
|
2626 |
n = send(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, _flags); |
|
2627 |
} |
|
2628 |
} while ((n < 0) && (errno == EINTR)); |
|
2629 |
__END_INTERRUPTABLE__ |
|
1885 | 2630 |
#endif |
2631 |
||
3657 | 2632 |
if (n < 0) { |
2633 |
error = __INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2634 |
} else { |
|
2635 |
RETURN (__MKSMALLINT(n)); |
|
2636 |
} |
|
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2637 |
} |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2638 |
#endif |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2639 |
bad: ; |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2640 |
%}. |
3126 | 2641 |
error notNil ifTrue:[ |
3657 | 2642 |
self writeError:error. |
3126 | 2643 |
]. |
2644 |
||
915
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2645 |
" |
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2646 |
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
|
2647 |
" |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2648 |
self primitiveFailed |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2649 |
! |
3dc0e3cd838a
added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents:
898
diff
changeset
|
2650 |
|
1885 | 2651 |
sendTo:anAddressBuffer buffer:buffer |
2652 |
"send datagramm data - fetch address of destination host from |
|
2653 |
anAddressBuffer, data from aDataBuffer. |
|
2654 |
Both must be ByteArray-like. The bytes in the addressBuffer must |
|
2655 |
be a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
2656 |
Return the number of bytes transmitted, or a negative number on error. |
|
2657 |
On error, the unix error code is left in the lastErrorNumber |
|
126 | 2658 |
instance variable. |
2659 |
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
|
2660 |
|
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2661 |
^ 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
|
2662 |
! |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2663 |
|
1885 | 2664 |
sendTo:anAddressBuffer buffer:buffer start:startIndex for:count |
2665 |
"send datagramm data - fetch address of destination host from |
|
2666 |
anAddressBuffer, data from aDataBuffer. |
|
2667 |
Both must be ByteArray-like. The bytes in the addressBuffer must |
|
2668 |
be a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
2669 |
Return the number of bytes transmitted, or a negative number on error. |
|
2670 |
On error, the unix error code is left in the lastErrorNumber |
|
2671 |
instance variable. |
|
817 | 2672 |
Flags is currently ignored; it is there for ST-80 compatibility." |
2673 |
||
1485
d8578cf3504b
gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents:
1474
diff
changeset
|
2674 |
^ self sendTo:anAddressBuffer buffer:buffer start:startIndex for:count flags:0 |
1885 | 2675 |
! |
2676 |
||
2677 |
sendTo:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes flags:flags |
|
2678 |
"send datagramm data - fetch address of destination host from |
|
2679 |
anAddressBuffer, data from aDataBuffer starting at startIndex, |
|
2680 |
sending count bytes. |
|
2681 |
Both must be ByteArray-like. The bytes in the addressBuffer must |
|
2682 |
be a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
2683 |
Return the number of bytes transmitted, or a negative number on error. |
|
2684 |
On error, the unix error code is left in the lastErrorNumber |
|
2685 |
instance variable." |
|
2686 |
||
3126 | 2687 |
|domainClass addr error| |
1885 | 2688 |
|
4234 | 2689 |
anAddressBuffer isSocketAddress ifTrue:[ |
4414 | 2690 |
addr := anAddressBuffer. |
1885 | 2691 |
] ifFalse:[ |
4414 | 2692 |
anAddressBuffer isByteArray ifFalse:[ |
2693 |
^ self error:'bad socketAddress argument' |
|
2694 |
]. |
|
2695 |
domainClass := self class socketAddressClassForDomain:domain. |
|
2696 |
domainClass isNil ifTrue:[ |
|
2697 |
^ self error:'invalid (unsupported) domain'. |
|
2698 |
]. |
|
2699 |
addr := domainClass hostAddress:anAddressBuffer. |
|
1885 | 2700 |
]. |
2701 |
%{ |
|
2702 |
#ifndef NO_SOCKET |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
2703 |
OBJ fp = __INST(handle); |
1885 | 2704 |
|
2705 |
if ((fp != nil) |
|
2706 |
&& __isSmallInteger(startIndex) |
|
2707 |
&& __isSmallInteger(nBytes)) { |
|
4414 | 2708 |
SOCKET sock; |
2709 |
INT objSize; |
|
2710 |
struct sockaddr *sockaddr_ptr; |
|
2711 |
union sockaddr_u sa; |
|
2712 |
socklen_t sockaddr_size, alen = 0; |
|
2713 |
INT sockAddrOffs; |
|
2714 |
INT n, offs; |
|
2715 |
char *extPtr; |
|
2716 |
int _flags = __longIntVal(flags); |
|
2717 |
unsigned long norder; |
|
2718 |
unsigned char *buffer; |
|
2719 |
unsigned char *allocatedBuffer = NULL; |
|
2720 |
||
2721 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
2722 |
||
2723 |
if (! __isBytes(addr)) { |
|
2724 |
sockaddr_size = 0; |
|
2725 |
sockaddr_ptr = (struct sockaddr *)0; |
|
2726 |
} else { |
|
2727 |
int nIndex; |
|
2728 |
OBJ cls; |
|
2729 |
||
2730 |
sockAddrOffs = 0; |
|
2731 |
if ((cls = __qClass(addr)) != @global(ByteArray)) |
|
2732 |
sockAddrOffs += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
2733 |
nIndex = __qSize(addr) - OHDR_SIZE; |
|
2734 |
sockaddr_size = nIndex - sockAddrOffs; |
|
2735 |
if (sockaddr_size > sizeof(sa)) { |
|
2736 |
console_fprintf(stderr, "Socket [warning]: bad socketAddr\n"); |
|
2737 |
goto bad; |
|
2738 |
} |
|
2739 |
memcpy(&sa, (__byteArrayVal(addr) + sockAddrOffs), sockaddr_size); |
|
2740 |
sockaddr_ptr = (struct sockaddr *)(&sa); |
|
2741 |
} |
|
2742 |
||
2743 |
if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad; |
|
2744 |
if (__isSmallInteger(nBytes)) { |
|
2745 |
if (__intVal(nBytes) < objSize) { |
|
2746 |
objSize = __intVal(nBytes); |
|
2747 |
} |
|
2748 |
} |
|
1885 | 2749 |
|
2750 |
#ifdef DO_WRAP_CALLS |
|
4414 | 2751 |
if (extPtr) { |
2752 |
buffer = extPtr + offs; |
|
2753 |
} else { |
|
2754 |
allocatedBuffer = buffer = (char *)malloc(objSize); |
|
2755 |
memcpy(allocatedBuffer, (char *)__InstPtr(aDataBuffer) + offs, objSize); |
|
2756 |
} |
|
2757 |
||
2758 |
do { |
|
2759 |
__threadErrno = 0; |
|
2760 |
n = (INT)STX_WSA_NOINT_CALL6("sendto", sendto, sock, buffer, objSize, _flags, sockaddr_ptr, sockaddr_size); |
|
2761 |
} while ((n < 0) && (__threadErrno == EINTR)); |
|
2762 |
if (n < 0) { |
|
2763 |
errno = __threadErrno; |
|
2764 |
} |
|
2765 |
||
2766 |
if (allocatedBuffer) { |
|
2767 |
free(allocatedBuffer); |
|
2768 |
} |
|
1885 | 2769 |
#else |
4414 | 2770 |
__BEGIN_INTERRUPTABLE__ |
2771 |
do { |
|
2772 |
if (extPtr) { |
|
2773 |
n = sendto(sock, extPtr + offs, objSize, _flags, sockaddr_ptr, sockaddr_size); |
|
2774 |
} else { |
|
2775 |
n = sendto(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, _flags, sockaddr_ptr, sockaddr_size); |
|
2776 |
} |
|
2777 |
} while ((n < 0) && (errno == EINTR)); |
|
2778 |
__END_INTERRUPTABLE__ |
|
1885 | 2779 |
#endif |
2780 |
||
4414 | 2781 |
if (n < 0) { |
2782 |
error = __INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2783 |
} else { |
|
2784 |
RETURN (__MKSMALLINT(n)); |
|
2785 |
} |
|
1885 | 2786 |
} |
2787 |
#endif |
|
2788 |
bad: ; |
|
2789 |
%}. |
|
3126 | 2790 |
error notNil ifTrue:[ |
4414 | 2791 |
self writeError:error. |
3126 | 2792 |
]. |
2793 |
||
1885 | 2794 |
" |
2795 |
arrive here if you try to send from an invalid buffer |
|
2796 |
(i.e. not ByteArray-like), |
|
2797 |
or if the addressBuffer is nonNil AND not a ByteArray/String |
|
2798 |
or if the addressBuffer is nonNil AND too small. |
|
2799 |
" |
|
2800 |
self primitiveFailed |
|
0 | 2801 |
! ! |
2802 |
||
4172 | 2803 |
!Socket methodsFor:'finalization'! |
2804 |
||
2805 |
finalize |
|
2806 |
self linger:0. "/ do an abortive release - discard buffered data |
|
2807 |
self closeFile. "/ does not wait due to abortive release. |
|
2808 |
! ! |
|
2809 |
||
3484 | 2810 |
!Socket methodsFor:'initialization'! |
2811 |
||
2812 |
initialize |
|
2813 |
super initialize. |
|
2814 |
"/ transparent |
|
2815 |
eolMode := nil. |
|
2816 |
! ! |
|
2817 |
||
3123 | 2818 |
!Socket methodsFor:'low level'! |
1885 | 2819 |
|
1935 | 2820 |
getSocketAdress |
2821 |
"BAD SPELLING, of #getSocketAddress, kept for compatibility with swazoo" |
|
2822 |
||
2823 |
<resource: #obsolete> |
|
2824 |
||
2825 |
^ self getSocketAddress |
|
2826 |
! |
|
2827 |
||
1885 | 2828 |
getSocketError |
2829 |
"get the SO_ERROR form the socket, which indicates the |
|
2830 |
result of an asynchronous operation" |
|
2831 |
||
2832 |
%{ |
|
2833 |
#ifndef NO_SOCKET |
|
2834 |
OBJ fp; |
|
2835 |
int err; |
|
2836 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
2837 |
fp = __INST(handle); |
1885 | 2838 |
if (fp == nil) { |
2839 |
err = EBADF; |
|
2840 |
} else { |
|
2841 |
unsigned int sz; |
|
2842 |
SOCKET sock; |
|
2843 |
||
2844 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
2845 |
sz = sizeof(err); |
|
2909 | 2846 |
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)(&err), &sz) < 0) { |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2847 |
# ifdef __win32__ |
1568
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
2848 |
errno = WSAGetLastError(); |
1558
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
2849 |
# endif |
1568
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
2850 |
err = errno; |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
2851 |
} |
1558
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
2852 |
} |
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
2853 |
|
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
2854 |
RETURN(__MKSMALLINT(err)); |
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
2855 |
#endif |
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
2856 |
%} |
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
2857 |
! |
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
2858 |
|
1935 | 2859 |
listenWithBacklog:aNumber |
2860 |
<resource: #obsolete> |
|
2861 |
"same as listenFor: - backward compatibility with old ST/X" |
|
3059 | 2862 |
self obsoleteMethodWarning. |
1935 | 2863 |
^ self listenFor:aNumber |
1885 | 2864 |
! |
2865 |
||
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2866 |
primAcceptOn:aServerSocket blocking:blocking |
3059 | 2867 |
"accept a connection on a server port (created with:'Socket>>newTCP:') |
2868 |
usage is: (Socket basicNew acceptOn:(Socket newTCP:9999)). |
|
1935 | 2869 |
Return the true if ok; false if not. |
2870 |
If blocking is true, the accept() syscall (and the whole smalltalk image) |
|
2871 |
will block, until a connection is accepted. |
|
2872 |
If blocking is false, this call will return immediately, if there is no connection pending." |
|
2873 |
||
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2874 |
|serverSocketHandle addr domainClass newHandle| |
1935 | 2875 |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
2876 |
handle notNil ifTrue:[ |
4414 | 2877 |
^ self errorAlreadyOpen. |
4180 | 2878 |
]. |
2879 |
serverSocketHandle := aServerSocket fileHandle. |
|
2880 |
serverSocketHandle isNil ifTrue:[ |
|
4414 | 2881 |
"socket is not open" |
2882 |
^ false |
|
1935 | 2883 |
]. |
2884 |
||
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2885 |
domain := aServerSocket domain. |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2886 |
socketType := aServerSocket type. |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2887 |
handleType := aServerSocket handleType. |
3059 | 2888 |
"unix domain sockets do not return a valid peer name on accept" |
1935 | 2889 |
domainClass := self class socketAddressClassForDomain:domain. |
2890 |
domainClass isNil ifTrue:[ |
|
4414 | 2891 |
^ self error:'invalid (unsupported) domain'. |
1935 | 2892 |
]. |
2893 |
addr := domainClass new. |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2894 |
newHandle := OperatingSystem socketAccessor new. |
1935 | 2895 |
|
2896 |
%{ /* STACK: 100000 */ |
|
1885 | 2897 |
#ifndef NO_SOCKET |
1935 | 2898 |
FILE *fp; |
2899 |
int flags; |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2900 |
SOCKET serverSocket, newSock; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2901 |
int _fd; |
1935 | 2902 |
union sockaddr_u sa; |
3059 | 2903 |
unsigned int alen; |
2904 |
||
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2905 |
# ifdef __win32__ |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2906 |
serverSocket = SOCKET_FROM_FILE_OBJECT(serverSocketHandle); |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2907 |
# else |
3247 | 2908 |
serverSocket = __intVal(serverSocketHandle); |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2909 |
# endif |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2910 |
|
1935 | 2911 |
# if defined(O_NONBLOCK) && defined(SET_NDELAY) |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2912 |
if (blocking == false) { |
4414 | 2913 |
flags = fcntl(serverSocket, F_GETFL); |
2914 |
fcntl(serverSocket, F_SETFL, flags | O_NONBLOCK); |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2915 |
} |
1935 | 2916 |
# endif |
2917 |
||
1885 | 2918 |
# ifdef DO_WRAP_CALLS |
2919 |
do { |
|
4414 | 2920 |
__threadErrno = 0; |
2921 |
alen = sizeof(sa); |
|
2922 |
newSock = (SOCKET)STX_WSA_CALL3("accept", accept, serverSocket, &sa, &alen); |
|
1935 | 2923 |
} while ((newSock < 0) && (__threadErrno == EINTR)); |
3127 | 2924 |
if (newSock < 0) { |
4414 | 2925 |
errno = __threadErrno; |
3126 | 2926 |
} |
1885 | 2927 |
# else |
2928 |
__BEGIN_INTERRUPTABLE__ |
|
2929 |
do { |
|
4414 | 2930 |
alen = sizeof(sa); |
2931 |
newSock = accept(serverSocket, (struct sockaddr *) &sa, &alen); |
|
1935 | 2932 |
} while ((newSock < 0) && (errno == EINTR)); |
1885 | 2933 |
__END_INTERRUPTABLE__ |
2934 |
# endif |
|
1935 | 2935 |
DBGFPRINTF((stderr, "SOCKET: accept newSock=%d\n", newSock)); |
2936 |
||
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2937 |
# if defined(O_NDELAY) && defined(SET_NDELAY) |
1935 | 2938 |
if (blocking == false) { |
4414 | 2939 |
fcntl(serverSocket, F_SETFL, flags); |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2940 |
} |
1935 | 2941 |
# endif |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2942 |
|
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2943 |
if (newSock == -1) { |
4414 | 2944 |
DBGPRINTF(("SOCKET: accept call failed errno=%d\n", errno)); |
2945 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2946 |
RETURN (false); |
|
1935 | 2947 |
} |
2948 |
||
2949 |
if (__isNonNilObject(addr)) { |
|
4414 | 2950 |
OBJ oClass = __qClass(addr); |
2951 |
int nInstVars, nInstBytes, objSize; |
|
2952 |
char *addrP; |
|
2953 |
||
2954 |
if (! __isBytes(addr) ) { |
|
2955 |
DBGPRINTF(("SOCKET: bad addr\n")); |
|
2956 |
closesocket(newSock); |
|
2957 |
RETURN (false); |
|
2958 |
} |
|
2959 |
||
2960 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
2961 |
nInstBytes = OHDR_SIZE + (nInstVars * sizeof(OBJ)); |
|
2962 |
objSize = __qSize(addr) - nInstBytes; |
|
2963 |
addrP = (char *)__InstPtr(addr) + nInstBytes; |
|
2964 |
if (objSize < alen) { |
|
2965 |
DBGPRINTF(("SOCKET: bad addr\n")); |
|
2966 |
closesocket(newSock); |
|
2967 |
RETURN (false); |
|
2968 |
} |
|
2969 |
||
2970 |
/* |
|
2971 |
* extract the partners address |
|
2972 |
*/ |
|
2973 |
memcpy(addrP, (char *)&sa, alen); |
|
1935 | 2974 |
} |
2975 |
||
2976 |
/* |
|
2977 |
* make it a FILE * |
|
2978 |
*/ |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2979 |
# ifdef __win32__ |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2980 |
# if 0 && (defined( __BORLANDC__ ) || defined( __MINGW__ )) |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2981 |
__stxWrapApiEnterCritical(); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2982 |
_fd = _open_osfhandle((long)newSock, 0); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2983 |
__stxWrapApiLeaveCritical(); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2984 |
DBGPRINTF(("SOCKET: sock=%d fd=%d\n", newSock, _fd)); |
2773 | 2985 |
# else |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
2986 |
_fd = (int)newSock; |
2773 | 2987 |
# endif |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2988 |
# else // ! __win32__ |
1935 | 2989 |
fp = fdopen(newSock, "r+"); |
2990 |
if (! fp) { |
|
4414 | 2991 |
DBGPRINTF(("SOCKET: fdopen call failed\n")); |
2992 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2993 |
closesocket(newSock); |
|
2994 |
DBGFPRINTF((stderr, "SOCKET: close (fdopen failed) (%d)\n", newSock)); |
|
2995 |
RETURN (false); |
|
1935 | 2996 |
} |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
2997 |
# endif // ! __win32__ |
1935 | 2998 |
|
2999 |
if ((@global(FileOpenTrace) == true) || __debugging__) { |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3000 |
# ifdef __win32__ |
4414 | 3001 |
console_fprintf(stderr, "fdopen [Socket accept] -> fd: %d (H: %"_lx_")\n", _fd, (INT)newSock); |
1935 | 3002 |
# else |
4414 | 3003 |
console_fprintf(stderr, "fdopen [Socket accept] -> %"_lx_" (fd: %d)\n", (INT)fp, newSock); |
1935 | 3004 |
# endif |
3005 |
} |
|
3006 |
||
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3007 |
# ifdef __win32__ |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3008 |
__externalAddressVal(newHandle) = _fd; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3009 |
__INST(handleType) = @symbol(socketHandle); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3010 |
# else |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3011 |
__externalAddressVal(newHandle) = fp; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3012 |
__INST(handleType) = @symbol(socketFilePointer); |
1935 | 3013 |
# endif |
3014 |
#endif /* not NO_SOCKET */ |
|
1558
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
3015 |
%}. |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3016 |
|
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3017 |
handle := newHandle. |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3018 |
buffered := false. |
1935 | 3019 |
mode := #readwrite. |
3020 |
binary := false. |
|
3620 | 3021 |
self registerForFinalization. |
3059 | 3022 |
peer := addr. |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3023 |
port := aServerSocket port. |
1935 | 3024 |
|
1558
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
3025 |
^ true |
2551 | 3026 |
! |
1885 | 3027 |
|
3028 |
setSocketOption:option argument:arg1 argument:arg2 |
|
3513 | 3029 |
|ok error| |
1885 | 3030 |
|
2551 | 3031 |
handle isNil ifTrue:[ |
3590 | 3032 |
^ self errorNotOpen |
2551 | 3033 |
]. |
3034 |
||
1885 | 3035 |
%{ /* STACK: 32000 */ |
2818 | 3036 |
#ifndef NO_SOCKET |
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3037 |
OBJ fp = __INST(handle); |
1885 | 3038 |
|
3039 |
if (fp != nil) { |
|
3590 | 3040 |
SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp); |
3041 |
int opt = -1; |
|
3042 |
int level = -1; |
|
3043 |
int usize = -1; |
|
3044 |
int ret; |
|
3045 |
union u { |
|
3046 |
BOOL u_bool; |
|
3047 |
int u_int; |
|
3048 |
struct linger u_linger; |
|
2516
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3049 |
# ifdef IP_ADD_MEMBERSHIP |
3590 | 3050 |
struct ip_mreq u_ip_mreq; |
2516
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3051 |
# endif |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3052 |
# if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO) |
3590 | 3053 |
struct timeval u_tv; |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3054 |
# endif |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3055 |
|
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3056 |
|
3590 | 3057 |
} u; |
2516
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3058 |
|
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3059 |
# ifdef IP_ADD_MEMBERSHIP |
3590 | 3060 |
if (option == @symbol(IP_DROP_MEMBERSHIP)) { |
3061 |
opt = IP_DROP_MEMBERSHIP; |
|
3062 |
goto dropOrAdd; |
|
3063 |
} |
|
3064 |
if (option == @symbol(IP_ADD_MEMBERSHIP)) { |
|
3065 |
/* add membership to a multicast group */ |
|
3066 |
opt = IP_ADD_MEMBERSHIP; |
|
2516
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3067 |
dropOrAdd: |
3590 | 3068 |
level = IPPROTO_IP; |
3069 |
usize = sizeof(u.u_ip_mreq); |
|
3070 |
if (__isByteArrayLike(arg1) && __isByteArrayLike(arg2)) { |
|
3071 |
memcpy(&u.u_ip_mreq.imr_multiaddr, __byteArrayVal(arg1), sizeof(u.u_ip_mreq.imr_multiaddr)); |
|
3072 |
memcpy(&u.u_ip_mreq.imr_interface, __byteArrayVal(arg2), sizeof(u.u_ip_mreq.imr_interface)); |
|
3073 |
} |
|
3074 |
else |
|
3075 |
goto argError; |
|
3076 |
} |
|
2516
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3077 |
# endif /* IP_ADD_MEMBERSHIP */ |
1885 | 3078 |
|
3079 |
# ifdef SO_BROADCAST |
|
3590 | 3080 |
if (option == @symbol(SO_BROADCAST)) { |
3081 |
/* Enables transmission and receipt of broadcast messages on the socket. */ |
|
3082 |
opt = SO_BROADCAST; |
|
3083 |
level = SOL_SOCKET; |
|
3084 |
usize = sizeof(u.u_bool); |
|
3085 |
if (arg1 == true) u.u_bool = TRUE; |
|
3086 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3087 |
else goto argError; |
|
3088 |
} |
|
1885 | 3089 |
# endif /* SO_BROADCAST */ |
3090 |
||
3091 |
# ifdef SO_CONDITIONAL |
|
3092 |
# if 0 |
|
3590 | 3093 |
if (option == @symbol(SO_CONDITIONAL)) { |
3094 |
/* Enables sockets to delay the acknowledgment of a connection until after the WSAAccept condition function is called. */ |
|
3095 |
opt = SO_CONDITIONAL; |
|
3096 |
level = SOL_SOCKET; |
|
3097 |
usize = sizeof(u.u_bool); |
|
3098 |
if (arg1 == true) u.u_bool = TRUE; |
|
3099 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3100 |
else goto argError; |
|
3101 |
} |
|
1885 | 3102 |
# endif |
3103 |
# endif /* SO_CONDITIONAL */ |
|
3104 |
||
3105 |
# ifdef SO_DEBUG |
|
3590 | 3106 |
if (option == @symbol(SO_DEBUG)) { |
3107 |
/* Records debugging information. */ |
|
3108 |
opt = SO_DEBUG; |
|
3109 |
level = SOL_SOCKET; |
|
3110 |
usize = sizeof(u.u_bool); |
|
3111 |
if (arg1 == true) u.u_bool = TRUE; |
|
3112 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3113 |
else goto argError; |
|
3114 |
} |
|
1885 | 3115 |
# endif /* SO_DEBUG */ |
3116 |
||
3117 |
# ifdef SO_DONTLINGER |
|
3590 | 3118 |
if (option == @symbol(SO_DONTLINGER)) { |
3119 |
/* Does not block close waiting for unsent data to be sent. |
|
3120 |
Setting this option is equivalent to setting SO_LINGER with l_onoff set to zero. */ |
|
3121 |
opt = SO_DONTLINGER; |
|
3122 |
level = SOL_SOCKET; |
|
3123 |
usize = sizeof(u.u_bool); |
|
3124 |
if (arg1 == true) u.u_bool = TRUE; |
|
3125 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3126 |
else goto argError; |
|
3127 |
} |
|
1885 | 3128 |
# endif /* SO_DONTLINGER */ |
3129 |
||
3130 |
# ifdef SO_DONTROUTE |
|
3590 | 3131 |
if (option == @symbol(SO_DONTROUTE)) { |
3132 |
/* Does not route: sends directly to interface. |
|
3133 |
Succeeds but is ignored on AF_INET sockets; |
|
3134 |
fails on AF_INET6 sockets with WSAENOPROTOOPT. |
|
3135 |
Not supported on ATM sockets (results in an error). */ |
|
3136 |
opt = SO_DONTROUTE; |
|
3137 |
level = SOL_SOCKET; |
|
3138 |
usize = sizeof(u.u_bool); |
|
3139 |
if (arg1 == true) u.u_bool = TRUE; |
|
3140 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3141 |
else goto argError; |
|
3142 |
} |
|
1885 | 3143 |
# endif /* SO_DONTROUTE */ |
3144 |
||
2516
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3145 |
#if defined(IPPROTO_TCP) && defined(TCP_NODELAY) |
3590 | 3146 |
if (option == @symbol(TCP_NODELAY)) { |
3147 |
/* enable/disable TCP_NODELAY (i.e. disable/enable the Nagle algorithm) */ |
|
3148 |
opt = TCP_NODELAY; |
|
3149 |
level = IPPROTO_TCP; |
|
3150 |
usize = sizeof(u.u_bool); |
|
3151 |
if (arg1 == true) u.u_bool = TRUE; |
|
3152 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3153 |
else goto argError; |
|
3154 |
} |
|
2516
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3155 |
# endif /* TCP_NODELAY */ |
592619f82885
Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents:
2465
diff
changeset
|
3156 |
|
1885 | 3157 |
# ifdef SO_KEEPALIVE |
3590 | 3158 |
if (option == @symbol(SO_KEEPALIVE)) { |
3159 |
/* Sends keep-alives. Not supported on ATM sockets (results in an error). */ |
|
3160 |
opt = SO_KEEPALIVE; |
|
3161 |
level = SOL_SOCKET; |
|
3162 |
usize = sizeof(u.u_bool); |
|
3163 |
if (arg1 == true) u.u_bool = TRUE; |
|
3164 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3165 |
else goto argError; |
|
3166 |
} |
|
1885 | 3167 |
# endif /* SO_KEEPALIVE */ |
3168 |
||
3169 |
# ifdef SO_LINGER |
|
3590 | 3170 |
if (option == @symbol(SO_LINGER)) { |
3171 |
/* Lingers on close if unsent data is present. */ |
|
3172 |
opt = SO_LINGER; |
|
3173 |
level = SOL_SOCKET; |
|
3174 |
usize = sizeof(u.u_linger); |
|
3175 |
if (arg1 == true) u.u_linger.l_onoff = TRUE; |
|
3176 |
else if (arg1 == false) u.u_linger.l_onoff = FALSE; |
|
3177 |
else goto argError; |
|
3178 |
if (arg2 == nil) u.u_linger.l_linger = 0; |
|
3179 |
else if (__isSmallInteger(arg2))u.u_linger.l_linger = __intVal(arg2); |
|
3180 |
else goto argError; |
|
3181 |
DBGPRINTF(("SOCKET: SO_LINGER %d %d\n", u.u_linger.l_onoff, u.u_linger.l_linger)); |
|
3182 |
} |
|
1885 | 3183 |
# endif /* SO_LINGER */ |
3184 |
||
3185 |
# ifdef SO_OOBINLINE |
|
3590 | 3186 |
if (option == @symbol(SO_OOBINLINE)) { |
3187 |
/* Receives OOB data in the normal data stream. */ |
|
3188 |
opt = SO_OOBINLINE; |
|
3189 |
level = SOL_SOCKET; |
|
3190 |
usize = sizeof(u.u_bool); |
|
3191 |
if (arg1 == true) u.u_bool = TRUE; |
|
3192 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3193 |
else goto argError; |
|
3194 |
} |
|
1885 | 3195 |
# endif /* SO_OOBINLINE */ |
3196 |
||
3197 |
# ifdef SO_RCVBUF |
|
3590 | 3198 |
if (option == @symbol(SO_RCVBUF)) { |
3199 |
/* Specifies the total per-socket buffer space reserved for receives. |
|
3200 |
This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window. */ |
|
3201 |
opt = SO_RCVBUF; |
|
3202 |
level = SOL_SOCKET; |
|
3203 |
usize = sizeof(u.u_int); |
|
3204 |
if (__isSmallInteger(arg1))u.u_int = __intVal(arg1); |
|
3205 |
else goto argError; |
|
3206 |
} |
|
1885 | 3207 |
# endif /* SO_RCVBUF */ |
3208 |
||
3209 |
# ifdef SO_SNDBUF |
|
3590 | 3210 |
if (option == @symbol(SO_SNDBUF)) { |
3211 |
/* Specifies the total per-socket buffer space reserved for sends. |
|
3212 |
This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window. */ |
|
3213 |
opt = SO_SNDBUF; |
|
3214 |
level = SOL_SOCKET; |
|
3215 |
usize = sizeof(u.u_int); |
|
3216 |
if (__isSmallInteger(arg1))u.u_int = __intVal(arg1); |
|
3217 |
else goto argError; |
|
3218 |
} |
|
1885 | 3219 |
# endif /* SO_SNDBUF */ |
3220 |
||
3221 |
# ifdef SO_REUSEADDR |
|
3590 | 3222 |
if (option == @symbol(SO_REUSEADDR)) { |
3223 |
/* Allows the socket to be bound to an address that is already in use. */ |
|
3224 |
opt = SO_REUSEADDR; |
|
3225 |
level = SOL_SOCKET; |
|
3226 |
usize = sizeof(u.u_bool); |
|
3227 |
if (arg1 == true) u.u_bool = TRUE; |
|
3228 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3229 |
else goto argError; |
|
3230 |
} |
|
1885 | 3231 |
# endif /* SO_OOBINLINE */ |
3232 |
||
3233 |
# ifdef SO_EXCLUSIVEADDRUSE |
|
3590 | 3234 |
if (option == @symbol(SO_EXCLUSIVEADDRUSE)) { |
3235 |
/* Enables a socket to be bound for exclusive access. |
|
3236 |
Does not require administrative privilege. */ |
|
3237 |
opt = SO_EXCLUSIVEADDRUSE; |
|
3238 |
level = SOL_SOCKET; |
|
3239 |
usize = sizeof(u.u_bool); |
|
3240 |
if (arg1 == true) u.u_bool = TRUE; |
|
3241 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3242 |
else goto argError; |
|
3243 |
} |
|
1885 | 3244 |
# endif /* SO_OOBINLINE */ |
3245 |
||
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3246 |
# ifdef SO_RCVTIMEO |
3590 | 3247 |
if ((option == @symbol(SO_RCVTIMEO)) |
3248 |
&& __isSmallInteger(arg1) |
|
3249 |
&& __isSmallInteger(arg2)) { |
|
3250 |
opt = SO_RCVTIMEO; |
|
3251 |
level = SOL_SOCKET; |
|
3252 |
u.u_tv.tv_sec = __intVal(arg1); |
|
3253 |
u.u_tv.tv_usec = __intVal(arg2); |
|
3254 |
usize = sizeof(u.u_tv); |
|
3255 |
} |
|
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3256 |
# endif /* SO_RCVTIMEO */ |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3257 |
|
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3258 |
# ifdef SO_SNDTIMEO |
3590 | 3259 |
if ((option == @symbol(SO_SNDTIMEO)) |
3260 |
&& __isSmallInteger(arg1) |
|
3261 |
&& __isSmallInteger(arg2)) { |
|
3262 |
opt = SO_SNDTIMEO; |
|
3263 |
level = SOL_SOCKET; |
|
3264 |
u.u_tv.tv_sec = __intVal(arg1); |
|
3265 |
u.u_tv.tv_usec = __intVal(arg2); |
|
3266 |
usize = sizeof(u.u_tv); |
|
3267 |
} |
|
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3268 |
# endif /* SO_SNDTIMEO */ |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3269 |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3270 |
# if !defined(IPV6_V6ONLY) && defined(__win32__) |
3513 | 3271 |
# define IPPROTO_IPV6 41 |
3272 |
# define IPV6_V6ONLY 27 |
|
3273 |
# endif |
|
3274 |
||
3275 |
# if defined(IPV6_V6ONLY) |
|
3590 | 3276 |
if (option == @symbol(IPV6_V6ONLY)) { |
3277 |
opt = IPV6_V6ONLY; |
|
3278 |
level = IPPROTO_IPV6; |
|
3279 |
usize = sizeof(u.u_bool); |
|
3280 |
if (arg1 == true) u.u_bool = TRUE; |
|
3281 |
else if (arg1 == false) u.u_bool = FALSE; |
|
3282 |
else goto argError; |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3283 |
# ifdef __win32__ |
3591 | 3284 |
console_fprintf(stderr, "%d %d %d %d\n", level, opt, usize, u.u_int); |
3514
2d81c5ffa475
win32_printf is only avail in: guess what
Claus Gittinger <cg@exept.de>
parents:
3513
diff
changeset
|
3285 |
# endif |
3590 | 3286 |
} |
3513 | 3287 |
# endif /* IPV6_V6ONLY */ |
3288 |
||
3590 | 3289 |
if (usize == -1) goto argError; |
3290 |
||
3291 |
ok = (setsockopt(sock, level, opt, (char *)&u, usize) >= 0) ? true : false; |
|
3292 |
if (ok == false) { |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3293 |
# ifdef __win32__ |
3590 | 3294 |
error = __mkSmallInteger(WSAGetLastError()); |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3295 |
# else |
3590 | 3296 |
error = __mkSmallInteger(errno); |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3297 |
# endif |
3590 | 3298 |
} |
3513 | 3299 |
|
1885 | 3300 |
} |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3301 |
argError: ; |
2818 | 3302 |
#endif /* NO_SOCKET */ |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3303 |
%}. |
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3304 |
ok isNil ifTrue:[ |
3590 | 3305 |
self primitiveFailed |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3306 |
]. |
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3307 |
ok ifFalse:[ |
3590 | 3308 |
'++++ Info: Socket>>#setSocketOption:... failed. error: ' infoPrint. error infoPrintCR. |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3309 |
]. |
2551 | 3310 |
! |
1885 | 3311 |
|
2823
10179ccfae5d
Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents:
2818
diff
changeset
|
3312 |
shutdown:howNum |
1885 | 3313 |
"shutDown the socket - inform it that no more I/O will be performed. |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3314 |
0 - read side (no further reads) |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3315 |
the tcp receive window is closed, so the peer cannot send more data. |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3316 |
1 - write side (no further writes) |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3317 |
first, all queued data will be delivered to the peer. |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3318 |
Then, an orderly release (TCP FIN) is sent to signal the peer, |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3319 |
that we will not send more data. |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3320 |
2 - both read side and write side." |
1885 | 3321 |
|
3322 |
%{ |
|
3323 |
#ifndef NO_SOCKET |
|
2823
10179ccfae5d
Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents:
2818
diff
changeset
|
3324 |
OBJ __handle = __INST(handle); |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3325 |
|
2823
10179ccfae5d
Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents:
2818
diff
changeset
|
3326 |
if ((__handle != nil) && __isSmallInteger(howNum)) { |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3327 |
SOCKET sock = SOCKET_FROM_FILE_OBJECT(__handle); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3328 |
INT how = __intVal(howNum); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3329 |
INT ret; |
2823
10179ccfae5d
Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents:
2818
diff
changeset
|
3330 |
|
1885 | 3331 |
# ifdef DO_WRAP_CALLS |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3332 |
do { |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3333 |
__threadErrno = 0; |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3334 |
DBGFPRINTF((stderr, "SOCKET: shutDown...\n")); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3335 |
ret = (INT)STX_WSA_NOINT_CALL2("shutdown", shutdown, sock, how); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3336 |
DBGFPRINTF((stderr, "SOCKET: shutDown -> %d (%d)\n", ret, __threadErrno)); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3337 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3338 |
# else |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3339 |
__BEGIN_INTERRUPTABLE__ |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3340 |
shutdown(sock, how); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3341 |
__END_INTERRUPTABLE__ |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3342 |
# endif |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
3343 |
} |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
3344 |
#endif |
1339 | 3345 |
%}. |
781
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
3346 |
! ! |
4d61e7588ff3
added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents:
776
diff
changeset
|
3347 |
|
1885 | 3348 |
!Socket methodsFor:'printing & storing'! |
3349 |
||
3350 |
printOn:aStream |
|
3530 | 3351 |
aStream nextPutAll:self className; nextPutAll:'('. |
3352 |
domain printOn:aStream. |
|
3353 |
aStream nextPutAll:' protocol='. |
|
1885 | 3354 |
protocol printOn:aStream. |
3355 |
aStream nextPutAll:' port='. |
|
1935 | 3356 |
self port printOn:aStream. |
1462 | 3357 |
aStream nextPutAll:' peer='. |
3358 |
peer printOn:aStream. |
|
1842 | 3359 |
aStream nextPut:$). |
1885 | 3360 |
! ! |
3361 |
||
3362 |
!Socket methodsFor:'queries'! |
|
3363 |
||
3364 |
domain |
|
3059 | 3365 |
"return the sockets addressing domain (i.e. #AF_INET, #AF_INET6, #AF_UNIX, ...)" |
1885 | 3366 |
|
3367 |
^ domain |
|
126 | 3368 |
! |
3369 |
||
3059 | 3370 |
getFullPeerAddress |
1935 | 3371 |
"implemented for swazoo project (primitive code can't be loaded as extension) |
1885 | 3372 |
Answer my own address (I am bound to this address). |
3373 |
Note that this address may change after a connect or accept." |
|
3374 |
||
3059 | 3375 |
|error domainClass addr| |
1885 | 3376 |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3377 |
handle isNil ifTrue:[ |
3217 | 3378 |
^ self errorNotOpen |
1885 | 3379 |
]. |
3380 |
||
3381 |
domainClass := self class socketAddressClassForDomain:domain. |
|
3382 |
domainClass isNil ifTrue:[ |
|
3217 | 3383 |
^ self error:'invalid (unsupported) domain'. |
1885 | 3384 |
]. |
3385 |
addr := domainClass new. |
|
3386 |
||
3387 |
%{ |
|
3388 |
#ifndef NO_SOCKET |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3389 |
OBJ fp = __INST(handle); |
1885 | 3390 |
SOCKET sock; |
3391 |
int ret; |
|
3392 |
union sockaddr_u sa; |
|
3059 | 3393 |
unsigned int alen = sizeof(sa); |
1885 | 3394 |
char *addrP; |
3395 |
int addrObjSize, nAddrInstBytes; |
|
3059 | 3396 |
OBJ addrClass; |
3397 |
int nAddrInstVars; |
|
1885 | 3398 |
|
3399 |
if (!__isNonNilObject(addr) || !__isBytes(addr)) { |
|
3217 | 3400 |
DBGPRINTF(("SOCKET: bad addr\n")); |
3401 |
error = @symbol(badArgument); |
|
3402 |
goto err; |
|
3059 | 3403 |
} |
3404 |
||
3405 |
addrClass = __qClass(addr); |
|
3406 |
nAddrInstVars = __intVal(__ClassInstPtr(addrClass)->c_ninstvars); |
|
3407 |
nAddrInstBytes = OHDR_SIZE + (nAddrInstVars * sizeof(OBJ)); |
|
3408 |
addrObjSize = __qSize(addr) - nAddrInstBytes; |
|
3409 |
||
3410 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
3411 |
ret = getpeername(sock, (struct sockaddr *)&sa, &alen); |
|
3412 |
if (ret < 0) { |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3413 |
# ifdef __win32__ |
3217 | 3414 |
errno = WSAGetLastError(); |
3059 | 3415 |
# endif |
3217 | 3416 |
DBGPRINTF(("SOCKET: getsocketname failed ret=%d errno=%d\n", ret, errno)); |
3417 |
error = __MKSMALLINT(errno); |
|
3418 |
goto err; |
|
3059 | 3419 |
} |
3420 |
||
3421 |
if (addrObjSize < alen) { |
|
3217 | 3422 |
DBGPRINTF(("SOCKET: bad addr\n")); |
3423 |
error = @symbol(badArgument); |
|
3424 |
goto err; |
|
3059 | 3425 |
} |
3426 |
||
3427 |
addrP = (char *)__InstPtr(addr) + nAddrInstBytes; |
|
3428 |
memcpy(addrP, (char *)&sa, alen); |
|
3429 |
||
3430 |
err:; |
|
3431 |
#else /* NO_SOCKET */ |
|
3432 |
error = @symbol(notImplemented); |
|
3433 |
#endif /* NO_SOCKET */ |
|
3434 |
%}. |
|
3435 |
error notNil ifTrue:[ |
|
3217 | 3436 |
^ self errorReporter reportOn:error |
3059 | 3437 |
]. |
3438 |
^ addr |
|
3439 |
! |
|
3440 |
||
3441 |
getFullSocketAddress |
|
3442 |
"implemented for swazoo project (primitive code can't be loaded as extension) |
|
3443 |
Answer my own address (I am bound to this address). |
|
3444 |
Note that this address may change after a connect or accept." |
|
3445 |
||
3446 |
|error domainClass addr| |
|
3447 |
||
3448 |
handle isNil ifTrue:[ |
|
3217 | 3449 |
^ self errorNotOpen |
3059 | 3450 |
]. |
3451 |
||
3452 |
domainClass := self class socketAddressClassForDomain:domain. |
|
3453 |
domainClass isNil ifTrue:[ |
|
3217 | 3454 |
^ self error:'invalid (unsupported) domain'. |
3059 | 3455 |
]. |
3456 |
addr := domainClass new. |
|
3457 |
||
3458 |
%{ |
|
3459 |
#ifndef NO_SOCKET |
|
3460 |
OBJ fp = __INST(handle); |
|
3461 |
SOCKET sock; |
|
3462 |
int ret; |
|
3463 |
union sockaddr_u sa; |
|
3464 |
unsigned int alen = sizeof(sa); |
|
3465 |
char *addrP; |
|
3466 |
int addrObjSize, nAddrInstBytes; |
|
3467 |
||
3468 |
if (!__isNonNilObject(addr) || !__isBytes(addr)) { |
|
3217 | 3469 |
DBGPRINTF(("SOCKET: bad addr\n")); |
3470 |
error = @symbol(badArgument); |
|
3471 |
goto err; |
|
1885 | 3472 |
} |
3473 |
||
3474 |
{ |
|
3217 | 3475 |
OBJ addrClass; |
3476 |
int nAddrInstVars; |
|
3477 |
||
3478 |
addrClass = __qClass(addr); |
|
3479 |
nAddrInstVars = __intVal(__ClassInstPtr(addrClass)->c_ninstvars); |
|
3480 |
nAddrInstBytes = OHDR_SIZE + (nAddrInstVars * sizeof(OBJ)); |
|
3481 |
addrObjSize = __qSize(addr) - nAddrInstBytes; |
|
1885 | 3482 |
} |
3483 |
||
3484 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
3485 |
ret = getsockname(sock, (struct sockaddr *)&sa, &alen); |
|
3486 |
if (ret < 0) { |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3487 |
# ifdef __win32__ |
3217 | 3488 |
errno = WSAGetLastError(); |
1885 | 3489 |
# endif |
3217 | 3490 |
DBGPRINTF(("SOCKET: getsocketname failed ret=%d errno=%d\n", ret, errno)); |
3491 |
error = __MKSMALLINT(errno); |
|
3492 |
goto err; |
|
1885 | 3493 |
} |
3494 |
||
3495 |
if (addrObjSize < alen) { |
|
3217 | 3496 |
DBGPRINTF(("SOCKET: bad addr\n")); |
3497 |
error = @symbol(badArgument); |
|
3498 |
goto err; |
|
1885 | 3499 |
} |
3500 |
||
3501 |
addrP = (char *)__InstPtr(addr) + nAddrInstBytes; |
|
3059 | 3502 |
memcpy(addrP, (char *)&sa, alen); |
1535 | 3503 |
|
3504 |
err:; |
|
3505 |
#else /* NO_SOCKET */ |
|
3506 |
error = @symbol(notImplemented); |
|
3507 |
#endif /* NO_SOCKET */ |
|
3508 |
%}. |
|
3509 |
error notNil ifTrue:[ |
|
3217 | 3510 |
^ self errorReporter reportOn:error |
1535 | 3511 |
]. |
3512 |
^ addr |
|
3513 |
! |
|
3514 |
||
1885 | 3515 |
getName |
3516 |
"return the name; here, we return the ports name" |
|
3517 |
||
1935 | 3518 |
^ self port printString |
126 | 3519 |
! |
3520 |
||
1885 | 3521 |
getPeer |
3522 |
"ST-80 compatibility: return an IPSocketAddress instance representing |
|
3523 |
my hostname/port combination. |
|
3524 |
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
|
3525 |
|
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
|
3526 |
^ peer |
102
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
3527 |
! |
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
3528 |
|
1885 | 3529 |
getPeerName |
4091 | 3530 |
"return the peer name; that's the hostname (or dotted name) of the |
25 | 3531 |
partners host after an accept." |
3532 |
||
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
|
3533 |
peerName isNil ifTrue:[ |
4414 | 3534 |
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
|
3535 |
]. |
25 | 3536 |
^ peerName |
28 | 3537 |
! |
3538 |
||
1885 | 3539 |
getSocketAddress |
3540 |
"implemented for swazoo project primitive code cant load as extension |
|
3541 |
answer my own address (I am bound to this address). |
|
1146 | 3542 |
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
|
3543 |
|
1535 | 3544 |
^ self getFullSocketAddress hostAddress |
1146 | 3545 |
! |
3546 |
||
1885 | 3547 |
isActive |
3548 |
"return true, if the receiver has a connection" |
|
3549 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3550 |
^ handle notNil |
1885 | 3551 |
! |
3552 |
||
3553 |
isConnected |
|
3554 |
"return true, if the receiver has a connection" |
|
3555 |
||
3556 |
^ self isActive |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3557 |
and:[peer notNil] |
1558
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
3558 |
! |
a563aac90660
No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents:
1542
diff
changeset
|
3559 |
|
1885 | 3560 |
port |
1935 | 3561 |
"return the port number (or name for unix-sockets) to which the socket is bound |
3562 |
- so this is the local port." |
|
3563 |
||
3564 |
"/ port isNil ifTrue:[ |
|
3565 |
"/ port := self getFullSocketAddress port. |
|
1505 | 3566 |
"/ ]. |
1935 | 3567 |
|
71 | 3568 |
^ port |
1885 | 3569 |
! |
3570 |
||
3059 | 3571 |
socketAddressClass |
4288 | 3572 |
"get the matching SocketAddress class for this socket" |
3059 | 3573 |
|
3574 |
|domainClass| |
|
3575 |
||
3576 |
domainClass := self class socketAddressClassForDomain:domain. |
|
3577 |
domainClass isNil ifTrue:[ |
|
4414 | 3578 |
^ self error:'invalid (unsupported) domain'. |
3059 | 3579 |
]. |
3580 |
^ domainClass. |
|
3581 |
! |
|
3582 |
||
1885 | 3583 |
type |
3584 |
"return the sockets connection type (i.e. #datagram, #stream etc)" |
|
3585 |
||
3586 |
^ socketType |
|
81 | 3587 |
! ! |
3588 |
||
126 | 3589 |
!Socket methodsFor:'socket setup'! |
3590 |
||
1885 | 3591 |
domain:domainArg type:typeArg |
3592 |
"set up socket with domain and type. |
|
3593 |
This is a low level entry; no binding, listening or connect |
|
3594 |
is done. Both arguments must be symbols from one of |
|
3059 | 3595 |
#AF_INET, #AF_INET6, #AF_UNIX ... and #stream, #datagram, #raw resp." |
126 | 3596 |
|
3137 | 3597 |
^ self domain:domainArg type:typeArg protocol:nil |
126 | 3598 |
! |
3599 |
||
1885 | 3600 |
domain:domainArg type:typeArg protocol:protocolNumber |
3601 |
"set up socket with domain, type and protocol number. |
|
3602 |
This is a low level entry; no binding, listening or connect |
|
3603 |
is done. Both arguments must be symbols from one of |
|
3059 | 3604 |
#AF_INET, #AF_INET6, #AF_UNIX ... and #stream, #datagram, #raw resp." |
1885 | 3605 |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3606 |
|domainName domainCode typeCode error newHandle| |
1885 | 3607 |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3608 |
handle notNil ifTrue:[ |
4414 | 3609 |
^ self errorAlreadyOpen |
1885 | 3610 |
]. |
3611 |
domainName := SocketAddress domainCodeFromName:domainArg. |
|
3612 |
domainCode := OperatingSystem domainCodeOf:domainName. |
|
3613 |
typeCode := OperatingSystem socketTypeCodeOf:typeArg. |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3614 |
newHandle := OperatingSystem socketAccessor new. |
1885 | 3615 |
%{ |
3616 |
#ifndef NO_SOCKET |
|
3617 |
FILE *fp; |
|
3137 | 3618 |
int dom, typ, proto = 0; |
1885 | 3619 |
int on = 1; |
3620 |
SOCKET sock; |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3621 |
int _fd; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3622 |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3623 |
# ifdef __win32__ |
3123 | 3624 |
# ifndef WSA_FLAG_NO_HANDLE_INHERIT |
3625 |
# define WSA_FLAG_NO_HANDLE_INHERIT 0x80 |
|
3626 |
# endif |
|
3627 |
static int noInheritFlag = WSA_FLAG_NO_HANDLE_INHERIT; |
|
3628 |
# endif |
|
1885 | 3629 |
|
3630 |
if (! __isSmallInteger(domainCode)) { |
|
4414 | 3631 |
error = @symbol(badArgument1); |
3632 |
goto out; |
|
1885 | 3633 |
} |
3634 |
if (! __isSmallInteger(typeCode)) { |
|
4414 | 3635 |
error = @symbol(badArgument2); |
3636 |
goto out; |
|
1885 | 3637 |
} |
3638 |
if (protocolNumber != nil) { |
|
4414 | 3639 |
if (!__isSmallInteger(protocolNumber)) { |
3640 |
error = @symbol(badArgument3); |
|
3641 |
goto out; |
|
3642 |
} |
|
3643 |
proto = __intVal(protocolNumber); |
|
1885 | 3644 |
} |
3645 |
||
3646 |
/* |
|
3647 |
* get address and protocol-family |
|
3648 |
*/ |
|
3649 |
dom = __intVal(domainCode); |
|
3650 |
typ = __intVal(typeCode); |
|
3651 |
||
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3652 |
# ifdef __win32__ |
3123 | 3653 |
sock = WSASocket(dom, typ, proto, 0, 0, noInheritFlag); |
3654 |
if (sock == INVALID_SOCKET && noInheritFlag) { |
|
4414 | 3655 |
// tried to open socket with WSA_FLAG_NO_HANDLE_INHERIT |
3656 |
// This fails on older windows versions, e.g. Windows XP |
|
3657 |
sock = WSASocket(dom, typ, proto, 0, 0, 0); |
|
3658 |
if (sock != INVALID_SOCKET) { |
|
3659 |
// no error without WSA_FLAG_NO_HANDLE_INHERIT, |
|
3660 |
// never use this flag again! |
|
3661 |
noInheritFlag = 0; |
|
3662 |
} |
|
3123 | 3663 |
} |
3664 |
if (sock == INVALID_SOCKET) { |
|
4414 | 3665 |
errno = WSAGetLastError(); |
3123 | 3666 |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3667 |
# else // !__win32__ |
3123 | 3668 |
|
1885 | 3669 |
sock = socket(dom, typ, proto); |
3123 | 3670 |
# if defined(EPROTONOSUPPORT) /* for SGI */ |
3671 |
if ((sock < 0) && (proto != 0) && (errno == EPROTONOSUPPORT)) { |
|
4414 | 3672 |
DBGPRINTF(("SOCKET: retry with UNSPEC protocol\n")); |
3673 |
proto = 0; |
|
3674 |
sock = socket(dom, typ, 0); |
|
1885 | 3675 |
} |
3676 |
# endif |
|
3123 | 3677 |
if (sock < 0) { |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3678 |
# endif // !__win32__ |
3123 | 3679 |
|
4414 | 3680 |
DBGPRINTF(("SOCKET: socket(dom=%d typ=%d proto=%d) call failed errno=%d\n", dom, typ, proto, errno)); |
3681 |
error = __MKSMALLINT(errno); |
|
1885 | 3682 |
} else { |
3123 | 3683 |
# if defined(SET_LINGER_WHEN_CREATING_SOCKET) && defined(SO_LINGER) |
4414 | 3684 |
{ |
3685 |
struct linger l; |
|
3686 |
||
3687 |
l.l_onoff = 1; |
|
3688 |
l.l_linger = 30; |
|
3689 |
setsockopt(sock, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); |
|
3690 |
} |
|
1885 | 3691 |
# endif |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3692 |
# ifdef __win32__ |
4414 | 3693 |
/* |
3694 |
* make it blocking |
|
3695 |
*/ |
|
3696 |
{ |
|
3697 |
unsigned long zero = 0; |
|
3698 |
ioctlsocket(sock, FIONBIO, &zero); |
|
3699 |
} |
|
3700 |
{ |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3701 |
# if 0 && (defined( __BORLANDC__ ) || defined( __MINGW__ )) |
4414 | 3702 |
/* |
3703 |
* make it a FILE * |
|
3704 |
*/ |
|
3705 |
__stxWrapApiEnterCritical(); |
|
3706 |
_fd = _open_osfhandle((long)sock, 0); |
|
3707 |
__stxWrapApiLeaveCritical(); |
|
2773 | 3708 |
# else |
4414 | 3709 |
_fd = (int)sock; |
2773 | 3710 |
# endif |
4414 | 3711 |
DBGPRINTF(("SOCKET: sock=%d fd=%d\n", sock, _fd)); |
3712 |
} |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3713 |
# else // !__win32__ |
4414 | 3714 |
fp = fdopen(sock, "r+"); |
3715 |
if (! fp) { |
|
3716 |
DBGPRINTF(("SOCKET: fdopen call failed\n")); |
|
3717 |
error = __MKSMALLINT(errno); |
|
3718 |
__BEGIN_INTERRUPTABLE__ |
|
3719 |
closesocket(sock); |
|
3720 |
DBGFPRINTF((stderr, "SOCKET: fdopen failed (%d)\n", sock)); |
|
3721 |
__END_INTERRUPTABLE__ |
|
3722 |
goto out; |
|
3723 |
} |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3724 |
# endif // !__win32__ |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3725 |
|
4414 | 3726 |
if (@global(FileOpenTrace) == true) { |
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3727 |
# ifdef __win32__ |
4414 | 3728 |
console_fprintf(stderr, "fdopen [Socket create] -> fd: %d (H: %"_lx_")\n", (INT)_fd, (INT)sock); |
3223 | 3729 |
# else |
4414 | 3730 |
console_fprintf(stderr, "fdopen [Socket] -> %"_lx_" (fd: %d)\n", (INT)fp, sock); |
3223 | 3731 |
# endif |
4414 | 3732 |
} |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3733 |
|
3843
fbced69597a7
oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents:
3742
diff
changeset
|
3734 |
# ifdef __win32__ |
4414 | 3735 |
__externalAddressVal(newHandle) = _fd; |
3736 |
__INST(handleType) = @symbol(socketHandle); |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3737 |
# else |
4414 | 3738 |
__externalAddressVal(newHandle) = fp; |
3739 |
__INST(handleType) = @symbol(socketFilePointer); |
|
1885 | 3740 |
# endif |
3741 |
} |
|
3742 |
#endif |
|
3743 |
out:; |
|
3744 |
%}. |
|
3745 |
||
3746 |
"all ok?" |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3747 |
handleType notNil ifTrue:[ |
4414 | 3748 |
handle := newHandle. |
3749 |
domain := domainArg. |
|
3750 |
socketType := typeArg. |
|
3751 |
self registerForFinalization. |
|
3752 |
^ self. |
|
1885 | 3753 |
]. |
3217 | 3754 |
error isInteger ifTrue:[ |
4414 | 3755 |
lastErrorNumber := error. |
3756 |
^ self openError:error. |
|
3217 | 3757 |
]. |
3758 |
^ self primitiveFailed:error. |
|
126 | 3759 |
|
3760 |
" |
|
3059 | 3761 |
Socket new domain:#AF_INET type:#stream |
3762 |
Socket new domain:#AF_UNIX type:#stream |
|
126 | 3763 |
" |
28 | 3764 |
! ! |
3765 |
||
4288 | 3766 |
|
28 | 3767 |
!Socket methodsFor:'specials'! |
3768 |
||
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3769 |
linger:anIntegerOrNil |
3224 | 3770 |
"set the linger behavior on close: |
3771 |
anIntegerOrNil == nil: close returns immediately, socket tries |
|
4414 | 3772 |
to send buffered data in background. |
3224 | 3773 |
anIntegerOrNil == 0: close returns immediately, bufferd data is discarded. |
3774 |
anIntegerOrNil > 0: close waits this many seconds for buffered data |
|
4414 | 3775 |
to be delivered, after this time buffered data is |
3776 |
discarded and close returns with an error" |
|
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3777 |
|
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3778 |
^ self |
4414 | 3779 |
setSocketOption:#'SO_LINGER' |
3780 |
argument:anIntegerOrNil notNil |
|
3781 |
argument:anIntegerOrNil. |
|
3224 | 3782 |
! |
3783 |
||
1885 | 3784 |
receiveBufferSize |
3785 |
"get the send buffer size - for special applications only. |
|
3786 |
Not all operatingSystems offer this functionality |
|
3787 |
(returns nil, if unsupported)" |
|
3788 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3789 |
handle isNil ifTrue:[ |
1885 | 3790 |
^ self errorNotOpen |
1335 | 3791 |
]. |
1885 | 3792 |
%{ |
3793 |
#if defined(SO_RCVBUF) && defined(SOL_SOCKET) |
|
3794 |
{ |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3795 |
OBJ fp = __INST(handle); |
1885 | 3796 |
SOCKET sock; |
3797 |
int opt; |
|
3798 |
unsigned int size; |
|
3799 |
||
3800 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
1568
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3801 |
if (getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&opt, &size) >= 0) { |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3802 |
RETURN( __MKSMALLINT(opt) ); |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3803 |
} |
1335 | 3804 |
} |
3805 |
#endif |
|
3806 |
%}. |
|
3807 |
^ nil |
|
3808 |
! |
|
3809 |
||
1885 | 3810 |
receiveBufferSize:size |
3811 |
"set the receive buffer size - for special applications only. |
|
3812 |
Not all operatingSystems offer this functionality |
|
3813 |
(returns false, if unsupported)" |
|
3814 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3815 |
handle isNil ifTrue:[ |
1885 | 3816 |
^ self errorNotOpen |
3817 |
]. |
|
3818 |
%{ |
|
3819 |
#if defined(SO_RCVBUF) && defined(SOL_SOCKET) |
|
3820 |
if (__isSmallInteger(size)) { |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3821 |
OBJ fp = __INST(handle); |
1885 | 3822 |
SOCKET sock; |
3823 |
int opt; |
|
3824 |
||
3825 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
1343 | 3826 |
opt = __intVal(size); |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3827 |
if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&opt, sizeof(int)) >= 0 ) { |
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3828 |
RETURN(true); |
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3829 |
} |
1335 | 3830 |
} |
3831 |
#endif |
|
3832 |
%}. |
|
3833 |
^ false |
|
3834 |
! |
|
3835 |
||
1885 | 3836 |
receiveTimeout |
3837 |
"get the receive timeout in millis - for special applications only. |
|
3838 |
Not all operatingSystems offer this functionality |
|
3839 |
(returns nil, if unsupported)" |
|
3840 |
||
3841 |
|millis| |
|
3842 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3843 |
handle isNil ifTrue:[ |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3844 |
^ self errorNotOpen |
1885 | 3845 |
]. |
3846 |
%{ |
|
3847 |
#if defined(SO_RCVTIMEO) && defined(SOL_SOCKET) |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3848 |
OBJ fp = __INST(handle); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3849 |
SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3850 |
int len; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3851 |
int __millis; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3852 |
struct timeval tv = {0, 0}; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3853 |
|
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3854 |
len = sizeof(struct timeval); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3855 |
if (getsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, &len) == 0) { |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3856 |
__millis = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3857 |
millis = __mkSmallInteger(__millis); |
1885 | 3858 |
# if 0 |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3859 |
console_fprintf(stderr, "getsockopt -> s:%d us:%d -> millis:%d\n", tv.tv_sec, tv.tv_usec, __millis); |
1885 | 3860 |
# endif |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3861 |
} else { |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3862 |
console_fprintf(stderr, "Socket [warning]: getsockopt %d failed; errno=%d\n", sock, errno); |
1568
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3863 |
} |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3864 |
#endif |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3865 |
%}. |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3866 |
millis notNil ifTrue:[^ millis / 1000 ]. |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3867 |
^ nil |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3868 |
! |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3869 |
|
1885 | 3870 |
receiveTimeout:seconds |
3871 |
"set the receive timeout - for special applications only. |
|
3872 |
Not all operatingSystems offer this functionality |
|
3873 |
(returns false, if unsupported)" |
|
3874 |
||
3875 |
|millis| |
|
3876 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3877 |
handle isNil ifTrue:[ |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3878 |
^ self errorNotOpen |
1885 | 3879 |
]. |
3880 |
millis := (seconds * 1000) rounded. |
|
3881 |
%{ |
|
3882 |
#if defined(SO_RCVTIMEO) && defined(SOL_SOCKET) |
|
3883 |
if (__isSmallInteger(millis)) { |
|
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3884 |
OBJ fp = __INST(handle); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3885 |
SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3886 |
int __millis = __intVal(millis); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3887 |
struct timeval tv = {0, 0}; |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3888 |
|
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3889 |
tv.tv_sec = __millis / 1000; |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3890 |
tv.tv_usec = (__millis % 1000) * 1000; |
1885 | 3891 |
# if 0 |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3892 |
console_fprintf(stderr, "setsockopt -> millis:%d -> s:%d us:%d \n", __millis, tv.tv_sec, tv.tv_usec); |
1885 | 3893 |
# endif |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3894 |
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(struct timeval)) == 0) { |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3895 |
RETURN(true); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3896 |
} |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3897 |
console_fprintf(stderr, "Socket [warning]: setsockopt %d failed; errno=%d\n", sock, errno); |
28 | 3898 |
} |
3899 |
#endif |
|
71 | 3900 |
%}. |
28 | 3901 |
^ false |
81 | 3902 |
! |
3903 |
||
1885 | 3904 |
sendBufferSize |
3905 |
"get the send buffer size - for special applications only. |
|
3906 |
Not all operatingSystems offer this functionality |
|
3907 |
(returns nil, if unsupported)" |
|
3908 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3909 |
handle isNil ifTrue:[ |
1885 | 3910 |
^ self errorNotOpen |
3911 |
]. |
|
3912 |
%{ |
|
3913 |
#if defined(SO_SNDBUF) && defined(SOL_SOCKET) |
|
3914 |
{ |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3915 |
OBJ fp = __INST(handle); |
1885 | 3916 |
SOCKET sock; |
3917 |
int opt; |
|
3918 |
unsigned int size; |
|
3919 |
||
3920 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
1568
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3921 |
if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&opt, &size) >= 0) { |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3922 |
RETURN( __MKSMALLINT(opt) ); |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3923 |
} |
1335 | 3924 |
} |
3925 |
#endif |
|
3926 |
%}. |
|
3927 |
^ nil |
|
3928 |
! |
|
3929 |
||
1885 | 3930 |
sendBufferSize:size |
3931 |
"set the send buffer size - for special applications only. |
|
3932 |
Not all operatingSystems offer this functionality |
|
3933 |
(returns false, if unsupported)" |
|
3934 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3935 |
handle isNil ifTrue:[ |
1885 | 3936 |
^ self errorNotOpen |
3937 |
]. |
|
3938 |
%{ |
|
3939 |
#if defined(SO_SNDBUF) && defined(SOL_SOCKET) |
|
3940 |
if (__isSmallInteger(size)) { |
|
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3941 |
OBJ fp = __INST(handle); |
1885 | 3942 |
SOCKET sock; |
3943 |
int opt; |
|
3944 |
||
3945 |
sock = SOCKET_FROM_FILE_OBJECT(fp); |
|
1343 | 3946 |
opt = __intVal(size); |
1529
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3947 |
if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&opt, sizeof(int)) >= 0) { |
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3948 |
RETURN(true); |
911273d090c1
Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents:
1505
diff
changeset
|
3949 |
} |
1335 | 3950 |
} |
3951 |
#endif |
|
3952 |
%}. |
|
3953 |
^ false |
|
3954 |
! |
|
3955 |
||
1885 | 3956 |
sendTimeout |
3957 |
"get the send timeout in millis - for special applications only. |
|
3958 |
Not all operatingSystems offer this functionality |
|
3959 |
(returns nil, if unsupported)" |
|
3960 |
||
3961 |
|millis| |
|
3962 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3963 |
handle isNil ifTrue:[ |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3964 |
^ self errorNotOpen |
1885 | 3965 |
]. |
3966 |
%{ |
|
3967 |
#if defined(SO_SNDTIMEO) && defined(SOL_SOCKET) |
|
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3968 |
OBJ fp = __INST(handle); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3969 |
SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3970 |
int len; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3971 |
int __millis; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3972 |
struct timeval tv = {0, 0}; |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3973 |
|
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3974 |
len = sizeof(struct timeval); |
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3975 |
if (getsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (void *)&tv, &len) == 0) { |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3976 |
__millis = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3977 |
millis = __mkSmallInteger(__millis); |
1885 | 3978 |
# if 0 |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3979 |
console_fprintf(stderr, "getsockopt -> s:%d us:%d -> millis:%d\n", tv.tv_sec, tv.tv_usec, __millis); |
1885 | 3980 |
# endif |
3246
c75a006f9a7a
Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents:
3224
diff
changeset
|
3981 |
} else { |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3982 |
console_fprintf(stderr, "Socket [warning]: getsockopt %d failed; errno=%d\n", sock, errno); |
1568
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3983 |
} |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3984 |
#endif |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3985 |
%}. |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3986 |
millis notNil ifTrue:[^ millis / 1000 ]. |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3987 |
^ nil |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3988 |
! |
3bca500e0c7a
send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents:
1561
diff
changeset
|
3989 |
|
1885 | 3990 |
sendTimeout:seconds |
3991 |
"set the send timeout - for special applications only. |
|
3992 |
Not all operatingSystems offer this functionality |
|
3993 |
(returns false, if unsupported)" |
|
3994 |
||
3995 |
|millis| |
|
3996 |
||
2321
6f4cc7b644db
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
2308
diff
changeset
|
3997 |
handle isNil ifTrue:[ |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
3998 |
^ self errorNotOpen |
1885 | 3999 |
]. |
4000 |
millis := (seconds * 1000) rounded. |
|
4001 |
%{ |
|
4002 |
#if defined(SO_SNDTIMEO) && defined(SOL_SOCKET) |
|
4003 |
if (__isSmallInteger(millis)) { |
|
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4004 |
OBJ fp = __INST(handle); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4005 |
SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4006 |
int __millis = __intVal(millis); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4007 |
struct timeval tv = {0, 0}; |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4008 |
|
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4009 |
tv.tv_sec = __millis / 1000; |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4010 |
tv.tv_usec = (__millis % 1000) * 1000; |
1885 | 4011 |
# if 0 |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4012 |
console_fprintf(stderr, "setsockopt -> millis:%d -> s:%d us:%d \n", __millis, tv.tv_sec, tv.tv_usec); |
1885 | 4013 |
# endif |
3467
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4014 |
if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (void *)&tv, sizeof(struct timeval)) == 0) { |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4015 |
RETURN(true); |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4016 |
} |
24d35ef42e17
socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents:
3462
diff
changeset
|
4017 |
console_fprintf(stderr, "Socket [warning]: setsockopt %d failed; errno=%d\n", sock, errno); |
126 | 4018 |
} |
4019 |
#endif |
|
4020 |
%}. |
|
4021 |
^ false |
|
1340 | 4022 |
! |
4023 |
||
1885 | 4024 |
setTCPCork:aBoolean |
4025 |
"enable/disable TCP_CORK (do-not-send-partial-frames) |
|
4026 |
For special applications only. |
|
4027 |
Not all OperatingSystems offer this functionality |
|
4028 |
(returns false, if unsupported)" |
|
4029 |
||
2551 | 4030 |
^ self setSocketOption:#'TCP_CORK' argument:aBoolean argument:nil. |
1885 | 4031 |
! |
4032 |
||
4033 |
setTCPNoDelay:aBoolean |
|
4034 |
"enable/disable TCP_NODELAY (i.e. disable/enable the Nagle algorithm) |
|
4035 |
For special applications only. |
|
4036 |
Not all OperatingSystems offer this functionality |
|
4037 |
(returns false, if unsupported)" |
|
4038 |
||
2551 | 4039 |
^ self setSocketOption:#'TCP_NODELAY' argument:aBoolean argument:nil. |
126 | 4040 |
! ! |
82 | 4041 |
|
4203 | 4042 |
|
1935 | 4043 |
!Socket methodsFor:'waiting'! |
4044 |
||
4045 |
waitForNewConnectionOrDataOnAny:otherConnections timeout:timeoutSeconds |
|
4046 |
"suspend the current process, until either a new connection comes |
|
4047 |
in at the receiver, or data arrives on any of the otherConnections. |
|
4048 |
For a new connection, an accept is performed and the new socket is returned. |
|
4049 |
For an old connection, that socket is returned. |
|
4050 |
In any case, the caller gets a socket to operate on as return value, |
|
4032 | 4051 |
or nil, if a timeout occurred. |
1935 | 4052 |
This method implements the inner wait-primitive of a multi-connection |
4053 |
server application." |
|
4054 |
||
4055 |
||
4056 |
|wasBlocked sema| |
|
4057 |
||
4058 |
"first, a quick check if data is already available" |
|
4059 |
self canReadWithoutBlocking ifTrue:[ |
|
4497 | 4060 |
^ self accept. |
1935 | 4061 |
]. |
4062 |
otherConnections do:[:aConnection | |
|
4497 | 4063 |
aConnection canReadWithoutBlocking ifTrue:[ |
4064 |
^ aConnection |
|
4065 |
] |
|
1935 | 4066 |
]. |
4067 |
||
3281 | 4068 |
"check again - prevent incoming interrupts from disturbing our setup" |
4069 |
wasBlocked := OperatingSystem blockInterrupts. |
|
1935 | 4070 |
[ |
4497 | 4071 |
sema := Semaphore name:'multiReadWait'. |
4072 |
otherConnections do:[:aConnection | |
|
4073 |
Processor signal:sema onInput:(aConnection fileDescriptor). |
|
4074 |
]. |
|
4075 |
Processor signal:sema onInput:(self fileDescriptor). |
|
4076 |
timeoutSeconds notNil ifTrue:[ |
|
4077 |
Processor signal:sema afterSeconds:timeoutSeconds |
|
4078 |
]. |
|
4079 |
Processor activeProcess state:#ioWait. |
|
4080 |
sema wait. |
|
3281 | 4081 |
] ifCurtailed:[ |
4497 | 4082 |
sema notNil ifTrue:[Processor disableSemaphore:sema]. |
4083 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
1935 | 4084 |
]. |
3281 | 4085 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
1935 | 4086 |
|
4087 |
"see who it was ..." |
|
4088 |
self canReadWithoutBlocking ifTrue:[ |
|
4497 | 4089 |
^ self accept. |
1935 | 4090 |
]. |
4091 |
otherConnections do:[:aConnection | |
|
4497 | 4092 |
aConnection canReadWithoutBlocking ifTrue:[ |
4093 |
^ aConnection |
|
4094 |
] |
|
1935 | 4095 |
]. |
4096 |
||
4097 |
"none - a timeout" |
|
4098 |
^ nil |
|
4497 | 4099 |
|
4100 |
"Modified: / 09-08-2017 / 11:59:50 / cg" |
|
1935 | 4101 |
! |
4102 |
||
3550 | 4103 |
waitForNewConnectionWithTimeout:timeoutSecondsOrNil |
1935 | 4104 |
"suspend the current process, until a new connection comes |
4105 |
in at the receiver or a timeout occurs. |
|
4106 |
For a new connection, an accept is performed and the new socket is returned. |
|
4032 | 4107 |
Returns nil, if a timeout occurred. |
1935 | 4108 |
This method implements the inner wait-primitive of a single-connection |
4109 |
server application." |
|
4110 |
||
3633 | 4111 |
|newSock| |
4112 |
||
3550 | 4113 |
(self readWaitWithTimeout:timeoutSecondsOrNil) ifTrue:[ |
4414 | 4114 |
"a timeout occurred - no connection within timeout" |
4115 |
^ nil |
|
1935 | 4116 |
]. |
4180 | 4117 |
self isOpen ifFalse:[ |
4414 | 4118 |
"socket has been closed while waiting" |
4119 |
^ nil. |
|
4180 | 4120 |
]. |
3633 | 4121 |
newSock := self class new. |
4122 |
(newSock primAcceptOn:self blocking:false) ifFalse:[ |
|
4414 | 4123 |
"should raise an error here" |
4124 |
^ nil |
|
3633 | 4125 |
]. |
4126 |
^ newSock |
|
1935 | 4127 |
! ! |
4128 |
||
464 | 4129 |
!Socket class methodsFor:'documentation'! |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
4130 |
|
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
4131 |
version |
3590 | 4132 |
^ '$Header$' |
2292 | 4133 |
! |
4134 |
||
4135 |
version_CVS |
|
3590 | 4136 |
^ '$Header$' |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
4137 |
! ! |
4497 | 4138 |