Socket.st
author Claus Gittinger <cg@exept.de>
Mon, 13 Feb 2017 20:49:42 +0100
changeset 4320 5b7848099dea
parent 4293 82b9928ddcae
child 4341 2b08cb3ff1a3
permissions -rw-r--r--
#DOCUMENTATION by cg class: Socket comment/format in: #hostWithAppletalkAddress: #hostWithIpAddress: #hostWithIpV6Address:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     1
"
4
1f66800df351 *** empty log message ***
claus
parents: 3
diff changeset
     2
 COPYRIGHT (c) 1992 by Claus Gittinger
42
506596f9a1a8 *** empty log message ***
claus
parents: 38
diff changeset
     3
	      All Rights Reserved
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     4
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
1cf8d1747859 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    11
"
888
a868b0e54a2b ipAddress stuff
Claus Gittinger <cg@exept.de>
parents: 887
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
a868b0e54a2b ipAddress stuff
Claus Gittinger <cg@exept.de>
parents: 887
diff changeset
    13
3462
cfdc47761d0f class: Socket
Claus Gittinger <cg@exept.de>
parents: 3281
diff changeset
    14
"{ NameSpace: Smalltalk }"
cfdc47761d0f class: Socket
Claus Gittinger <cg@exept.de>
parents: 3281
diff changeset
    15
63
7dd3d5b7877e *** empty log message ***
claus
parents: 58
diff changeset
    16
NonPositionableExternalStream subclass:#Socket
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
    17
	instanceVariableNames:'domain socketType protocol port peer peerName listening'
206
77166a6b3ee6 For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents: 203
diff changeset
    18
	classVariableNames:''
77166a6b3ee6 For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents: 203
diff changeset
    19
	poolDictionaries:''
77166a6b3ee6 For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents: 203
diff changeset
    20
	category:'Streams-External'
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    21
!
1cf8d1747859 Initial revision
claus
parents:
diff changeset
    22
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    23
!Socket primitiveDefinitions!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    24
%{
1009
e0839f923a3c double underlines;
Claus Gittinger <cg@exept.de>
parents: 1007
diff changeset
    25
/* #define DGRAM_DEBUG /* */
e0839f923a3c double underlines;
Claus Gittinger <cg@exept.de>
parents: 1007
diff changeset
    26
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
    27
#ifdef __MINGW__
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
    28
# include <stdio.h>
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
    29
#endif
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
    30
#include "stxOSDefs.h"
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    31
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
    32
#ifdef __win32__
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
    33
//#define DEBUG 1
2818
94d67df5c00d compilable with tcc
Claus Gittinger <cg@exept.de>
parents: 2773
diff changeset
    34
/* this is to catch uses of those - there should be none */
1503
4d97e4191d04 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1499
diff changeset
    35
# undef __BEGIN_INTERRUPTABLE__
4d97e4191d04 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1499
diff changeset
    36
# undef __END_INTERRUPTABLE__
2818
94d67df5c00d compilable with tcc
Claus Gittinger <cg@exept.de>
parents: 2773
diff changeset
    37
# ifdef __TCC__
94d67df5c00d compilable with tcc
Claus Gittinger <cg@exept.de>
parents: 2773
diff changeset
    38
#  define __BEGIN_INTERRUPTABLE__ xxxx
94d67df5c00d compilable with tcc
Claus Gittinger <cg@exept.de>
parents: 2773
diff changeset
    39
#  define __END_INTERRUPTABLE__ yyyy
94d67df5c00d compilable with tcc
Claus Gittinger <cg@exept.de>
parents: 2773
diff changeset
    40
# else
94d67df5c00d compilable with tcc
Claus Gittinger <cg@exept.de>
parents: 2773
diff changeset
    41
#  define __BEGIN_INTERRUPTABLE__ ..
94d67df5c00d compilable with tcc
Claus Gittinger <cg@exept.de>
parents: 2773
diff changeset
    42
#  define __END_INTERRUPTABLE__ ..
94d67df5c00d compilable with tcc
Claus Gittinger <cg@exept.de>
parents: 2773
diff changeset
    43
# endif
1503
4d97e4191d04 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1499
diff changeset
    44
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
    45
# define WRAP_STDIO
2823
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
    46
// The default is linger until all data is sent in background
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
    47
// so the close on the socket is non-blocking
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
    48
// linger.onoff=off linger.time= *irrelevant*
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
    49
//# define SET_LINGER_WHEN_CREATING_SOCKET
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
    50
3742
a12020350fa0 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3741
diff changeset
    51
# ifdef __xxMINGW__
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
    52
extern HANDLE _get_osfhandle();
1845
df4d79bfba9d closesocket is still tricky
Claus Gittinger <cg@exept.de>
parents: 1844
diff changeset
    53
# endif
df4d79bfba9d closesocket is still tricky
Claus Gittinger <cg@exept.de>
parents: 1844
diff changeset
    54
3620
c648d063bccc #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3617
diff changeset
    55
# if defined(__BORLANDC__)
c648d063bccc #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3617
diff changeset
    56
// not defined in borland headers
c648d063bccc #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3617
diff changeset
    57
typedef unsigned int socklen_t;
c648d063bccc #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3617
diff changeset
    58
# endif
c648d063bccc #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3617
diff changeset
    59
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
    60
# if 0 && (defined( __BORLANDC__ ) || defined( __MINGW__ ))
2915
896fffb0f02e mingw64
Claus Gittinger <cg@exept.de>
parents: 2909
diff changeset
    61
#  define SOCKET_FROM_FD(fd)               (SOCKET)(_get_osfhandle(fd))
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
    62
# else
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
    63
#  define SOCKET_FROM_FD(fd)               (SOCKET)(fd)
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
    64
# endif
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
    65
#else /* not __win32__ */
3215
342d793d938c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3214
diff changeset
    66
# define SOCKET_FROM_FD(fd)   (fd)
342d793d938c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3214
diff changeset
    67
# define closesocket(sock)    close(sock)
342d793d938c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3214
diff changeset
    68
# define SOCKET               int
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
    69
#endif /* ! __win32__ */
1503
4d97e4191d04 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1499
diff changeset
    70
1843
8ee90d243b49 closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
    71
#define SOCKET_FROM_FILE(f)             (SOCKET_FROM_FD(fileno(f)))
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
    72
#define SOCKET_FROM_FILE_OBJECT(f)      (__INST(handleType) == @symbol(socketHandle) ? SOCKET_FROM_FD(__FILEVal(f)) : SOCKET_FROM_FILE(__FILEVal(f)))
1539
c0e89dcb58b3 non blocking stuff done right here;
Claus Gittinger <cg@exept.de>
parents: 1538
diff changeset
    73
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
    74
#ifndef __MINGW__
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
    75
# include <stdio.h>
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
    76
#endif
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    77
#include <errno.h>
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    78
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
    79
#ifdef WANT__TCP_DOT_H
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
    80
# include <netinet/tcp.h>
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
    81
#endif
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
    82
1344
70d73917ac78 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1343
diff changeset
    83
#ifdef WANT__NETDB_DOT_H
70d73917ac78 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1343
diff changeset
    84
# include <netdb.h>
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
    85
#endif
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
    86
739
eb3878300c3b removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents: 726
diff changeset
    87
#if defined(TRY_AGAIN) || defined(HOST_NOT_FOUND)
eb3878300c3b removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents: 726
diff changeset
    88
# define USE_H_ERRNO
eb3878300c3b removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents: 726
diff changeset
    89
#endif
eb3878300c3b removed WIN32 ifdef's - win32 uses Socket_win32.st anyway.
Claus Gittinger <cg@exept.de>
parents: 726
diff changeset
    90
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    91
/*
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    92
 * on some systems errno is a macro ... check for it here
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    93
 */
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    94
#ifndef errno
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    95
 extern errno;
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    96
#endif
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    97
648
58e11a70ddaf gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents: 617
diff changeset
    98
#ifdef USE_H_ERRNO
58e11a70ddaf gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents: 617
diff changeset
    99
# ifndef h_errno
58e11a70ddaf gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents: 617
diff changeset
   100
 extern h_errno;
58e11a70ddaf gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents: 617
diff changeset
   101
# endif
58e11a70ddaf gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents: 617
diff changeset
   102
#endif
58e11a70ddaf gethostbyname behaves strange, if a name-server lookup is involved.
Claus Gittinger <cg@exept.de>
parents: 617
diff changeset
   103
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   104
#ifdef DEBUG
1839
c10cd58ec334 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1781
diff changeset
   105
# define DBGPRINTF(x)    { if (__debugging__) console_printf x; }
c10cd58ec334 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1781
diff changeset
   106
# define DBGFPRINTF(x)   { if (__debugging__) console_fprintf x; }
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   107
#else
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   108
# define DBGPRINTF(x)    /* as nothing */
821
40b0b69bf4b5 more debugs; fixed at-addresses
Claus Gittinger <cg@exept.de>
parents: 820
diff changeset
   109
# define DBGFPRINTF(x)   /* as nothing */
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   110
#endif
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   111
1531
ec98158988ca compilable under unix
Claus Gittinger <cg@exept.de>
parents: 1529
diff changeset
   112
#ifndef TRUE
1535
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
   113
# define TRUE   1
1531
ec98158988ca compilable under unix
Claus Gittinger <cg@exept.de>
parents: 1529
diff changeset
   114
#endif
ec98158988ca compilable under unix
Claus Gittinger <cg@exept.de>
parents: 1529
diff changeset
   115
#ifndef FALSE
1535
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
   116
# define FALSE  0
1531
ec98158988ca compilable under unix
Claus Gittinger <cg@exept.de>
parents: 1529
diff changeset
   117
#endif
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
   118
#ifndef __win32__
1531
ec98158988ca compilable under unix
Claus Gittinger <cg@exept.de>
parents: 1529
diff changeset
   119
typedef int BOOL;
ec98158988ca compilable under unix
Claus Gittinger <cg@exept.de>
parents: 1529
diff changeset
   120
#endif
ec98158988ca compilable under unix
Claus Gittinger <cg@exept.de>
parents: 1529
diff changeset
   121
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
   122
#ifdef __win32__
1843
8ee90d243b49 closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
   123
# undef stdout
8ee90d243b49 closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
   124
# undef stderr
8ee90d243b49 closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
   125
# define stdout __win32_stdout()
8ee90d243b49 closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
   126
# define stderr __win32_stderr()
8ee90d243b49 closeSocket after fclose: no longer (Q: is this the right thing)
Claus Gittinger <cg@exept.de>
parents: 1842
diff changeset
   127
#endif
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   128
%}
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   129
! !
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   130
511
c071e228cb24 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 510
diff changeset
   131
!Socket primitiveVariables!
c071e228cb24 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 510
diff changeset
   132
%{
1844
decc9d62520d oops - debug
Claus Gittinger <cg@exept.de>
parents: 1843
diff changeset
   133
static int __debugging__ = 0;
511
c071e228cb24 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 510
diff changeset
   134
%}
c071e228cb24 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 510
diff changeset
   135
! !
c071e228cb24 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 510
diff changeset
   136
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   137
!Socket primitiveFunctions!
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   138
%{
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   139
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
   140
static int
3616
Stefan Vogel <sv@exept.de>
parents: 3615
diff changeset
   141
setupBufferParameters(OBJ aDataBuffer, OBJ startIndex, char **p_extPtr, INT *p_offs, size_t *p_objSize)
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   142
{
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   143
	char *extPtr = 0;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   144
	int sIdx = 0, objSize = 0, offs = 0;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   145
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   146
	sIdx = 0;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   147
	if (__isSmallInteger(startIndex)) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   148
	    sIdx = __intVal(startIndex) - 1;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   149
	}
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   150
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   151
	if (__isExternalBytesLike(aDataBuffer)) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   152
	    OBJ sz;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   153
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   154
	    extPtr = (char *)(__externalBytesAddress(aDataBuffer));
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   155
	    sz = __externalBytesSize(aDataBuffer);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   156
	    if (__isSmallInteger(sz)) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   157
		objSize = __intVal(sz);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   158
	    } else {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   159
		objSize = 0; /* unknown */
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   160
	    }
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   161
	    offs = sIdx;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   162
	} else {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   163
	    OBJ oClass;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   164
	    int nInstVars, nInstBytes;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   165
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   166
	    extPtr = (char *)0;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   167
	    oClass = __Class(aDataBuffer);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   168
	    switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   169
		case BYTEARRAY:
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   170
		    offs = sIdx;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   171
		    break;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   172
		case WORDARRAY:
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   173
		case SWORDARRAY:
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   174
		    offs = sIdx * 2;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   175
		    break;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   176
		case LONGARRAY:
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   177
		case SLONGARRAY:
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   178
		    offs = sIdx * 4;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   179
		    break;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   180
		case LONGLONGARRAY:
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   181
		case SLONGLONGARRAY:
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   182
		    offs = sIdx * 8;
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   183
# ifdef __NEED_LONGLONG_ALIGN
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   184
		    offs += 4;
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   185
# endif
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   186
		    break;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   187
		case FLOATARRAY:
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   188
		    offs = sIdx * sizeof(float);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   189
		    break;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   190
		case DOUBLEARRAY:
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   191
		    offs = sIdx * sizeof(double);
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   192
# ifdef __NEED_DOUBLE_ALIGN
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   193
		    offs += 4;
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   194
# endif
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   195
		    break;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   196
		default:
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   197
		    *p_objSize = -1;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   198
		    return 0;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   199
	    }
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   200
	    nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   201
	    nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   202
	    offs = offs + nInstBytes;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   203
	    objSize = __Size(aDataBuffer) - offs;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   204
	}
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   205
	*p_extPtr = extPtr;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   206
	*p_objSize = objSize;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   207
	*p_offs = offs;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
   208
	return 1;
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   209
}
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   210
%}
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   211
! !
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
   212
464
600b101a7035 alen in accept
Claus Gittinger <cg@exept.de>
parents: 463
diff changeset
   213
!Socket class methodsFor:'documentation'!
25
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   214
31
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   215
copyright
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   216
"
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   217
 COPYRIGHT (c) 1992 by Claus Gittinger
42
506596f9a1a8 *** empty log message ***
claus
parents: 38
diff changeset
   218
	      All Rights Reserved
31
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   219
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   220
 This software is furnished under a license and may be used
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   221
 only in accordance with the terms of that license and with the
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   222
 inclusion of the above copyright notice.   This software may not
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   223
 be provided or otherwise made available to, or used by, any
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   224
 other person.  No title to or ownership of the software is
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   225
 hereby transferred.
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   226
"
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   227
!
e223f3cf2995 *** empty log message ***
claus
parents: 28
diff changeset
   228
25
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   229
documentation
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   230
"
524
8287da54eccf more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents: 513
diff changeset
   231
    This class provides access to sockets for interprocess communication.
25
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   232
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
   233
    Currently, only IP and UNIX domain sockets are really fully
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
   234
    tested and supported.
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
   235
    Code for appletalk is included, but was never tested ...
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
   236
    More may be added in the future.
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
   237
    (the code is prepared for things like SNA or decNet;
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
   238
     however, right now, this code is empty and needs a little work.
3989
d99b5df54e27 #DOCUMENTATION by mawalch
mawalch
parents: 3988
diff changeset
   239
     Implementing those is pretty straightforward, once the address
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
   240
     data structures are known.)
524
8287da54eccf more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents: 513
diff changeset
   241
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
   242
    Due to historic reasons (I started this class, before I got hold of some
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
   243
    code using ST-80 Sockets i.e. RemoteInvocation), there is some old interface
3481
b28077459fde class: Socket
Claus Gittinger <cg@exept.de>
parents: 3467
diff changeset
   244
    which is still supported.
b28077459fde class: Socket
Claus Gittinger <cg@exept.de>
parents: 3467
diff changeset
   245
    This may vanish; use the #family:type: or #newTCPxxx and #newUDPxxx interfaces,
71
claus
parents: 63
diff changeset
   246
    together with the bind/listen and accept calls,
claus
parents: 63
diff changeset
   247
    which are meant to be compatible to ST-80's UnixSocketAccessor interface.
claus
parents: 63
diff changeset
   248
3481
b28077459fde class: Socket
Claus Gittinger <cg@exept.de>
parents: 3467
diff changeset
   249
    TODO: cleanup historic leftovers,
3989
d99b5df54e27 #DOCUMENTATION by mawalch
mawalch
parents: 3988
diff changeset
   250
          change to raise more signals on errors.
259
6d36f3ac42a2 documentation
Claus Gittinger <cg@exept.de>
parents: 220
diff changeset
   251
6d36f3ac42a2 documentation
Claus Gittinger <cg@exept.de>
parents: 220
diff changeset
   252
    [author:]
3989
d99b5df54e27 #DOCUMENTATION by mawalch
mawalch
parents: 3988
diff changeset
   253
        Claus Gittinger
48
18b9353c9d07 *** empty log message ***
claus
parents: 44
diff changeset
   254
"
18b9353c9d07 *** empty log message ***
claus
parents: 44
diff changeset
   255
!
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
   256
48
18b9353c9d07 *** empty log message ***
claus
parents: 44
diff changeset
   257
examples
18b9353c9d07 *** empty log message ***
claus
parents: 44
diff changeset
   258
"
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
   259
    example (get help info from an nntp server):
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   260
                                                                        [exBegin]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   261
        |sock|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   262
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   263
        sock := Socket newTCPclientToHost:'smtp.exept.de' port:'smtp'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   264
        sock isNil ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   265
            self warn:'no smtp daemon is running'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   266
            ^ self
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   267
        ].
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   268
        Transcript showCR:sock nextLine.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   269
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   270
        sock nextPutAll:'HELO STX socket test'; cr.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   271
        Transcript showCR:sock nextLine.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   272
        sock close
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   273
                                                                        [exEnd]
209
2152fceba563 finger example
Claus Gittinger <cg@exept.de>
parents: 208
diff changeset
   274
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
   275
209
2152fceba563 finger example
Claus Gittinger <cg@exept.de>
parents: 208
diff changeset
   276
    example (connect to finger daemon, get users entry):
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   277
                                                                        [exBegin]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   278
        |sock entry|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   279
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   280
        sock := Socket newTCPclientToHost:'localhost' port:'finger'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   281
        sock isNil ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   282
            self warn:'no finger daemon is running'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   283
            ^ self
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   284
        ].
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   285
        sock useCRLF:true.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   286
        sock buffered:false.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   287
        sock isNil ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   288
            Transcript showCR:'cannot connect to local finger daemon'
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   289
        ] ifFalse:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   290
            sock nextPutAll:(OperatingSystem getLoginName).
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   291
            sock cr.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   292
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   293
            entry := sock nextLine.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   294
            Transcript showCR:entry.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   295
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   296
            sock close
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   297
        ]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   298
                                                                        [exEnd]
99
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
   299
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
   300
    example (connect to an ftp server):
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   301
                                                                        [exBegin]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   302
        |sock|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   303
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   304
        sock := Socket newTCPclientToHost:'www.exept.de' port:'ftp'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   305
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   306
        sock buffered:false.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   307
        Transcript showCR:sock nextLine.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   308
        sock nextPutAll:('USER ' , 'anonymous'); cr.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   309
        Transcript showCR:sock nextLine.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   310
        sock nextPutAll:('PASS ' , 'fooBar'); cr.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   311
        Transcript showCR:sock nextLine.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   312
        sock nextPutAll:'HELP'; cr.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   313
        [
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   314
            |line|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   315
            line := sock nextLine.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   316
            Transcript showCR:line.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   317
            (line at:4) = $-
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   318
        ] whileTrue.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   319
        sock close.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   320
4192
5b2c3f2c8d13 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4180
diff changeset
   321
        'don't know enough of the ftp protocol to continue here ...'
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   322
                                                                        [exEnd]
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
   323
99
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
   324
93
Claus Gittinger <cg@exept.de>
parents: 92
diff changeset
   325
    example (connect to an snmp server [UDP]):
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   326
    Note: this is not a real connection, only the destination address is
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   327
          being fixed.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   328
                                                                        [exBegin]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   329
        |sock port|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   330
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   331
        sock := Socket newUDP.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   332
        port := Socket portOfService:'snmp'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   333
        sock connectTo:'localhost' port:port.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   334
        sock buffered:false.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   335
        Transcript showCR:'got it'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   336
        sock close.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   337
                                                                        [exEnd]
42
506596f9a1a8 *** empty log message ***
claus
parents: 38
diff changeset
   338
99
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
   339
42
506596f9a1a8 *** empty log message ***
claus
parents: 38
diff changeset
   340
    example (await connection from a client and read some data):
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   341
                                                                        [exBegin]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   342
        |connectSock sock|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   343
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   344
        connectSock := Socket newTCPserverAtPort:9998.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   345
        connectSock isNil ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   346
            Transcript showCR:'socket setup failed.'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   347
        ] ifFalse:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   348
            Transcript showCR:'listen ..'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   349
            (connectSock listenFor:5) ifFalse:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   350
                Transcript showCR:'listen failed.'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   351
            ] ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   352
                Transcript showCR:'wait'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   353
                connectSock readWait.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   354
                Transcript showCR:'accept'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   355
                sock := connectSock accept.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   356
                sock isNil ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   357
                    Transcript showCR:'accept failed.'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   358
                ] ifFalse:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   359
                    sock buffered:false.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   360
                    Transcript showCR:'server: got it'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   361
                    'can now do transfer via sock'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   362
                    Transcript showCR:'read'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   363
                    Transcript showCR:('got: ' , sock nextLine).
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   364
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   365
                    Transcript showCR:'close'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   366
                    sock close
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   367
                ].
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   368
                connectSock close.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   369
            ]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   370
        ]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   371
                                                                        [exEnd]
42
506596f9a1a8 *** empty log message ***
claus
parents: 38
diff changeset
   372
99
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
   373
42
506596f9a1a8 *** empty log message ***
claus
parents: 38
diff changeset
   374
    example (connect to above server and send some data):
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   375
                                                                        [exBegin]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   376
        |sock|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   377
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   378
        sock := Socket newTCPclientToHost:'localhost' port:9998.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   379
        sock isNil ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   380
            Transcript showCR:'nope'
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   381
        ] ifFalse:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   382
            sock buffered:false.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   383
            Transcript showCR:'client: got it'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   384
            'can now do transfer via sock'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   385
            Transcript showCR:'sending <hello>'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   386
            sock nextPutLine:'hello'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   387
            sock close
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   388
        ]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   389
                                                                        [exEnd]
