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