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