117
03cefad7e40b UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents: 116
diff changeset
   390
03cefad7e40b UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents: 116
diff changeset
   391
    example: UNIX domain socket (await connection from a client and read some data):
1009
e0839f923a3c double underlines;
Claus Gittinger <cg@exept.de>
parents: 1007
diff changeset
   392
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   393
        |connectSock sock|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   394
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   395
        '/tmp/ud_socket' asFilename remove.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   396
        connectSock := Socket newUNIXserverAt:'/tmp/ud_socket'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   397
        connectSock isNil ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   398
            Transcript showCR:'socket setup failed.'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   399
        ] ifFalse:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   400
            Transcript showCR:'listen ..'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   401
            (connectSock listenFor:5) ifFalse:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   402
                Transcript showCR:'listen failed.'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   403
            ] ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   404
                Transcript showCR:'wait'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   405
                connectSock buffered:false.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   406
                connectSock readWait.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   407
                Transcript showCR:'accept'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   408
                sock := connectSock accept.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   409
                sock isNil ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   410
                    Transcript showCR:'accept failed.'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   411
                ] ifFalse:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   412
                    sock buffered:false.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   413
                    Transcript showCR:'server: got it'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   414
                    'can now do transfer via sock'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   415
                    Transcript showCR:'read'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   416
                    Transcript showCR:('got: ' , sock nextLine).
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   417
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   418
                    Transcript showCR:'close'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   419
                    sock close
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   420
                ].
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   421
                connectSock close.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   422
            ]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   423
        ]
117
03cefad7e40b UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents: 116
diff changeset
   424
03cefad7e40b UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents: 116
diff changeset
   425
118
142d5c273454 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 117
diff changeset
   426
    example (connect to above server and send some data;
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   427
             Notice, this fails, if above server code is executed in the same ST/X image
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   428
                     (at least on LINUX), since the OS does not correctly handle
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   429
                     a connect from within an interrupted accept system call
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   430
                     On SGI's SVR4, this works ok
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   431
                                                                        [exBegin]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   432
        |sock|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   433
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   434
        sock := Socket newUNIXclientTo:'/tmp/ud_socket'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   435
        sock isNil ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   436
            Transcript showCR:'nope'
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   437
        ] ifFalse:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   438
            sock buffered:false.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   439
            Transcript showCR:'client: got it'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   440
            'can now do transfer via sock'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   441
            Transcript showCR:'sending <hello>'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   442
            sock nextPutLine:'hello'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   443
            sock close
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   444
        ]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   445
                                                                        [exEnd]
1007
5267b5738779 UDP examples
Claus Gittinger <cg@exept.de>
parents: 1004
diff changeset
   446
5267b5738779 UDP examples
Claus Gittinger <cg@exept.de>
parents: 1004
diff changeset
   447
5267b5738779 UDP examples
Claus Gittinger <cg@exept.de>
parents: 1004
diff changeset
   448
    example (UDP await packet from a client and read some data):
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   449
                                                                        [exBegin]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   450
        |udpSock sock addr n dataBuffer|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   451
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   452
        udpSock := Socket newUDPserverAtPort:9999.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   453
        udpSock isNil ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   454
            Transcript showCR:'socket setup failed.'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   455
        ] ifFalse:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   456
            Transcript showCR:'wait'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   457
            udpSock readWait.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   458
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   459
            addr := IPSocketAddress new.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   460
            dataBuffer := ByteArray new:1000.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   461
            n := udpSock receiveFrom:addr buffer:dataBuffer start:1 for:dataBuffer size.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   462
            n > 0 ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   463
                Transcript showCR:('got: ' , n printString , 'bytes  from ' , addr printString).
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   464
                Transcript showCR:('data: ' , (dataBuffer copyTo:n) printString).
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   465
            ] ifFalse:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   466
                Transcript showCR:'read failed'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   467
            ].
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   468
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   469
            Transcript showCR:'close'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   470
            udpSock close
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   471
        ]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   472
                                                                        [exEnd]
1007
5267b5738779 UDP examples
Claus Gittinger <cg@exept.de>
parents: 1004
diff changeset
   473
    example (connect to above UDP server and send some data;
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   474
                                                                        [exBegin]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   475
        |sock|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   476
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   477
        sock := Socket newUDP.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   478
        sock isNil ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   479
            Transcript showCR:'nope'
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   480
        ] ifFalse:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   481
            sock sendTo:(IPSocketAddress new hostName:'localhost' port:9999) buffer:'hello world'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   482
            sock close
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   483
        ]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   484
                                                                        [exEnd]
99
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
   485
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
   486
    example: pingWalk (try to ping hosts on the local network)
2824
b00dc482dd03 comment/format in: #examples
Stefan Vogel <sv@exept.de>
parents: 2823
diff changeset
   487
    Note: it dosen't use ICMP ping, but tries to reache the echo service,
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   488
          which is disabled on most OS.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   489
                                                                        [exBegin]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   490
        |myAddress list top hosts walkProcess port|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   491
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   492
        myAddress := OperatingSystem getNetworkAddresses
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   493
                        keysAndValuesSelect:[:eachIFName :eachAddress|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   494
                            eachAddress isLocal not
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   495
                            and:[eachIFName = 'wlan0']
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   496
                        ].
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   497
        myAddress := myAddress first hostAddress.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   498
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   499
        port := Socket portOfService:'echo'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   500
        port isNil ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   501
            self error:'dont know echo port'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   502
            ^ self
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   503
        ].
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   504
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   505
        top := StandardSystemView new.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   506
        top label:'PING net walk'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   507
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   508
        list := ScrollableView for:ListView in:top.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   509
        list origin:0.0@0.0 corner:1.0@1.0.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   510
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   511
        top openAndWait.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   512
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   513
        walkProcess := [
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   514
            |l low hi direction tryHostID dottedName hostName conn addr|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   515
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   516
            l := SortedCollection new.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   517
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   518
            ' only works with type C-net
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   519
              the code below could simply do 1 to:254 do:[:hostID }
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   520
              but, to probe likely hosts earlier, the probing is done
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   521
              ping-pong like around my ip-address (assuming, that other machines
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   522
              have numbers around my own)'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   523
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   524
            low := hi := (myAddress at:4).
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   525
            direction := 1.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   526
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   527
            [low > 0 or:[hi < 255]] whileTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   528
                direction > 0 ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   529
                    hi := hi + 1.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   530
                    tryHostID := hi.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   531
                    direction := -1.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   532
                ] ifFalse:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   533
                    low := low - 1.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   534
                    tryHostID := low.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   535
                    direction := 1.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   536
                ].
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   537
                (tryHostID between:1 and:254) ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   538
                    dottedName := (myAddress at:1) printString
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   539
                                  , '.' , (myAddress at:2) printString
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   540
                                  , '.' , (myAddress at:3) printString
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   541
                                  , '.' , tryHostID printString.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   542
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   543
                    top label:'PING net walk - trying ' , dottedName.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   544
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   545
                    top windowGroup withCursor:Cursor wait do:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   546
                        conn := Socket newTCPclientToHost:dottedName port:port withTimeout:1000.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   547
                        conn notNil ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   548
                            addr := Socket ipAddressOfHost:dottedName.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   549
                            hostName := Socket hostWithIpAddress:addr.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   550
                            hostName isNil ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   551
                                hostName :='?'
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   552
                            ].
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   553
                            l add:(dottedName paddedTo:15 with:Character space)
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   554
                                   , ' '
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   555
                                   , (hostName paddedTo:15 with:Character space)
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   556
                                   , ' up & reachable'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   557
                            list list:l.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   558
                            conn close.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   559
                        ]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   560
                    ].
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   561
                ].
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   562
            ].
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   563
            top label:'PING reachable hosts'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   564
        ] forkAt:(Processor userBackgroundPriority).
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   565
        walkProcess name:'ping net walker'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   566
                                                                        [exEnd]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   567
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   568
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   569
        This example creates a simple UDP server that accepts
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   570
        single packets from anybody and broadcasts them to all
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   571
        clients that have connected so far.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   572
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   573
                                                                        [exBegin]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   574
        | socket address buffer msgSize clients |
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   575
        clients := Set new.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   576
        address := IPSocketAddress new.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   577
        buffer := String new: 1024.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   578
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   579
        socket := self newUDPserverAtPort: 6666.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   580
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   581
        Transcript showCR: 'server starting'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   582
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   583
        [
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   584
            [true] whileTrue: [
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   585
                (socket readWaitWithTimeoutMs: 200) ifFalse: [
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   586
                    msgSize := socket
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   587
                            receiveFrom: address
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   588
                            buffer: buffer
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   589
                            start: 1
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   590
                            for: buffer size.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   591
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   592
                    clients add: address copy.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   593
                    clients do: [ :clientAddress |
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   594
                            socket
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   595
                                    sendTo: clientAddress
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   596
                                    buffer: buffer
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   597
                                    start: 1
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   598
                                    for: msgSize]]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   599
            ]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   600
        ] ensure:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   601
            Transcript showCR: 'server shutting down'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   602
            socket close
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   603
        ]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   604
                                                                        [exEnd]
817
eda0065cbd47 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 814
diff changeset
   605
eda0065cbd47 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 814
diff changeset
   606
   send a datagram to above server:
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   607
                                                                        [exBegin]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   608
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   609
        | socket address buffer host msg |
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   610
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   611
        host := Dialog
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   612
                request: 'What is the name of the server''s host?'
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   613
                initialAnswer: 'localhost'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   614
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   615
        socket := self newUDP.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   616
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   617
        address := IPSocketAddress hostName: host port: 6666.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   618
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   619
        buffer := ByteArray new: 1000.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   620
        [
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   621
            [(msg := Dialog request: 'Say something') isEmpty] whileFalse:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   622
                | replySize stream |
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   623
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   624
                socket writeWait.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   625
                stream := buffer writeStream.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   626
                stream nextPutAll: msg.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   627
                socket sendTo:address buffer:buffer start:1 for:stream position.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   628
                socket readWait.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   629
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   630
                replySize := socket receiveFrom:address buffer:buffer.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   631
                replySize > 0 ifTrue: [
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   632
                    Transcript cr; nextPutAll: 'Server acknowledged: '.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   633
                    Transcript show: ((buffer copyFrom: 1 to: replySize) asString)
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   634
                ]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   635
            ]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   636
        ] ensure: [socket close].
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   637
        Transcript cr
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   638
                                                                        [exEnd]
1316
ffe05bfee9b5 example
Claus Gittinger <cg@exept.de>
parents: 1313
diff changeset
   639
ffe05bfee9b5 example
Claus Gittinger <cg@exept.de>
parents: 1313
diff changeset
   640
ffe05bfee9b5 example
Claus Gittinger <cg@exept.de>
parents: 1313
diff changeset
   641
   loopBack:
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   642
                                                                        [exBegin]
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   643
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   644
        |readerTask readingSocket writingSocket|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   645
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   646
        readingSocket := self newTCPserverAtPort:9999.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   647
        readerTask :=
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   648
            [
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   649
                |connection|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   650
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   651
                readingSocket listenFor:1.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   652
                connection := readingSocket accept.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   653
                readingSocket close.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   654
                [connection atEnd] whileFalse:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   655
                    Transcript showCR:(connection nextLine).
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   656
                ].
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   657
                connection close.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   658
            ] fork.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   659
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   660
        Delay waitForSeconds:1.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   661
        writingSocket := self newTCPclientToHost:'localhost' port:9999.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   662
        writingSocket nextPutLine:'Hello'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   663
        writingSocket nextPutLine:'World'.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   664
        writingSocket close.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   665
                                                                        [exEnd]
25
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   666
"
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   667
! !
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   668
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
   669
!Socket class methodsFor:'instance creation'!
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   670
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   671
bindTo:aSocketAddress type:aTypeSymbol
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   672
    "create a socket for a specific type
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   673
     and bind it to aSocketAddress.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   674
     Type must be:
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   675
	#stream, #datagram or #raw
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   676
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   677
     Neither connect nor connect-wait is done."
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   678
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   679
    |newSock|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   680
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   681
    newSock := self domain:aSocketAddress domain type:aTypeSymbol.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   682
    [
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   683
	newSock bindTo:aSocketAddress reuseAddress:true.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   684
    ] ifCurtailed:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   685
	newSock close.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   686
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   687
    ^ newSock
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   688
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   689
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   690
    "
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   691
	Socket bindTo:(IPSocketAddress anyHost port:8081) type:#stream.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   692
	Socket bindTo:(IPv6SocketAddress anyHost port:8081) type:#datagram.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   693
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   694
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   695
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   696
domain:domainSymbol type:type
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   697
    "create a socket for domain and type -
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   698
     neither any connect nor binding is done.
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
   699
     Domain must be one of the symbols:
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   700
	#inet, #unix, #appletalk, #decnet, #xns, ...;
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
   701
     Type must be:
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   702
	#stream, #datagram or #raw
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   703
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   704
     XXX: currently only the #AF_INET and #AF_INET6 and #AF_UNIX domains are supported"
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   705
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   706
    ^ self new domain:domainSymbol type:type
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   707
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   708
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   709
     Socket domain:#AF_INET type:#stream
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   710
     Socket domain:#AF_INET type:#datagram
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   711
     Socket domain:#AF_INET6 type:#stream
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   712
     Socket domain:#AF_INET6 type:#datagram
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   713
     Socket domain:#AF_UNIX type:#stream
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   714
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   715
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   716
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   717
newTCP
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   718
    "create a TCP socket - no binding or other setup is done,
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   719
     neither connect nor connect-wait is done."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   720
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   721
    ^ self new domain:#'AF_INET' type:#stream
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   722
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   723
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   724
      Socket newTCP
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   725
    "
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   726
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   727
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   728
newTCP:aServiceOrNil
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   729
    "create a TCP socket for a service -
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   730
     neither connect nor connect-wait is done."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   731
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   732
    |newSock socketAddress socketAddressClass|
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   733
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   734
    newSock := self newTCP.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   735
    (newSock notNil and:[aServiceOrNil notNil]) ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   736
	[
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   737
	    socketAddressClass := newSock socketAddressClass.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   738
	    socketAddress := socketAddressClass hostName:nil serviceName:aServiceOrNil type:#stream.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   739
	    socketAddress hostAddress:socketAddressClass anyAddress.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   740
	    newSock bindTo:socketAddress reuseAddress:true.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   741
	] ifCurtailed:[
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   742
	    newSock close.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   743
	]
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   744
    ].
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   745
    ^ newSock
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   746
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   747
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
   748
    "
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   749
	Socket newTCP:'http-alt'.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   750
	Socket newTCP:9996.
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
   751
    "
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   752
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   753
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   754
newTCPclientToAddress:aHostAddress port:aService
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   755
    "create a new TCP client socket connecting to a service.
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   756
     Return a socket instance if ok, nil on failure.
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   757
     Block until a connection is established (but only the current thread;
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
   758
     not the whole smalltalk).
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   759
     See also: #newTCPclientToAddress:port:withTimeout:"
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   760
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   761
    ^ self newTCPclientToAddress:aHostAddress port:aService withTimeout:nil
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   762
!
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   763
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   764
newTCPclientToAddress:aSocketAddressOrByteArray port:aService withTimeout:millis
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   765
    "create a new TCP client socket connecting to a service.
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   766
     Return a socket instance if ok, nil on failure.
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   767
     If the millis arg is nonNil, stop trying to connect after that many milliseconds
811
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
   768
     and return nil."
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   769
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   770
    |port socketAddress|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   771
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   772
    aService isString ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   773
	port := self portOfService:aService protocol:#tcp.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   774
    ] ifFalse:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   775
	port := aService.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   776
    ].
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   777
    socketAddress := aSocketAddressOrByteArray isSocketAddress
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   778
			ifTrue:[aSocketAddressOrByteArray]
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   779
			ifFalse:[
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   780
			    "Passing ByteArrays is obsolete and only supported for IPv4"
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   781
			    IPSocketAddress hostAddress:aSocketAddressOrByteArray
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   782
			].
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   783
    port notNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   784
	socketAddress port:port.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   785
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   786
    ^ self newTCPclientToAddress:socketAddress withTimeout:millis
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   787
!
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   788
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   789
newTCPclientToAddress:aSocketAddress withTimeout:millis
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   790
    "create a new TCP client socket connecting to a service.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   791
     Return a socket instance if ok, nil on failure.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   792
     If the millis arg is nonNil, stop trying to connect after that many milliseconds
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   793
     and return nil."
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   794
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   795
    |socket|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   796
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   797
    socket := self domain:aSocketAddress domain type:#stream.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   798
    (socket connectTo:aSocketAddress withTimeout:millis) ifFalse:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   799
	socket close.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   800
	^ nil
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   801
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   802
    ^ socket.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   803
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   804
    "
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   805
	self newTCPclientToAddress:(IPv6SocketAddress hostName:'www.exept.de' port:80) withTimeout:nil
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   806
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   807
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   808
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   809
newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   810
    "create a new TCP client socket connecting to a service.
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   811
     If hostNameOrAddress is a string, try all the resolved addresses regardless
3988
7bf762b79544 #DOCUMENTATION by mawalch
mawalch
parents: 3985
diff changeset
   812
     whether for IPv4 or IPv6.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   813
     Return a socket instance if ok, nil on failure.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   814
     Block until a connection is established (but only the current thread;
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
   815
     not the whole smalltalk).
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   816
     See also: #newTCPclientToHost:port:withTimeout:"
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   817
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
   818
    ^ self
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   819
        newTCPclientToHost:hostNameOrAddress
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   820
        port:aPortOrServiceName
4239
827d8755965f #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4234
diff changeset
   821
        domain:self defaultIpDomainForConnect
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   822
        withTimeout:nil
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   823
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   824
    "
4239
827d8755965f #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4234
diff changeset
   825
      Socket newTCPclientToHost:'www.exept.de' port:'https'
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   826
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   827
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   828
    "Created: 31.10.1995 / 18:54:11 / cg"
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   829
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   830
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   831
newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName domain:aDomainSymbolOrNil withTimeout:millis
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   832
    "create a new TCP client socket connecting to a service on hostNameOrAddress.
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   833
     If hostNameOrAddress is a string, try all the resolved addresses.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   834
     Return a socket instance if ok, nil on failure.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   835
     Set aDomainSymbolOrNil to #AF_INET of #AF_INET6 to connect via a defined protocol.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   836
     Set aDomainSymbolOrNil to nil, to try all protocols.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   837
     If the millis arg is nonNil, stop trying to connect after that many milliseconds
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   838
     and return nil."
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   839
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   840
    |socket addressInfoList|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   841
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   842
    hostNameOrAddress isString ifFalse:[
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   843
        ^ self newTCPclientToAddress:hostNameOrAddress port:aPortOrServiceName withTimeout:millis.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   844
    ].
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   845
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   846
    addressInfoList := SocketAddress
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   847
                            getAddressInfo:hostNameOrAddress
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   848
                            serviceName:aPortOrServiceName
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   849
                            domain:aDomainSymbolOrNil
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   850
                            type:#stream
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   851
                            protocol:nil
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   852
                            flags:0.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   853
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   854
    addressInfoList do:[:eachAddressInfo|
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   855
        |domainSymbol lastDomainSymbol|
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   856
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   857
        domainSymbol := eachAddressInfo domain.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   858
        domainSymbol ~~ lastDomainSymbol ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   859
            socket notNil ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   860
                socket close.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   861
            ].
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   862
            socket := self new domain:domainSymbol type:#stream.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   863
            lastDomainSymbol := domainSymbol.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   864
        ].
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   865
        (socket connectTo:eachAddressInfo socketAddress withTimeout:millis) ifTrue:[
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   866
            ^ socket.
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   867
        ].
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   868
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   869
    socket notNil ifTrue:[
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   870
        socket close.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   871
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   872
    ^ nil.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   873
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   874
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   875
      Socket newTCPclientToHost:'www.exept.de' port:80 domain:#'AF_INET' withTimeout:1000.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   876
      Socket newTCPclientToHost:'www.exept.de' port:80 domain:#'AF_INET6' withTimeout:1000.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   877
      Socket newTCPclientToHost:'www.exept.de' port:80 domain:nil withTimeout:1000.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   878
      Socket newTCPclientToHost:'localhost' port:'nntp' withTimeout:1000
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   879
    "
604
b302a21afece Remove redundant code.
Stefan Vogel <sv@exept.de>
parents: 589
diff changeset
   880
b302a21afece Remove redundant code.
Stefan Vogel <sv@exept.de>
parents: 589
diff changeset
   881
    "Modified: / 16.1.1998 / 09:47:06 / stefan"
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   882
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   883
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   884
newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName withTimeout:millis
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   885
    "create a new TCP client socket connecting to a service on hostNameOrAddress.
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   886
     If hostNameOrAddress is a string, try all the resolved addresses regardless
3988
7bf762b79544 #DOCUMENTATION by mawalch
mawalch
parents: 3985
diff changeset
   887
     whether for IPv4 or IPv6.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   888
     Return a socket instance if ok, nil on failure.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   889
     If the millis arg is nonNil, stop trying to connect after that many milliseconds
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   890
     and return nil."
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   891
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   892
    ^ self
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   893
        newTCPclientToHost:hostNameOrAddress
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   894
        port:aPortOrServiceName
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   895
        domain:self defaultIpDomainForConnect
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   896
        withTimeout:millis
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   897
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   898
506
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
   899
newTCPserverAtAnonymousPort
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
   900
    "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
   901
     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
   902
811
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
   903
    ^ self newTCPserverAtPort:0
506
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
   904
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
   905
!
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
   906
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   907
newTCPserverAtPort:aService
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   908
    "create a new TCP server socket providing service."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   909
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
   910
    ^ self newTCP:aService
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   911
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   912
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   913
newUDP
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   914
    "create a UDP socket - no binding or other setup is done,
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   915
     neither connect nor connect-wait is done."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   916
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   917
    ^ self new domain:#'AF_INET' type:#datagram
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   918
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   919
    "Socket newUDP"
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   920
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   921
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   922
newUDP:aServiceOrNil
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   923
    "create a UDP socket for a service -
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   924
     neither connect nor connect-wait is done."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   925
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   926
    |newSock socketAddressClass socketAddress|
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   927
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   928
    newSock := self newUDP.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   929
    (newSock notNil and:[aServiceOrNil notNil]) ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   930
	[
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   931
	    socketAddressClass := newSock socketAddressClass.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   932
	    socketAddress := socketAddressClass hostName:nil serviceName:aServiceOrNil type:#stream.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   933
	    socketAddress hostAddress:socketAddressClass anyAddress.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   934
	    newSock bindTo:socketAddress reuseAddress:true.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   935
	] ifCurtailed:[
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   936
	    newSock close.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   937
	]
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   938
    ].
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   939
    ^ newSock
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   940
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
   941
    "
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   942
	Socket newUDP:4444.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   943
	Socket newUDP:'activesync'.
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
   944
    "
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   945
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   946
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   947
newUDPserverAtPort:aService
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   948
    "create a new UDP server socket providing service."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   949
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
   950
    ^ self newUDP:aService
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   951
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   952
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   953
newUNIX
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   954
    "create a UNIX domain socket - no binding or other setup is done,
