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