811
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
   955
     neither connect nor connect-wait is done.
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
   956
     If the system does not support unix domain sockets (i.e. VMS or MSDOS),
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
   957
     return nil."
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   958
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   959
    ^ self new domain:#'AF_UNIX' type:#stream
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   960
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   961
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   962
     Socket newUNIX
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   963
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   964
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   965
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
   966
newUNIXclientTo:pathName
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   967
    "create a new UNIX client socket connecting to a pathname.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   968
     Return a socket instance if ok, nil on failure.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   969
     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
   970
     not the whole smalltalk).
811
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
   971
     If the system does not support unix domain sockets (i.e. VMS or MSDOS),
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
   972
     return nil.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   973
     See also: #newUNIXclientTo:withTimeout:"
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   974
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   975
    ^ self newUNIXclientTo:pathName withTimeout:nil
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   976
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   977
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   978
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   979
newUNIXclientTo:pathName withTimeout:millis
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   980
    "create a new UNIX client socket connecting to a pathname.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   981
     Return a socket instance if ok, nil on failure.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   982
     If the millis arg is nonNil, stop trying to connect after that many milliseconds
811
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
   983
     and return nil.
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
   984
     If the system does not support unix domain sockets (i.e. VMS or MSDOS),
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
   985
     return nil."
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   986
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   987
    |newSock|
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   988
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   989
    newSock := self newUNIX.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   990
    newSock notNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   991
	(newSock connectTo:(UDSocketAddress name:pathName) withTimeout:millis) ifFalse:[
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   992
	    newSock close.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   993
	    ^ nil
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   994
	]
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   995
    ].
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   996
    ^ newSock
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   997
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   998
    "
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   999
	Socket newUNIXclientTo:'/tmp/foo'
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1000
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1001
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1002
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1003
newUNIXserverAt:pathName
811
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
  1004
    "create a new UNIX server socket providing service at a pathname.
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
  1005
     If the system does not support unix domain sockets (i.e. VMS or MSDOS),
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
  1006
     return nil."
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1007
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1008
    ^ self bindTo:(UDSocketAddress name:pathName) type:#stream.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1009
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1010
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1011
     |s s2|
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1012
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1013
    '/tmp/foo' asFilename remove.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1014
     s := Socket newUNIXserverAt:'/tmp/foo'.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1015
     s listenFor:5.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1016
     s2 := s accept.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1017
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1018
! !
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1019
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1020
!Socket class methodsFor:'Compatibility-ST80'!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1021
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1022
family:domainSymbol type:typeSymbol
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1023
    "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
  1024
     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
  1025
     Type must be #stream, #datagram or #raw."
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1026
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1027
    ^ self domain:domainSymbol type:typeSymbol
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1028
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1029
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1030
     Socket family:#AF_INET type:#stream
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1031
     Socket family:#AF_INET type:#datagram
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1032
     Socket family:#AF_UNIX type:#stream
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1033
    "
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1034
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1035
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1036
getHostname
3462
cfdc47761d0f class: Socket
Claus Gittinger <cg@exept.de>
parents: 3281
diff changeset
  1037
    "return the computer's hostname string"
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1038
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1039
    ^ OperatingSystem getHostName
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1040
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1041
    "Created: / 27.2.1998 / 02:32:17 / cg"
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1042
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1043
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1044
sockStream
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1045
    "return the type code for stream sockets"
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1046
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1047
    ^ #stream
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1048
! !
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1049
1600
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1050
!Socket class methodsFor:'Compatibility-Squeak'!
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1051
2900
0e8b305e9a98 class: Socket
Claus Gittinger <cg@exept.de>
parents: 2899
diff changeset
  1052
deadlineSecs:seconds
0e8b305e9a98 class: Socket
Claus Gittinger <cg@exept.de>
parents: 2899
diff changeset
  1053
    ^ Timestamp now addSeconds:seconds
0e8b305e9a98 class: Socket
Claus Gittinger <cg@exept.de>
parents: 2899
diff changeset
  1054
!
0e8b305e9a98 class: Socket
Claus Gittinger <cg@exept.de>
parents: 2899
diff changeset
  1055
1600
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1056
initializeNetwork
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1057
    "/ intentionally left blank here
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1058
!
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1059
3485
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1060
standardDeadline
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1061
    "a standard timeout in seconds for connection setup;
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1062
     not really used in ST/X code (but by some code ported from squeak)"
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1063
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1064
    ^ 30
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1065
!
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1066
2292
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  1067
standardTimeout
3485
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1068
    "a standard timeout in seconds for transfers;
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1069
     not really used in ST/X code (but by some code ported from squeak)"
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1070
2292
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  1071
    ^ 30
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  1072
!
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  1073
1600
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1074
wildcardPort
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1075
    ^ nil
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1076
! !
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1077
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1078
!Socket class methodsFor:'Compatibility-VW'!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1079
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1080
AF_INET
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1081
    ^ #AF_INET
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1082
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1083
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1084
SOCK_STREAM
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1085
    ^ #SOCK_STREAM
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1086
! !
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1087
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1088
!Socket class methodsFor:'Signal constants'!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1089
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1090
brokenConnectionSignal
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1091
    "return the signal used to tell broken connections.
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1092
     Since in unix, this is the same as the broken pipe signal,
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1093
     return that one.
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1094
     (for other Operatingsystems, this may change ..)"
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1095
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1096
    ^ PipeStream brokenPipeSignal
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1097
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1098
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1099
invalidArgumentsSignal
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1100
    "dummy for compatibility"
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1101
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1102
    ^ self errorSignal
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1103
! !
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1104
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1105
!Socket class methodsFor:'debugging'!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1106
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1107
debug:aBoolean
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1108
    "turn on/off internal debugprints.
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1109
     This method is for ST/X debugging only and
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1110
     may  be removed in later versions"
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1111
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1112
%{  /* NOCONTEXT */
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1113
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1114
    __debugging__ = (aBoolean == true);
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1115
%}
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1116
    "
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1117
     Socket debug:true
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1118
     Socket debug:false
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1119
    "
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1120
! !
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1121
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1122
!Socket class methodsFor:'defaults'!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1123
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1124
defaultIpDomainForConnect
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1125
    "answer the domain used to look up host names for connect:
4239
827d8755965f #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4234
diff changeset
  1126
        #AF_INET    use only IPv4
827d8755965f #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4234
diff changeset
  1127
        #AF_INET6   use only IPv6
827d8755965f #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4234
diff changeset
  1128
        nil         use both IPv4 and IPv6"
827d8755965f #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4234
diff changeset
  1129
827d8755965f #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4234
diff changeset
  1130
    ^ nil
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1131
! !
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1132
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1133
!Socket class methodsFor:'obsolete'!
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1134
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1135
connectTo:service on:host
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1136
    <resource: #obsolete>
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1137
    "standard & easy client setup:
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1138
	create new client tcp socket, bind and connect;
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1139
	return the socket.
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1140
     The thread blocks (interruptable), until the connection is established."
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1141
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1142
    ^ self newTCPclientToHost:host port:(self portOfService:service).
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1143
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1144
    "
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1145
     Socket connectTo:9995 on:'clam'
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1146
     Socket connectTo:4711 on:'exept'
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1147
     Socket connectTo:'finger' on:'clam'
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1148
     Socket connectTo:'ftp' on:'exept'
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1149
     Socket connectTo:'nntp' on:(OperatingSystem getEnvironment:'NNTPSERVER')
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1150
    "
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1151
!
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1152
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1153
networkLongOrderIsMSB
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1154
    "return the well known fact, that network byte order is most significant byte first"
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1155
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1156
    <resource: #obsolete>
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1157
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1158
    ^ true
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1159
!
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1160
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1161
networkShortOrderIsMSB
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1162
    "return the well known fact, that network byte order is most significant byte first"
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1163
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1164
    <resource: #obsolete>
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1165
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1166
    ^ true
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1167
!
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1168
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1169
provide:aService
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1170
    "standard & easy server setup:
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1171
     create a new TCP server socket providing a service."
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1172
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1173
    <resource:#obsolete>
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1174
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1175
    |newSock|
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1176
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1177
    self obsoleteMethodWarning:'use #newTCPserverAtPort: / #listen'.
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1178
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1179
    newSock := self newTCPserverAtPort:(self portOfService:aService).
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1180
    newSock notNil ifTrue:[
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1181
	newSock listenFor:5.
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1182
    ].
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1183
    ^ newSock
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1184
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1185
    "
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1186
     Socket provide:9995
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1187
     (Socket provide:9996) accept
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1188
     Socket provide:'nntp'
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1189
    "
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1190
! !
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1191
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1192
!Socket class methodsFor:'obsolete host queries'!
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1193
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1194
appletalkAddressOfHost:aHostName
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1195
    <resource: #obsolete>
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1196
    "return the APPLETALK address for a hostname as a byteArray,
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1197
     where the network bytes come first (no matter what the local byteorder is)
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1198
     followed by the node byte.
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1199
     If the host is unknown, return nil.
812
c8101ea70e6c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 811
diff changeset
  1200
     This is the reverse operation to #hostWithAppletalkAddress:.
c8101ea70e6c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 811
diff changeset
  1201
     WARNING: untested code - I have no appletalk to test this."
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1202
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1203
    NameLookupError
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1204
	handle:[:ex |
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1205
	    ^ nil
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1206
	]
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1207
	do:[
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1208
	    ^ (AppletalkSocketAddress hostName:aHostName) address
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1209
	]
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1210
!
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1211
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1212
hostWithAppletalkAddress:addrByteArray
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1213
    <resource: #obsolete>
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1214
    "return the hostname for an APPLETALK address.
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1215
     The address is supposed to be a byteArray consisting of 3 bytes,
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1216
     the network bytes come first (no matter what the local byteorder is).
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1217
     The last byte is the node number.
4320
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1218
     Nil is returned for an unknown host or if it's not an appletalk host.
812
c8101ea70e6c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 811
diff changeset
  1219
     This is is the reverse operation to #appletalkAddressOfHost:.
c8101ea70e6c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 811
diff changeset
  1220
     WARNING: untested code - I have no appletalk to test this."
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1221
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1222
    NameLookupError
4320
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1223
        handle:[:ex |
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1224
            ^ nil
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1225
        ]
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1226
        do:[
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1227
            ^ (AppletalkSocketAddress hostAddress:addrByteArray) hostName
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1228
        ]
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1229
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1230
    "
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1231
     Socket appletalkAddressOfHost:'yourAppleHere'
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1232
     Socket hostWithAppletalkAddress:#[1 2 3]
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1233
     "
4320
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1234
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1235
    "Modified (comment): / 13-02-2017 / 20:30:56 / cg"
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1236
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1237
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1238
hostWithIpAddress:addrByteArray
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1239
    <resource: #obsolete>
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1240
    "return the hostname for an IP (internet-) address.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1241
     The address is supposed to be a byteArray consisting of 4 bytes,
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1242
     the network bytes come first (no matter what the local byteorder is).
4320
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1243
     Nil is returned for an unknown host or if it's not an internet host.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1244
     This is the reverse operation to #ipAddressOfHost:."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1245
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1246
    NameLookupError
4320
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1247
        handle:[:ex |
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1248
            ^ nil
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1249
        ]
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1250
        do:[
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1251
            ^ (IPSocketAddress hostAddress:addrByteArray) hostName
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1252
        ]
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1253
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1254
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1255
     Socket ipAddressOfHost:'clam'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1256
     Socket hostWithIpAddress:(Socket ipAddressOfHost:'clam')
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1257
     Socket ipAddressOfHost:'porty'
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1258
     Socket hostWithIpAddress:(Socket ipAddressOfHost:'porty')
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1259
     Socket hostWithIpAddress:#[1 2 3 4]
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1260
     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
  1261
     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
  1262
     Socket hostWithIpAddress:(Socket ipAddressOfHost:'www.altavista.com')
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1263
     "
4320
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1264
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1265
    "Modified (comment): / 13-02-2017 / 20:31:01 / cg"
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1266
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1267
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1268
hostWithIpV6Address:addrByteArray
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1269
    <resource: #obsolete>
820
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1270
    "return the hostname for an IPv6 (internet-) address.
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1271
     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
  1272
     the network bytes come first (no matter what the local byteorder is).
4320
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1273
     Nil is returned for an unknown host or if it's not an internet host.
820
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1274
     This is the reverse operation to #ipV6AddressOfHost:."
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1275
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1276
    NameLookupError
4320
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1277
        handle:[:ex |
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1278
            ^ nil
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1279
        ]
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1280
        do:[
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1281
            ^ (IPv6SocketAddress hostAddress:addrByteArray) hostName
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1282
        ]
820
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1283
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1284
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1285
     Socket ipV6AddressOfHost:'clam'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1286
     Socket hostWithIpV6Address:(Socket ipAddressOfHost:'clam')
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1287
     Socket ipV6AddressOfHost:'porty'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1288
     Socket hostWithIpV6Address:(Socket ipAddressOfHost:'porty')
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1289
     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
  1290
     Socket ipV6AddressOfHost:'www.exept.de'
820
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1291
     "
4320
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1292
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1293
    "Modified (comment): / 13-02-2017 / 20:31:08 / cg"
820
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1294
!
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1295
6
claus
parents: 4
diff changeset
  1296
ipAddressOfHost:aHostName
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1297
    <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
  1298
    "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
  1299
     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
  1300
     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
  1301
     This is the reverse operation to #hostWithIpAddress:."
6
claus
parents: 4
diff changeset
  1302
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1303
    NameLookupError
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1304
	handle:[:ex |
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1305
	    ^ nil
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1306
	]
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1307
	do:[
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1308
	    ^ (IPSocketAddress hostName:aHostName) address
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1309
	]
21
33eb5ffad09d *** empty log message ***
claus
parents: 8
diff changeset
  1310
71
claus
parents: 63
diff changeset
  1311
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1312
     Socket ipAddressOfHost:'localhost'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1313
     Socket ipAddressOfHost:'exept'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1314
     Socket ipAddressOfHost:'1.2.3.4'
1274
f257a1b16b4d use naemLookup from SocketAddress for ipv6
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
  1315
     Socket ipAddressOfHost:'193.15.16.17'
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1316
     Socket ipAddressOfHost:'josef'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1317
     Socket ipAddressOfHost:'styx.com'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1318
     Socket hostWithIpAddress:(Socket ipAddressOfHost:'localhost')
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1319
     Socket ipAddressOfHost:(Socket hostWithIpAddress:'127.0.0.1')
71
claus
parents: 63
diff changeset
  1320
    "
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1321
!
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1322
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1323
ipV6AddressOfHost:aHostName
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1324
    <resource: #obsolete>
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1325
    "return the IPv6 (internet-) number for a hostname as a byteArray,
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1326
     where the network bytes come first (no matter what the cpus byteOrder is).
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1327
     If the host is unknown, return nil.
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1328
     This is the reverse operation to #hostWithIpV6Address:."
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1329
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1330
    NameLookupError
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1331
	handle:[:ex |
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1332
	    ^ nil
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1333
	]
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1334
	do:[
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1335
	    ^ (IPv6SocketAddress hostName:aHostName) address
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1336
	]
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1337
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1338
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1339
     Socket ipV6AddressOfHost:'localhost'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1340
     Socket ipV6AddressOfHost:'exept'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1341
     Socket ipV6AddressOfHost:'exept.exept.de'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1342
     Socket ipV6AddressOfHost:'www.google.de'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1343
     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
  1344
     Socket ipV6AddressOfHost:'josef'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1345
     Socket ipV6AddressOfHost:'styx.com'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1346
     Socket hostWithIpV6Address:(Socket ipV6AddressOfHost:'localhost')
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1347
     Socket ipV6AddressOfHost:(Socket hostV6WithIpAddress:'127.0.0.1')
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1348
    "
531
ca4c73813531 category changes
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1349
! !
ca4c73813531 category changes
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1350
ca4c73813531 category changes
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1351
!Socket class methodsFor:'queries'!
ca4c73813531 category changes
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1352
1260
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1353
peerFromDomain:domain name:peerName port:port
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1354
    |addrClass|
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1355
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1356
    addrClass := self socketAddressClassForDomain:domain.
1263
23161346a162 no longer callBack into sockets getHostName/getAddress code.
Claus Gittinger <cg@exept.de>
parents: 1260
diff changeset
  1357
    ^ 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
  1358
!
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  1359
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  1360
peerNameFromDomain:domain peer:peer
1260
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1361
    |addrClass|
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1362
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1363
    addrClass := self socketAddressClassForDomain:domain.
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1364
    ^ 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
  1365
!
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  1366
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1367
portOfService:aNameOrNumber
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1368
    "returns the port-number for a given IP-service
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1369
     or nil if no such service exists;
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1370
     - used to convert service names to portNumbers"
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1371
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1372
    ^ self portOfService:aNameOrNumber protocol:nil
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1373
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1374
    "
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1375
     Socket portOfService:'finger'
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1376
     Socket portOfService:'nntp'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1377
     Socket portOfService:'echo'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1378
     Socket portOfService:'snmp'
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1379
    "
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1380
!
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1381
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1382
portOfService:aNameOrNumber protocol:aProtocol
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1383
    "returns the port-number for a given IP-service
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1384
     or nil if no such service exists;
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1385
     - used to convert service names to portNumbers"
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1386
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  1387
%{ /* UNLIMITEDSTACK(noWIN32) */
418
317e55c6c281 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 403
diff changeset
  1388
#ifndef NO_SOCKET
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1389
    struct servent *servent = NULL;
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1390
    char *protocol;
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1391
    int tryBoth = 0;
524
8287da54eccf more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents: 513
diff changeset
  1392
    short portNo;
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1393
604
b302a21afece Remove redundant code.
Stefan Vogel <sv@exept.de>
parents: 589
diff changeset
  1394
    if (__isSmallInteger(aNameOrNumber)) {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1395
	RETURN ( aNameOrNumber );
92
Claus Gittinger <cg@exept.de>
parents: 91
diff changeset
  1396
    }
Claus Gittinger <cg@exept.de>
parents: 91
diff changeset
  1397
2331
b5280e1b3b9f changed:
Stefan Vogel <sv@exept.de>
parents: 2327
diff changeset
  1398
    if (__isStringLike(aProtocol)) {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1399
	protocol = (char *)__stringVal(aProtocol);
604
b302a21afece Remove redundant code.
Stefan Vogel <sv@exept.de>
parents: 589
diff changeset
  1400
    } else {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1401
	protocol = "tcp";
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1402
	tryBoth = 1;
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1403
    }
524
8287da54eccf more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents: 513
diff changeset
  1404
2331
b5280e1b3b9f changed:
Stefan Vogel <sv@exept.de>
parents: 2327
diff changeset
  1405
    if (__isStringLike(aNameOrNumber)) {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1406
	servent = getservbyname((char *) __stringVal(aNameOrNumber), protocol);
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1407
	if (servent != NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1408
	    RETURN ( __MKSMALLINT(ntohs(servent->s_port)) );
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1409
	}
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1410
	if (tryBoth) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1411
	    servent = getservbyname((char *) __stringVal(aNameOrNumber), "udp");
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1412
	    if (servent != NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1413
		RETURN ( __MKSMALLINT(ntohs(servent->s_port)) );
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1414
	    }
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1415
	}
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1416
	RETURN ( nil );
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1417
    }
418
317e55c6c281 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 403
diff changeset
  1418
#endif
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1419
    RETURN ( nil );
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1420
%}
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1421
    "
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1422
     Socket portOfService:'echo' protocol:'udp'
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1423
     Socket portOfService:'echo' protocol:'tcp'
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1424
    "
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1425
!
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1426
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1427
protocolOfService:aNameOrNumber
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1428
    "returns the protocol (as string) for a given IP-service
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1429
     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
  1430
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
  1431
%{  /* UNLIMITEDSTACK(noWIN32) */
418
317e55c6c281 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 403
diff changeset
  1432
#ifndef NO_SOCKET
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1433
    struct servent *servent = NULL;
524
8287da54eccf more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents: 513
diff changeset
  1434
    short portNo;
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1435
58
bd6753bf0401 *** empty log message ***
claus
parents: 51
diff changeset
  1436
    if (__isSmallInteger(aNameOrNumber)) {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1437
	portNo = __intVal(aNameOrNumber);
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1438
	servent = getservbyport(htons(portNo), "tcp") ;
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1439
	if (servent == NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1440
	    servent = getservbyport(htons(portNo), "udp") ;
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1441
	    if (servent == NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1442
		RETURN ( nil );
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1443
	    }
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1444
	}
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1445
    } else {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1446
	if (__isStringLike(aNameOrNumber)) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1447
	    servent = getservbyname((char *) __stringVal(aNameOrNumber), "tcp");
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1448
	    if (servent == NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1449
		servent = getservbyname((char *) __stringVal(aNameOrNumber), "udp");
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1450
		if (servent == NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1451
		    RETURN ( nil );
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1452
		}
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1453
	    }
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1454
	}
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1455
    }
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1456
    if (servent) {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1457
	RETURN ( __MKSTRING(servent->s_proto) );
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1458
    }
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
  1459
#endif /* !NO_SOCKET */
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1460
    RETURN ( nil );
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1461
%}
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1462
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1463
     Socket protocolOfService:'finger'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1464
     Socket protocolOfService:'nntp'
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1465
     Socket protocolOfService:'xxx'
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1466
     Socket protocolOfService:79
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1467
     Socket protocolOfService:'snmp'
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1468
    "
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1469
!
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1470
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
  1471
socketAddressClassForDomain:domain
1230
2cfb8c270cb5 more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents: 1224
diff changeset
  1472
    ^ SocketAddress knownClassFromCode:domain
2cfb8c270cb5 more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents: 1224
diff changeset
  1473
2cfb8c270cb5 more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents: 1224
diff changeset
  1474
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1475
     self socketAddressClassForDomain:#AF_INET
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1476
     self socketAddressClassForDomain:#AF_UNIX
1230
2cfb8c270cb5 more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents: 1224
diff changeset
  1477
    "
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
  1478
!
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  1479
532
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1480
supportedProtocolFamilies
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1481
    "return a collection of supported protocol families.
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1482
     This list specifies what the Socket class supports -
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1483
     socket creation may still fail, if your system was built
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1484
     without it."
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1485
1280
b973855bfe74 moved all socket constant queries from SocketHandle
Claus Gittinger <cg@exept.de>
parents: 1278
diff changeset
  1486
    ^ OperatingSystem supportedProtocolFamilies
532
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1487
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1488
    "
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1489
     Socket supportedProtocolFamilies
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1490
    "
559
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1491
!
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1492
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1493
typeOfProtocol:aProtocol
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1494
    "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
  1495
     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
  1496
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1497
    (aProtocol = 'tcp') ifTrue:[^ #stream].
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1498
    (aProtocol = 'udp') ifTrue:[^ #datagram].
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1499
    (aProtocol = 'ip')  ifTrue:[^ #raw].
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1500
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1501
     unix domain
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1502
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1503
    (aProtocol = 'ud')  ifTrue:[^ #stream].
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1504
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1505
     add x25 stuff (if any) here ...
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1506
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1507
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1508
     add appletalk stuff (if any) here ...
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1509
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1510
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1511
     add other stuff (if any) here ...
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1512
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1513
    ^ nil
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1514
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1515
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1516
     Socket typeOfProtocol:'tcp'
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1517
     Socket typeOfProtocol:'ucp'
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1518
     Socket typeOfProtocol:(Socket protocolOfService:'nntp')
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1519
     Socket typeOfProtocol:(Socket protocolOfService:'echo')
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1520
    "
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1521
! !
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1522
1202
c52279df60d8 method category rename
Claus Gittinger <cg@exept.de>
parents: 1187
diff changeset
  1523
!Socket methodsFor:'Compatibility-Dolphin'!
1110
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1524
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1525
setReceiveTimeout: milliseconds
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1526
    self receiveTimeout:(milliseconds / 1000)
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1527
!
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1528
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1529
setSendTimeout: milliseconds
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1530
    self sendTimeout:(milliseconds / 1000)
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1531
! !
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1532
1202
c52279df60d8 method category rename
Claus Gittinger <cg@exept.de>
parents: 1187
diff changeset
  1533
!Socket methodsFor:'Compatibility-ST80'!
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1534
887
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1535
acceptNonBlock
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1536
    ^ self accept
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1537
!
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1538
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1539
ioConnection
887
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1540
    ^ self
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1541
!
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1542
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1543
notReadySignal
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1544
    "ST-80 mimicry.
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1545
     for now - this is not yet raised"
71
claus
parents: 63
diff changeset
  1546
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1547
    ^ Signal new
486
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1548
!
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1549
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
  1550
readAppendStream
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1551
    "ST-80 mimicry.
4293
82b9928ddcae #OTHER by mawalch
mawalch
parents: 4288
diff changeset
  1552
     In ST-80, socket is not a stream, but refers to one.
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1553
     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
  1554
     the actual stream.
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1555
     In ST/X, sockets inherit from stream, so
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1556
     this method returns the receiver, for transparency"
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1557
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
  1558
    ^ self
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
  1559
!
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
  1560
486
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1561
readStream
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1562
    "ST-80 mimicry.
4293
82b9928ddcae #OTHER by mawalch
mawalch
parents: 4288
diff changeset
  1563
     In ST-80, socket is not a stream, but refers to one.
486
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1564
     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
  1565
     the actual stream.
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1566
     In ST/X, sockets inherit from stream, so
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1567
     this method returns the receiver, for transparency"
486
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1568
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1569
    ^ self
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1570
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1571
    "Created: 24.1.1997 / 23:52:57 / cg"
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1572
!
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1573
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1574
writeStream
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1575
    "ST-80 mimicry.
4293
82b9928ddcae #OTHER by mawalch
mawalch
parents: 4288
diff changeset
  1576
     In ST-80, socket is not a stream, but refers to one.
486
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1577
     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
  1578
     the actual stream.
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1579
     In ST/X, sockets inherit from stream, so
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1580
     this method returns the receiver, for transparency"
486
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1581
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1582
    ^ self
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1583
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1584
    "Created: 24.1.1997 / 10:34:35 / cg"
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1585
    "Modified: 24.1.1997 / 23:52:52 / cg"
71
claus
parents: 63
diff changeset
  1586
! !
claus
parents: 63
diff changeset
  1587
1202
c52279df60d8 method category rename
Claus Gittinger <cg@exept.de>
parents: 1187
diff changeset
  1588
!Socket methodsFor:'Compatibility-Squeak'!
771
74abb04ef57c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 752
diff changeset
  1589
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1590
address
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1591
    ^ self getSocketAddress
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1592
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1593
2308
d8b410a4f38b added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents: 2292
diff changeset
  1594
connectToHostNamed:hostName port:portNr
d8b410a4f38b added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents: 2292
diff changeset
  1595
    self connectTo:hostName port:portNr
d8b410a4f38b added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents: 2292
diff changeset
  1596
!
d8b410a4f38b added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents: 2292
diff changeset
  1597
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1598
dataAvailable
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1599
    ^ self canReadWithoutBlocking
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1600
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1601
1886
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1602
destroy
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1603
    self close
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1604
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1605
    "Created: / 04-06-2007 / 21:29:03 / cg"
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1606
!
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1607
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1608
listenOn:aPortNr
1882
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1609
    self listenOn:aPortNr backlogSize:5
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1610
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1611
    "Modified: / 31-05-2007 / 17:59:53 / cg"
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1612
!
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1613
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1614
listenOn:aPortNr backlogSize:aNumber
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1615
    self bindTo:aPortNr address:nil.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1616
    self listenFor:aNumber
1882
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1617
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1618
    "Created: / 31-05-2007 / 17:59:47 / cg"
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1619
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1620
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1621
peerName
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1622
    "return my peer (i.e. ipAddr + port);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1623
     May return nil if not yet setup completely."
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1624
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1625
    ^ self getPeer
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1626
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1627
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1628
sendData: aStringOrByteArray
3155
d1884a37375c Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents: 3152
diff changeset
  1629
    "Send all of the data in the given array, even if it requires multiple calls to send it all.
d1884a37375c Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents: 3152
diff changeset
  1630
     Return the number of bytes sent."
d1884a37375c Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents: 3152
diff changeset
  1631
d1884a37375c Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents: 3152
diff changeset
  1632
    ^ self nextPutBytes:aStringOrByteArray size from:aStringOrByteArray startingAt:1.
1600
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1633
!
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1634
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1635
setOption: optionName value: optionValue
2899
a1f9a142d49b class: Socket
Claus Gittinger <cg@exept.de>
parents: 2891
diff changeset
  1636
    ^ self setSocketOption:(optionName asSymbol) argument:optionValue argument:nil
1887
f26d9b04244d changed #setOption:value:
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1637
f26d9b04244d changed #setOption:value:
Claus Gittinger <cg@exept.de>
parents: 1886
diff changeset
  1638
    "Modified: / 04-06-2007 / 21:23:19 / cg"
1600
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1639
!
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1640
2904
d2e1eead2bfb class: Socket
Claus Gittinger <cg@exept.de>
parents: 2900
diff changeset
  1641
waitForConnectionUntil:aTimestamp
d2e1eead2bfb class: Socket
Claus Gittinger <cg@exept.de>
parents: 2900
diff changeset
  1642
    "return true if connected"
d2e1eead2bfb class: Socket
Claus Gittinger <cg@exept.de>
parents: 2900
diff changeset
  1643
d2e1eead2bfb class: Socket
Claus Gittinger <cg@exept.de>
parents: 2900
diff changeset
  1644
    self readWaitWithTimeoutMs: (aTimestamp millisecondDeltaFrom:Timestamp now).
d2e1eead2bfb class: Socket
Claus Gittinger <cg@exept.de>
parents: 2900
diff changeset
  1645
    ^ self isConnected
771
74abb04ef57c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 752
diff changeset
  1646
! !
74abb04ef57c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 752
diff changeset
  1647
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1648
!Socket methodsFor:'accepting connections'!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1649
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1650
accept
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1651
    "create a new TCP socket from accepting on the receiver.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1652
     This method will suspend the current process if no connection is waiting.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1653
     For ST-80 compatibility"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1654
3633
3a21f142432e #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3620
diff changeset
  1655
    ^ self waitForNewConnectionWithTimeout:nil
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1656
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1657
    "
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1658
     |sock newSock|
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1659
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1660
     sock := Socket provide:8004.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1661
     sock listenFor:5.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1662
     newSock := sock accept.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1663
    "
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1664
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1665
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1666
blockingAccept
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1667
    "create a new TCP socket from accepting on the receiver.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1668
     This method will suspend the smalltalk image with all smalltalk processes if no connection is waiting.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1669
     For ST-80 compatibility"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1670
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1671
    |newSock|
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1672
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1673
    newSock := self class new.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1674
    (newSock primAcceptOn:self blocking:true) ifFalse:[
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1675
	"should raise an error here"
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1676
	^ nil
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1677
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1678
    ^ newSock
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1679
! !
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1680
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1681
!Socket methodsFor:'binding'!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1682
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1683
bindAnonymously
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1684
    "bind to any address. A free port will be allocated.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1685
     Our own socket address will be determined after conection set up.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1686
     This is the default after the socket has been created"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1687
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1688
    ^ self bindTo:nil reuseAddress:false
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1689
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1690
    "
1938
831af28cc848 comments
Stefan Vogel <sv@exept.de>
parents: 1935
diff changeset
  1691
      self newTCP bindAnonymously; listenFor:1; yourself
4206
ac9a6e096796 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4203
diff changeset
  1692
      self newTCP bindAnonymously; port
1938
831af28cc848 comments
Stefan Vogel <sv@exept.de>
parents: 1935
diff changeset
  1693
    "
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1694
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1695
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1696
bindAnonymouslyToAddress:aSocketAddress
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1697
    "bind to address addressString.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1698
     A free port will be allocated"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1699
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1700
    ^ self
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1701
	bindTo:0
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1702
	address:aSocketAddress
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1703
	reuseAddress:false
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1704
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1705
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1706
      self newTCP bindAnonymouslyToAddress:IPSocketAddress localHost; listenFor:1; yourself
1938
831af28cc848 comments
Stefan Vogel <sv@exept.de>
parents: 1935
diff changeset
  1707
    "
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1708
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1709
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1710
bindTo:aSocketAddress
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1711
    "ST80 compatible bind, expecting a socketAddress argument.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1712
     The socketAddress object (an instance of SocketAddress)
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1713
     is supposed to respond to #portOrName and #address requests."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1714
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1715
    ^ self bindTo:aSocketAddress reuseAddress:true
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1716
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1717
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1718
bindTo:portNrOrNameString address:addressString
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1719
    "Old interface: bind the socket to an address
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1720
     - returns true if ok, false otherwise.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1721
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1722
     The interpretation of hostOrPathNameOrSocketAddrOrNil portNrOrName depends on the domain:
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1723
	Best use a SocketAddress
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1724
	For backward compatibility:
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1725
	    AF_INET domain can also use (4byte) byteArray like internet numbers,
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1726
	    AF_UNIX domain cab use pathname strings."
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1727
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1728
    ^ self
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1729
	bindTo:portNrOrNameString
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1730
	address:addressString
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1731
	reuseAddress:true
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1732
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1733
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1734
bindTo:portNrOrNameOrNil address:hostOrPathNameOrSocketAddrOrNil reuseAddress:reuse
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1735
    "Old interface: bind the socket to an address
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1736
     - returns true if ok, false otherwise.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1737
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1738
     The interpretation of hostOrPathNameOrSocketAddrOrNil portNrOrName depends on the domain:
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1739
	Best use a SocketAddress
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1740
	For backward compatibility:
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1741
	    AF_INET domain can also use (4byte) byteArray like internet numbers,
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1742
	    AF_UNIX domain can use pathname strings.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1743
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1744
     The reuse boolean argument controls if the SO_REUSEADDR socket option
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1745
     is to be set (to avoid the 'bind: address in use' error).
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1746
    "
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1747
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1748
    |socketAddress|
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1749
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1750
    handle isNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1751
	^ self errorNotOpen
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1752
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1753
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1754
    hostOrPathNameOrSocketAddrOrNil isNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1755
	socketAddress := self socketAddressClass anyHost.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1756
    ] ifFalse:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1757
	(hostOrPathNameOrSocketAddrOrNil isSocketAddress) ifTrue:[
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1758
	    socketAddress := hostOrPathNameOrSocketAddrOrNil.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1759
	] ifFalse:[
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1760
	    "backward compatibility: support for byteArray and string arg"
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1761
	    hostOrPathNameOrSocketAddrOrNil isString ifTrue:[
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1762
		socketAddress := self socketAddressClass hostName:hostOrPathNameOrSocketAddrOrNil.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1763
	    ] ifFalse:[
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1764
		hostOrPathNameOrSocketAddrOrNil isByteCollection ifFalse:[
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1765
		    ^ self error:'bindTo: bad host (socketAddress) argument'
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1766
		].
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1767
		socketAddress := self socketAddressClass hostAddress:hostOrPathNameOrSocketAddrOrNil.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1768
	    ].
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1769
	].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1770
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1771
    portNrOrNameOrNil notNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1772
	socketAddress port:portNrOrNameOrNil.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1773
    ].
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1774
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1775
    ^ self bindTo:socketAddress reuseAddress:reuse.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1776
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1777
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1778
     (Socket domain:#'AF_INET' type:#stream)
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1779
	 bindTo:2144 address:nil; yourself
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1780
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1781
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1782
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1783
bindTo:aSocketAddress reuseAddress:reuse
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1784
    "Bind the socket to aSocketAddress - returns true if ok, false otherwise.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1785
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1786
     The reuse boolean argument controls if the SO_REUSEADDR socket option
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1787
     is to be set (to avoid the 'bind: address in use' error).
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1788
3137
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1789
     You can bind to nil to bind to anyHost and assign a random port."
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1790
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1791
    |ok error socketAddress|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1792
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1793
    handle isNil ifTrue:[
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1794
        ^ self errorNotOpen
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1795
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1796
    socketAddress := aSocketAddress.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1797
    socketAddress isNil ifTrue:[
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1798
        "ok, get a all zero socket address, so it is for anyHost
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1799
         and the port will be assigned"
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1800
        socketAddress := self socketAddressClass new.
3513
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  1801
    ].
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  1802
    domain == #'AF_INET6' ifTrue:[
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1803
        "accept also IPv4 connections on IPv6 sockets (this is off by default for windows"
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1804
        self setSocketOption:#'IPV6_V6ONLY' argument:false argument:nil.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1805
    ].
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1806
    ok := false.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1807
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1808
%{  /* STACK: 100000 */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1809
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1810
    OBJ fp = __INST(handle);
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1811
    SOCKET sock;
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1812
    union sockaddr_u sa;
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1813
    int sockaddr_size;
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  1814
    INT ret;
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1815
    int sockAddrOffs;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1816
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1817
    if (fp == nil) {
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1818
        goto getOutOfHere;
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1819
    }
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1820
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1821
    if (! __isBytes(socketAddress)) {
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1822
        error = __mkSmallInteger(-1);
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1823
        goto getOutOfHere;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1824
    }
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1825
    /* get the socket-address */
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1826
    if (__isNonNilObject(socketAddress)){
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1827
        int nIndex;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1828
        OBJ cls = __qClass(socketAddress);
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1829
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1830
        sockAddrOffs = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1831
        nIndex = __qSize(socketAddress) - OHDR_SIZE;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1832
        sockaddr_size = nIndex - sockAddrOffs;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1833
        if (sockaddr_size > sizeof(sa)) {
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1834
            error=__mkSmallInteger(-2);
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1835
            goto getOutOfHere;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1836
        }
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1837
        memcpy(&sa, __byteArrayVal(socketAddress) + sockAddrOffs, sockaddr_size);
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1838
    }
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1839
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1840
    sock = SOCKET_FROM_FILE_OBJECT(fp);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1841
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1842
# ifdef SO_REUSEADDR
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1843
    if (reuse == true) {
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1844
        int on = 1;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1845
        if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0) {
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1846
            DBGPRINTF(("SOCKET: setsockopt - SO_REUSEADDR failed\n"));
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1847
        }
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1848
    }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1849
# endif /* SO_REUSEADDR */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1850
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1851
# ifdef BIND_BLOCKS
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1852
#  ifdef DO_WRAP_CALLS
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1853
    do {
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1854
        __threadErrno = 0;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1855
        ret = STX_WSA_NOINT_CALL3("bind", bind, sock, &sa, sockaddr_size);
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1856
    } while ((ret < 0) && (__threadErrno == EINTR));
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1857
    if (ret < 0) {
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1858
        errno = __threadErrno;
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1859
    }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1860
#  else
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1861
    __BEGIN_INTERRUPTABLE__
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1862
    do {
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1863
        ret = bind(sock, (struct sockaddr *)&sa, sockaddr_size);
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1864
    } while ((ret < 0) && (errno == EINTR));
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1865
    __END_INTERRUPTABLE__
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1866
#  endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1867
# else
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1868
    errno = 0;
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1869
    ret = bind(sock, (struct sockaddr *)&sa, sockaddr_size);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1870
# endif
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1871
    if (ret < 0) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  1872
# ifdef __win32__
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1873
        if (errno == 0) {
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1874
            errno = WSAGetLastError();
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1875
        }
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1876
# endif
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1877
        DBGPRINTF(("SOCKET: bind failed errno=%d\n", errno));
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1878
        error = __INST(lastErrorNumber) = __MKSMALLINT(errno);
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1879
        goto getOutOfHere;
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1880
    } else {
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1881
        ok = true;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1882
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1883
#endif /* NO_SOCKET */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1884
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1885
getOutOfHere: ;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1886
%}.
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  1887
    ok ifFalse:[
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1888
        |errorHolder errorString|
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1889
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1890
        error isInteger ifTrue:[
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1891
            errorHolder := OperatingSystem errorHolderForNumber:error.
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1892
            errorString := errorHolder errorString.
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1893
        ] ifFalse:[
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1894
            errorString := error.
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1895
        ].
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1896
        OpenError newException
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1897
            errorString:('cannot bind socket to address: %1 (%2)'
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1898
                            bindWith:socketAddress
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1899
                            with:errorString);
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1900
            errorCode:error;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1901
            osErrorHolder:errorHolder;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1902
            parameter:self;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1903
            raiseRequest.
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1904
        "maybe someone catches the error and binds to some other port..."
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1905
        ^ true.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1906
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1907
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1908
    port := socketAddress port.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1909
    port == 0 ifTrue:[
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1910
        "this is a bind to a random port, now we can get the real port"
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1911
        port := self getFullSocketAddress port.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1912
    ].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1913
    ^ true
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1914
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1915
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1916
     (Socket domain:#'AF_INET' type:#stream)
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1917
        bindTo:(IPSocketAddress anyHost port:445) reuseAddress:false;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1918
        yourself.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1919
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1920
     (Socket domain:#'AF_INET' type:#stream)
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1921
        bindTo:139 reuseAddress:false;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1922
        yourself.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1923
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1924
     (Socket domain:#'AF_INET6' type:#stream)
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1925
        bindTo:nil reuseAddress:false;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1926
        yourself.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1927
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1928
     (Socket domain:#'AF_INET' type:#stream)
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1929
        bindTo:(IPSocketAddress localHost port:2122) reuseAddress:false;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1930
        yourself.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1931
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1932
     (Socket domain:#'AF_UNIX' type:#stream)
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1933
        bindTo:nil reuseAddress:false;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  1934
        yourself.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1935
    "
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1936
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1937
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1938
listenFor:aNumber
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1939
    "start listening; return true if ok, false on error
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1940
     aNumber is the number of connect requests, that may be queued on the socket"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1941
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1942
    handle isNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1943
	^ self errorNotOpen
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1944
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1945
%{
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1946
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1947
    OBJ fp = __INST(handle);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1948
    SOCKET sock;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1949
    int ret;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1950
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1951
    if (! __isSmallInteger(aNumber)) {
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1952
	DBGPRINTF(("SOCKET: invalid arg\n"));
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1953
	RETURN (false);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1954
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1955
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1956
    sock = SOCKET_FROM_FILE_OBJECT(fp);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1957
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1958
#ifdef LISTEN_BLOCKS
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1959
# ifdef DO_WRAP_CALLS
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1960
    do {
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1961
	__threadErrno = 0;
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1962
	ret = STX_WSA_NOINT_CALL2("listen", listen, sock, __intVal(aNumber));
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1963
    } while ((ret < 0) && (__threadErrno == EINTR));
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1964
    if (ret < 0) {
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1965
	errno = __threadErrno;
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1966
    }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1967
# else
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1968
    __BEGIN_INTERRUPTABLE__
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1969
    do {
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1970
	ret = listen(sock, __intVal(aNumber));
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1971
    } while ((ret < 0) && (errno == EINTR));
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1972
    __END_INTERRUPTABLE__
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1973
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1974
#else
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1975
    errno = 0;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1976
    ret = listen(sock, __intVal(aNumber));
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1977
#endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1978
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1979
    if (ret < 0) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  1980
# ifdef __win32__
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1981
	if (errno == 0) {
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1982
	    errno = WSAGetLastError();
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1983
	}
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  1984
# endif
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1985
	DBGPRINTF(("SOCKET: listen call failed errno=%d\n", errno));
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1986
	__INST(lastErrorNumber) = __MKSMALLINT(errno);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1987
	RETURN (false);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1988
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1989
#else
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1990
    RETURN (false);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1991
#endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1992
%}.
3157
44f5f9f79256 initialize methods
Stefan Vogel <sv@exept.de>
parents: 3155
diff changeset
  1993
    listening := aNumber.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1994
    ^ true
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1995
! !
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1996
1576
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  1997
!Socket methodsFor:'closing'!
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  1998
4172
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  1999
abortAndClose
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2000
    "immediately abort the connection:
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2001
        discard buffered data and close the stream"
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2002
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2003
    self linger:0.     
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2004
    self close.         
3224
bad706c50347 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3223
diff changeset
  2005
!
bad706c50347 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3223
diff changeset
  2006
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2007
shutDown
4172
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2008
    "shutDown (initiate a graceful close) 
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2009
     and close (free the filedescriptor) the socket.
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2010
     The close will return immediately and buffered data will be sent in the 
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2011
     background, unless you set linger"
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2012
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2013
    self shutdown:2.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2014
    self close
1576
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  2015
!
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  2016
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2017
shutDownInput
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2018
    "shutDown the input side of the socket.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2019
     Any read on the socket will signal end-of-file from now on.
1839
c10cd58ec334 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1781
diff changeset
  2020
     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
  2021
     (e.g. setting the TCP-Windowsize to 0)"
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  2022
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  2023
    self shutdown:0.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2024
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2025
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2026
shutDownOutput
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2027
    "shutDown the output side of the socket.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2028
     Any write to the socket will signal end-of-file from now on.
4172
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2029
     An orderly release (TCP FIN) will be initiated after the last buffered data
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2030
     has been sent, so the other side will get a end-of-file condition eventually.
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2031
     If you set linger > 0, the operation will wait until buffered data 
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2032
     has been delivered to the peer.
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2033
     Otherwise the operation returns immediately."
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  2034
1884
ee0d640a25a7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1882
diff changeset
  2035
    self shutdown:1.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2036
! !
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2037
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2038
!Socket methodsFor:'connecting'!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2039
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2040
connectTo:aSocketAddress
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2041
    ^ self connectTo:aSocketAddress withTimeout:nil
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2042
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2043
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2044
connectTo:hostOrPathName port:portNrOrName
3655
eb81fa3d8407 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3633
diff changeset
  2045
    <resource: #obsolete>
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2046
    "Backward compatibility connect; connect to port, portNrOrNameOrNil on host, hostOrPathNameOrSocketAddr.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2047
     For backward compatibility, hostOrPathNameOrSocketAddr may be also a string or a byteArray,
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2048
     but it is recommended to pass SocketAddress instances.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2049
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2050
     Return true if ok, false otherwise.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2051
     The current process will block (but not the whole Smalltalk) until the connection is established.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2052
     See also: #connectTo:port:withTimeout: for a somewhat nicer interface."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2053
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2054
    ^ self connectTo:hostOrPathName port:portNrOrName withTimeout:nil
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2055
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2056
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2057
connectTo:hostOrPathNameOrSocketAddr port:portNrOrNameOrNil withTimeout:timeout
3655
eb81fa3d8407 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3633
diff changeset
  2058
    <resource: #obsolete>
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2059
    "Backward compatibility connect; connect to port, portNrOrNameOrNil on host, hostOrPathNameOrSocketAddr.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2060
     For backward compatibility, hostOrPathNameOrSocketAddr may be also a string or a byteArray,
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2061
     but it is recommended to pass SocketAddress instances.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2062
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2063
     Return true if ok, false otherwise.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2064
     The current process will block (but not the whole Smalltalk) until the connection is established,
4038
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2065
     or timeout milliseconds have passed."
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2066
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2067
    |domainClass socketAddress|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2068
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2069
    (hostOrPathNameOrSocketAddr isSocketAddress) ifTrue:[
4038
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2070
        socketAddress := hostOrPathNameOrSocketAddr.
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2071
        portNrOrNameOrNil notNil ifTrue:[
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2072
            socketAddress port:portNrOrNameOrNil.
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2073
        ].
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2074
    ] ifFalse:[
4038
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2075
        "backward compatibility: support for byteArray and string arg"
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2076
        domainClass := self class socketAddressClassForDomain:domain.
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2077
        domainClass isNil ifTrue:[
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2078
            ^ self error:'invalid (unsupported) domain'.
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2079
        ].
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2080
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2081
        hostOrPathNameOrSocketAddr isString ifTrue:[
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2082
            socketAddress := domainClass hostName:hostOrPathNameOrSocketAddr serviceName:portNrOrNameOrNil type:#SOCK_STREAM.
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2083
            peerName := hostOrPathNameOrSocketAddr.
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2084
        ] ifFalse:[
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2085
            hostOrPathNameOrSocketAddr isByteCollection ifFalse:[
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2086
                ^ self error:'connectTo: bad host (socketAddress) argument'
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2087
            ].
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2088
            socketAddress := domainClass hostAddress:hostOrPathNameOrSocketAddr port:portNrOrNameOrNil.
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2089
        ].
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2090
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2091
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2092
    ^ self connectTo:socketAddress withTimeout:timeout.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2093
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2094
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2095
       |sock|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2096
       sock := Socket newTCP.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2097
       sock connectTo:'localhost' port:21 withTimeout:1000.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2098
       sock
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2099
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2100
       |sock|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2101
       sock := Socket newTCP.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2102
       sock connectTo:'localhost' port:9876 withTimeout:2000.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2103
       sock
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2104
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2105
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2106
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2107
connectTo:aSocketAddress withTimeout:timeout
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2108
    "Connect to a SocketAddress.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2109
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2110
     Return true if ok, false otherwise.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2111
     The current process will block (but not the whole Smalltalk) until the connection is established,
4038
3ff1884f5acd #OTHER by mawalch
mawalch
parents: 4032
diff changeset
  2112
     or timeout milliseconds have passed."
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2113
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2114
    |isAsync err|
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2115
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2116
    handle isNil ifTrue:[
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2117
        ^ self errorNotOpen
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2118
    ].
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2119
    isAsync := false.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2120
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2121
%{  /* STACK: 100000 */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2122
#ifndef NO_SOCKET
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2123
//#define DUMP_ADDRESS 1
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2124
    OBJ fp = __INST(handle);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2125
    union sockaddr_u sa;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2126
    SOCKET sock;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2127
    int a;
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  2128
    INT ret;
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  2129
    int oldFlags;
3741
2a39a558012f mingw change
Claus Gittinger <cg@exept.de>
parents: 3657
diff changeset
  2130
# ifdef __MINGW64__
2a39a558012f mingw change
Claus Gittinger <cg@exept.de>
parents: 3657
diff changeset
  2131
    unsigned long on = 1;
2a39a558012f mingw change
Claus Gittinger <cg@exept.de>
parents: 3657
diff changeset
  2132
# else
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2133
    int on = 1;
3741
2a39a558012f mingw change
Claus Gittinger <cg@exept.de>
parents: 3657
diff changeset
  2134
#endif
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2135
    int sockaddr_size;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2136
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2137
    if (!__isNonNilObject(aSocketAddress) || !__isBytes(aSocketAddress)) {
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2138
        DBGPRINTF(("SOCKET: invalid socketAddress\n"));
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2139
        err = @symbol(argumentError);
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2140
        goto out;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2141
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2142
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2143
    {
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2144
        int sockAddrOffs = 0;
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2145
        int nIndex =__byteArraySize(aSocketAddress);
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2146
        OBJ cls = __qClass(aSocketAddress);
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2147
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2148
        //if (cls != @global(ByteArray))
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2149
        //    sockAddrOffs = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2150
        sockaddr_size = nIndex - sockAddrOffs;
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2151
        if (sockaddr_size > sizeof(sa)) {
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2152
            DBGPRINTF(("SOCKET: invalid (short) socketAddress\n"));
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2153
            err = @symbol(argumentError);
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2154
            goto out;
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2155
        }
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2156
        memcpy(&sa, __byteArrayVal(aSocketAddress) + sockAddrOffs, sockaddr_size);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2157
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2158
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2159
    sock = SOCKET_FROM_FILE_OBJECT(fp);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2160
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2161
#ifdef __win32__
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2162
     ioctlsocket(sock, FIONBIO, &on);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2163
#elif defined(O_NONBLOCK)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2164
    /*
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2165
     * set to non-blocking and wait later
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2166
     */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2167
    oldFlags = fcntl(sock, F_GETFL, 0);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2168
    /* on SUNOS4.x, would use fcntl(osfd, F_SETFL, flags | FNDELAY); */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2169
    fcntl(sock, F_SETFL, oldFlags | O_NONBLOCK);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2170
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2171
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2172
    /*
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2173
     * connect
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2174
     */
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2175
4039
87f0dc2e89d8 #OTHER by mawalch
mawalch
parents: 4038
diff changeset
  2176
// we do not use wrap calls any longer, because they have problems with aborting.
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2177
// we use nonblocking accept() instead.
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2178
# if 0 && defined(DO_WRAP_CALLS)
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  2179
    // __setWrapCallDebugging(1,1);
2414
886ecf4b36cd changed:
ab
parents: 2340
diff changeset
  2180
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2181
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2182
    do {
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2183
        DBGFPRINTF((stderr, "SOCKET: (sock=%d) connect...\n", sock));
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2184
        ret = STX_WSA_NOINT_CALL3("connect", connect, sock, &sa, (INT)sockaddr_size);
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2185
        DBGFPRINTF((stderr, "SOCKET: connect(%d) -> %"_ld_" (%d)\n", sock, (INT)ret, __threadErrno));
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2186
    } while ((ret < 0) && (__threadErrno == EINTR));
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2187
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2188
    if (ret < 0) {
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2189
        int optLen = sizeof(errno);
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2190
        errno = __threadErrno;
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2191
#if 0
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2192
        if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &errno, &optLen) == SOCKET_ERROR) {
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2193
            DBGFPRINTF((stderr, "SOCKET: getsockopt(SO_ERROR) failed: %d\n", WSAGetLastError()));
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2194
        }
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2195
#endif // 0
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2196
    }
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2197
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2198
    // __setWrapCallDebugging(1,0);
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2199
# else // !DO_WRAP_CALLS
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2200
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2201
#  if !defined(__win32__) && !defined(O_NONBLOCK)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2202
    __BEGIN_INTERRUPTABLE__
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2203
#  endif
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2204
    do {
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2205
        ret = connect(sock, (struct sockaddr *)&sa, sockaddr_size);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2206
    } while ((ret < 0)
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2207
#  ifdef __win32__
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2208
             && (errno = WSAGetLastError())
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2209
#  endif
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2210
             && ((errno == EINTR)
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2211
#  ifdef EAGAIN
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2212
                 || (errno == EAGAIN)
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2213
#  endif
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2214
                ));
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2215
#  if !defined(__win32__) && !defined(O_NONBLOCK)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2216
    __END_INTERRUPTABLE__
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2217
#  endif
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2218
#endif
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2219
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2220
#if defined(__win32__) && !defined(EINPROGRESS)
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2221
# define EINPROGRESSS WSAEINPROGRESSS
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2222
# define EALREADY WSAEALREADY
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2223
#endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2224
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2225
    if (ret < 0) {
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2226
# if defined(EINPROGRESS) || defined(EALREADY)
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2227
        if (0
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2228
#  ifdef __win32__
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2229
            || (errno == WSAEWOULDBLOCK)
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2230
#  endif
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2231
#  ifdef EINPROGRESS
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2232
            || (errno == EINPROGRESS)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2233
#  endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2234
#  ifdef EALREADY
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2235
            || (errno == EALREADY)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2236
#  endif
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2237
        ) {
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2238
            /*
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2239
             * This was a nonblocking operation that will take some time.
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2240
             * Do a select on read to get informed when the operation is ready.
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2241
             */
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2242
            DBGFPRINTF((stderr, "SOCKET: isAsync is true\n"));
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2243
            isAsync = true;
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2244
        } else
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2245
# endif /* EINPROGRESS or EALREADY */
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2246
        {
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2247
            DBGFPRINTF((stderr, "SOCKET: connect failed ret=%"_ld_" errno=%d __threadErrno=%d\n",
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2248
                        (INT)ret, errno, __threadErrno ));
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2249
# ifdef DUMP_ADDRESS
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2250
            {
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2251
                unsigned char *cp = (unsigned char *)(&sa);
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2252
                int i;
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2253
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2254
                console_printf("address data:\n");
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2255
                for (i=0; i<sockaddr_size; i++) {
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2256
                    console_printf(" %02x\n", *cp++);
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2257
                }
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2258
            }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2259
# endif
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2260
            err = __MKSMALLINT(errno);
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2261
        }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2262
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2263
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2264
# ifdef __win32__
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2265
    {
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2266
        int off = 0;
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2267
        ioctlsocket(sock, FIONBIO, &off);
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2268
    }
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2269
# elif defined(O_NONBLOCK) // Linux / Unix
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2270
    fcntl(sock, F_SETFL, oldFlags);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2271
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2272
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2273
# else /* NO_SOCKET */
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2274
    err = @symbol(notSupported);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2275
# endif /* NO_SOCKET */
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2276
out:;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2277
%}.
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  2278
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2279
    err notNil ifTrue:[
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2280
        err isSymbol ifTrue:[
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2281
            self primitiveFailed:err.
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2282
        ].
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2283
        lastErrorNumber := err.
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2284
        ^ false.
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2285
        "/ Once we will raise an exception instead of returning false (and have to change some code above):
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2286
"/        (OperatingSystem errorHolderForNumber:err) reportError.
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2287
    ].
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2288
    isAsync ifTrue:[
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2289
        (self writeExceptionWaitWithTimeoutMs:timeout) ifTrue:[
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2290
            "/ a timeout occurred
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2291
            "/ should I cancel the connect?
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2292
            lastErrorNumber := OperatingSystem errorNumberFor:#ETIMEDOUT.
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2293
            ^ false.
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2294
        ].
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2295
        err := self getSocketError.
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2296
        err ~~ 0 ifTrue:[
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2297
            lastErrorNumber := err.
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2298
            ^ false.
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2299
        ].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2300
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2301
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
  2302
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2303
    peer := aSocketAddress.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2304
    port isNil ifTrue:[
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2305
        "socket has not been explicitly bound,
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2306
         after connect it has been bound implicitly - fetch the port"
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  2307
        port := self getFullSocketAddress port.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2308
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2309
    ^ true
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2310
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2311
    "
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2312
       |sock|
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2313
       sock := Socket newTCP.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2314
       sock connectTo:(IPSocketAddress localHost port:21) withTimeout:1000.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2315
       sock
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2316
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2317
       |sock|
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2318
       sock := Socket newTCP.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2319
       sock connectTo:(IPSocketAddress localHost port:9876) withTimeout:2000.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2320
       sock
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2321
    "
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2322
! !
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2323
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2324
!Socket methodsFor:'datagram transmission'!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2325
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2326
receiveBuffer:aDataBuffer start:startIndex for:nBytes
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2327
    "receive data
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2328
     Return the number of bytes received, or a negative number on error.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2329
     On error, the unix error code is left in the lastErrorNumber
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2330
     instance variable.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2331
     The thread blocks until data arrives - you may want to wait before
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2332
     receiving, using #readWait or #readWaitWithTimeout:."
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2333
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2334
    |nReceived error|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2335
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2336
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2337
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2338
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2339
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2340
    if (fp != nil) {
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2341
	SOCKET sock;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2342
	INT objSize, offs;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2343
	INT n;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2344
	char *extPtr;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2345
	unsigned char *buffer;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2346
	unsigned char *allocatedBuffer = NULL;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2347
	INT flags = 0;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2348
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2349
	sock = SOCKET_FROM_FILE_OBJECT(fp);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2350
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2351
	if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2352
	if (__isSmallInteger(nBytes)) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2353
	    if (__intVal(nBytes) < objSize) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2354
		objSize = __intVal(nBytes);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2355
	    }
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2356
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2357
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2358
# ifdef DO_WRAP_CALLS
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2359
	if (extPtr) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2360
	    buffer = extPtr + offs;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2361
	} else {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2362
	    allocatedBuffer = buffer = (char *)malloc(objSize);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2363
	}
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2364
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2365
	do {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2366
	    __threadErrno = 0;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2367
	    n = (INT)STX_WSA_NOINT_CALL4("recv", recv, sock, buffer, objSize, flags);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2368
	} while ((n < 0) && (__threadErrno == EINTR));
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2369
	if (n < 0) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2370
	    errno = __threadErrno;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2371
	}
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2372
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2373
	if (allocatedBuffer) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2374
	    if (n > 0) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2375
		bcopy(allocatedBuffer, (char *)__InstPtr(aDataBuffer) + offs, n);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2376
	    }
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2377
	    free(allocatedBuffer);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2378
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2379
# else
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2380
	__BEGIN_INTERRUPTABLE__
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2381
	do {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2382
	    if (extPtr) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2383
		n = recv(sock, extPtr + offs, objSize, flags);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2384
	    } else {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2385
		n = recv(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, flags);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2386
	    }
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2387
	} while ((n < 0) && (errno == EINTR));
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2388
	__END_INTERRUPTABLE__
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2389
# endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2390
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2391
	if (n < 0) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2392
	    error = __INST(lastErrorNumber) = __MKSMALLINT(errno);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2393
	} else {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2394
	    RETURN(__MKSMALLINT(n));
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2395
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2396
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2397
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2398
bad: ;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2399
%}.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2400
    error notNil ifTrue:[
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2401
	^ self readError:error.
915
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2402
    ].
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2403
    "
1282
7e4ffdfd19f6 remove historic leftover
Claus Gittinger <cg@exept.de>
parents: 1280
diff changeset
  2404
     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
  2405
    "
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2406
    self primitiveFailed
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2407
!
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2408
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2409
receiveFrom:anAddressBuffer buffer:aDataBuffer
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2410
    "receive datagramm data - put address of originating host into
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2411
     anAddressBuffer, data into aBuffer.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2412
     Both must be ByteArray-like. The addressBuffer must
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2413
     provide space for a valid address for my domain (i.e. for inet, a 4-byte byteArray).
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2414
     Return the number of bytes received, or a negative number on error.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2415
     On error, the unix error code is left in the lastErrorNumber
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  2416
     instance variable."
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  2417
3615
ffe86e1b14ed #BUGFIX
Stefan Vogel <sv@exept.de>
parents: 3614
diff changeset
  2418
    ^ self receiveFrom:anAddressBuffer buffer:aDataBuffer start:1 for:(aDataBuffer size) flags:0
99
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
  2419
!
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
  2420
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2421
receiveFrom:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes
3615
ffe86e1b14ed #BUGFIX
Stefan Vogel <sv@exept.de>
parents: 3614
diff changeset
  2422
    ^ self receiveFrom:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes flags:0
ffe86e1b14ed #BUGFIX
Stefan Vogel <sv@exept.de>
parents: 3614
diff changeset
  2423
!
ffe86e1b14ed #BUGFIX
Stefan Vogel <sv@exept.de>
parents: 3614
diff changeset
  2424
ffe86e1b14ed #BUGFIX
Stefan Vogel <sv@exept.de>
parents: 3614
diff changeset
  2425
receiveFrom:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes flags:flags
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2426
    "receive datagramm data
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2427
     - put address of originating host into anAddressBuffer, data into aBuffer.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2428
     For backward compatibility, the addressBuffer may be a non-SocketAddress;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2429
     then, it must be a byteArray with appropriate size for the addressBytes.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2430
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2431
     Return the number of bytes received, or a negative number on error.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2432
     On error, the unix error code is left in the lastErrorNumber
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2433
     instance variable.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2434
     The thread blocks until data arrives - you may want to wait before
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2435
     receiving, using #readWait or #readWaitWithTimeout:."
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2436
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2437
    |domainClass addr addrLen nReceived error|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2438
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2439
    domainClass := self class socketAddressClassForDomain:domain.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2440
    domainClass isNil ifTrue:[
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2441
        ^ self error:'invalid (unsupported) domain'.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2442
    ].
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2443
    anAddressBuffer isSocketAddress ifTrue:[
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2444
        anAddressBuffer class == domainClass ifFalse:[
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2445
            ^ self error:'addressBuffer class mismatch (domain)'.
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2446
        ].
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2447
        addr := anAddressBuffer.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2448
    ] ifFalse:[
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2449
        anAddressBuffer notNil ifTrue:[
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2450
            addr := domainClass new.
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2451
        ].
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2452
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2453
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2454
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2455
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2456
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2457
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2458
    if (fp != nil) {
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2459
        SOCKET sock;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2460
        size_t objSize;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2461
        union sockaddr_u sa;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2462
        socklen_t alen = 0;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2463
        INT n, offs;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2464
        int _flags = __longIntVal(flags);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2465
        char *extPtr;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2466
        unsigned char *allocatedBuffer = NULL, *buffer = NULL;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2467
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2468
        sock = SOCKET_FROM_FILE_OBJECT(fp);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2469
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2470
        if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2471
        if (__isSmallInteger(nBytes)) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2472
            if (__intVal(nBytes) < objSize) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2473
                objSize = __intVal(nBytes);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2474
            }
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2475
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2476
# ifdef DO_WRAP_CALLS
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2477
        if (extPtr) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2478
            buffer = extPtr + offs;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2479
        } else {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2480
            allocatedBuffer = buffer = (char *)malloc(objSize);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2481
        }
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2482
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2483
        do {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2484
            __threadErrno = 0;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2485
            alen = sizeof(sa);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2486
            n = (INT)STX_WSA_NOINT_CALL6("recvfrom", recvfrom, sock, buffer, objSize, _flags, (struct sockaddr *)&sa, &alen);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2487
        } while ((n < 0) && (__threadErrno == EINTR));
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2488
        if (n < 0) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2489
            errno = __threadErrno;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2490
        }
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2491
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2492
        if (allocatedBuffer) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2493
            if (n > 0) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2494
                memcpy((char *)__InstPtr(aDataBuffer) + offs, allocatedBuffer, n);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2495
            }
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2496
            free(allocatedBuffer);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2497
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2498
# else
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2499
        __BEGIN_INTERRUPTABLE__
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2500
        do {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2501
            alen = sizeof(sa);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2502
            if (extPtr) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2503
                n = recvfrom(sock, extPtr + offs, objSize, _flags, (struct sockaddr *) &sa, &alen);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2504
            } else {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2505
                n = recvfrom(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, _flags, (struct sockaddr *) &sa, &alen);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2506
            }
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2507
        } while ((n < 0) && (errno == EINTR));
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2508
        __END_INTERRUPTABLE__
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2509
# endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2510
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2511
        if (n >= 0) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2512
            if (__isNonNilObject(addr)) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2513
                char *addrPtr;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2514
                OBJ oClass;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2515
                int nInstVars, nInstBytes, objSize;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2516
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2517
                oClass = __qClass(addr);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2518
                if (! __isBytes(addr) )
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2519
                    goto bad;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2520
                nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2521
                nInstBytes = OHDR_SIZE + (nInstVars * sizeof(OBJ));
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2522
                objSize = __qSize(addr) - nInstBytes;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2523
                addrPtr = (char *)__InstPtr(addr) + nInstBytes;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2524
                if (objSize < alen)
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2525
                    goto bad;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2526
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2527
                /*
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2528
                 * extract the datagrams address
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2529
                 */
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2530
                memcpy(addrPtr, (char *)&sa, alen);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2531
                addrLen = __MKSMALLINT(alen);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2532
            }
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2533
        }
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2534
        if (n < 0) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2535
            error = __INST(lastErrorNumber) = __MKSMALLINT(errno);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2536
        }
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2537
        nReceived = __MKSMALLINT(n);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2538
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2539
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2540
bad: ;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2541
%}.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2542
    error notNil ifTrue:[
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2543
        ^ self readError:error.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2544
    ].
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2545
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2546
    nReceived notNil ifTrue:[
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2547
        addrLen notNil ifTrue:[
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2548
            (addr == anAddressBuffer) ifFalse:[
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2549
                self obsoleteFeatureWarning:'please use a socketAddress argument'.
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2550
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2551
                "can be a ByteArray for backward compatibility"
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2552
                anAddressBuffer replaceFrom:1 to:addrLen with:(addr hostAddress).
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2553
            ].
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2554
        ].
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2555
        ^ nReceived
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2556
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2557
    "
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2558
     arrive here if you try to receive into an invalid buffer
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2559
     (i.e. not ByteArray-like),
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  2560
     or if the addressBuffer is nonNil AND not a SocketAddress/ByteArray
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  2561
     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
  2562
    "
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  2563
    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
  2564
!
7789fb1c9fa2 more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents: 115
diff changeset
  2565
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2566
sendBuffer:aDataBuffer start:startIndex for:nBytes flags:flags
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2567
    "send data.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2568
     Both must be ByteArray-like. The bytes in the addressBuffer must
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2569
     be a valid address for my domain (i.e. for inet, a 4-byte byteArray).
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2570
     Return the number of bytes transmitted, or a negative number on error.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2571
     On error, the unix error code is left in the lastErrorNumber
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2572
     instance variable."
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2573
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2574
    |error|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2575
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2576
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2577
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2578
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2579
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2580
    if ((fp != nil)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2581
     && __isSmallInteger(startIndex)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2582
     && __isSmallInteger(nBytes)) {
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2583
	SOCKET sock;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2584
	INT objSize, n, offs;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2585
	char *extPtr;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2586
	int _flags = __longIntVal(flags);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2587
	unsigned long norder;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2588
	unsigned char *buffer, *allocatedBuffer = NULL;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2589
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2590
	sock = SOCKET_FROM_FILE_OBJECT(fp);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2591
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2592
	if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2593
	if (__isSmallInteger(nBytes)) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2594
	    if (__intVal(nBytes) < objSize) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2595
		objSize = __intVal(nBytes);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2596
	    }
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2597
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2598
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2599
# ifdef DGRAM_DEBUG
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2600
	console_printf("sending %d bytes ...\n", nBytes);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2601
# endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2602
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2603
#ifdef DO_WRAP_CALLS
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2604
	if (extPtr) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2605
	    buffer = extPtr + offs;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2606
	} else {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2607
	    allocatedBuffer = buffer = (char *)malloc(objSize);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2608
	    bcopy((char *)__InstPtr(aDataBuffer) + offs, allocatedBuffer, objSize);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2609
	}
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2610
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2611
	do {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2612
	    __threadErrno = 0;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2613
	    n = (INT)STX_WSA_NOINT_CALL4("send", send, sock, buffer, objSize, _flags);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2614
	} while ((n < 0) && (__threadErrno == EINTR));
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2615
	if (n < 0) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2616
	    errno = __threadErrno;
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2617
	}
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2618
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2619
	if (allocatedBuffer) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2620
	    free(allocatedBuffer);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2621
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2622
#else
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2623
	__BEGIN_INTERRUPTABLE__
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2624
	do {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2625
	    if (extPtr) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2626
		n = send(sock, extPtr + offs, objSize, _flags);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2627
	    } else {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2628
		n = send(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, _flags);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2629
	    }
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2630
	} while ((n < 0) && (errno == EINTR));
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2631
	__END_INTERRUPTABLE__
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2632
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2633
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2634
	if (n < 0) {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2635
	    error = __INST(lastErrorNumber) = __MKSMALLINT(errno);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2636
	} else {
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2637
	    RETURN (__MKSMALLINT(n));
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2638
	}
915
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2639
    }
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2640
#endif
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2641
bad: ;
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2642
%}.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2643
    error notNil ifTrue:[
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2644
	self writeError:error.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2645
    ].
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2646
915
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2647
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  2648
     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
  2649
    "
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2650
    self primitiveFailed
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2651
!
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2652
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2653
sendTo:anAddressBuffer buffer:buffer
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2654
    "send datagramm data - fetch address of destination host from
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2655
     anAddressBuffer, data from aDataBuffer.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2656
     Both must be ByteArray-like. The bytes in the addressBuffer must
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2657
     be a valid address for my domain (i.e. for inet, a 4-byte byteArray).
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2658
     Return the number of bytes transmitted, or a negative number on error.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2659
     On error, the unix error code is left in the lastErrorNumber
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  2660
     instance variable.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  2661
     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
  2662
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  2663
    ^ 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
  2664
!
7789fb1c9fa2 more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents: 115
diff changeset
  2665
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2666
sendTo:anAddressBuffer buffer:buffer start:startIndex for:count
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2667
    "send datagramm data - fetch address of destination host from
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2668
     anAddressBuffer, data from aDataBuffer.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2669
     Both must be ByteArray-like. The bytes in the addressBuffer must
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2670
     be a valid address for my domain (i.e. for inet, a 4-byte byteArray).
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2671
     Return the number of bytes transmitted, or a negative number on error.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2672
     On error, the unix error code is left in the lastErrorNumber
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2673
     instance variable.
817
eda0065cbd47 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 814
diff changeset
  2674
     Flags is currently ignored; it is there for ST-80 compatibility."
eda0065cbd47 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 814
diff changeset
  2675
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  2676
    ^ self sendTo:anAddressBuffer buffer:buffer start:startIndex for:count flags:0
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2677
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2678
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2679
sendTo:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes flags:flags
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2680
    "send datagramm data - fetch address of destination host from
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2681
     anAddressBuffer, data from aDataBuffer starting at startIndex,
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2682
     sending count bytes.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2683
     Both must be ByteArray-like. The bytes in the addressBuffer must
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2684
     be a valid address for my domain (i.e. for inet, a 4-byte byteArray).
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2685
     Return the number of bytes transmitted, or a negative number on error.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2686
     On error, the unix error code is left in the lastErrorNumber
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2687
     instance variable."
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2688
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2689
    |domainClass addr error|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2690
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2691
    anAddressBuffer isSocketAddress ifTrue:[
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2692
        addr := anAddressBuffer.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2693
    ] ifFalse:[
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2694
        anAddressBuffer isByteArray ifFalse:[
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2695
            ^ self error:'bad socketAddress argument'
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2696
        ].
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2697
        domainClass := self class socketAddressClassForDomain:domain.
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2698
        domainClass isNil ifTrue:[
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2699
            ^ self error:'invalid (unsupported) domain'.
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2700
        ].
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2701
        addr := domainClass hostAddress:anAddressBuffer.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2702
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2703
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2704
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2705
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2706
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2707
    if ((fp != nil)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2708
     && __isSmallInteger(startIndex)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2709
     && __isSmallInteger(nBytes)) {
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2710
        SOCKET sock;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2711
        INT objSize;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2712
        struct sockaddr *sockaddr_ptr;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2713
        union sockaddr_u sa;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2714
        socklen_t sockaddr_size, alen = 0;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2715
        INT sockAddrOffs;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2716
        INT n, offs;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2717
        char *extPtr;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2718
        int _flags = __longIntVal(flags);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2719
        unsigned long norder;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2720
        unsigned char *buffer;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2721
        unsigned char *allocatedBuffer = NULL;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2722
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2723
        sock = SOCKET_FROM_FILE_OBJECT(fp);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2724
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2725
        if (! __isBytes(addr)) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2726
            sockaddr_size = 0;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2727
            sockaddr_ptr = (struct sockaddr *)0;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2728
        } else {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2729
            int nIndex;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2730
            OBJ cls;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2731
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2732
            sockAddrOffs = 0;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2733
            if ((cls = __qClass(addr)) != @global(ByteArray))
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2734
                sockAddrOffs += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2735
            nIndex = __qSize(addr) - OHDR_SIZE;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2736
            sockaddr_size = nIndex - sockAddrOffs;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2737
            if (sockaddr_size > sizeof(sa)) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2738
                console_fprintf(stderr, "Socket [warning]: bad socketAddr\n");
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2739
                goto bad;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2740
            }
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2741
            memcpy(&sa, (__byteArrayVal(addr) + sockAddrOffs), sockaddr_size);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2742
            sockaddr_ptr = (struct sockaddr *)(&sa);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2743
        }
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2744
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2745
        if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2746
        if (__isSmallInteger(nBytes)) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2747
            if (__intVal(nBytes) < objSize) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2748
                objSize = __intVal(nBytes);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2749
            }
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2750
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2751
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2752
#ifdef DO_WRAP_CALLS
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2753
        if (extPtr) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2754
            buffer = extPtr + offs;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2755
        } else {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2756
            allocatedBuffer = buffer = (char *)malloc(objSize);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2757
            bcopy((char *)__InstPtr(aDataBuffer) + offs, allocatedBuffer, objSize);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2758
        }
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2759
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2760
        do {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2761
            __threadErrno = 0;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2762
            n = (INT)STX_WSA_NOINT_CALL6("sendto", sendto, sock, buffer, objSize, _flags, sockaddr_ptr, sockaddr_size);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2763
        } while ((n < 0) && (__threadErrno == EINTR));
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2764
        if (n < 0) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2765
            errno = __threadErrno;
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2766
        }
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2767
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2768
        if (allocatedBuffer) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2769
            free(allocatedBuffer);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2770
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2771
#else
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2772
        __BEGIN_INTERRUPTABLE__
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2773
        do {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2774
            if (extPtr) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2775
                n = sendto(sock, extPtr + offs, objSize, _flags, sockaddr_ptr, sockaddr_size);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2776
            } else {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2777
                n = sendto(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, _flags, sockaddr_ptr, sockaddr_size);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2778
            }
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2779
        } while ((n < 0) && (errno == EINTR));
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2780
        __END_INTERRUPTABLE__
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2781
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2782
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2783
        if (n < 0) {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2784
            error = __INST(lastErrorNumber) = __MKSMALLINT(errno);
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2785
        } else {
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2786
            RETURN (__MKSMALLINT(n));
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2787
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2788
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2789
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2790
bad: ;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2791
%}.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2792
    error notNil ifTrue:[
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2793
        self writeError:error.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2794
    ].
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2795
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2796
    "
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2797
     arrive here if you try to send from an invalid buffer
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2798
     (i.e. not ByteArray-like),
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2799
     or if the addressBuffer is nonNil AND not a ByteArray/String
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2800
     or if the addressBuffer is nonNil AND too small.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2801
    "
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2802
    self primitiveFailed
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  2803
! !
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  2804
4172
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2805
!Socket methodsFor:'finalization'!
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2806
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2807
finalize
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2808
    self linger:0.      "/ do an abortive release - discard buffered data
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2809
    self closeFile.     "/ does not wait due to abortive release.
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2810
! !
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2811
3484
ccd8683e4e7e class: Socket
Claus Gittinger <cg@exept.de>
parents: 3481
diff changeset
  2812
!Socket methodsFor:'initialization'!
ccd8683e4e7e class: Socket
Claus Gittinger <cg@exept.de>
parents: 3481
diff changeset
  2813
ccd8683e4e7e class: Socket
Claus Gittinger <cg@exept.de>
parents: 3481
diff changeset
  2814
initialize
ccd8683e4e7e class: Socket
Claus Gittinger <cg@exept.de>
parents: 3481
diff changeset
  2815
    super initialize.
ccd8683e4e7e class: Socket
Claus Gittinger <cg@exept.de>
parents: 3481
diff changeset
  2816
    "/ transparent
ccd8683e4e7e class: Socket
Claus Gittinger <cg@exept.de>
parents: 3481
diff changeset
  2817
    eolMode := nil.
ccd8683e4e7e class: Socket
Claus Gittinger <cg@exept.de>
parents: 3481
diff changeset
  2818
! !
ccd8683e4e7e class: Socket
Claus Gittinger <cg@exept.de>
parents: 3481
diff changeset
  2819
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2820
!Socket methodsFor:'low level'!
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2821
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2822
getSocketAdress
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2823
    "BAD SPELLING, of #getSocketAddress, kept for compatibility with swazoo"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2824
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2825
    <resource: #obsolete>
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2826
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2827
    ^ self getSocketAddress
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2828
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2829
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2830
getSocketError
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2831
    "get the SO_ERROR form the socket, which indicates the
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2832
     result of an asynchronous operation"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2833
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2834
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2835
#ifndef NO_SOCKET
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2836
    OBJ fp;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2837
    int err;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2838
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2839
    fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2840
    if (fp == nil) {
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2841
	err = EBADF;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2842
    } else {
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2843
	unsigned int sz;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2844
	SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2845
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2846
	sock = SOCKET_FROM_FILE_OBJECT(fp);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2847
	sz = sizeof(err);
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  2848
	if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)(&err), &sz) < 0) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2849
# ifdef __win32__
1568
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  2850
	    errno = WSAGetLastError();
1558
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2851
# endif
1568
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  2852
	    err = errno;
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  2853
	}
1558
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2854
    }
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2855
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2856
    RETURN(__MKSMALLINT(err));
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2857
#endif
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2858
%}
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2859
!
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  2860
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2861
listenWithBacklog:aNumber
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2862
    <resource: #obsolete>
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2863
    "same as listenFor: - backward compatibility with old ST/X"
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2864
    self obsoleteMethodWarning.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2865
    ^ self listenFor:aNumber
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2866
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2867
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2868
primAcceptOn:aServerSocket blocking:blocking
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2869
    "accept a connection on a server port (created with:'Socket>>newTCP:')
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2870
     usage is: (Socket basicNew acceptOn:(Socket newTCP:9999)).
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2871
     Return the true if ok; false if not.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2872
     If blocking is true, the accept() syscall (and the whole smalltalk image)
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2873
     will block, until a connection is accepted.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2874
     If blocking is false, this call will return immediately, if there is no connection pending."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2875
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2876
    |serverSocketHandle addr domainClass newHandle|
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2877
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2878
    handle notNil ifTrue:[
4180
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2879
        ^ self errorAlreadyOpen.
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2880
    ].
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2881
    serverSocketHandle := aServerSocket fileHandle.
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2882
    serverSocketHandle isNil ifTrue:[
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2883
        "socket is not open"
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2884
        ^ false
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2885
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2886
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2887
    domain := aServerSocket domain.
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2888
    socketType := aServerSocket type.
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2889
    handleType := aServerSocket handleType.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2890
    "unix domain sockets do not return a valid peer name on accept"
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2891
    domainClass := self class socketAddressClassForDomain:domain.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2892
    domainClass isNil ifTrue:[
4180
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2893
        ^ self error:'invalid (unsupported) domain'.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2894
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2895
    addr := domainClass new.
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2896
    newHandle := OperatingSystem socketAccessor new.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2897
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2898
%{  /* STACK: 100000 */
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2899
#ifndef NO_SOCKET
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2900
    FILE *fp;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2901
    int flags;
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2902
    SOCKET serverSocket, newSock;
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2903
    int _fd;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2904
    union sockaddr_u sa;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2905
    unsigned int alen;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2906
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2907
# ifdef __win32__
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2908
    serverSocket = SOCKET_FROM_FILE_OBJECT(serverSocketHandle);
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2909
# else
3247
259f71b89b6e class: Socket
Stefan Vogel <sv@exept.de>
parents: 3246
diff changeset
  2910
    serverSocket = __intVal(serverSocketHandle);
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2911
# endif
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2912
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2913
# if defined(O_NONBLOCK) && defined(SET_NDELAY)
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2914
    if (blocking == false) {
4180
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2915
        flags = fcntl(serverSocket, F_GETFL);
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2916
        fcntl(serverSocket, F_SETFL, flags | O_NONBLOCK);
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2917
    }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2918
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2919
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2920
# ifdef DO_WRAP_CALLS
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2921
    do {
4180
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2922
        __threadErrno = 0;
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2923
        alen = sizeof(sa);
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2924
        newSock = (SOCKET)STX_WSA_CALL3("accept", accept, serverSocket, &sa, &alen);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2925
    } while ((newSock < 0) && (__threadErrno == EINTR));
3127
333e87ba225a class: Socket
Stefan Vogel <sv@exept.de>
parents: 3126
diff changeset
  2926
    if (newSock < 0) {
4180
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2927
        errno = __threadErrno;
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2928
    }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2929
# else
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2930
    __BEGIN_INTERRUPTABLE__
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2931
    do {
4180
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2932
        alen = sizeof(sa);
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2933
        newSock = accept(serverSocket, (struct sockaddr *) &sa, &alen);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2934
    } while ((newSock < 0) && (errno == EINTR));
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2935
    __END_INTERRUPTABLE__
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2936
# endif
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2937
    DBGFPRINTF((stderr, "SOCKET: accept newSock=%d\n", newSock));
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2938
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2939
# if defined(O_NDELAY) && defined(SET_NDELAY)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2940
    if (blocking == false) {
4180
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2941
        fcntl(serverSocket, F_SETFL, flags);
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2942
    }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2943
# endif
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2944
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2945
    if (newSock == -1) {
4180
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2946
        DBGPRINTF(("SOCKET: accept call failed errno=%d\n", errno));
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2947
        __INST(lastErrorNumber) = __MKSMALLINT(errno);
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2948
        RETURN (false);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2949
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2950
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2951
    if (__isNonNilObject(addr)) {
4180
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2952
        OBJ oClass = __qClass(addr);
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2953
        int nInstVars, nInstBytes, objSize;
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2954
        char *addrP;
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2955
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2956
        if (! __isBytes(addr) ) {
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2957
            DBGPRINTF(("SOCKET: bad addr\n"));
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2958
            closesocket(newSock);
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2959
            RETURN (false);
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2960
        }
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2961
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2962
        nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2963
        nInstBytes = OHDR_SIZE + (nInstVars * sizeof(OBJ));
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2964
        objSize = __qSize(addr) - nInstBytes;
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2965
        addrP = (char *)__InstPtr(addr) + nInstBytes;
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2966
        if (objSize < alen) {
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2967
            DBGPRINTF(("SOCKET: bad addr\n"));
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2968
            closesocket(newSock);
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2969
            RETURN (false);
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2970
        }
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2971
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2972
        /*
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2973
         * extract the partners address
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2974
         */
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2975
        memcpy(addrP, (char *)&sa, alen);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2976
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2977
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2978
    /*
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2979
     * make it a FILE *
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2980
     */
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2981
# ifdef __win32__
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2982
#  if 0 && (defined( __BORLANDC__ ) || defined( __MINGW__ ))
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2983
    __stxWrapApiEnterCritical();
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2984
    _fd = _open_osfhandle((long)newSock, 0);
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2985
    __stxWrapApiLeaveCritical();
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2986
    DBGPRINTF(("SOCKET: sock=%d fd=%d\n", newSock, _fd));
2773
ab3049f4a351 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2748
diff changeset
  2987
#  else
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  2988
    _fd = (int)newSock;
2773
ab3049f4a351 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2748
diff changeset
  2989
#  endif
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2990
# else // ! __win32__
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2991
    fp = fdopen(newSock, "r+");
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2992
    if (! fp) {
4180
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2993
        DBGPRINTF(("SOCKET: fdopen call failed\n"));
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2994
        __INST(lastErrorNumber) = __MKSMALLINT(errno);
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2995
        closesocket(newSock);
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2996
        DBGFPRINTF((stderr, "SOCKET: close (fdopen failed) (%d)\n", newSock));
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  2997
        RETURN (false);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2998
    }
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2999
# endif // ! __win32__
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3000
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3001
    if ((@global(FileOpenTrace) == true) || __debugging__) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3002
# ifdef __win32__
4180
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  3003
        console_fprintf(stderr, "fdopen [Socket accept] -> fd: %d (H: %"_lx_")\n", _fd, (INT)newSock);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3004
# else
4180
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  3005
        console_fprintf(stderr, "fdopen [Socket accept] -> %"_lx_" (fd: %d)\n", (INT)fp, newSock);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3006
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3007
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3008
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3009
# ifdef __win32__
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3010
    __externalAddressVal(newHandle) = _fd;
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3011
    __INST(handleType) = @symbol(socketHandle);
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3012
# else
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3013
    __externalAddressVal(newHandle) = fp;
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3014
    __INST(handleType) = @symbol(socketFilePointer);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3015
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3016
#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
  3017
%}.
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3018
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3019
    handle := newHandle.
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3020
    buffered := false.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3021
    mode := #readwrite.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3022
    binary := false.
3620
c648d063bccc #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3617
diff changeset
  3023
    self registerForFinalization.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3024
    peer := addr.
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3025
    port := aServerSocket port.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3026
1558
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  3027
    ^ true
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  3028
!
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3029
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3030
setSocketOption:option argument:arg1 argument:arg2
3513
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3031
    |ok error|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3032
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  3033
    handle isNil ifTrue:[
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3034
	^ self errorNotOpen
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  3035
    ].
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  3036
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3037
%{  /* STACK: 32000 */
2818
94d67df5c00d compilable with tcc
Claus Gittinger <cg@exept.de>
parents: 2773
diff changeset
  3038
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3039
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3040
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3041
    if (fp != nil) {
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3042
	SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3043
	int opt = -1;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3044
	int level = -1;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3045
	int usize = -1;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3046
	int ret;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3047
	union u {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3048
	    BOOL        u_bool;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3049
	    int         u_int;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3050
	    struct linger  u_linger;
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3051
# ifdef IP_ADD_MEMBERSHIP
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3052
	    struct ip_mreq u_ip_mreq;
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3053
# endif
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3054
# if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3055
	    struct timeval u_tv;
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3056
# endif
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3057
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3058
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3059
	} u;
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3060
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3061
# ifdef IP_ADD_MEMBERSHIP
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3062
	if (option == @symbol(IP_DROP_MEMBERSHIP)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3063
	    opt = IP_DROP_MEMBERSHIP;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3064
	    goto dropOrAdd;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3065
	}
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3066
	if (option == @symbol(IP_ADD_MEMBERSHIP)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3067
	    /* add membership to a multicast group */
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3068
	    opt = IP_ADD_MEMBERSHIP;
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3069
dropOrAdd:
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3070
	    level = IPPROTO_IP;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3071
	    usize = sizeof(u.u_ip_mreq);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3072
	    if (__isByteArrayLike(arg1) && __isByteArrayLike(arg2)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3073
		memcpy(&u.u_ip_mreq.imr_multiaddr, __byteArrayVal(arg1), sizeof(u.u_ip_mreq.imr_multiaddr));
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3074
		memcpy(&u.u_ip_mreq.imr_interface, __byteArrayVal(arg2), sizeof(u.u_ip_mreq.imr_interface));
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3075
	    }
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3076
	    else
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3077
		goto argError;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3078
	}
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3079
# endif /* IP_ADD_MEMBERSHIP */
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3080
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3081
# ifdef SO_BROADCAST
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3082
	if (option == @symbol(SO_BROADCAST)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3083
	    /* Enables transmission and receipt of broadcast messages on the socket. */
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3084
	    opt = SO_BROADCAST;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3085
	    level = SOL_SOCKET;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3086
	    usize = sizeof(u.u_bool);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3087
	    if (arg1 == true) u.u_bool = TRUE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3088
	    else if (arg1 == false) u.u_bool = FALSE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3089
	    else goto argError;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3090
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3091
# endif /* SO_BROADCAST */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3092
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3093
# ifdef SO_CONDITIONAL
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3094
#  if 0
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3095
	if (option == @symbol(SO_CONDITIONAL)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3096
	    /* Enables sockets to delay the acknowledgment of a connection until after the WSAAccept condition function is called. */
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3097
	    opt = SO_CONDITIONAL;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3098
	    level = SOL_SOCKET;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3099
	    usize = sizeof(u.u_bool);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3100
	    if (arg1 == true) u.u_bool = TRUE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3101
	    else if (arg1 == false) u.u_bool = FALSE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3102
	    else goto argError;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3103
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3104
#  endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3105
# endif /* SO_CONDITIONAL */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3106
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3107
# ifdef SO_DEBUG
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3108
	if (option == @symbol(SO_DEBUG)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3109
	    /* Records debugging information. */
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3110
	    opt = SO_DEBUG;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3111
	    level = SOL_SOCKET;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3112
	    usize = sizeof(u.u_bool);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3113
	    if (arg1 == true) u.u_bool = TRUE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3114
	    else if (arg1 == false) u.u_bool = FALSE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3115
	    else goto argError;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3116
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3117
# endif /* SO_DEBUG */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3118
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3119
# ifdef SO_DONTLINGER
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3120
	if (option == @symbol(SO_DONTLINGER)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3121
	    /* Does not block close waiting for unsent data to be sent.
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3122
	       Setting this option is equivalent to setting SO_LINGER with l_onoff set to zero. */
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3123
	    opt = SO_DONTLINGER;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3124
	    level = SOL_SOCKET;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3125
	    usize = sizeof(u.u_bool);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3126
	    if (arg1 == true) u.u_bool = TRUE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3127
	    else if (arg1 == false) u.u_bool = FALSE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3128
	    else goto argError;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3129
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3130
# endif /* SO_DONTLINGER */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3131
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3132
# ifdef SO_DONTROUTE
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3133
	if (option == @symbol(SO_DONTROUTE)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3134
	    /* Does not route: sends directly to interface.
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3135
	       Succeeds but is ignored on AF_INET sockets;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3136
	       fails on AF_INET6 sockets with WSAENOPROTOOPT.
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3137
	       Not supported on ATM sockets (results in an error). */
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3138
	    opt = SO_DONTROUTE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3139
	    level = SOL_SOCKET;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3140
	    usize = sizeof(u.u_bool);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3141
	    if (arg1 == true) u.u_bool = TRUE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3142
	    else if (arg1 == false) u.u_bool = FALSE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3143
	    else goto argError;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3144
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3145
# endif /* SO_DONTROUTE */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3146
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3147
#if defined(IPPROTO_TCP) && defined(TCP_NODELAY)
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3148
	if (option == @symbol(TCP_NODELAY)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3149
	    /* enable/disable TCP_NODELAY (i.e. disable/enable the Nagle algorithm) */
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3150
	    opt = TCP_NODELAY;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3151
	    level = IPPROTO_TCP;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3152
	    usize = sizeof(u.u_bool);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3153
	    if (arg1 == true) u.u_bool = TRUE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3154
	    else if (arg1 == false) u.u_bool = FALSE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3155
	    else goto argError;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3156
	}
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3157
# endif /* TCP_NODELAY */
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3158
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3159
# ifdef SO_KEEPALIVE
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3160
	if (option == @symbol(SO_KEEPALIVE)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3161
	    /* Sends keep-alives. Not supported on ATM sockets (results in an error). */
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3162
	    opt = SO_KEEPALIVE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3163
	    level = SOL_SOCKET;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3164
	    usize = sizeof(u.u_bool);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3165
	    if (arg1 == true) u.u_bool = TRUE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3166
	    else if (arg1 == false) u.u_bool = FALSE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3167
	    else goto argError;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3168
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3169
# endif /* SO_KEEPALIVE */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3170
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3171
# ifdef SO_LINGER
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3172
	if (option == @symbol(SO_LINGER)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3173
	    /* Lingers on close if unsent data is present. */
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3174
	    opt = SO_LINGER;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3175
	    level = SOL_SOCKET;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3176
	    usize = sizeof(u.u_linger);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3177
	    if (arg1 == true) u.u_linger.l_onoff = TRUE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3178
	    else if (arg1 == false) u.u_linger.l_onoff = FALSE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3179
	    else goto argError;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3180
	    if (arg2 == nil) u.u_linger.l_linger = 0;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3181
	    else if (__isSmallInteger(arg2))u.u_linger.l_linger = __intVal(arg2);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3182
	    else goto argError;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3183
	    DBGPRINTF(("SOCKET: SO_LINGER %d %d\n", u.u_linger.l_onoff, u.u_linger.l_linger));
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3184
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3185
# endif /* SO_LINGER */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3186
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3187
# ifdef SO_OOBINLINE
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3188
	if (option == @symbol(SO_OOBINLINE)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3189
	    /* Receives OOB data in the normal data stream. */
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3190
	    opt = SO_OOBINLINE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3191
	    level = SOL_SOCKET;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3192
	    usize = sizeof(u.u_bool);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3193
	    if (arg1 == true) u.u_bool = TRUE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3194
	    else if (arg1 == false) u.u_bool = FALSE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3195
	    else goto argError;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3196
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3197
# endif /* SO_OOBINLINE */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3198
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3199
# ifdef SO_RCVBUF
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3200
	if (option == @symbol(SO_RCVBUF)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3201
	    /* Specifies the total per-socket buffer space reserved for receives.
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3202
	       This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window. */
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3203
	    opt = SO_RCVBUF;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3204
	    level = SOL_SOCKET;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3205
	    usize = sizeof(u.u_int);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3206
	    if (__isSmallInteger(arg1))u.u_int = __intVal(arg1);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3207
	    else goto argError;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3208
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3209
# endif /* SO_RCVBUF */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3210
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3211
# ifdef SO_SNDBUF
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3212
	if (option == @symbol(SO_SNDBUF)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3213
	    /* Specifies the total per-socket buffer space reserved for sends.
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3214
	       This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window. */
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3215
	    opt = SO_SNDBUF;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3216
	    level = SOL_SOCKET;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3217
	    usize = sizeof(u.u_int);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3218
	    if (__isSmallInteger(arg1))u.u_int = __intVal(arg1);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3219
	    else goto argError;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3220
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3221
# endif /* SO_SNDBUF */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3222
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3223
# ifdef SO_REUSEADDR
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3224
	if (option == @symbol(SO_REUSEADDR)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3225
	    /* Allows the socket to be bound to an address that is already in use.  */
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3226
	    opt = SO_REUSEADDR;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3227
	    level = SOL_SOCKET;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3228
	    usize = sizeof(u.u_bool);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3229
	    if (arg1 == true) u.u_bool = TRUE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3230
	    else if (arg1 == false) u.u_bool = FALSE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3231
	    else goto argError;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3232
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3233
# endif /* SO_OOBINLINE */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3234
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3235
# ifdef SO_EXCLUSIVEADDRUSE
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3236
	if (option == @symbol(SO_EXCLUSIVEADDRUSE)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3237
	    /* Enables a socket to be bound for exclusive access.
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3238
	       Does not require administrative privilege.  */
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3239
	    opt = SO_EXCLUSIVEADDRUSE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3240
	    level = SOL_SOCKET;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3241
	    usize = sizeof(u.u_bool);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3242
	    if (arg1 == true) u.u_bool = TRUE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3243
	    else if (arg1 == false) u.u_bool = FALSE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3244
	    else goto argError;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3245
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3246
# endif /* SO_OOBINLINE */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3247
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3248
# ifdef SO_RCVTIMEO
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3249
	if ((option == @symbol(SO_RCVTIMEO))
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3250
	 && __isSmallInteger(arg1)
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3251
	 && __isSmallInteger(arg2)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3252
	    opt = SO_RCVTIMEO;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3253
	    level = SOL_SOCKET;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3254
	    u.u_tv.tv_sec = __intVal(arg1);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3255
	    u.u_tv.tv_usec = __intVal(arg2);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3256
	    usize = sizeof(u.u_tv);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3257
	}
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3258
# endif /* SO_RCVTIMEO */
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3259
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3260
# ifdef SO_SNDTIMEO
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3261
	if ((option == @symbol(SO_SNDTIMEO))
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3262
	 && __isSmallInteger(arg1)
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3263
	 && __isSmallInteger(arg2)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3264
	    opt = SO_SNDTIMEO;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3265
	    level = SOL_SOCKET;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3266
	    u.u_tv.tv_sec = __intVal(arg1);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3267
	    u.u_tv.tv_usec = __intVal(arg2);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3268
	    usize = sizeof(u.u_tv);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3269
	}
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3270
# endif /* SO_SNDTIMEO */
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3271
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3272
# if !defined(IPV6_V6ONLY) && defined(__win32__)
3513
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3273
#  define IPPROTO_IPV6 41
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3274
#  define IPV6_V6ONLY 27
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3275
# endif
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3276
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3277
# if defined(IPV6_V6ONLY)
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3278
	if (option == @symbol(IPV6_V6ONLY)) {
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3279
	    opt = IPV6_V6ONLY;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3280
	    level = IPPROTO_IPV6;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3281
	    usize = sizeof(u.u_bool);
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3282
	    if (arg1 == true) u.u_bool = TRUE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3283
	    else if (arg1 == false) u.u_bool = FALSE;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3284
	    else goto argError;
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3285
#  ifdef __win32__
3591
71ce2d98b99d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3590
diff changeset
  3286
	    console_fprintf(stderr, "%d %d %d %d\n", level, opt, usize, u.u_int);
3514
2d81c5ffa475 win32_printf is only avail in: guess what
Claus Gittinger <cg@exept.de>
parents: 3513
diff changeset
  3287
#  endif
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3288
	}
3513
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3289
# endif /* IPV6_V6ONLY */
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3290
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3291
	if (usize == -1) goto argError;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3292
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3293
	ok = (setsockopt(sock, level, opt, (char *)&u, usize) >= 0) ? true : false;
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3294
	if (ok == false) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3295
# ifdef __win32__
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3296
	    error = __mkSmallInteger(WSAGetLastError());
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3297
# else
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3298
	    error = __mkSmallInteger(errno);
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3299
# endif
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3300
	}
3513
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3301
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3302
    }
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3303
argError: ;
2818
94d67df5c00d compilable with tcc
Claus Gittinger <cg@exept.de>
parents: 2773
diff changeset
  3304
#endif /* NO_SOCKET */
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3305
%}.
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3306
    ok isNil ifTrue:[
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3307
	self primitiveFailed
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3308
    ].
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3309
    ok ifFalse:[
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  3310
	'++++ Info: Socket>>#setSocketOption:... failed. error: ' infoPrint. error infoPrintCR.
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3311
    ].
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  3312
!
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3313
2823
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
  3314
shutdown:howNum
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3315
    "shutDown the socket - inform it that no more I/O will be performed.
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3316
	 0 - read side   (no further reads)
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3317
	     the tcp receive window is closed, so the peer cannot send more data.
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3318
	 1 - write side  (no further writes)
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3319
	     first, all queued data will be delivered to the peer.
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3320
	     Then, an orderly release (TCP FIN) is sent to signal the peer,
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3321
	     that we will not send more data.
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3322
	 2 - both read side and write side."
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3323
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3324
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3325
#ifndef NO_SOCKET
2823
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
  3326
    OBJ __handle = __INST(handle);
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3327
2823
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
  3328
    if ((__handle != nil) && __isSmallInteger(howNum)) {
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3329
	SOCKET sock = SOCKET_FROM_FILE_OBJECT(__handle);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3330
	INT how = __intVal(howNum);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3331
	INT ret;
2823
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
  3332
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3333
# ifdef DO_WRAP_CALLS
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3334
	do {
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3335
	    __threadErrno = 0;
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3336
	    DBGFPRINTF((stderr, "SOCKET: shutDown...\n"));
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3337
	    ret = (INT)STX_WSA_NOINT_CALL2("shutdown", shutdown, sock, how);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3338
	    DBGFPRINTF((stderr, "SOCKET: shutDown -> %d (%d)\n", ret, __threadErrno));
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3339
	} while ((ret < 0) && (__threadErrno == EINTR));
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3340
# else
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3341
	__BEGIN_INTERRUPTABLE__
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3342
	shutdown(sock, how);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3343
	__END_INTERRUPTABLE__
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3344
# endif
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  3345
    }
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  3346
#endif
1339
cc973a42aa54 shutdown: added
Claus Gittinger <cg@exept.de>
parents: 1338
diff changeset
  3347
%}.
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  3348
! !
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  3349
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3350
!Socket methodsFor:'printing & storing'!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3351
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3352
printOn:aStream
3530
ce2171c0fe57 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3514
diff changeset
  3353
    aStream nextPutAll:self className; nextPutAll:'('.
ce2171c0fe57 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3514
diff changeset
  3354
    domain printOn:aStream.
ce2171c0fe57 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3514
diff changeset
  3355
    aStream nextPutAll:' protocol='.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3356
    protocol printOn:aStream.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3357
    aStream nextPutAll:' port='.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3358
    self port printOn:aStream.
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  3359
    aStream nextPutAll:' peer='.
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  3360
    peer printOn:aStream.
1842
88c04d1e9e6b changed #printOn:
Stefan Vogel <sv@exept.de>
parents: 1839
diff changeset
  3361
    aStream nextPut:$).
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3362
! !
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3363
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3364
!Socket methodsFor:'queries'!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3365
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3366
domain
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3367
    "return the sockets addressing domain (i.e. #AF_INET, #AF_INET6, #AF_UNIX, ...)"
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3368
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3369
    ^ domain
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3370
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3371
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3372
getFullPeerAddress
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3373
    "implemented for swazoo project (primitive code can't be loaded as extension)
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3374
     Answer my own address (I am bound to this address).
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3375
     Note that this address may change after a connect or accept."
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3376
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3377
    |error domainClass addr|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3378
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3379
    handle isNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3380
	^ self errorNotOpen
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3381
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3382
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3383
    domainClass := self class socketAddressClassForDomain:domain.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3384
    domainClass isNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3385
	^ self error:'invalid (unsupported) domain'.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3386
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3387
    addr := domainClass new.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3388
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3389
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3390
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3391
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3392
    SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3393
    int ret;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3394
    union sockaddr_u sa;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3395
    unsigned int alen = sizeof(sa);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3396
    char *addrP;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3397
    int addrObjSize, nAddrInstBytes;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3398
    OBJ addrClass;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3399
    int nAddrInstVars;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3400
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3401
    if (!__isNonNilObject(addr) || !__isBytes(addr)) {
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3402
	DBGPRINTF(("SOCKET: bad addr\n"));
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3403
	error = @symbol(badArgument);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3404
	goto err;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3405
    }
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3406
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3407
    addrClass = __qClass(addr);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3408
    nAddrInstVars = __intVal(__ClassInstPtr(addrClass)->c_ninstvars);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3409
    nAddrInstBytes = OHDR_SIZE + (nAddrInstVars * sizeof(OBJ));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3410
    addrObjSize = __qSize(addr) - nAddrInstBytes;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3411
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3412
    sock = SOCKET_FROM_FILE_OBJECT(fp);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3413
    ret = getpeername(sock, (struct sockaddr *)&sa, &alen);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3414
    if (ret < 0) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3415
# ifdef __win32__
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3416
	errno = WSAGetLastError();
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3417
# endif
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3418
	DBGPRINTF(("SOCKET: getsocketname failed ret=%d errno=%d\n", ret, errno));
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3419
	error = __MKSMALLINT(errno);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3420
	goto err;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3421
    }
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3422
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3423
    if (addrObjSize < alen) {
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3424
	DBGPRINTF(("SOCKET: bad addr\n"));
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3425
	error = @symbol(badArgument);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3426
	goto err;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3427
    }
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3428
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3429
    addrP = (char *)__InstPtr(addr) + nAddrInstBytes;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3430
    memcpy(addrP, (char *)&sa, alen);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3431
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3432
err:;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3433
#else /* NO_SOCKET */
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3434
    error = @symbol(notImplemented);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3435
#endif /* NO_SOCKET */
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3436
%}.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3437
    error notNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3438
	^ self errorReporter reportOn:error
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3439
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3440
    ^ addr
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3441
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3442
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3443
getFullSocketAddress
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3444
    "implemented for swazoo project (primitive code can't be loaded as extension)
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3445
     Answer my own address (I am bound to this address).
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3446
     Note that this address may change after a connect or accept."
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3447
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3448
    |error domainClass addr|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3449
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3450
    handle isNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3451
	^ self errorNotOpen
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3452
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3453
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3454
    domainClass := self class socketAddressClassForDomain:domain.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3455
    domainClass isNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3456
	^ self error:'invalid (unsupported) domain'.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3457
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3458
    addr := domainClass new.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3459
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3460
%{
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3461
#ifndef NO_SOCKET
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3462
    OBJ fp = __INST(handle);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3463
    SOCKET sock;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3464
    int ret;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3465
    union sockaddr_u sa;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3466
    unsigned int alen = sizeof(sa);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3467
    char *addrP;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3468
    int addrObjSize, nAddrInstBytes;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3469
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3470
    if (!__isNonNilObject(addr) || !__isBytes(addr)) {
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3471
	DBGPRINTF(("SOCKET: bad addr\n"));
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3472
	error = @symbol(badArgument);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3473
	goto err;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3474
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3475
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3476
    {
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3477
	OBJ addrClass;
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3478
	int nAddrInstVars;
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3479
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3480
	addrClass = __qClass(addr);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3481
	nAddrInstVars = __intVal(__ClassInstPtr(addrClass)->c_ninstvars);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3482
	nAddrInstBytes = OHDR_SIZE + (nAddrInstVars * sizeof(OBJ));
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3483
	addrObjSize = __qSize(addr) - nAddrInstBytes;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3484
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3485
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3486
    sock = SOCKET_FROM_FILE_OBJECT(fp);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3487
    ret = getsockname(sock, (struct sockaddr *)&sa, &alen);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3488
    if (ret < 0) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3489
# ifdef __win32__
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3490
	errno = WSAGetLastError();
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3491
# endif
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3492
	DBGPRINTF(("SOCKET: getsocketname failed ret=%d errno=%d\n", ret, errno));
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3493
	error = __MKSMALLINT(errno);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3494
	goto err;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3495
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3496
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3497
    if (addrObjSize < alen) {
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3498
	DBGPRINTF(("SOCKET: bad addr\n"));
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3499
	error = @symbol(badArgument);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3500
	goto err;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3501
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3502
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3503
    addrP = (char *)__InstPtr(addr) + nAddrInstBytes;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3504
    memcpy(addrP, (char *)&sa, alen);
1535
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3505
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3506
err:;
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3507
#else /* NO_SOCKET */
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3508
    error = @symbol(notImplemented);
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3509
#endif /* NO_SOCKET */
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3510
%}.
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3511
    error notNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3512
	^ self errorReporter reportOn:error
1535
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3513
    ].
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3514
    ^ addr
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3515
!
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3516
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3517
getName
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3518
    "return the name; here, we return the ports name"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3519
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3520
    ^ self port printString
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3521
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3522
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3523
getPeer
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3524
    "ST-80 compatibility: return an IPSocketAddress instance representing
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3525
     my hostname/port combination.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3526
     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
  3527
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
  3528
    ^ peer
102
5dc264007513 added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
  3529
!
5dc264007513 added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
  3530
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3531
getPeerName
4091
ff4070ceffad #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4039
diff changeset
  3532
    "return the peer name; that's the hostname (or dotted name) of the
25
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
  3533
     partners host after an accept."
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
  3534
1187
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  3535
    peerName isNil ifTrue:[
4091
ff4070ceffad #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4039
diff changeset
  3536
        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
  3537
    ].
25
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
  3538
    ^ peerName
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3539
!
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3540
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3541
getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3542
    "implemented for swazoo project primitive code cant load as extension
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3543
     answer my own address (I am bound to this address).
1146
71a9df78ba9e bad spelling
Stefan Vogel <sv@exept.de>
parents: 1136
diff changeset
  3544
     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
  3545
1535
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3546
    ^ self getFullSocketAddress hostAddress
1146
71a9df78ba9e bad spelling
Stefan Vogel <sv@exept.de>
parents: 1136
diff changeset
  3547
!
71a9df78ba9e bad spelling
Stefan Vogel <sv@exept.de>
parents: 1136
diff changeset
  3548
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3549
isActive
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3550
    "return true, if the receiver has a connection"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3551
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3552
    ^ handle notNil
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3553
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3554
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3555
isConnected
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3556
    "return true, if the receiver has a connection"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3557
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3558
    ^ self isActive
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3559
	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
  3560
!
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  3561
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3562
port
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3563
    "return the port number (or name for unix-sockets) to which the socket is bound
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3564
     - so this is the local port."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3565
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3566
"/    port isNil ifTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3567
"/        port := self getFullSocketAddress port.
1505
Stefan Vogel <sv@exept.de>
parents: 1504
diff changeset
  3568
"/    ].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3569
71
claus
parents: 63
diff changeset
  3570
    ^ port
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3571
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3572
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3573
socketAddressClass
4288
00e575acf898 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4261
diff changeset
  3574
    "get the matching SocketAddress class for this socket"
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3575
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3576
    |domainClass|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3577
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3578
    domainClass := self class socketAddressClassForDomain:domain.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3579
    domainClass isNil ifTrue:[
4288
00e575acf898 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4261
diff changeset
  3580
        ^ self error:'invalid (unsupported) domain'.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3581
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3582
    ^ domainClass.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3583
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3584
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3585
type
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3586
    "return the sockets connection type (i.e. #datagram, #stream etc)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3587
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3588
    ^ socketType
81
claus
parents: 79
diff changeset
  3589
! !
claus
parents: 79
diff changeset
  3590
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3591
!Socket methodsFor:'socket setup'!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3592
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3593
domain:domainArg type:typeArg
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3594
    "set up socket with domain and type.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3595
     This is a low level entry; no binding, listening or connect
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3596
     is done. Both arguments must be symbols from one of
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3597
      #AF_INET, #AF_INET6, #AF_UNIX ... and #stream, #datagram, #raw resp."
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3598
3137
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  3599
    ^ self domain:domainArg type:typeArg protocol:nil
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3600
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3601
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3602
domain:domainArg type:typeArg protocol:protocolNumber
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3603
    "set up socket with domain, type and protocol number.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3604
     This is a low level entry; no binding, listening or connect
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3605
     is done. Both arguments must be symbols from one of
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3606
     #AF_INET, #AF_INET6, #AF_UNIX ... and #stream, #datagram, #raw resp."
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3607
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3608
    |domainName domainCode typeCode error newHandle|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3609
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3610
    handle notNil ifTrue:[
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3611
        ^ self errorAlreadyOpen
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3612
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3613
    domainName := SocketAddress domainCodeFromName:domainArg.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3614
    domainCode := OperatingSystem domainCodeOf:domainName.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3615
    typeCode := OperatingSystem socketTypeCodeOf:typeArg.
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3616
    newHandle := OperatingSystem socketAccessor new.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3617
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3618
#ifndef NO_SOCKET
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3619
    FILE *fp;
3137
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  3620
    int dom, typ, proto = 0;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3621
    int on = 1;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3622
    SOCKET sock;
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3623
    int _fd;
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3624
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3625
# ifdef __win32__
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3626
#  ifndef WSA_FLAG_NO_HANDLE_INHERIT
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3627
#   define WSA_FLAG_NO_HANDLE_INHERIT 0x80
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3628
#  endif
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3629
    static int noInheritFlag = WSA_FLAG_NO_HANDLE_INHERIT;
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3630
# endif
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3631
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3632
    if (! __isSmallInteger(domainCode)) {
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3633
        error = @symbol(badArgument1);
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3634
        goto out;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3635
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3636
    if (! __isSmallInteger(typeCode)) {
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3637
        error = @symbol(badArgument2);
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3638
        goto out;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3639
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3640
    if (protocolNumber != nil) {
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3641
        if (!__isSmallInteger(protocolNumber)) {
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3642
            error = @symbol(badArgument3);
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3643
            goto out;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3644
        }
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3645
        proto = __intVal(protocolNumber);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3646
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3647
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3648
    /*
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3649
     * get address and protocol-family
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3650
     */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3651
    dom = __intVal(domainCode);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3652
    typ = __intVal(typeCode);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3653
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3654
# ifdef __win32__
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3655
    sock = WSASocket(dom, typ, proto, 0, 0, noInheritFlag);
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3656
    if (sock == INVALID_SOCKET && noInheritFlag) {
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3657
        // tried to open socket with WSA_FLAG_NO_HANDLE_INHERIT
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3658
        // This fails on older windows versions, e.g. Windows XP
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3659
        sock = WSASocket(dom, typ, proto, 0, 0, 0);
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3660
        if (sock != INVALID_SOCKET) {
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3661
            // no error without WSA_FLAG_NO_HANDLE_INHERIT,
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3662
            // never use this flag again!
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3663
            noInheritFlag = 0;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3664
        }
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3665
    }
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3666
    if (sock == INVALID_SOCKET) {
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3667
        errno = WSAGetLastError();
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3668
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3669
# else  // !__win32__
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3670
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3671
    sock = socket(dom, typ, proto);
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3672
# if defined(EPROTONOSUPPORT) /* for SGI */
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3673
    if ((sock < 0) && (proto != 0) && (errno == EPROTONOSUPPORT)) {
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3674
        DBGPRINTF(("SOCKET: retry with UNSPEC protocol\n"));
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3675
        proto = 0;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3676
        sock = socket(dom, typ, 0);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3677
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3678
# endif
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3679
    if (sock < 0) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3680
# endif // !__win32__
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3681
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3682
        DBGPRINTF(("SOCKET: socket(dom=%d typ=%d proto=%d) call failed errno=%d\n", dom, typ, proto, errno));
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3683
        error = __MKSMALLINT(errno);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3684
    } else {
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3685
# if defined(SET_LINGER_WHEN_CREATING_SOCKET) && defined(SO_LINGER)
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3686
        {
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3687
            struct linger l;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3688
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3689
            l.l_onoff = 1;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3690
            l.l_linger = 30;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3691
            setsockopt(sock, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3692
        }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3693
# endif
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3694
# ifdef __win32__
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3695
        /*
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3696
         * make it blocking
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3697
         */
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3698
        {
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3699
            unsigned long zero = 0;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3700
            ioctlsocket(sock, FIONBIO, &zero);
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3701
        }
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3702
        {
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3703
#  if 0 && (defined( __BORLANDC__ ) || defined( __MINGW__ ))
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3704
            /*
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3705
             * make it a FILE *
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3706
             */
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3707
            __stxWrapApiEnterCritical();
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3708
            _fd = _open_osfhandle((long)sock, 0);
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3709
            __stxWrapApiLeaveCritical();
2773
ab3049f4a351 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2748
diff changeset
  3710
#  else
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3711
            _fd = (int)sock;
2773
ab3049f4a351 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2748
diff changeset
  3712
#  endif
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3713
            DBGPRINTF(("SOCKET: sock=%d fd=%d\n", sock, _fd));
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3714
        }
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3715
# else  // !__win32__
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3716
        fp = fdopen(sock, "r+");
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3717
        if (! fp) {
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3718
            DBGPRINTF(("SOCKET: fdopen call failed\n"));
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3719
            error = __MKSMALLINT(errno);
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3720
            __BEGIN_INTERRUPTABLE__
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3721
            closesocket(sock);
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3722
            DBGFPRINTF((stderr, "SOCKET: fdopen failed (%d)\n", sock));
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3723
            __END_INTERRUPTABLE__
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3724
            goto out;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3725
        }
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3726
# endif // !__win32__
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3727
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3728
        if (@global(FileOpenTrace) == true) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3729
# ifdef __win32__
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3730
            console_fprintf(stderr, "fdopen [Socket create] -> fd: %d (H: %"_lx_")\n", (INT)_fd, (INT)sock);
3223
b126729a904b class: Socket
Stefan Vogel <sv@exept.de>
parents: 3218
diff changeset
  3731
# else
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3732
            console_fprintf(stderr, "fdopen [Socket] -> %"_lx_" (fd: %d)\n", (INT)fp, sock);
3223
b126729a904b class: Socket
Stefan Vogel <sv@exept.de>
parents: 3218
diff changeset
  3733
# endif
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3734
        }
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3735
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3736
# ifdef __win32__
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3737
        __externalAddressVal(newHandle) = _fd;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3738
        __INST(handleType) = @symbol(socketHandle);
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3739
# else
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3740
        __externalAddressVal(newHandle) = fp;
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3741
        __INST(handleType) = @symbol(socketFilePointer);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3742
# endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3743
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3744
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3745
out:;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3746
%}.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3747
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3748
    "all ok?"
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3749
    handleType notNil ifTrue:[
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3750
        handle := newHandle.
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3751
        domain := domainArg.
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3752
        socketType := typeArg.
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3753
        self registerForFinalization.
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3754
        ^ self.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3755
    ].
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3756
    error isInteger ifTrue:[
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3757
        lastErrorNumber := error.
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  3758
        ^ self openError:error.
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3759
    ].
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3760
    ^ self primitiveFailed:error.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3761
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3762
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3763
     Socket new domain:#AF_INET type:#stream
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3764
     Socket new domain:#AF_UNIX type:#stream
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3765
    "
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3766
! !
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3767
4288
00e575acf898 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4261
diff changeset
  3768
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3769
!Socket methodsFor:'specials'!
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3770
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3771
linger:anIntegerOrNil
3224
bad706c50347 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3223
diff changeset
  3772
    "set the linger behavior on close:
bad706c50347 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3223
diff changeset
  3773
      anIntegerOrNil == nil: close returns immediately, socket tries
4172
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  3774
                             to send buffered data in background.
3224
bad706c50347 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3223
diff changeset
  3775
      anIntegerOrNil == 0:   close returns immediately, bufferd data is discarded.
bad706c50347 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3223
diff changeset
  3776
      anIntegerOrNil > 0:    close waits this many seconds for buffered data
4172
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  3777
                             to be delivered, after this time buffered data is
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  3778
                             discarded and close returns with an error"
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3779
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3780
    ^ self
4172
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  3781
        setSocketOption:#'SO_LINGER'
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  3782
        argument:anIntegerOrNil notNil
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  3783
        argument:anIntegerOrNil.
3224
bad706c50347 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3223
diff changeset
  3784
!
bad706c50347 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3223
diff changeset
  3785
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3786
receiveBufferSize
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3787
    "get the send buffer size - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3788
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3789
     (returns nil, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3790
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3791
    handle isNil ifTrue:[
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3792
	^ self errorNotOpen
1335
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3793
    ].
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3794
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3795
#if defined(SO_RCVBUF) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3796
    {
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3797
	OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3798
	SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3799
	int opt;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3800
	unsigned int size;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3801
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3802
	sock = SOCKET_FROM_FILE_OBJECT(fp);
1568
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3803
	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
  3804
	    RETURN( __MKSMALLINT(opt) );
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3805
	}
1335
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3806
    }
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3807
#endif
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3808
%}.
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3809
    ^ nil
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3810
!
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3811
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3812
receiveBufferSize:size
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3813
    "set the receive buffer size - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3814
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3815
     (returns false, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3816
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3817
    handle isNil ifTrue:[
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3818
	^ self errorNotOpen
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3819
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3820
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3821
#if defined(SO_RCVBUF) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3822
    if (__isSmallInteger(size)) {
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3823
	OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3824
	SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3825
	int opt;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3826
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3827
	sock = SOCKET_FROM_FILE_OBJECT(fp);
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  3828
	opt = __intVal(size);
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3829
	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
  3830
	    RETURN(true);
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3831
	}
1335
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3832
    }
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3833
#endif
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3834
%}.
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3835
    ^ false
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3836
!
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3837
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3838
receiveTimeout
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3839
    "get the receive timeout in millis - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3840
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3841
     (returns nil, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3842
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3843
    |millis|
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3844
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3845
    handle isNil ifTrue:[
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3846
	^ self errorNotOpen
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3847
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3848
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3849
#if defined(SO_RCVTIMEO) && defined(SOL_SOCKET)
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3850
    OBJ fp = __INST(handle);
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3851
    SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp);
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3852
    int len;
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3853
    int __millis;
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3854
    struct timeval tv = {0, 0};
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3855
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3856
    len = sizeof(struct timeval);
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3857
    if (getsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, &len) == 0) {
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3858
	__millis = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3859
	millis = __mkSmallInteger(__millis);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3860
# if 0
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3861
	console_fprintf(stderr, "getsockopt -> s:%d us:%d -> millis:%d\n", tv.tv_sec, tv.tv_usec, __millis);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3862
# endif
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3863
    } else {
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3864
	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
  3865
    }
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3866
#endif
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3867
%}.
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3868
    millis notNil ifTrue:[^ millis / 1000 ].
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3869
    ^ nil
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3870
!
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3871
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3872
receiveTimeout:seconds
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3873
    "set the receive timeout - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3874
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3875
     (returns false, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3876
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3877
    |millis|
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3878
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3879
    handle isNil ifTrue:[
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3880
	^ self errorNotOpen
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3881
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3882
    millis := (seconds * 1000) rounded.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3883
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3884
#if defined(SO_RCVTIMEO) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3885
    if (__isSmallInteger(millis)) {
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3886
	OBJ fp = __INST(handle);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3887
	SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3888
	int __millis = __intVal(millis);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3889
	struct timeval tv = {0, 0};
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3890
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3891
	tv.tv_sec = __millis / 1000;
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3892
	tv.tv_usec = (__millis % 1000) * 1000;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3893
# if 0
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3894
	console_fprintf(stderr, "setsockopt -> millis:%d -> s:%d us:%d \n", __millis, tv.tv_sec, tv.tv_usec);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3895
# endif
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3896
	if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(struct timeval)) == 0) {
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3897
	    RETURN(true);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3898
	}
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3899
	console_fprintf(stderr, "Socket [warning]: setsockopt %d failed; errno=%d\n", sock, errno);
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3900
    }
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3901
#endif
71
claus
parents: 63
diff changeset
  3902
%}.
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3903
    ^ false
81
claus
parents: 79
diff changeset
  3904
!
claus
parents: 79
diff changeset
  3905
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3906
sendBufferSize
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3907
    "get the send buffer size - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3908
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3909
     (returns nil, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3910
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3911
    handle isNil ifTrue:[
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3912
	^ self errorNotOpen
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3913
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3914
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3915
#if defined(SO_SNDBUF) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3916
    {
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3917
	OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3918
	SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3919
	int opt;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3920
	unsigned int size;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3921
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3922
	sock = SOCKET_FROM_FILE_OBJECT(fp);
1568
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3923
	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
  3924
	    RETURN( __MKSMALLINT(opt) );
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3925
	}
1335
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3926
    }
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3927
#endif
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3928
%}.
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3929
    ^ nil
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3930
!
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3931
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3932
sendBufferSize:size
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3933
    "set the send buffer size - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3934
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3935
     (returns false, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3936
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3937
    handle isNil ifTrue:[
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3938
	^ self errorNotOpen
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3939
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3940
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3941
#if defined(SO_SNDBUF) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3942
    if (__isSmallInteger(size)) {
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3943
	OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3944
	SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3945
	int opt;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3946
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3947
	sock = SOCKET_FROM_FILE_OBJECT(fp);
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  3948
	opt = __intVal(size);
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3949
	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
  3950
	    RETURN(true);
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3951
	}
1335
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3952
    }
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3953
#endif
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3954
%}.
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3955
    ^ false
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3956
!
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  3957
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3958
sendTimeout
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3959
    "get the send timeout in millis - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3960
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3961
     (returns nil, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3962
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3963
    |millis|
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3964
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3965
    handle isNil ifTrue:[
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3966
	^ self errorNotOpen
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3967
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3968
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3969
#if defined(SO_SNDTIMEO) && defined(SOL_SOCKET)
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3970
    OBJ fp = __INST(handle);
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3971
    SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp);
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3972
    int len;
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3973
    int __millis;
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3974
    struct timeval tv = {0, 0};
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3975
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3976
    len = sizeof(struct timeval);
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3977
    if (getsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (void *)&tv, &len) == 0) {
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3978
	__millis = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3979
	millis = __mkSmallInteger(__millis);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3980
# if 0
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3981
	console_fprintf(stderr, "getsockopt -> s:%d us:%d -> millis:%d\n", tv.tv_sec, tv.tv_usec, __millis);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3982
# endif
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3983
    } else {
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3984
	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
  3985
    }
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3986
#endif
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3987
%}.
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3988
    millis notNil ifTrue:[^ millis / 1000 ].
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3989
    ^ nil
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3990
!
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3991
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3992
sendTimeout:seconds
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3993
    "set the send timeout - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3994
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3995
     (returns false, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3996
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3997
    |millis|
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3998
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3999
    handle isNil ifTrue:[
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  4000
	^ self errorNotOpen
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4001
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4002
    millis := (seconds * 1000) rounded.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4003
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4004
#if defined(SO_SNDTIMEO) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4005
    if (__isSmallInteger(millis)) {
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  4006
	OBJ fp = __INST(handle);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  4007
	SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  4008
	int __millis = __intVal(millis);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  4009
	struct timeval tv = {0, 0};
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  4010
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  4011
	tv.tv_sec = __millis / 1000;
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  4012
	tv.tv_usec = (__millis % 1000) * 1000;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4013
# if 0
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  4014
	console_fprintf(stderr, "setsockopt -> millis:%d -> s:%d us:%d \n", __millis, tv.tv_sec, tv.tv_usec);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4015
# endif
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  4016
	if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (void *)&tv, sizeof(struct timeval)) == 0) {
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  4017
	    RETURN(true);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  4018
	}
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  4019
	console_fprintf(stderr, "Socket [warning]: setsockopt %d failed; errno=%d\n", sock, errno);
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  4020
    }
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  4021
#endif
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  4022
%}.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  4023
    ^ false
1340
eb9b1f1340e5 setTCP_NODELAY
Claus Gittinger <cg@exept.de>
parents: 1339
diff changeset
  4024
!
eb9b1f1340e5 setTCP_NODELAY
Claus Gittinger <cg@exept.de>
parents: 1339
diff changeset
  4025
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4026
setTCPCork:aBoolean
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4027
    "enable/disable TCP_CORK (do-not-send-partial-frames)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4028
     For special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4029
     Not all OperatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4030
     (returns false, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4031
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  4032
    ^ self setSocketOption:#'TCP_CORK' argument:aBoolean argument:nil.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4033
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4034
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4035
setTCPNoDelay:aBoolean
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4036
    "enable/disable TCP_NODELAY (i.e. disable/enable the Nagle algorithm)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4037
     For special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4038
     Not all OperatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4039
     (returns false, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4040
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  4041
    ^ self setSocketOption:#'TCP_NODELAY' argument:aBoolean argument:nil.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  4042
! !
82
claus
parents: 81
diff changeset
  4043
4203
defdf140ad57 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4192
diff changeset
  4044
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4045
!Socket methodsFor:'waiting'!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4046
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4047
waitForNewConnectionOrDataOnAny:otherConnections timeout:timeoutSeconds
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4048
    "suspend the current process, until either a new connection comes
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4049
     in at the receiver, or data arrives on any of the otherConnections.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4050
     For a new connection, an accept is performed and the new socket is returned.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4051
     For an old connection, that socket is returned.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4052
     In any case, the caller gets a socket to operate on as return value,
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4053
     or nil, if a timeout occurred.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4054
     This method implements the inner wait-primitive of a multi-connection
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4055
     server application."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4056
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4057
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4058
    |wasBlocked sema|
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4059
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4060
    "first, a quick check if data is already available"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4061
    self canReadWithoutBlocking ifTrue:[
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4062
        ^ self accept.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4063
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4064
    otherConnections do:[:aConnection |
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4065
        aConnection canReadWithoutBlocking ifTrue:[
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4066
            ^ aConnection
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4067
        ]
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4068
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4069
3281
bd9392932db1 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3247
diff changeset
  4070
    "check again - prevent incoming interrupts from disturbing our setup"
bd9392932db1 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3247
diff changeset
  4071
    wasBlocked := OperatingSystem blockInterrupts.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4072
    [
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4073
        sema := Semaphore new name:'multiReadWait'.
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4074
        otherConnections do:[:aConnection |
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4075
            Processor signal:sema onInput:(aConnection fileDescriptor).
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4076
        ].
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4077
        Processor signal:sema onInput:(self fileDescriptor).
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4078
        timeoutSeconds notNil ifTrue:[
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4079
            Processor signal:sema afterSeconds:timeoutSeconds
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4080
        ].
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4081
        Processor activeProcess state:#ioWait.
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4082
        sema wait.
3281
bd9392932db1 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3247
diff changeset
  4083
    ] ifCurtailed:[
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4084
        sema notNil ifTrue:[Processor disableSemaphore:sema].
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4085
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4086
    ].
3281
bd9392932db1 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3247
diff changeset
  4087
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4088
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4089
    "see who it was ..."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4090
    self canReadWithoutBlocking ifTrue:[
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4091
        ^ self accept.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4092
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4093
    otherConnections do:[:aConnection |
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4094
        aConnection canReadWithoutBlocking ifTrue:[
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4095
            ^ aConnection
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4096
        ]
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4097
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4098
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4099
    "none - a timeout"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4100
    ^ nil
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4101
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4102
3550
58fab5f59ee8 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3530
diff changeset
  4103
waitForNewConnectionWithTimeout:timeoutSecondsOrNil
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4104
    "suspend the current process, until a new connection comes
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4105
     in at the receiver or a timeout occurs.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4106
     For a new connection, an accept is performed and the new socket is returned.
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4107
     Returns nil, if a timeout occurred.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4108
     This method implements the inner wait-primitive of a single-connection
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4109
     server application."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4110
3633
3a21f142432e #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3620
diff changeset
  4111
    |newSock|
3a21f142432e #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3620
diff changeset
  4112
3550
58fab5f59ee8 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3530
diff changeset
  4113
    (self readWaitWithTimeout:timeoutSecondsOrNil) ifTrue:[
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4114
        "a timeout occurred - no connection within timeout"
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4115
        ^ nil
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4116
    ].
4180
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  4117
    self isOpen ifFalse:[
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  4118
        "socket has been closed while waiting"
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  4119
        ^ nil.
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  4120
    ].
3633
3a21f142432e #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3620
diff changeset
  4121
    newSock := self class new.
3a21f142432e #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3620
diff changeset
  4122
    (newSock primAcceptOn:self blocking:false) ifFalse:[
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4123
        "should raise an error here"
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4124
        ^ nil
3633
3a21f142432e #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3620
diff changeset
  4125
    ].
3a21f142432e #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3620
diff changeset
  4126
    ^ newSock
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4127
! !
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4128
464
600b101a7035 alen in accept
Claus Gittinger <cg@exept.de>
parents: 463
diff changeset
  4129
!Socket class methodsFor:'documentation'!
206
77166a6b3ee6 For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents: 203
diff changeset
  4130
77166a6b3ee6 For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents: 203
diff changeset
  4131
version
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  4132
    ^ '$Header$'
2292
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  4133
!
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  4134
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  4135
version_CVS
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  4136
    ^ '$Header$'
206
77166a6b3ee6 For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents: 203
diff changeset
  4137
! !
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
  4138