Socket.st
author Stefan Vogel <sv@exept.de>
Sat, 15 Feb 2020 18:43:21 +0100
changeset 5450 7bc191b240c5
parent 5445 41990ba5aeff
child 5451 d98c8a76546d
permissions -rw-r--r--
#FEATURE by stefan class: Socket added: #isSSLSocket
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)))
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
    72
#define SOCKET_FROM_FILE_OBJECT(f)      (__INST(handleType) == @symbol(socketHandle) ? SOCKET_FROM_FD((INT)(__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,
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
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:]
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
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):
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   260
									[exBegin]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   261
	|sock|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   262
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   263
	sock := Socket newTCPclientToHost:'smtp.exept.de' port:'smtp'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   264
	sock isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   265
	    self warn:'no smtp daemon is running'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   266
	    ^ self
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   267
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   268
	Transcript showCR:sock nextLine.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   269
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   270
	sock nextPutAll:'HELO STX socket test'; cr.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   271
	Transcript showCR:sock nextLine.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   272
	sock close
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
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):
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   277
									[exBegin]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   278
	|sock entry|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   279
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   280
	sock := Socket newTCPclientToHost:'localhost' port:'finger'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   281
	sock isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   282
	    self warn:'no finger daemon is running'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   283
	    ^ self
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   284
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   285
	sock useCRLF:true.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   286
	sock buffered:false.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   287
	sock isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   288
	    Transcript showCR:'cannot connect to local finger daemon'
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   289
	] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   290
	    sock nextPutAll:(OperatingSystem getLoginName).
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   291
	    sock cr.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   292
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   293
	    entry := sock nextLine.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   294
	    Transcript showCR:entry.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   295
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   296
	    sock close
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   297
	]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
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):
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   301
									[exBegin]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   302
	|sock|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   303
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   304
	sock := Socket newTCPclientToHost:'www.exept.de' port:'ftp'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   305
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   306
	sock buffered:false.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   307
	Transcript showCR:sock nextLine.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   308
	sock nextPutAll:('USER ' , 'anonymous'); cr.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   309
	Transcript showCR:sock nextLine.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   310
	sock nextPutAll:('PASS ' , 'fooBar'); cr.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   311
	Transcript showCR:sock nextLine.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   312
	sock nextPutAll:'HELP'; cr.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   313
	[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   314
	    |line|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   315
	    line := sock nextLine.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   316
	    Transcript showCR:line.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   317
	    (line at:4) = $-
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   318
	] whileTrue.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   319
	sock close.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   320
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   321
	'don't know enough of the ftp protocol to continue here ...'
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
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
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   327
	  being fixed.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   328
									[exBegin]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   329
	|sock port|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   330
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   331
	sock := Socket newUDP.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   332
	port := Socket portOfService:'snmp'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   333
	sock connectTo:'224.1.2.3' port:port.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   334
	sock buffered:false.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   335
	Transcript showCR:'got it'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   336
	sock close.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
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):
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   341
									[exBegin]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   342
	|connectSock sock|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   343
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   344
	connectSock := Socket newTCPserverAtPort:9998.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   345
	connectSock isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   346
	    Transcript showCR:'socket setup failed.'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   347
	] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   348
	    Transcript showCR:'listen ..'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   349
	    (connectSock listenFor:5) ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   350
		Transcript showCR:'listen failed.'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   351
	    ] ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   352
		Transcript showCR:'wait'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   353
		connectSock readWait.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   354
		Transcript showCR:'accept'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   355
		sock := connectSock accept.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   356
		sock isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   357
		    Transcript showCR:'accept failed.'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   358
		] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   359
		    sock buffered:false.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   360
		    Transcript showCR:'server: got it'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   361
		    'can now do transfer via sock'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   362
		    Transcript showCR:'read'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   363
		    Transcript showCR:('got: ' , sock nextLine).
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   364
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   365
		    Transcript showCR:'close'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   366
		    sock close
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   367
		].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   368
		connectSock close.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   369
	    ]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   370
	]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
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):
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   375
									[exBegin]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   376
	|sock|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   377
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   378
	sock := Socket newTCPclientToHost:'localhost' port:9998.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   379
	sock isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   380
	    Transcript showCR:'nope'
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   381
	] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   382
	    sock buffered:false.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   383
	    Transcript showCR:'client: got it'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   384
	    'can now do transfer via sock'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   385
	    Transcript showCR:'sending <hello>'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   386
	    sock nextPutLine:'hello'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   387
	    sock close
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   388
	]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
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
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   393
	|connectSock sock|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   394
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   395
	'/tmp/ud_socket' asFilename remove.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   396
	connectSock := Socket newUNIXserverAt:'/tmp/ud_socket'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   397
	connectSock isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   398
	    Transcript showCR:'socket setup failed.'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   399
	] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   400
	    Transcript showCR:'listen ..'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   401
	    (connectSock listenFor:5) ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   402
		Transcript showCR:'listen failed.'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   403
	    ] ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   404
		Transcript showCR:'wait'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   405
		connectSock buffered:false.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   406
		connectSock readWait.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   407
		Transcript showCR:'accept'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   408
		sock := connectSock accept.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   409
		sock isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   410
		    Transcript showCR:'accept failed.'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   411
		] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   412
		    sock buffered:false.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   413
		    Transcript showCR:'server: got it'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   414
		    'can now do transfer via sock'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   415
		    Transcript showCR:'read'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   416
		    Transcript showCR:('got: ' , sock nextLine).
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   417
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   418
		    Transcript showCR:'close'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   419
		    sock close
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   420
		].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   421
		connectSock close.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   422
	    ]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
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;
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   427
	     Notice, this fails, if above server code is executed in the same ST/X image
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   428
		     (at least on LINUX), since the OS does not correctly handle
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   429
		     a connect from within an interrupted accept system call
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   430
		     On SGI's SVR4, this works ok
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   431
									[exBegin]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   432
	|sock|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   433
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   434
	sock := Socket newUNIXclientTo:'/tmp/ud_socket'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   435
	sock isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   436
	    Transcript showCR:'nope'
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   437
	] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   438
	    sock buffered:false.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   439
	    Transcript showCR:'client: got it'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   440
	    'can now do transfer via sock'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   441
	    Transcript showCR:'sending <hello>'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   442
	    sock nextPutLine:'hello'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   443
	    sock close
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   444
	]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
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):
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   449
									[exBegin]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   450
	|udpSock sock addr n dataBuffer|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   451
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   452
	udpSock := Socket newUDPserverAtPort:9999.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   453
	udpSock isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   454
	    Transcript showCR:'socket setup failed.'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   455
	] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   456
	    Transcript showCR:'wait'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   457
	    udpSock readWait.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   458
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   459
	    addr := IPSocketAddress new.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   460
	    dataBuffer := ByteArray new:1000.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   461
	    n := udpSock receiveFrom:addr buffer:dataBuffer start:1 for:dataBuffer size.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   462
	    n > 0 ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   463
		Transcript showCR:('got: ' , n printString , 'bytes  from ' , addr printString).
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   464
		Transcript showCR:('data: ' , (dataBuffer copyTo:n) printString).
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   465
	    ] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   466
		Transcript showCR:'read failed'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   467
	    ].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   468
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   469
	    Transcript showCR:'close'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   470
	    udpSock close
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   471
	]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   472
									[exEnd]
4738
f915f985a54c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4736
diff changeset
   473
1007
5267b5738779 UDP examples
Claus Gittinger <cg@exept.de>
parents: 1004
diff changeset
   474
    example (connect to above UDP server and send some data;
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   475
									[exBegin]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   476
	|sock|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   477
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   478
	sock := Socket newUDP.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   479
	sock isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   480
	    Transcript showCR:'nope'
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   481
	] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   482
	    sock
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   483
		sendTo:(IPSocketAddress hostName:'127.0.0.1' port:9999) buffer:'hello world'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   484
	    sock close
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   485
	]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   486
									[exEnd]
4738
f915f985a54c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4736
diff changeset
   487
f915f985a54c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4736
diff changeset
   488
f915f985a54c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4736
diff changeset
   489
    example (UDP await packet from a client and read some multicast data):
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   490
									[exBegin]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   491
	|udpSock sock addr n dataBuffer|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   492
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   493
	udpSock := Socket newUDPserverAtPort:9999.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   494
	udpSock isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   495
	    Transcript showCR:'socket setup failed.'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   496
	] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   497
	    udpSock setSocketOption:#'IP_ADD_MEMBERSHIP' argument:#[224 1 2 3] argument:#[0 0 0 0].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   498
	    Transcript showCR:'wait'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   499
	    udpSock readWait.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   500
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   501
	    addr := IPSocketAddress new.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   502
	    dataBuffer := ByteArray new:1000.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   503
	    n := udpSock receiveFrom:addr buffer:dataBuffer start:1 for:dataBuffer size.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   504
	    n > 0 ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   505
		Transcript showCR:('got: ' , n printString , 'bytes  from ' , addr printString).
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   506
		Transcript showCR:('data: ' , (dataBuffer copyTo:n) printString).
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   507
	    ] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   508
		Transcript showCR:'read failed'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   509
	    ].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   510
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   511
	    Transcript showCR:'close'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   512
	    udpSock close
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   513
	]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   514
									[exEnd]
4738
f915f985a54c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4736
diff changeset
   515
f915f985a54c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4736
diff changeset
   516
    example (connect to above UDP server and send some multicast data):
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   517
									[exBegin]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   518
	|sock|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   519
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   520
	sock := Socket newUDP.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   521
	sock isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   522
	    Transcript showCR:'nope'
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   523
	] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   524
	    sock
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   525
		setSocketOption:#'IP_MULTICAST_TTL' argument:3 argument:nil;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   526
		sendTo:(IPSocketAddress hostName:'224.1.2.3' port:9999) buffer:'hello world'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   527
	    sock close
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   528
	]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   529
									[exEnd]
99
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
   530
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
   531
    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
   532
    Note: it dosen't use ICMP ping, but tries to reache the echo service,
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   533
	  which is disabled on most OS.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   534
									[exBegin]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   535
	|myAddress list top hosts walkProcess port|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   536
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   537
	myAddress := OperatingSystem getNetworkAddresses
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   538
			keysAndValuesSelect:[:eachIFName :eachAddress|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   539
			    eachAddress isLocal not
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   540
			    and:[eachIFName = 'wlan0']
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   541
			].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   542
	myAddress := myAddress first hostAddress.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   543
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   544
	port := Socket portOfService:'echo'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   545
	port isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   546
	    self error:'dont know echo port'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   547
	    ^ self
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   548
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   549
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   550
	top := StandardSystemView new.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   551
	top label:'PING net walk'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   552
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   553
	list := ScrollableView for:ListView in:top.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   554
	list origin:0.0@0.0 corner:1.0@1.0.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   555
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   556
	top openAndWait.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   557
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   558
	walkProcess := [
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   559
	    |l low hi direction tryHostID dottedName hostName conn addr|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   560
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   561
	    l := SortedCollection new.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   562
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   563
	    ' only works with type C-net
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   564
	      the code below could simply do 1 to:254 do:[:hostID }
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   565
	      but, to probe likely hosts earlier, the probing is done
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   566
	      ping-pong like around my ip-address (assuming, that other machines
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   567
	      have numbers around my own)'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   568
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   569
	    low := hi := (myAddress at:4).
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   570
	    direction := 1.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   571
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   572
	    [low > 0 or:[hi < 255]] whileTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   573
		direction > 0 ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   574
		    hi := hi + 1.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   575
		    tryHostID := hi.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   576
		    direction := -1.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   577
		] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   578
		    low := low - 1.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   579
		    tryHostID := low.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   580
		    direction := 1.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   581
		].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   582
		(tryHostID between:1 and:254) ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   583
		    dottedName := (myAddress at:1) printString
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   584
				  , '.' , (myAddress at:2) printString
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   585
				  , '.' , (myAddress at:3) printString
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   586
				  , '.' , tryHostID printString.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   587
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   588
		    top label:'PING net walk - trying ' , dottedName.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   589
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   590
		    top windowGroup withCursor:Cursor wait do:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   591
			conn := Socket newTCPclientToHost:dottedName port:port withTimeout:1000.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   592
			conn notNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   593
			    addr := Socket ipAddressOfHost:dottedName.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   594
			    hostName := Socket hostWithIpAddress:addr.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   595
			    hostName isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   596
				hostName :='?'
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   597
			    ].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   598
			    l add:(dottedName paddedTo:15 with:Character space)
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   599
				   , ' '
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   600
				   , (hostName paddedTo:15 with:Character space)
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   601
				   , ' up & reachable'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   602
			    list list:l.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   603
			    conn close.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   604
			]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   605
		    ].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   606
		].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   607
	    ].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   608
	    top label:'PING reachable hosts'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   609
	] forkAt:(Processor userBackgroundPriority).
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   610
	walkProcess name:'ping net walker'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   611
									[exEnd]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   612
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   613
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   614
	This example creates a simple UDP server that accepts
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   615
	single packets from anybody and broadcasts them to all
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   616
	clients that have connected so far.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   617
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   618
									[exBegin]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   619
	| socket address buffer msgSize clients |
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   620
	clients := Set new.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   621
	address := IPSocketAddress new.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   622
	buffer := String new: 1024.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   623
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   624
	socket := self newUDPserverAtPort: 6666.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   625
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   626
	Transcript showCR: 'server starting'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   627
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   628
	[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   629
	    [true] whileTrue: [
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   630
		(socket readWaitWithTimeoutMs: 200) ifFalse: [
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   631
		    msgSize := socket
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   632
			    receiveFrom: address
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   633
			    buffer: buffer
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   634
			    start: 1
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   635
			    for: buffer size.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   636
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   637
		    clients add: address copy.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   638
		    clients do: [ :clientAddress |
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   639
			    socket
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   640
				    sendTo: clientAddress
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   641
				    buffer: buffer
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   642
				    start: 1
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   643
				    for: msgSize]]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   644
	    ]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   645
	] ensure:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   646
	    Transcript showCR: 'server shutting down'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   647
	    socket close
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   648
	]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   649
									[exEnd]
817
eda0065cbd47 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 814
diff changeset
   650
eda0065cbd47 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 814
diff changeset
   651
   send a datagram to above server:
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   652
									[exBegin]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   653
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   654
	| socket address buffer host msg |
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   655
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   656
	host := Dialog
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   657
		request: 'What is the name of the server''s host?'
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   658
		initialAnswer: 'localhost'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   659
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   660
	socket := self newUDP.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   661
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   662
	address := IPSocketAddress hostName: host port: 6666.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   663
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   664
	buffer := ByteArray new: 1000.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   665
	[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   666
	    [(msg := Dialog request: 'Say something') isEmpty] whileFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   667
		| replySize stream |
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   668
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   669
		socket writeWait.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   670
		stream := buffer writeStream.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   671
		stream nextPutAll: msg.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   672
		socket sendTo:address buffer:buffer start:1 for:stream position.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   673
		socket readWait.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   674
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   675
		replySize := socket receiveFrom:address buffer:buffer.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   676
		replySize > 0 ifTrue: [
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   677
		    Transcript cr; nextPutAll: 'Server acknowledged: '.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   678
		    Transcript show: ((buffer copyFrom: 1 to: replySize) asString)
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   679
		]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   680
	    ]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   681
	] ensure: [socket close].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   682
	Transcript cr
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   683
									[exEnd]
1316
ffe05bfee9b5 example
Claus Gittinger <cg@exept.de>
parents: 1313
diff changeset
   684
ffe05bfee9b5 example
Claus Gittinger <cg@exept.de>
parents: 1313
diff changeset
   685
ffe05bfee9b5 example
Claus Gittinger <cg@exept.de>
parents: 1313
diff changeset
   686
   loopBack:
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   687
									[exBegin]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   688
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   689
	|readerTask readingSocket writingSocket|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   690
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   691
	readingSocket := self newTCPserverAtPort:9999.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   692
	readerTask :=
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   693
	    [
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   694
		|connection|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   695
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   696
		readingSocket listenFor:1.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   697
		connection := readingSocket accept.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   698
		readingSocket close.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   699
		[connection atEnd] whileFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   700
		    Transcript showCR:(connection nextLine).
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   701
		].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   702
		connection close.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   703
	    ] fork.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   704
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   705
	Delay waitForSeconds:1.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   706
	writingSocket := self newTCPclientToHost:'localhost' port:9999.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   707
	writingSocket nextPutLine:'Hello'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   708
	writingSocket nextPutLine:'World'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   709
	writingSocket close.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   710
									[exEnd]
25
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   711
"
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   712
! !
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
   713
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
   714
!Socket class methodsFor:'instance creation'!
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   715
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   716
bindTo:aSocketAddress type:aTypeSymbol
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   717
    "create a socket for a specific type
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   718
     and bind it to aSocketAddress.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   719
     Type must be:
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   720
	#stream, #datagram or #raw
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   721
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   722
     Neither connect nor connect-wait is done."
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
    |newSock|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   725
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   726
    newSock := self domain:aSocketAddress domain type:aTypeSymbol.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   727
    [
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   728
	newSock bindTo:aSocketAddress reuseAddress:true.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   729
    ] ifCurtailed:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   730
	newSock close.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   731
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   732
    ^ newSock
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   733
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   734
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   735
    "
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   736
	Socket bindTo:(IPSocketAddress anyHost port:8081) type:#stream.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   737
	Socket bindTo:(IPv6SocketAddress anyHost port:8081) type:#datagram.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   738
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   739
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   740
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   741
domain:domainSymbol type:type
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   742
    "create a socket for domain and type -
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   743
     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
   744
     Domain must be one of the symbols:
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   745
	#inet, #unix, #appletalk, #decnet, #xns, ...;
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
   746
     Type must be:
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   747
	#stream, #datagram or #raw
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   748
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   749
     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
   750
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   751
    ^ self new domain:domainSymbol type:type
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
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   754
     Socket domain:#AF_INET type:#stream
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   755
     Socket domain:#AF_INET type:#datagram
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   756
     Socket domain:#AF_INET6 type:#stream
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   757
     Socket domain:#AF_INET6 type:#datagram
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   758
     Socket domain:#AF_UNIX type:#stream
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   759
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   760
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   761
4955
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   762
newIPv6
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   763
    "create an IPv6 socket - no binding or other setup is done,
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   764
     neither connect nor connect-wait is done."
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   765
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   766
    ^ self new domain:#'AF_INET6' type:#stream
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   767
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   768
    "
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   769
      Socket newIPv6
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   770
    "
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   771
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   772
    "Created: / 27-05-2019 / 14:06:23 / Claus Gittinger"
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   773
!
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   774
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   775
newIPv6:portNrOrServiceName
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   776
    "create an IPv6 socket for a service -
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   777
     neither connect nor connect-wait is done."
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   778
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   779
    |newSock socketAddress socketAddressClass|
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   780
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   781
    newSock := self newIPv6.
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   782
    (newSock notNil and:[portNrOrServiceName notNil]) ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   783
	[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   784
	    socketAddressClass := newSock socketAddressClass.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   785
	    socketAddress := socketAddressClass hostName:nil serviceName:portNrOrServiceName type:#stream.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   786
	    socketAddress hostAddress:socketAddressClass anyAddress.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   787
	    newSock bindTo:socketAddress reuseAddress:true.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   788
	] ifCurtailed:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   789
	    newSock close.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   790
	]
4955
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   791
    ].
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   792
    ^ newSock
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   793
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   794
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   795
    "
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   796
     (Socket newIPv6:9996).
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   797
4961
a1c49a18e83d #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4955
diff changeset
   798
     test (eval the code below, then open a browser on (replace by your own IPv6 address)
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   799
	http://[2003:a:e4d:bb01:a17f:9312:54cb:fed7]:9997/
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   800
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   801
     (Socket newIPv6:9997)
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   802
	listenFor:1;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   803
	readWait;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   804
	accept.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   805
4955
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   806
    "
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   807
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   808
    "Created: / 27-05-2019 / 14:06:49 / Claus Gittinger"
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   809
!
a5b7af921f9e #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4944
diff changeset
   810
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   811
newTCP
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   812
    "create a TCP socket - no binding or other setup is done,
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   813
     neither connect nor connect-wait is done."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   814
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   815
    ^ self new domain:#'AF_INET' type:#stream
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   816
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   817
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   818
      Socket newTCP
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   819
    "
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   820
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   821
4944
f4f8254bfb3b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   822
newTCP:portNrOrServiceName
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   823
    "create a TCP socket for a service -
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   824
     neither connect nor connect-wait is done."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   825
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   826
    |newSock socketAddress socketAddressClass|
126
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
    newSock := self newTCP.
4944
f4f8254bfb3b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   829
    (newSock notNil and:[portNrOrServiceName notNil]) ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   830
	[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   831
	    socketAddressClass := newSock socketAddressClass.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   832
	    socketAddress := socketAddressClass hostName:nil serviceName:portNrOrServiceName type:#stream.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   833
	    socketAddress hostAddress:socketAddressClass anyAddress.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   834
	    newSock bindTo:socketAddress reuseAddress:true.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   835
	] ifCurtailed:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   836
	    newSock close.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   837
	]
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   838
    ].
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   839
    ^ newSock
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   840
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   841
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
   842
    "
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   843
	Socket newTCP:'http-alt'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   844
	Socket newTCP:9996.
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
   845
    "
4944
f4f8254bfb3b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   846
f4f8254bfb3b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
   847
    "Modified (comment): / 17-05-2019 / 13:10:18 / Claus Gittinger"
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   848
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   849
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   850
newTCPclientToAddress:aHostAddress port:aService
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   851
    "create a new TCP client socket connecting to a service.
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   852
     Return a socket instance if ok, nil on failure.
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   853
     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
   854
     not the whole smalltalk).
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   855
     See also: #newTCPclientToAddress:port:withTimeout:"
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   856
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   857
    ^ self newTCPclientToAddress:aHostAddress port:aService withTimeout:nil
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   858
!
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   859
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
   860
newTCPclientToAddress:aSocketAddressOrByteArray port:aService withTimeout:milliSecondsOrTimeDuration
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   861
    "create a new TCP client socket connecting to a service.
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   862
     Return a socket instance if ok, nil on failure.
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   863
     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
   864
     and return nil."
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   865
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   866
    |port socketAddress|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   867
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   868
    aService isString ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   869
	port := self portOfService:aService protocol:#tcp.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   870
    ] ifFalse:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   871
	port := aService.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   872
    ].
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
   873
    socketAddress := aSocketAddressOrByteArray isSocketAddress
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   874
			ifTrue:[aSocketAddressOrByteArray]
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   875
			ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   876
			    "Passing ByteArrays is obsolete and only supported for IPv4"
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   877
			    IPSocketAddress hostAddress:aSocketAddressOrByteArray
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   878
			].
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   879
    port notNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   880
	socketAddress port:port.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   881
    ].
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
   882
    ^ self newTCPclientToAddress:socketAddress withTimeout:milliSecondsOrTimeDuration
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
   883
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
   884
    "Modified (format): / 19-01-2018 / 18:26:51 / stefan"
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   885
!
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
   886
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
   887
newTCPclientToAddress:aSocketAddress withTimeout:milliSecondsOrTimeDuration
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   888
    "create a new TCP client socket connecting to a service.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   889
     Return a socket instance if ok, nil on failure.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   890
     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
   891
     and return nil."
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   892
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   893
    |socket|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   894
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   895
    socket := self domain:aSocketAddress domain type:#stream.
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
   896
    ^ [
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   897
	(socket connectTo:aSocketAddress withTimeout:milliSecondsOrTimeDuration)
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   898
	ifTrue:[socket] ifFalse:[socket close. nil]
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
   899
    ] ifCurtailed:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   900
	socket close.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   901
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   902
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   903
    "
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   904
	self newTCPclientToAddress:(IPv6SocketAddress hostName:'www.exept.de' port:80) withTimeout:nil
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   905
    "
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
   906
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
   907
    "Modified (format): / 19-01-2018 / 18:22:30 / stefan"
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   908
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   909
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   910
newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   911
    "create a new TCP client socket connecting to a service.
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   912
     If hostNameOrAddress is a string, try all the resolved addresses regardless
3988
7bf762b79544 #DOCUMENTATION by mawalch
mawalch
parents: 3985
diff changeset
   913
     whether for IPv4 or IPv6.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   914
     Return a socket instance if ok, nil on failure.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   915
     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
   916
     not the whole smalltalk).
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   917
     See also: #newTCPclientToHost:port:withTimeout:"
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   918
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
   919
    ^ self
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
   920
	newTCPclientToHost:hostNameOrAddress
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
   921
	port:aPortOrServiceName
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
   922
	domain:self defaultIpDomainForConnect
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
   923
	withTimeout:nil
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   924
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   925
    "
4239
827d8755965f #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4234
diff changeset
   926
      Socket newTCPclientToHost:'www.exept.de' port:'https'
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
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   929
    "Created: 31.10.1995 / 18:54:11 / cg"
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   930
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   931
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
   932
newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName domain:aDomainSymbolOrNil withTimeout:milliSecondsOrTimeDuration
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   933
    "create a new TCP client socket connecting to a service on hostNameOrAddress.
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
   934
     If hostNameOrAddress is a string, try all the resolved addresses.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   935
     Return a socket instance if ok, nil on failure.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   936
     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
   937
     Set aDomainSymbolOrNil to nil, to try all protocols.
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
   938
     If the milliSecondsOrTimeDuration arg is nonNil, stop trying to connect after that many milliSecondsOrTimeDurationeconds
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   939
     and return nil."
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   940
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
   941
    |socket addressList lastDomainSymbol lastNotification|
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   942
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   943
    hostNameOrAddress isString ifFalse:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   944
	^ self newTCPclientToAddress:hostNameOrAddress port:aPortOrServiceName withTimeout:milliSecondsOrTimeDuration.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   945
    ].
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   946
4341
2b08cb3ff1a3 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4320
diff changeset
   947
    addressList := SocketAddress
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   948
			allForHostName:hostNameOrAddress
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   949
			serviceName:aPortOrServiceName
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   950
			domain:aDomainSymbolOrNil
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   951
			type:#stream.
4341
2b08cb3ff1a3 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4320
diff changeset
   952
2b08cb3ff1a3 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4320
diff changeset
   953
    addressList do:[:eachAddress|
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   954
	|domainSymbol|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   955
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   956
	domainSymbol := eachAddress domain.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   957
	domainSymbol ~~ lastDomainSymbol ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   958
	    socket notNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   959
		socket close.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   960
	    ].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   961
	    socket := self new domain:domainSymbol type:#stream.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   962
	    lastDomainSymbol := domainSymbol.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   963
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   964
	[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   965
	    (socket connectTo:eachAddress withTimeout:milliSecondsOrTimeDuration) ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   966
		^ socket.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   967
	    ].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   968
	] on:SocketErrorNotification do:[:ex|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   969
	    lastNotification := ex.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   970
	].
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   971
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   972
    socket notNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   973
	socket close.
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
   974
    ].
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
   975
    lastNotification notNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   976
	"if someone is interested in the notification - raise the last one"
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   977
	lastNotification raiseRequest.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   978
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   979
    ^ nil.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   980
4791
1323fc4dc376 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4774
diff changeset
   981
    "<<EOC
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   982
      Socket newTCPclientToHost:'www.exept.de' port:80 domain:#'AF_INET' withTimeout:1000.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   983
      Socket newTCPclientToHost:'www.exept.de' port:80 domain:#'AF_INET6' withTimeout:1000.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   984
      Socket newTCPclientToHost:'www.exept.de' port:80 domain:nil withTimeout:1000.
4791
1323fc4dc376 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4774
diff changeset
   985
1323fc4dc376 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4774
diff changeset
   986
      [
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   987
	"if nobody is listening (Connection refused") the timeout does not apply"
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   988
	Socket newTCPclientToHost:'localhost' port:'nntp' withTimeout:10s
4791
1323fc4dc376 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4774
diff changeset
   989
      ] on:SocketErrorNotification do:[:ex|
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
   990
	ex halt.
4791
1323fc4dc376 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4774
diff changeset
   991
      ].
1323fc4dc376 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4774
diff changeset
   992
EOC"
604
b302a21afece Remove redundant code.
Stefan Vogel <sv@exept.de>
parents: 589
diff changeset
   993
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
   994
    "Modified: / 19-01-2018 / 18:12:29 / stefan"
4791
1323fc4dc376 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4774
diff changeset
   995
    "Modified: / 28-01-2019 / 10:48:18 / Stefan Vogel"
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   996
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   997
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
   998
newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName withTimeout:milliSecondsOrTimeDuration
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
   999
    "create a new TCP client socket connecting to a service on hostNameOrAddress.
3985
f4df11cffecf #OTHER by mawalch
mawalch
parents: 3843
diff changeset
  1000
     If hostNameOrAddress is a string, try all the resolved addresses regardless
3988
7bf762b79544 #DOCUMENTATION by mawalch
mawalch
parents: 3985
diff changeset
  1001
     whether for IPv4 or IPv6.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1002
     Return a socket instance if ok, nil on failure.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1003
     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
  1004
     and return nil."
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1005
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1006
    ^ self
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1007
	newTCPclientToHost:hostNameOrAddress
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1008
	port:aPortOrServiceName
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1009
	domain:self defaultIpDomainForConnect
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1010
	withTimeout:milliSecondsOrTimeDuration
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  1011
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  1012
    "Modified (format): / 19-01-2018 / 18:24:57 / stefan"
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1013
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1014
506
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
  1015
newTCPserverAtAnonymousPort
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
  1016
    "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
  1017
     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
  1018
811
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
  1019
    ^ self newTCPserverAtPort:0
506
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
  1020
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
  1021
!
11ef93d12870 support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents: 486
diff changeset
  1022
4944
f4f8254bfb3b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
  1023
newTCPserverAtPort:portNrOrServiceName
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1024
    "create a new TCP server socket providing service."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1025
4944
f4f8254bfb3b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
  1026
    ^ self newTCP:portNrOrServiceName
f4f8254bfb3b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
  1027
f4f8254bfb3b #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4942
diff changeset
  1028
    "Modified (format): / 17-05-2019 / 13:10:33 / Claus Gittinger"
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1029
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1030
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1031
newUDP
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1032
    "create a UDP socket - no binding or other setup is done,
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1033
     neither connect nor connect-wait is done."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1034
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1035
    ^ self new domain:#'AF_INET' type:#datagram
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1036
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1037
    "Socket newUDP"
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1038
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1039
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1040
newUDP:aServiceOrNil
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1041
    "create a UDP socket for a service -
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1042
     neither connect nor connect-wait is done."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1043
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1044
    |newSock socketAddressClass socketAddress|
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1045
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1046
    newSock := self newUDP.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1047
    (newSock notNil and:[aServiceOrNil notNil]) ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1048
	[
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1049
	    socketAddressClass := newSock socketAddressClass.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1050
	    socketAddress := socketAddressClass hostName:nil serviceName:aServiceOrNil type:#stream.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1051
	    socketAddress hostAddress:socketAddressClass anyAddress.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1052
	    newSock bindTo:socketAddress reuseAddress:true.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1053
	] ifCurtailed:[
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1054
	    newSock close.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1055
	]
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1056
    ].
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1057
    ^ newSock
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1058
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1059
    "
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1060
	Socket newUDP:4444.
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1061
	Socket newUDP:'activesync'.
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1062
    "
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1063
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1064
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1065
newUDPserverAtPort:aService
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1066
    "create a new UDP server socket providing service."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1067
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1068
    ^ self newUDP:aService
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1069
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1070
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1071
newUNIX
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1072
    "create a UNIX domain socket - no binding or other setup is done,
811
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
  1073
     neither connect nor connect-wait is done.
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
  1074
     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
  1075
     return nil."
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1076
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1077
    ^ self new domain:#'AF_UNIX' type:#stream
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1078
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1079
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1080
     Socket newUNIX
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1081
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1082
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1083
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1084
newUNIXclientTo:pathName
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1085
    "create a new UNIX client socket connecting to a pathname.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1086
     Return a socket instance if ok, nil on failure.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1087
     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
  1088
     not the whole smalltalk).
811
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
  1089
     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
  1090
     return nil.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1091
     See also: #newUNIXclientTo:withTimeout:"
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1092
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1093
    ^ self newUNIXclientTo:pathName withTimeout:nil
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1094
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1095
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1096
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  1097
newUNIXclientTo:pathName withTimeout:milliSecondsOrTimeDuration
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1098
    "create a new UNIX client socket connecting to a pathname.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1099
     Return a socket instance if ok, nil on failure.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1100
     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
  1101
     and return nil.
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
  1102
     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
  1103
     return nil."
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1104
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  1105
    |socket|
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  1106
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  1107
    socket := self newUNIX.
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  1108
    socket isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1109
	^ nil.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1110
    ].
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  1111
    ^ [
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1112
	(socket connectTo:(UDSocketAddress name:pathName) withTimeout:milliSecondsOrTimeDuration)
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1113
	ifTrue:[socket] ifFalse:[socket close. nil]
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  1114
    ] ifCurtailed:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1115
	socket close.
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  1116
    ].
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1117
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1118
    "
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1119
	Socket newUNIXclientTo:'/tmp/foo'
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1120
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1121
	[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1122
	    Socket newUNIXclientTo:'/tmp/foo'
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1123
	] on:SocketErrorNotification do:[:ex|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1124
	    ex halt.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1125
	]
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1126
    "
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  1127
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  1128
    "Modified (format): / 19-01-2018 / 18:23:24 / stefan"
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1129
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1130
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1131
newUNIXserverAt:pathName
811
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
  1132
    "create a new UNIX server socket providing service at a pathname.
72d9ac1a7f95 comments
Claus Gittinger <cg@exept.de>
parents: 809
diff changeset
  1133
     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
  1134
     return nil."
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1135
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1136
    ^ self bindTo:(UDSocketAddress name:pathName) type:#stream.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1137
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1138
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1139
     |s s2|
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1140
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1141
    '/tmp/foo' asFilename remove.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1142
     s := Socket newUNIXserverAt:'/tmp/foo'.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1143
     s listenFor:5.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1144
     s2 := s accept.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1145
    "
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1146
! !
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1147
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1148
!Socket class methodsFor:'Compatibility-ST80'!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1149
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1150
family:domainSymbol type:typeSymbol
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1151
    "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
  1152
     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
  1153
     Type must be #stream, #datagram or #raw."
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1154
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1155
    ^ self domain:domainSymbol type:typeSymbol
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1156
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1157
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1158
     Socket family:#AF_INET type:#stream
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1159
     Socket family:#AF_INET type:#datagram
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1160
     Socket family:#AF_UNIX type:#stream
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1161
    "
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1162
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1163
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1164
getHostname
3462
cfdc47761d0f class: Socket
Claus Gittinger <cg@exept.de>
parents: 3281
diff changeset
  1165
    "return the computer's hostname string"
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1166
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1167
    ^ OperatingSystem getHostName
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1168
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1169
    "Created: / 27.2.1998 / 02:32:17 / cg"
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1170
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1171
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1172
sockStream
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1173
    "return the type code for stream sockets"
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1174
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1175
    ^ #stream
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1176
! !
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1177
1600
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1178
!Socket class methodsFor:'Compatibility-Squeak'!
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1179
2900
0e8b305e9a98 class: Socket
Claus Gittinger <cg@exept.de>
parents: 2899
diff changeset
  1180
deadlineSecs:seconds
0e8b305e9a98 class: Socket
Claus Gittinger <cg@exept.de>
parents: 2899
diff changeset
  1181
    ^ Timestamp now addSeconds:seconds
0e8b305e9a98 class: Socket
Claus Gittinger <cg@exept.de>
parents: 2899
diff changeset
  1182
!
0e8b305e9a98 class: Socket
Claus Gittinger <cg@exept.de>
parents: 2899
diff changeset
  1183
1600
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1184
initializeNetwork
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1185
    "/ intentionally left blank here
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1186
!
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1187
4587
caa262d036d2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4570
diff changeset
  1188
openConnectionToHostNamed:hostName port:portNr
caa262d036d2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4570
diff changeset
  1189
    ^ self newTCPclientToHost:hostName port:portNr
caa262d036d2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4570
diff changeset
  1190
!
caa262d036d2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4570
diff changeset
  1191
3485
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1192
standardDeadline
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1193
    "a standard timeout in seconds for connection setup;
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1194
     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
  1195
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1196
    ^ 30
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1197
!
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1198
2292
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  1199
standardTimeout
3485
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1200
    "a standard timeout in seconds for transfers;
2afd6854bff9 class: Socket
Claus Gittinger <cg@exept.de>
parents: 3484
diff changeset
  1201
     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
  1202
2292
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  1203
    ^ 30
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  1204
!
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  1205
1600
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1206
wildcardPort
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1207
    ^ nil
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1208
! !
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1209
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1210
!Socket class methodsFor:'Compatibility-VW'!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1211
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1212
AF_INET
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1213
    ^ #AF_INET
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1214
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1215
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1216
SOCK_STREAM
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1217
    ^ #SOCK_STREAM
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1218
! !
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1219
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1220
!Socket class methodsFor:'Signal constants'!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1221
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1222
brokenConnectionSignal
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1223
    "return the signal used to tell broken connections.
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1224
     Since in unix, this is the same as the broken pipe signal,
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1225
     return that one.
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1226
     (for other Operatingsystems, this may change ..)"
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1227
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1228
    ^ PipeStream brokenPipeSignal
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1229
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1230
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1231
invalidArgumentsSignal
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1232
    "dummy for compatibility"
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1233
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1234
    ^ self errorSignal
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1235
! !
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1236
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1237
!Socket class methodsFor:'debugging'!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1238
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1239
debug:aBoolean
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1240
    "turn on/off internal debugprints.
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1241
     This method is for ST/X debugging only and
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1242
     may  be removed in later versions"
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1243
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1244
%{  /* NOCONTEXT */
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1245
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1246
    __debugging__ = (aBoolean == true);
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1247
%}
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1248
    "
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1249
     Socket debug:true
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1250
     Socket debug:false
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1251
    "
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1252
! !
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1253
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1254
!Socket class methodsFor:'defaults'!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1255
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1256
defaultIpDomainForConnect
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1257
    "answer the domain used to look up host names for connect:
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1258
	#AF_INET    use only IPv4
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1259
	#AF_INET6   use only IPv6
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1260
	nil         use both IPv4 and IPv6"
4239
827d8755965f #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4234
diff changeset
  1261
827d8755965f #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4234
diff changeset
  1262
    ^ nil
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1263
! !
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1264
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1265
!Socket class methodsFor:'obsolete'!
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1266
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1267
connectTo:service on:host
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1268
    <resource: #obsolete>
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1269
    "standard & easy client setup:
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1270
	create new client tcp socket, bind and connect;
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1271
	return the socket.
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1272
     The thread blocks (interruptable), until the connection is established."
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1273
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1274
    ^ self newTCPclientToHost:host port:(self portOfService:service).
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1275
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1276
    "
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1277
     Socket connectTo:9995 on:'clam'
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1278
     Socket connectTo:4711 on:'exept'
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1279
     Socket connectTo:'finger' on:'clam'
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1280
     Socket connectTo:'ftp' on:'exept'
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1281
     Socket connectTo:'nntp' on:(OperatingSystem getEnvironment:'NNTPSERVER')
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1282
    "
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1283
!
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1284
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1285
networkLongOrderIsMSB
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1286
    "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
  1287
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1288
    <resource: #obsolete>
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1289
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1290
    ^ true
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1291
!
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1292
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1293
networkShortOrderIsMSB
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1294
    "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
  1295
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1296
    <resource: #obsolete>
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1297
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1298
    ^ true
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1299
!
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1300
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1301
provide:aService
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1302
    "standard & easy server setup:
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1303
     create a new TCP server socket providing a service."
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1304
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1305
    <resource:#obsolete>
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1306
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1307
    |newSock|
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1308
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1309
    self obsoleteMethodWarning:'use #newTCPserverAtPort: / #listen'.
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1310
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1311
    newSock := self newTCPserverAtPort:(self portOfService:aService).
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1312
    newSock notNil ifTrue:[
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1313
	newSock listenFor:5.
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1314
    ].
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1315
    ^ newSock
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1316
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1317
    "
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1318
     Socket provide:9995
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1319
     (Socket provide:9996) accept
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1320
     Socket provide:'nntp'
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1321
    "
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1322
! !
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1323
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1324
!Socket class methodsFor:'obsolete host queries'!
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1325
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1326
appletalkAddressOfHost:aHostName
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1327
    <resource: #obsolete>
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1328
    "return the APPLETALK address for a hostname as a byteArray,
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1329
     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
  1330
     followed by the node byte.
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1331
     If the host is unknown, return nil.
812
c8101ea70e6c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 811
diff changeset
  1332
     This is the reverse operation to #hostWithAppletalkAddress:.
c8101ea70e6c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 811
diff changeset
  1333
     WARNING: untested code - I have no appletalk to test this."
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1334
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1335
    NameLookupError
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1336
	handle:[:ex |
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1337
	    ^ nil
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1338
	]
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1339
	do:[
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1340
	    ^ (AppletalkSocketAddress hostName:aHostName) address
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1341
	]
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1342
!
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1343
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1344
hostWithAppletalkAddress:addrByteArray
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1345
    <resource: #obsolete>
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1346
    "return the hostname for an APPLETALK address.
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1347
     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
  1348
     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
  1349
     The last byte is the node number.
4320
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1350
     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
  1351
     This is is the reverse operation to #appletalkAddressOfHost:.
c8101ea70e6c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 811
diff changeset
  1352
     WARNING: untested code - I have no appletalk to test this."
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1353
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1354
    NameLookupError
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1355
	handle:[:ex |
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1356
	    ^ nil
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1357
	]
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1358
	do:[
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1359
	    ^ (AppletalkSocketAddress hostAddress:addrByteArray) hostName
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1360
	]
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1361
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1362
    "
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1363
     Socket appletalkAddressOfHost:'yourAppleHere'
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1364
     Socket hostWithAppletalkAddress:#[1 2 3]
525
5891518d17a3 appletalk support - untested
Claus Gittinger <cg@exept.de>
parents: 524
diff changeset
  1365
     "
4320
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1366
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1367
    "Modified (comment): / 13-02-2017 / 20:30:56 / cg"
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1368
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1369
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1370
hostWithIpAddress:addrByteArray
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1371
    <resource: #obsolete>
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1372
    "return the hostname for an IP (internet-) address.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1373
     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
  1374
     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
  1375
     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
  1376
     This is the reverse operation to #ipAddressOfHost:."
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1377
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1378
    NameLookupError
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1379
	handle:[:ex |
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1380
	    ^ nil
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1381
	]
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1382
	do:[
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1383
	    ^ (IPSocketAddress hostAddress:addrByteArray) hostName
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1384
	]
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1385
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1386
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1387
     Socket ipAddressOfHost:'clam'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1388
     Socket hostWithIpAddress:(Socket ipAddressOfHost:'clam')
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1389
     Socket ipAddressOfHost:'porty'
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1390
     Socket hostWithIpAddress:(Socket ipAddressOfHost:'porty')
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1391
     Socket hostWithIpAddress:#[1 2 3 4]
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1392
     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
  1393
     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
  1394
     Socket hostWithIpAddress:(Socket ipAddressOfHost:'www.altavista.com')
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1395
     "
4320
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1396
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1397
    "Modified (comment): / 13-02-2017 / 20:31:01 / cg"
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1398
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1399
1278
7251c65b8d38 use nameLookup from SocketAddress for all addr-families
Claus Gittinger <cg@exept.de>
parents: 1274
diff changeset
  1400
hostWithIpV6Address:addrByteArray
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1401
    <resource: #obsolete>
820
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1402
    "return the hostname for an IPv6 (internet-) address.
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1403
     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
  1404
     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
  1405
     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
  1406
     This is the reverse operation to #ipV6AddressOfHost:."
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1407
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1408
    NameLookupError
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1409
	handle:[:ex |
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1410
	    ^ nil
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1411
	]
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1412
	do:[
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1413
	    ^ (IPv6SocketAddress hostAddress:addrByteArray) hostName
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  1414
	]
820
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1415
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1416
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1417
     Socket ipV6AddressOfHost:'clam'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1418
     Socket hostWithIpV6Address:(Socket ipAddressOfHost:'clam')
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1419
     Socket ipV6AddressOfHost:'porty'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1420
     Socket hostWithIpV6Address:(Socket ipAddressOfHost:'porty')
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1421
     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
  1422
     Socket ipV6AddressOfHost:'www.exept.de'
820
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1423
     "
4320
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1424
5b7848099dea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4293
diff changeset
  1425
    "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
  1426
!
b6860c3660c8 Fix address sizes when calling gethostbyaddr
Stefan Vogel <sv@exept.de>
parents: 819
diff changeset
  1427
6
claus
parents: 4
diff changeset
  1428
ipAddressOfHost:aHostName
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1429
    <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
  1430
    "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
  1431
     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
  1432
     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
  1433
     This is the reverse operation to #hostWithIpAddress:."
6
claus
parents: 4
diff changeset
  1434
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1435
    NameLookupError
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1436
	handle:[:ex |
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1437
	    ^ nil
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1438
	]
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1439
	do:[
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1440
	    ^ (IPSocketAddress hostName:aHostName) address
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1441
	]
21
33eb5ffad09d *** empty log message ***
claus
parents: 8
diff changeset
  1442
71
claus
parents: 63
diff changeset
  1443
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1444
     Socket ipAddressOfHost:'localhost'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1445
     Socket ipAddressOfHost:'exept'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1446
     Socket ipAddressOfHost:'1.2.3.4'
1274
f257a1b16b4d use naemLookup from SocketAddress for ipv6
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
  1447
     Socket ipAddressOfHost:'193.15.16.17'
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1448
     Socket ipAddressOfHost:'josef'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1449
     Socket ipAddressOfHost:'styx.com'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1450
     Socket hostWithIpAddress:(Socket ipAddressOfHost:'localhost')
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1451
     Socket ipAddressOfHost:(Socket hostWithIpAddress:'127.0.0.1')
71
claus
parents: 63
diff changeset
  1452
    "
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1453
!
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1454
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1455
ipV6AddressOfHost:aHostName
1911
cb6a52d6944a Mark obsolete methods.
Stefan Vogel <sv@exept.de>
parents: 1887
diff changeset
  1456
    <resource: #obsolete>
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1457
    "return the IPv6 (internet-) number for a hostname as a byteArray,
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1458
     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
  1459
     If the host is unknown, return nil.
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1460
     This is the reverse operation to #hostWithIpV6Address:."
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1461
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1462
    NameLookupError
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1463
	handle:[:ex |
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1464
	    ^ nil
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1465
	]
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1466
	do:[
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1467
	    ^ (IPv6SocketAddress hostName:aHostName) address
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  1468
	]
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1469
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1470
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1471
     Socket ipV6AddressOfHost:'localhost'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1472
     Socket ipV6AddressOfHost:'exept'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1473
     Socket ipV6AddressOfHost:'exept.exept.de'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1474
     Socket ipV6AddressOfHost:'www.google.de'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1475
     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
  1476
     Socket ipV6AddressOfHost:'josef'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1477
     Socket ipV6AddressOfHost:'styx.com'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1478
     Socket hostWithIpV6Address:(Socket ipV6AddressOfHost:'localhost')
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1479
     Socket ipV6AddressOfHost:(Socket hostV6WithIpAddress:'127.0.0.1')
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1480
    "
531
ca4c73813531 category changes
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1481
! !
ca4c73813531 category changes
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1482
ca4c73813531 category changes
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1483
!Socket class methodsFor:'queries'!
ca4c73813531 category changes
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1484
1260
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1485
peerFromDomain:domain name:peerName port:port
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1486
    |addrClass|
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1487
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1488
    addrClass := self socketAddressClassForDomain:domain.
1263
23161346a162 no longer callBack into sockets getHostName/getAddress code.
Claus Gittinger <cg@exept.de>
parents: 1260
diff changeset
  1489
    ^ 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
  1490
!
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  1491
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  1492
peerNameFromDomain:domain peer:peer
1260
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1493
    |addrClass|
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1494
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1495
    addrClass := self socketAddressClassForDomain:domain.
9a8fb2391174 instance creation of peer refactored
Claus Gittinger <cg@exept.de>
parents: 1235
diff changeset
  1496
    ^ 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
  1497
!
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  1498
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1499
portOfService:aNameOrNumber
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1500
    "returns the port-number for a given IP-service
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1501
     or nil if no such service exists;
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1502
     - used to convert service names to portNumbers"
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1503
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1504
    ^ self portOfService:aNameOrNumber protocol:nil
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1505
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1506
    "
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1507
     Socket portOfService:'finger'
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1508
     Socket portOfService:'nntp'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1509
     Socket portOfService:'echo'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1510
     Socket portOfService:'snmp'
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1511
    "
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1512
!
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1513
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1514
portOfService:aNameOrNumber protocol:aProtocol
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1515
    "returns the port-number for a given IP-service
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1516
     or nil if no such service exists;
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1517
     - used to convert service names to portNumbers"
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1518
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  1519
%{ /* UNLIMITEDSTACK(noWIN32) */
418
317e55c6c281 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 403
diff changeset
  1520
#ifndef NO_SOCKET
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1521
    struct servent *servent = NULL;
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1522
    char *protocol;
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1523
    int tryBoth = 0;
524
8287da54eccf more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents: 513
diff changeset
  1524
    short portNo;
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1525
604
b302a21afece Remove redundant code.
Stefan Vogel <sv@exept.de>
parents: 589
diff changeset
  1526
    if (__isSmallInteger(aNameOrNumber)) {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1527
	RETURN ( aNameOrNumber );
92
Claus Gittinger <cg@exept.de>
parents: 91
diff changeset
  1528
    }
Claus Gittinger <cg@exept.de>
parents: 91
diff changeset
  1529
2331
b5280e1b3b9f changed:
Stefan Vogel <sv@exept.de>
parents: 2327
diff changeset
  1530
    if (__isStringLike(aProtocol)) {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1531
	protocol = (char *)__stringVal(aProtocol);
604
b302a21afece Remove redundant code.
Stefan Vogel <sv@exept.de>
parents: 589
diff changeset
  1532
    } else {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1533
	protocol = "tcp";
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1534
	tryBoth = 1;
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1535
    }
524
8287da54eccf more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents: 513
diff changeset
  1536
2331
b5280e1b3b9f changed:
Stefan Vogel <sv@exept.de>
parents: 2327
diff changeset
  1537
    if (__isStringLike(aNameOrNumber)) {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1538
	servent = getservbyname((char *) __stringVal(aNameOrNumber), protocol);
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1539
	if (servent != NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1540
	    RETURN ( __MKSMALLINT(ntohs(servent->s_port)) );
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1541
	}
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1542
	if (tryBoth) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1543
	    servent = getservbyname((char *) __stringVal(aNameOrNumber), "udp");
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1544
	    if (servent != NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1545
		RETURN ( __MKSMALLINT(ntohs(servent->s_port)) );
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1546
	    }
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1547
	}
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1548
	RETURN ( nil );
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1549
    }
418
317e55c6c281 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 403
diff changeset
  1550
#endif
91
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1551
    RETURN ( nil );
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1552
%}
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1553
    "
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1554
     Socket portOfService:'echo' protocol:'udp'
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1555
     Socket portOfService:'echo' protocol:'tcp'
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1556
    "
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1557
!
Claus Gittinger <cg@exept.de>
parents: 85
diff changeset
  1558
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1559
protocolOfService:aNameOrNumber
819
4cc15700d123 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 817
diff changeset
  1560
    "returns the protocol (as string) for a given IP-service
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1561
     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
  1562
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
  1563
%{  /* UNLIMITEDSTACK(noWIN32) */
418
317e55c6c281 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 403
diff changeset
  1564
#ifndef NO_SOCKET
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1565
    struct servent *servent = NULL;
524
8287da54eccf more empty code for other domains;
Claus Gittinger <cg@exept.de>
parents: 513
diff changeset
  1566
    short portNo;
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1567
58
bd6753bf0401 *** empty log message ***
claus
parents: 51
diff changeset
  1568
    if (__isSmallInteger(aNameOrNumber)) {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1569
	portNo = __intVal(aNameOrNumber);
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1570
	servent = getservbyport(htons(portNo), "tcp") ;
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1571
	if (servent == NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1572
	    servent = getservbyport(htons(portNo), "udp") ;
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1573
	    if (servent == NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1574
		RETURN ( nil );
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1575
	    }
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1576
	}
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1577
    } else {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1578
	if (__isStringLike(aNameOrNumber)) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1579
	    servent = getservbyname((char *) __stringVal(aNameOrNumber), "tcp");
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1580
	    if (servent == NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1581
		servent = getservbyname((char *) __stringVal(aNameOrNumber), "udp");
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1582
		if (servent == NULL) {
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1583
		    RETURN ( nil );
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1584
		}
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1585
	    }
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1586
	}
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1587
    }
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1588
    if (servent) {
2340
2dc6e31f0142 handleType
Claus Gittinger <cg@exept.de>
parents: 2331
diff changeset
  1589
	RETURN ( __MKSTRING(servent->s_proto) );
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1590
    }
1504
92005eec5c9d fixed moving-object-when-interrupted-with-scavenge bugs;
Claus Gittinger <cg@exept.de>
parents: 1503
diff changeset
  1591
#endif /* !NO_SOCKET */
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1592
    RETURN ( nil );
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  1593
%}
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1594
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1595
     Socket protocolOfService:'finger'
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1596
     Socket protocolOfService:'nntp'
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1597
     Socket protocolOfService:'xxx'
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1598
     Socket protocolOfService:79
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1599
     Socket protocolOfService:'snmp'
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1600
    "
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1601
!
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1602
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
  1603
socketAddressClassForDomain:domain
1230
2cfb8c270cb5 more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents: 1224
diff changeset
  1604
    ^ SocketAddress knownClassFromCode:domain
2cfb8c270cb5 more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents: 1224
diff changeset
  1605
2cfb8c270cb5 more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents: 1224
diff changeset
  1606
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1607
     self socketAddressClassForDomain:#AF_INET
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1608
     self socketAddressClassForDomain:#AF_UNIX
1230
2cfb8c270cb5 more functionality moved to SocketAccessor
Claus Gittinger <cg@exept.de>
parents: 1224
diff changeset
  1609
    "
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
  1610
!
7e5f2f6b177d getHostByAddr - call is now done lazy, when the socket is asked for the peer
Claus Gittinger <cg@exept.de>
parents: 1186
diff changeset
  1611
532
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1612
supportedProtocolFamilies
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1613
    "return a collection of supported protocol families.
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1614
     This list specifies what the Socket class supports -
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1615
     socket creation may still fail, if your system was built
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1616
     without it."
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1617
1280
b973855bfe74 moved all socket constant queries from SocketHandle
Claus Gittinger <cg@exept.de>
parents: 1278
diff changeset
  1618
    ^ OperatingSystem supportedProtocolFamilies
532
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1619
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1620
    "
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1621
     Socket supportedProtocolFamilies
f997c3873602 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 531
diff changeset
  1622
    "
559
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1623
!
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1624
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1625
typeOfProtocol:aProtocol
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1626
    "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
  1627
     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
  1628
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1629
    (aProtocol = 'tcp') ifTrue:[^ #stream].
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1630
    (aProtocol = 'udp') ifTrue:[^ #datagram].
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1631
    (aProtocol = 'ip')  ifTrue:[^ #raw].
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1632
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1633
     unix domain
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1634
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1635
    (aProtocol = 'ud')  ifTrue:[^ #stream].
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1636
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1637
     add x25 stuff (if any) here ...
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1638
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1639
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1640
     add appletalk stuff (if any) here ...
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1641
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1642
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1643
     add other stuff (if any) here ...
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1644
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1645
    ^ nil
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1646
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1647
    "
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1648
     Socket typeOfProtocol:'tcp'
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1649
     Socket typeOfProtocol:'ucp'
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1650
     Socket typeOfProtocol:(Socket protocolOfService:'nntp')
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1651
     Socket typeOfProtocol:(Socket protocolOfService:'echo')
61012867ebed cleanup if #connect is interrupted
Claus Gittinger <cg@exept.de>
parents: 551
diff changeset
  1652
    "
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1653
! !
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1654
1202
c52279df60d8 method category rename
Claus Gittinger <cg@exept.de>
parents: 1187
diff changeset
  1655
!Socket methodsFor:'Compatibility-Dolphin'!
1110
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1656
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1657
setReceiveTimeout: milliseconds
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1658
    self receiveTimeout:(milliseconds / 1000)
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1659
!
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1660
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1661
setSendTimeout: milliseconds
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1662
    self sendTimeout:(milliseconds / 1000)
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1663
! !
0876a3d30fd2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1035
diff changeset
  1664
1202
c52279df60d8 method category rename
Claus Gittinger <cg@exept.de>
parents: 1187
diff changeset
  1665
!Socket methodsFor:'Compatibility-ST80'!
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  1666
887
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1667
acceptNonBlock
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1668
    ^ self accept
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1669
!
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1670
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  1671
ioConnection
887
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1672
    ^ self
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1673
!
2b2dc6b59b5a more VW compatibility
Claus Gittinger <cg@exept.de>
parents: 876
diff changeset
  1674
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1675
notReadySignal
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1676
    "ST-80 mimicry.
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1677
     for now - this is not yet raised"
71
claus
parents: 63
diff changeset
  1678
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  1679
    ^ Signal new
486
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1680
!
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1681
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
  1682
readAppendStream
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1683
    "ST-80 mimicry.
4293
82b9928ddcae #OTHER by mawalch
mawalch
parents: 4288
diff changeset
  1684
     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
  1685
     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
  1686
     the actual stream.
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1687
     In ST/X, sockets inherit from stream, so
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1688
     this method returns the receiver, for transparency"
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1689
576
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
  1690
    ^ self
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
  1691
!
13acab81fad0 more ST80 compatibility
ca
parents: 572
diff changeset
  1692
486
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1693
readStream
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1694
    "ST-80 mimicry.
4293
82b9928ddcae #OTHER by mawalch
mawalch
parents: 4288
diff changeset
  1695
     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
  1696
     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
  1697
     the actual stream.
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1698
     In ST/X, sockets inherit from stream, so
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1699
     this method returns the receiver, for transparency"
486
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1700
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1701
    ^ self
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1702
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1703
    "Created: 24.1.1997 / 23:52:57 / cg"
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1704
!
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1705
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1706
writeStream
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1707
    "ST-80 mimicry.
4293
82b9928ddcae #OTHER by mawalch
mawalch
parents: 4288
diff changeset
  1708
     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
  1709
     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
  1710
     the actual stream.
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1711
     In ST/X, sockets inherit from stream, so
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  1712
     this method returns the receiver, for transparency"
486
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1713
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1714
    ^ self
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1715
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1716
    "Created: 24.1.1997 / 10:34:35 / cg"
ed4e215e83ef ST-80 mimicry
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
  1717
    "Modified: 24.1.1997 / 23:52:52 / cg"
71
claus
parents: 63
diff changeset
  1718
! !
claus
parents: 63
diff changeset
  1719
1202
c52279df60d8 method category rename
Claus Gittinger <cg@exept.de>
parents: 1187
diff changeset
  1720
!Socket methodsFor:'Compatibility-Squeak'!
771
74abb04ef57c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 752
diff changeset
  1721
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1722
address
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1723
    ^ self getSocketAddress
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1724
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1725
2308
d8b410a4f38b added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents: 2292
diff changeset
  1726
connectToHostNamed:hostName port:portNr
d8b410a4f38b added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents: 2292
diff changeset
  1727
    self connectTo:hostName port:portNr
d8b410a4f38b added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents: 2292
diff changeset
  1728
!
d8b410a4f38b added: #connectToHostNamed:port:
Claus Gittinger <cg@exept.de>
parents: 2292
diff changeset
  1729
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1730
dataAvailable
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1731
    ^ self canReadWithoutBlocking
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1732
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1733
1886
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1734
destroy
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1735
    self close
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1736
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1737
    "Created: / 04-06-2007 / 21:29:03 / cg"
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1738
!
e0d23764c809 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1885
diff changeset
  1739
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1740
listenOn:aPortNr
1882
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1741
    self listenOn:aPortNr backlogSize:5
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1742
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1743
    "Modified: / 31-05-2007 / 17:59:53 / cg"
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1744
!
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1745
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1746
listenOn:aPortNr backlogSize:aNumber
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1747
    self bindTo:aPortNr address:nil.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1748
    self listenFor:aNumber
1882
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1749
6f818f33c85a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1863
diff changeset
  1750
    "Created: / 31-05-2007 / 17:59:47 / cg"
1462
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1751
!
5602fce52b42 squeak stuff
Claus Gittinger <cg@exept.de>
parents: 1413
diff changeset
  1752
4587
caa262d036d2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4570
diff changeset
  1753
noTimeout
caa262d036d2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4570
diff changeset
  1754
    "disable timeouts - dummy for now"
caa262d036d2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4570
diff changeset
  1755
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1756
    ^ self
4587
caa262d036d2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4570
diff changeset
  1757
!
caa262d036d2 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4570
diff changeset
  1758
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1759
peerName
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1760
    "return my peer (i.e. ipAddr + port);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1761
     May return nil if not yet setup completely."
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1762
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1763
    ^ self getPeer
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1764
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1765
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  1766
sendData: aStringOrByteArray
3155
d1884a37375c Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents: 3152
diff changeset
  1767
    "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
  1768
     Return the number of bytes sent."
d1884a37375c Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents: 3152
diff changeset
  1769
d1884a37375c Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents: 3152
diff changeset
  1770
    ^ self nextPutBytes:aStringOrByteArray size from:aStringOrByteArray startingAt:1.
1600
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1771
!
f3acd5382fa1 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1584
diff changeset
  1772
2904
d2e1eead2bfb class: Socket
Claus Gittinger <cg@exept.de>
parents: 2900
diff changeset
  1773
waitForConnectionUntil:aTimestamp
d2e1eead2bfb class: Socket
Claus Gittinger <cg@exept.de>
parents: 2900
diff changeset
  1774
    "return true if connected"
d2e1eead2bfb class: Socket
Claus Gittinger <cg@exept.de>
parents: 2900
diff changeset
  1775
d2e1eead2bfb class: Socket
Claus Gittinger <cg@exept.de>
parents: 2900
diff changeset
  1776
    self readWaitWithTimeoutMs: (aTimestamp millisecondDeltaFrom:Timestamp now).
d2e1eead2bfb class: Socket
Claus Gittinger <cg@exept.de>
parents: 2900
diff changeset
  1777
    ^ self isConnected
771
74abb04ef57c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 752
diff changeset
  1778
! !
74abb04ef57c checkin from browser
Claus Gittinger <cg@exept.de>
parents: 752
diff changeset
  1779
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1780
!Socket methodsFor:'accepting connections'!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1781
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1782
accept
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1783
    "create a new TCP socket from accepting on the receiver.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1784
     This method will suspend the current process if no connection is waiting.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1785
     For ST-80 compatibility"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1786
3633
3a21f142432e #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3620
diff changeset
  1787
    ^ self waitForNewConnectionWithTimeout:nil
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1788
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1789
    "
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1790
     |sock newSock|
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1791
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1792
     sock := Socket provide:8004.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1793
     sock listenFor:5.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1794
     newSock := sock accept.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1795
    "
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1796
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1797
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1798
blockingAccept
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1799
    "create a new TCP socket from accepting on the receiver.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1800
     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
  1801
     For ST-80 compatibility"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1802
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1803
    |newSock|
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1804
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1805
    newSock := self class new.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1806
    (newSock primAcceptOn:self blocking:true) ifFalse:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1807
	"should raise an error here - primitive code raises a notification"
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1808
	^ nil
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1809
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1810
    ^ newSock
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  1811
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  1812
    "Modified (format): / 19-01-2018 / 13:32:25 / stefan"
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1813
! !
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1814
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1815
!Socket methodsFor:'binding'!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1816
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1817
bindAnonymously
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1818
    "bind to any address. A free port will be allocated.
4942
9f424bed67c4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4796
diff changeset
  1819
     Our own socket address will be determined after connection set up.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1820
     This is the default after the socket has been created"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1821
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1822
    ^ self bindTo:nil reuseAddress:false
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1823
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1824
    "
1938
831af28cc848 comments
Stefan Vogel <sv@exept.de>
parents: 1935
diff changeset
  1825
      self newTCP bindAnonymously; listenFor:1; yourself
4206
ac9a6e096796 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4203
diff changeset
  1826
      self newTCP bindAnonymously; port
1938
831af28cc848 comments
Stefan Vogel <sv@exept.de>
parents: 1935
diff changeset
  1827
    "
4942
9f424bed67c4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4796
diff changeset
  1828
9f424bed67c4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4796
diff changeset
  1829
    "Modified (comment): / 08-05-2019 / 14:40:11 / Claus Gittinger"
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1830
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1831
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1832
bindAnonymouslyToAddress:aSocketAddress
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1833
    "bind to address addressString.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1834
     A free port will be allocated"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1835
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1836
    ^ self
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1837
	bindTo:0
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1838
	address:aSocketAddress
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  1839
	reuseAddress:false
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1840
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1841
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1842
      self newTCP bindAnonymouslyToAddress:IPSocketAddress localHost; listenFor:1; yourself
1938
831af28cc848 comments
Stefan Vogel <sv@exept.de>
parents: 1935
diff changeset
  1843
    "
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1844
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1845
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1846
bindTo:aSocketAddress
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1847
    "ST80 compatible bind, expecting a socketAddress argument.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1848
     The socketAddress object (an instance of SocketAddress)
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1849
     is supposed to respond to #portOrName and #address requests."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1850
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1851
    ^ self bindTo:aSocketAddress reuseAddress:true
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1852
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1853
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1854
bindTo:portNrOrNameString address:addressString
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1855
    "Old interface: bind the socket to an address
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1856
     - returns true if ok, false otherwise.
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  1857
     Notify with a SocketErrorNotification on error.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1858
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1859
     The interpretation of hostOrPathNameOrSocketAddrOrNil portNrOrName depends on the domain:
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1860
	Best use a SocketAddress
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1861
	For backward compatibility:
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1862
	    AF_INET domain can also use (4byte) byteArray like internet numbers,
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1863
	    AF_UNIX domain cab use pathname strings."
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1864
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1865
    ^ self
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1866
	bindTo:portNrOrNameString
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1867
	address:addressString
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1868
	reuseAddress:true
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  1869
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  1870
    "Modified (comment): / 19-01-2018 / 13:36:54 / stefan"
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1871
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1872
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1873
bindTo:portNrOrNameOrNil address:hostOrPathNameOrSocketAddrOrNil reuseAddress:reuse
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1874
    "Old interface: bind the socket to an address
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  1875
     Notify with a SocketErrorNotification on error.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1876
     - returns true if ok, false otherwise.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1877
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1878
     The interpretation of hostOrPathNameOrSocketAddrOrNil portNrOrName depends on the domain:
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1879
	Best use a SocketAddress
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1880
	For backward compatibility:
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1881
	    AF_INET domain can also use (4byte) byteArray like internet numbers,
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1882
	    AF_UNIX domain can use pathname strings.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1883
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1884
     The reuse boolean argument controls if the SO_REUSEADDR socket option
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1885
     is to be set (to avoid the 'bind: address in use' error).
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1886
    "
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1887
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1888
    |socketAddress|
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1889
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1890
    handle isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1891
	^ self errorNotOpen
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1892
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1893
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1894
    hostOrPathNameOrSocketAddrOrNil isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1895
	socketAddress := self socketAddressClass anyHost.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1896
    ] ifFalse:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1897
	(hostOrPathNameOrSocketAddrOrNil isSocketAddress) ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1898
	    socketAddress := hostOrPathNameOrSocketAddrOrNil.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1899
	] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1900
	    "backward compatibility: support for byteArray and string arg"
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1901
	    hostOrPathNameOrSocketAddrOrNil isString ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1902
		socketAddress := self socketAddressClass hostName:hostOrPathNameOrSocketAddrOrNil.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1903
	    ] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1904
		hostOrPathNameOrSocketAddrOrNil isByteCollection ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1905
		    ^ self error:'bindTo: bad host (socketAddress) argument'
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1906
		].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1907
		socketAddress := self socketAddressClass hostAddress:hostOrPathNameOrSocketAddrOrNil.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1908
	    ].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1909
	].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1910
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1911
    portNrOrNameOrNil notNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1912
	socketAddress port:portNrOrNameOrNil.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1913
    ].
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1914
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1915
    ^ self bindTo:socketAddress reuseAddress:reuse.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1916
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1917
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1918
     (Socket domain:#'AF_INET' type:#stream)
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1919
	 bindTo:2144 address:nil; yourself
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1920
    "
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  1921
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  1922
    "Modified (comment): / 19-01-2018 / 13:37:13 / stefan"
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
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1925
bindTo:aSocketAddress reuseAddress:reuse
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1926
    "Bind the socket to aSocketAddress - returns true if ok, false otherwise.
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  1927
     Notify with a SocketErrorNotification on error.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1928
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1929
     The reuse boolean argument controls if the SO_REUSEADDR socket option
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1930
     is to be set (to avoid the 'bind: address in use' error).
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1931
3137
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  1932
     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
  1933
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1934
    |ok error socketAddress|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1935
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1936
    handle isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1937
	^ self errorNotOpen
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1938
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1939
    socketAddress := aSocketAddress.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1940
    socketAddress isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1941
	"ok, get an all zero socket address, so it is for anyHost
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1942
	 and the port will be assigned"
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1943
	socketAddress := self socketAddressClass new.
3513
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  1944
    ].
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  1945
    domain == #'AF_INET6' ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1946
	"accept also IPv4 connections on IPv6 sockets (this is off by default for windows"
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1947
	self setSocketOption:#'IPV6_V6ONLY' argument:false argument:nil.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1948
    ].
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1949
    ok := false.
1935
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
%{  /* STACK: 100000 */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1952
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1953
    OBJ fp = __INST(handle);
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1954
    SOCKET sock;
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1955
    union sockaddr_u sa;
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1956
    int sockaddr_size;
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  1957
    INT ret;
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1958
    int sockAddrOffs;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1959
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1960
    if (fp == nil) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1961
	goto getOutOfHere;
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1962
    }
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1963
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1964
    if (! __isBytes(socketAddress)) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1965
	error = __mkSmallInteger(-1);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1966
	goto getOutOfHere;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1967
    }
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1968
    /* get the socket-address */
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  1969
    if (__isNonNilObject(socketAddress)){
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1970
	int nIndex;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1971
	OBJ cls = __qClass(socketAddress);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1972
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1973
	sockAddrOffs = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1974
	nIndex = __qSize(socketAddress) - OHDR_SIZE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1975
	sockaddr_size = nIndex - sockAddrOffs;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1976
	if (sockaddr_size > sizeof(sa)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1977
	    error=__mkSmallInteger(-2);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1978
	    goto getOutOfHere;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1979
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1980
	memcpy(&sa, __byteArrayVal(socketAddress) + sockAddrOffs, sockaddr_size);
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1981
    }
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1982
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1983
    sock = SOCKET_FROM_FILE_OBJECT(fp);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1984
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1985
# ifdef SO_REUSEADDR
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1986
    if (reuse == true) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1987
	int on = 1;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1988
	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1989
	    DBGPRINTF(("SOCKET: setsockopt - SO_REUSEADDR failed\n"));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1990
	}
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1991
    }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1992
# endif /* SO_REUSEADDR */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1993
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1994
# ifdef BIND_BLOCKS
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  1995
#  ifdef DO_WRAP_CALLS
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  1996
    do {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1997
	__threadErrno = 0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  1998
	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
  1999
    } while ((ret < 0) && (__threadErrno == EINTR));
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2000
    if (ret < 0) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2001
	errno = __threadErrno;
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2002
    }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2003
#  else
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2004
    __BEGIN_INTERRUPTABLE__
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2005
    do {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2006
	ret = bind(sock, (struct sockaddr *)&sa, sockaddr_size);
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2007
    } while ((ret < 0) && (errno == EINTR));
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2008
    __END_INTERRUPTABLE__
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2009
#  endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2010
# else
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2011
    errno = 0;
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2012
    ret = bind(sock, (struct sockaddr *)&sa, sockaddr_size);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2013
# endif
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2014
    if (ret < 0) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2015
# ifdef __win32__
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2016
	if (errno == 0) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2017
	    errno = WSAGetLastError();
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2018
	}
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2019
# endif
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2020
	DBGPRINTF(("SOCKET: bind failed errno=%d\n", errno));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2021
	error = __INST(lastErrorNumber) = __MKSMALLINT(errno);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2022
	goto getOutOfHere;
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2023
    } else {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2024
	ok = true;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2025
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2026
#endif /* NO_SOCKET */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2027
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2028
getOutOfHere: ;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2029
%}.
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2030
    ok ifFalse:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2031
	|errorHolder errorString|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2032
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2033
	error isInteger ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2034
	    errorHolder := OperatingSystem errorHolderForNumber:error.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2035
	    errorString := errorHolder errorString.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2036
	] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2037
	    errorString := error.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2038
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2039
	OpenError newException
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2040
	    errorString:('cannot bind socket to address: %1 (%2)'
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2041
			    bindWith:socketAddress
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2042
			    with:errorString);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2043
	    errorCode:error;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2044
	    osErrorHolder:errorHolder;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2045
	    parameter:self;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2046
	    raiseRequest.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2047
	"maybe someone catches the error and binds to some other port..."
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2048
	^ true.
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
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2051
    port := socketAddress port.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2052
    port == 0 ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2053
	"this is a bind to a random port, now we can get the real port"
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2054
	port := self getFullSocketAddress port.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2055
    ].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2056
    ^ true
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2057
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2058
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2059
     (Socket domain:#'AF_INET' type:#stream)
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2060
	bindTo:(IPSocketAddress anyHost port:445) reuseAddress:false;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2061
	yourself.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2062
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2063
     (Socket domain:#'AF_INET' type:#stream)
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2064
	bindTo:139 reuseAddress:false;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2065
	yourself.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2066
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2067
     (Socket domain:#'AF_INET6' type:#stream)
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2068
	bindTo:nil reuseAddress:false;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2069
	yourself.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2070
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2071
     (Socket domain:#'AF_INET' type:#stream)
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2072
	bindTo:(IPSocketAddress localHost port:2122) reuseAddress:false;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2073
	yourself.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2074
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2075
     (Socket domain:#'AF_UNIX' type:#stream)
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2076
	bindTo:nil reuseAddress:false;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2077
	yourself.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2078
    "
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2079
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2080
    "Modified: / 19-01-2018 / 13:37:21 / stefan"
4942
9f424bed67c4 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4796
diff changeset
  2081
    "Modified (format): / 08-05-2019 / 14:41:18 / Claus Gittinger"
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2082
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2083
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2084
listenFor:aNumber
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2085
    "start listening; return true if ok, false on error.
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2086
     Notify with a SocketErrorNotification on error.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2087
     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
  2088
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2089
    |err|
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2090
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2091
    handle isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2092
	^ self errorNotOpen
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2093
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2094
%{
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2095
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2096
    OBJ fp = __INST(handle);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2097
    SOCKET sock;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2098
    int ret;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2099
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2100
    if (! __isSmallInteger(aNumber)) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2101
	DBGPRINTF(("SOCKET: invalid arg\n"));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2102
	err = @symbol(badArgument);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2103
	goto out;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2104
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2105
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2106
    sock = SOCKET_FROM_FILE_OBJECT(fp);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2107
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2108
#ifdef LISTEN_BLOCKS
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2109
# ifdef DO_WRAP_CALLS
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2110
    do {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2111
	__threadErrno = 0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2112
	ret = STX_WSA_NOINT_CALL2("listen", listen, sock, __intVal(aNumber));
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2113
    } while ((ret < 0) && (__threadErrno == EINTR));
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2114
    if (ret < 0) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2115
	errno = __threadErrno;
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2116
    }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2117
# else
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2118
    __BEGIN_INTERRUPTABLE__
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2119
    do {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2120
	ret = listen(sock, __intVal(aNumber));
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2121
    } while ((ret < 0) && (errno == EINTR));
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2122
    __END_INTERRUPTABLE__
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2123
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2124
#else
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2125
    errno = 0;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2126
    ret = listen(sock, __intVal(aNumber));
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2127
#endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2128
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2129
    if (ret < 0) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2130
# ifdef __win32__
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2131
	if (errno == 0) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2132
	    errno = WSAGetLastError();
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2133
	}
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2134
# endif
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2135
	err = __MKSMALLINT(errno);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2136
	DBGPRINTF(("SOCKET: listen call failed errno=%d\n", errno));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2137
	goto out;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2138
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2139
#else
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2140
    RETURN (false);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2141
#endif
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2142
out:;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2143
%}.
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2144
    err notNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2145
	err isSymbol ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2146
	    self error:err.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2147
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2148
	^ self reportError:err.
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2149
    ].
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2150
3157
44f5f9f79256 initialize methods
Stefan Vogel <sv@exept.de>
parents: 3155
diff changeset
  2151
    listening := aNumber.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2152
    ^ true
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2153
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2154
    "
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2155
       [
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2156
	   |sock|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2157
	   sock := Socket newTCP.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2158
	   sock listenFor:1.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2159
	   sock
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2160
       ] on:SocketErrorNotification do:[:ex|
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2161
	   ex halt.
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2162
       ].
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2163
    "
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2164
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2165
    "Modified (comment): / 19-01-2018 / 13:50:09 / stefan"
1935
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
1576
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  2168
!Socket methodsFor:'closing'!
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  2169
4172
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2170
abortAndClose
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2171
    "immediately abort the connection:
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  2172
	discard buffered data and close the stream"
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  2173
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  2174
    self linger:0.
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  2175
    self close.
3224
bad706c50347 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3223
diff changeset
  2176
!
bad706c50347 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3223
diff changeset
  2177
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2178
shutDown
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  2179
    "shutDown (initiate a graceful close)
4172
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2180
     and close (free the filedescriptor) the socket.
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  2181
     The close will return immediately and buffered data will be sent in the
4172
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2182
     background, unless you set linger"
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2183
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2184
    self shutdown:2.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2185
    self close
1576
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  2186
!
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  2187
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2188
shutDownInput
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2189
    "shutDown the input side of the socket.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2190
     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
  2191
     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
  2192
     (e.g. setting the TCP-Windowsize to 0)"
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  2193
a2ffb22c5f5e New: #shutDownInput #shutDownOutput
Stefan Vogel <sv@exept.de>
parents: 1568
diff changeset
  2194
    self shutdown:0.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2195
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2196
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2197
shutDownOutput
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2198
    "shutDown the output side of the socket.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2199
     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
  2200
     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
  2201
     has been sent, so the other side will get a end-of-file condition eventually.
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  2202
     If you set linger > 0, the operation will wait until buffered data
4172
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2203
     has been delivered to the peer.
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  2204
     Otherwise the operation returns immediately."
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  2205
1884
ee0d640a25a7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1882
diff changeset
  2206
    self shutdown:1.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2207
! !
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2208
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2209
!Socket methodsFor:'connecting'!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2210
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2211
connectTo:aSocketAddress
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2212
    ^ self connectTo:aSocketAddress withTimeout:nil
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2213
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2214
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2215
connectTo:hostOrPathName port:portNrOrName
3655
eb81fa3d8407 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3633
diff changeset
  2216
    <resource: #obsolete>
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2217
    "Backward compatibility connect; connect to port, portNrOrNameOrNil on host, hostOrPathNameOrSocketAddr.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2218
     For backward compatibility, hostOrPathNameOrSocketAddr may be also a string or a byteArray,
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2219
     but it is recommended to pass SocketAddress instances.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2220
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2221
     Return true if ok, false otherwise.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2222
     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
  2223
     See also: #connectTo:port:withTimeout: for a somewhat nicer interface."
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
    ^ self connectTo:hostOrPathName port:portNrOrName withTimeout:nil
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2226
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2227
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  2228
connectTo:hostOrPathNameOrSocketAddr port:portNrOrNameOrNil withTimeout:milliSecondsOrTimeDuration
3655
eb81fa3d8407 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3633
diff changeset
  2229
    <resource: #obsolete>
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2230
    "Backward compatibility connect; connect to port, portNrOrNameOrNil on host, hostOrPathNameOrSocketAddr.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2231
     For backward compatibility, hostOrPathNameOrSocketAddr may be also a string or a byteArray,
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2232
     but it is recommended to pass SocketAddress instances.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2233
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2234
     Return true if ok, false otherwise.
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2235
     Notify with a SocketErrorNotification on error.
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2236
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2237
     The current process will block (but not the whole Smalltalk) until the connection is established,
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  2238
     or milliSecondsOrTimeDuration milliseconds have passed."
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2239
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2240
    |domainClass socketAddress|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2241
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2242
    (hostOrPathNameOrSocketAddr isSocketAddress) ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2243
	socketAddress := hostOrPathNameOrSocketAddr.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2244
	portNrOrNameOrNil notNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2245
	    socketAddress port:portNrOrNameOrNil.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2246
	].
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2247
    ] ifFalse:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2248
	"backward compatibility: support for byteArray and string arg"
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2249
	domainClass := self class socketAddressClassForDomain:domain.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2250
	domainClass isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2251
	    ^ self error:'invalid (unsupported) domain'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2252
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2253
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2254
	hostOrPathNameOrSocketAddr isString ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2255
	    socketAddress := domainClass hostName:hostOrPathNameOrSocketAddr serviceName:portNrOrNameOrNil type:#SOCK_STREAM.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2256
	    peerName := hostOrPathNameOrSocketAddr.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2257
	] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2258
	    hostOrPathNameOrSocketAddr isByteCollection ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2259
		^ self error:'connectTo: bad host (socketAddress) argument'
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2260
	    ].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2261
	    socketAddress := domainClass hostAddress:hostOrPathNameOrSocketAddr port:portNrOrNameOrNil.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2262
	].
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2263
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2264
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  2265
    ^ self connectTo:socketAddress withTimeout:milliSecondsOrTimeDuration.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2266
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2267
    "
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2268
       |sock|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2269
       sock := Socket newTCP.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2270
       sock connectTo:'localhost' port:21 withTimeout:1000.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2271
       sock
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2272
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2273
       |sock|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2274
       sock := Socket newTCP.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2275
       sock connectTo:'localhost' port:9876 withTimeout:2000.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2276
       sock
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2277
    "
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2278
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  2279
    "Modified (comment): / 19-01-2018 / 17:48:08 / stefan"
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2280
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2281
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  2282
connectTo:aSocketAddress withTimeout:milliSecondsOrTimeDuration
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2283
    "Connect to a SocketAddress.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2284
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2285
     Return true if ok, false otherwise.
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2286
     Notify with a SocketErrorNotification on error.
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2287
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2288
     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
  2289
     or timeout milliseconds have passed."
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2290
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  2291
    |isAsync err timeoutMs milliSeconds|
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2292
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2293
    handle isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2294
	^ self errorNotOpen
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2295
    ].
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2296
    isAsync := false.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2297
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2298
%{  /* STACK: 100000 */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2299
#ifndef NO_SOCKET
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2300
//#define DUMP_ADDRESS 1
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2301
    OBJ fp = __INST(handle);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2302
    union sockaddr_u sa;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2303
    SOCKET sock;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2304
    int a;
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  2305
    INT ret;
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  2306
    int oldFlags;
3741
2a39a558012f mingw change
Claus Gittinger <cg@exept.de>
parents: 3657
diff changeset
  2307
# ifdef __MINGW64__
2a39a558012f mingw change
Claus Gittinger <cg@exept.de>
parents: 3657
diff changeset
  2308
    unsigned long on = 1;
2a39a558012f mingw change
Claus Gittinger <cg@exept.de>
parents: 3657
diff changeset
  2309
# else
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2310
    int on = 1;
3741
2a39a558012f mingw change
Claus Gittinger <cg@exept.de>
parents: 3657
diff changeset
  2311
#endif
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2312
    int sockaddr_size;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2313
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2314
    if (!__isNonNilObject(aSocketAddress) || !__isBytes(aSocketAddress)) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2315
	DBGPRINTF(("SOCKET: invalid socketAddress\n"));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2316
	err = @symbol(argumentError);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2317
	goto out;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2318
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2319
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2320
    {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2321
	int sockAddrOffs = 0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2322
	int nIndex =__byteArraySize(aSocketAddress);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2323
	OBJ cls = __qClass(aSocketAddress);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2324
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2325
	//if (cls != @global(ByteArray))
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2326
	//    sockAddrOffs = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2327
	sockaddr_size = nIndex - sockAddrOffs;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2328
	if (sockaddr_size > sizeof(sa)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2329
	    DBGPRINTF(("SOCKET: invalid (short) socketAddress\n"));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2330
	    err = @symbol(argumentError);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2331
	    goto out;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2332
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2333
	memcpy(&sa, __byteArrayVal(aSocketAddress) + sockAddrOffs, sockaddr_size);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2334
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2335
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2336
    sock = SOCKET_FROM_FILE_OBJECT(fp);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2337
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2338
#ifdef __win32__
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2339
     ioctlsocket(sock, FIONBIO, &on);
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2340
#elif defined(O_NONBLOCK)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2341
    /*
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2342
     * set to non-blocking and wait later
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2343
     */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2344
    oldFlags = fcntl(sock, F_GETFL, 0);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2345
    /* on SUNOS4.x, would use fcntl(osfd, F_SETFL, flags | FNDELAY); */
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2346
    fcntl(sock, F_SETFL, oldFlags | O_NONBLOCK);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2347
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2348
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2349
    /*
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2350
     * connect
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2351
     */
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2352
4039
87f0dc2e89d8 #OTHER by mawalch
mawalch
parents: 4038
diff changeset
  2353
// we do not use wrap calls any longer, because they have problems with aborting.
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2354
// we use nonblocking accept() instead.
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2355
# if 0 && defined(DO_WRAP_CALLS)
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  2356
    // __setWrapCallDebugging(1,1);
2414
886ecf4b36cd changed:
ab
parents: 2340
diff changeset
  2357
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2358
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2359
    do {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2360
	DBGFPRINTF((stderr, "SOCKET: (sock=%d) connect...\n", sock));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2361
	ret = STX_WSA_NOINT_CALL3("connect", connect, sock, &sa, (INT)sockaddr_size);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2362
	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
  2363
    } while ((ret < 0) && (__threadErrno == EINTR));
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2364
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2365
    if (ret < 0) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2366
	int optLen = sizeof(errno);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2367
	errno = __threadErrno;
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  2368
#if 0
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2369
	if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &errno, &optLen) == SOCKET_ERROR) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2370
	    DBGFPRINTF((stderr, "SOCKET: getsockopt(SO_ERROR) failed: %d\n", WSAGetLastError()));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2371
	}
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2372
#endif // 0
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2373
    }
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2374
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2375
    // __setWrapCallDebugging(1,0);
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2376
# else // !DO_WRAP_CALLS
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2377
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2378
#  if !defined(__win32__) && !defined(O_NONBLOCK)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2379
    __BEGIN_INTERRUPTABLE__
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2380
#  endif
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2381
    do {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2382
	ret = connect(sock, (struct sockaddr *)&sa, sockaddr_size);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2383
    } while ((ret < 0)
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2384
#  ifdef __win32__
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2385
	     && (errno = WSAGetLastError())
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2386
#  endif
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2387
	     && ((errno == EINTR)
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2388
#  ifdef EAGAIN
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2389
		 || (errno == EAGAIN)
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2390
#  endif
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2391
		));
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2392
#  if !defined(__win32__) && !defined(O_NONBLOCK)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2393
    __END_INTERRUPTABLE__
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2394
#  endif
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2395
#endif
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2396
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2397
#if defined(__win32__) && !defined(EINPROGRESS)
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2398
# define EINPROGRESSS WSAEINPROGRESSS
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2399
# define EALREADY WSAEALREADY
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2400
#endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2401
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2402
    if (ret < 0) {
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2403
# if defined(EINPROGRESS) || defined(EALREADY)
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2404
	if (0
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2405
#  ifdef __win32__
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2406
	    || (errno == WSAEWOULDBLOCK)
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2407
#  endif
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2408
#  ifdef EINPROGRESS
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2409
	    || (errno == EINPROGRESS)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2410
#  endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2411
#  ifdef EALREADY
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2412
	    || (errno == EALREADY)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2413
#  endif
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2414
	) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2415
	    /*
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2416
	     * This was a nonblocking operation that will take some time.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2417
	     * Do a select on read to get informed when the operation is ready.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2418
	     */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2419
	    DBGFPRINTF((stderr, "SOCKET: isAsync is true\n"));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2420
	    isAsync = true;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2421
	} else
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2422
# endif /* EINPROGRESS or EALREADY */
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2423
	{
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2424
	    DBGFPRINTF((stderr, "SOCKET: connect failed ret=%"_ld_" errno=%d __threadErrno=%d\n",
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2425
			(INT)ret, errno, __threadErrno ));
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2426
# ifdef DUMP_ADDRESS
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2427
	    {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2428
		unsigned char *cp = (unsigned char *)(&sa);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2429
		int i;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2430
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2431
		console_printf("address data:\n");
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2432
		for (i=0; i<sockaddr_size; i++) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2433
		    console_printf(" %02x\n", *cp++);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2434
		}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2435
	    }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2436
# endif
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2437
	    err = __MKSMALLINT(errno);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2438
	}
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2439
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2440
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  2441
# ifdef __win32__
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2442
    {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2443
	int off = 0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2444
	ioctlsocket(sock, FIONBIO, &off);
3657
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2445
    }
Stefan Vogel <sv@exept.de>
parents: 3655
diff changeset
  2446
# elif defined(O_NONBLOCK) // Linux / Unix
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2447
    fcntl(sock, F_SETFL, oldFlags);
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2448
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2449
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2450
# else /* NO_SOCKET */
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2451
    err = @symbol(notSupported);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2452
# endif /* NO_SOCKET */
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2453
out:;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2454
%}.
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  2455
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2456
    err notNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2457
	err isSymbol ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2458
	    self primitiveFailed:err.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2459
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2460
	^ self reportError:err.
3077
032905ddd57a class: Socket
Claus Gittinger <cg@exept.de>
parents: 3072
diff changeset
  2461
    ].
4234
eb1b470a27ee #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4206
diff changeset
  2462
    isAsync ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2463
	milliSeconds := milliSecondsOrTimeDuration.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2464
	milliSecondsOrTimeDuration isTimeDuration ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2465
	    milliSeconds := milliSecondsOrTimeDuration asMilliseconds.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2466
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2467
	(self writeExceptionWaitWithTimeoutMs:milliSeconds) ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2468
	    "/ a timeout occurred
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2469
	    "/ should I cancel the connect?
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2470
	    ^ self reportError:(OperatingSystem errorNumberFor:#ETIMEDOUT).
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2471
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2472
	err := self getSocketError.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2473
	err ~~ 0 ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2474
	    ^ self reportError:err.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2475
	].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2476
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2477
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
  2478
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2479
    peer := aSocketAddress.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2480
    port isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2481
	"socket has not been explicitly bound,
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2482
	 after connect it has been bound implicitly - fetch the port"
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2483
	port := self getFullSocketAddress port.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2484
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2485
    ^ true
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2486
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2487
    "
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2488
       |sock|
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2489
       sock := Socket newTCP.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2490
       sock connectTo:(IPSocketAddress localHost port:21) withTimeout:1000.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2491
       sock
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2492
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2493
       [
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2494
	   |sock|
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2495
	   sock := Socket newTCP.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2496
	   sock connectTo:(IPSocketAddress localHost port:21) withTimeout:1000.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2497
	   sock
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2498
       ] on:SocketErrorNotification do:[:ex|
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2499
	   ex halt.
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2500
       ].
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2501
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2502
       |sock|
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2503
       sock := Socket newTCP.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  2504
       sock connectTo:(IPSocketAddress localHost port:9876) withTimeout:2000.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2505
       sock
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2506
    "
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  2507
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  2508
    "Modified: / 19-01-2018 / 17:57:36 / stefan"
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2509
! !
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  2510
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2511
!Socket methodsFor:'datagram transmission'!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2512
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2513
receiveBuffer:aDataBuffer start:startIndex for:nBytes
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2514
    "receive data
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2515
     Return the number of bytes received.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2516
     The thread blocks until data arrives - you may want to wait before
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2517
     receiving, using #readWait or #readWaitWithTimeout:."
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2518
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2519
    |nReceived error|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2520
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2521
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2522
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2523
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2524
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2525
    if (fp != nil) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2526
	SOCKET sock;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2527
	INT objSize, offs;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2528
	INT n;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2529
	char *extPtr;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2530
	unsigned char *buffer;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2531
	unsigned char *allocatedBuffer = NULL;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2532
	INT flags = 0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2533
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2534
	sock = SOCKET_FROM_FILE_OBJECT(fp);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2535
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2536
	if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2537
	if (__isSmallInteger(nBytes)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2538
	    if (__intVal(nBytes) < objSize) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2539
		objSize = __intVal(nBytes);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2540
	    }
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2541
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2542
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2543
# ifdef DO_WRAP_CALLS
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2544
	if (extPtr) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2545
	    buffer = extPtr + offs;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2546
	} else {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2547
	    allocatedBuffer = buffer = (char *)malloc(objSize);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2548
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2549
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2550
	do {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2551
	    __threadErrno = 0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2552
	    n = (INT)STX_WSA_NOINT_CALL4("recv", recv, sock, buffer, objSize, flags);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2553
	} while ((n < 0) && (__threadErrno == EINTR));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2554
	if (n < 0) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2555
	    errno = __threadErrno;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2556
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2557
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2558
	if (allocatedBuffer) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2559
	    if (n > 0) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2560
		memcpy((char *)__InstPtr(aDataBuffer) + offs, allocatedBuffer, n);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2561
	    }
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2562
	    free(allocatedBuffer);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2563
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2564
# else
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2565
	__BEGIN_INTERRUPTABLE__
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2566
	do {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2567
	    if (extPtr) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2568
		n = recv(sock, extPtr + offs, objSize, flags);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2569
	    } else {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2570
		n = recv(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, flags);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2571
	    }
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2572
	} while ((n < 0) && (errno == EINTR));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2573
	__END_INTERRUPTABLE__
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2574
# endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2575
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2576
	if (n < 0) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2577
	    error = __INST(lastErrorNumber) = __MKSMALLINT(errno);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2578
	} else {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2579
	    RETURN(__MKSMALLINT(n));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2580
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2581
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2582
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2583
bad: ;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2584
%}.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2585
    error notNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2586
	^ self readError:error.
915
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2587
    ].
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2588
    "
1282
7e4ffdfd19f6 remove historic leftover
Claus Gittinger <cg@exept.de>
parents: 1280
diff changeset
  2589
     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
  2590
    "
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2591
    self primitiveFailed
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2592
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2593
    "Modified: / 21-03-2018 / 19:35:09 / stefan"
99
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
  2594
!
7d016cc30052 fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents: 98
diff changeset
  2595
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2596
receiveFrom:aSocketAddress buffer:aDataBuffer
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2597
    "receive datagramm data - put address of originating host into
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2598
     aSocketAddress, data into aDataBuffer.
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2599
     aDataBuffer must be ByteArray-like.
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2600
     For backward compatibility, aSocketAddress may be a non-SocketAddress;
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2601
     then, it must be a byteArray with appropriate size for the addressBytes.
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2602
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2603
     Return the number of bytes received.
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2604
     The thread blocks until data arrives - you may want to wait before
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2605
     receiving, using #readWait or #readWaitWithTimeout:."
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2606
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2607
    ^ self receiveFrom:aSocketAddress buffer:aDataBuffer start:1 for:(aDataBuffer size) flags:0
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2608
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2609
    "Modified (comment): / 21-03-2018 / 19:33:59 / stefan"
3615
ffe86e1b14ed #BUGFIX
Stefan Vogel <sv@exept.de>
parents: 3614
diff changeset
  2610
!
ffe86e1b14ed #BUGFIX
Stefan Vogel <sv@exept.de>
parents: 3614
diff changeset
  2611
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2612
receiveFrom:aSocketAddress buffer:aDataBuffer start:startIndex for:nBytes
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2613
    ^ self receiveFrom:aSocketAddress buffer:aDataBuffer start:startIndex for:nBytes flags:0
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2614
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2615
    "Modified (format): / 21-03-2018 / 19:32:27 / stefan"
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2616
!
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2617
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2618
receiveFrom:aSocketAddress buffer:aDataBuffer start:startIndex for:nBytes flags:flags
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2619
    "receive datagramm data
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2620
     - put address of originating host into aSocketAddress, data into aDataBuffer.
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2621
     aDataBuffer must be ByteArray-like.
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2622
     For backward compatibility, aSocketAddress may be a non-SocketAddress;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2623
     then, it must be a byteArray with appropriate size for the addressBytes.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2624
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2625
     Return the number of bytes received.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2626
     The thread blocks until data arrives - you may want to wait before
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2627
     receiving, using #readWait or #readWaitWithTimeout:."
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2628
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2629
    |domainClass addr addrLen nReceived error|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2630
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2631
    domainClass := self class socketAddressClassForDomain:domain.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2632
    domainClass isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2633
	^ self error:'invalid (unsupported) domain'.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2634
    ].
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2635
    aSocketAddress isSocketAddress ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2636
	aSocketAddress class == domainClass ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2637
	    ^ self error:'addressBuffer class mismatch (domain)'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2638
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2639
	addr := aSocketAddress.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2640
    ] ifFalse:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2641
	aSocketAddress notNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2642
	    addr := domainClass new.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2643
	].
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2644
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2645
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2646
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2647
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2648
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2649
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2650
    if (fp != nil) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2651
	SOCKET sock;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2652
	size_t objSize;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2653
	union sockaddr_u sa;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2654
	socklen_t alen = 0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2655
	INT n, offs;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2656
	int _flags = __longIntVal(flags);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2657
	char *extPtr;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2658
	unsigned char *allocatedBuffer = NULL, *buffer = NULL;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2659
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2660
	sock = SOCKET_FROM_FILE_OBJECT(fp);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2661
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2662
	if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2663
	if (__isSmallInteger(nBytes)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2664
	    if (__intVal(nBytes) < objSize) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2665
		objSize = __intVal(nBytes);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2666
	    }
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2667
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2668
# ifdef DO_WRAP_CALLS
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2669
	if (extPtr) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2670
	    buffer = extPtr + offs;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2671
	} else {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2672
	    allocatedBuffer = buffer = (char *)malloc(objSize);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2673
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2674
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2675
	do {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2676
	    __threadErrno = 0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2677
	    alen = sizeof(sa);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2678
	    n = (INT)STX_WSA_NOINT_CALL6("recvfrom", recvfrom, sock, buffer, objSize, _flags, (struct sockaddr *)&sa, &alen);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2679
	} while ((n < 0) && (__threadErrno == EINTR));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2680
	if (n < 0) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2681
	    errno = __threadErrno;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2682
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2683
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2684
	if (allocatedBuffer) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2685
	    if (n > 0) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2686
		memcpy((char *)__InstPtr(aDataBuffer) + offs, allocatedBuffer, n);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2687
	    }
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2688
	    free(allocatedBuffer);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2689
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2690
# else
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2691
	__BEGIN_INTERRUPTABLE__
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2692
	do {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2693
	    alen = sizeof(sa);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2694
	    if (extPtr) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2695
		n = recvfrom(sock, extPtr + offs, objSize, _flags, (struct sockaddr *) &sa, &alen);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2696
	    } else {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2697
		n = recvfrom(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, _flags, (struct sockaddr *) &sa, &alen);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2698
	    }
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2699
	} while ((n < 0) && (errno == EINTR));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2700
	__END_INTERRUPTABLE__
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2701
# endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2702
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2703
	if (n >= 0) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2704
	    if (__isNonNilObject(addr)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2705
		char *addrPtr;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2706
		OBJ oClass;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2707
		int nInstVars, nInstBytes, objSize;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2708
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2709
		oClass = __qClass(addr);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2710
		if (! __isBytes(addr) )
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2711
		    goto bad;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2712
		nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2713
		nInstBytes = OHDR_SIZE + (nInstVars * sizeof(OBJ));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2714
		objSize = __qSize(addr) - nInstBytes;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2715
		addrPtr = (char *)__InstPtr(addr) + nInstBytes;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2716
		if (objSize < alen)
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2717
		    goto bad;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2718
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2719
		/*
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2720
		 * extract the datagrams address
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2721
		 */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2722
		memcpy(addrPtr, (char *)&sa, alen);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2723
		addrLen = __MKSMALLINT(alen);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2724
	    }
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2725
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2726
	if (n < 0) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2727
	    error = __INST(lastErrorNumber) = __MKSMALLINT(errno);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2728
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2729
	nReceived = __MKSMALLINT(n);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2730
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2731
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2732
bad: ;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2733
%}.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2734
    error notNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2735
	^ self readError:error.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2736
    ].
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2737
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2738
    nReceived notNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2739
	addrLen notNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2740
	    (addr == aSocketAddress) ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2741
		self obsoleteFeatureWarning:'please use a socketAddress argument'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2742
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2743
		"can be a ByteArray for backward compatibility"
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2744
		aSocketAddress replaceFrom:1 to:addrLen with:(addr hostAddress).
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2745
	    ].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2746
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2747
	^ nReceived
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2748
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2749
    "
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2750
     arrive here if you try to receive into an invalid buffer
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2751
     (i.e. not ByteArray-like),
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  2752
     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
  2753
     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
  2754
    "
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  2755
    self primitiveFailed
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2756
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2757
    "Modified (comment): / 21-03-2018 / 19:33:49 / stefan"
116
7789fb1c9fa2 more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents: 115
diff changeset
  2758
!
7789fb1c9fa2 more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents: 115
diff changeset
  2759
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2760
sendBuffer:aDataBuffer start:startIndex for:nBytes flags:flags
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2761
    "send data. aDataBuffer be ByteArray-like.
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2762
     Return the number of bytes transmitted, or a negative number on error."
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2763
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2764
    |error|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2765
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2766
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2767
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2768
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2769
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2770
    if ((fp != nil)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2771
     && __isSmallInteger(startIndex)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2772
     && __isSmallInteger(nBytes)) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2773
	SOCKET sock;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2774
	INT objSize, n, offs;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2775
	char *extPtr;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2776
	int _flags = __longIntVal(flags);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2777
	unsigned long norder;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2778
	unsigned char *buffer, *allocatedBuffer = NULL;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2779
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2780
	sock = SOCKET_FROM_FILE_OBJECT(fp);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2781
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2782
	if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2783
	if (__isSmallInteger(nBytes)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2784
	    if (__intVal(nBytes) < objSize) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2785
		objSize = __intVal(nBytes);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2786
	    }
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2787
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2788
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2789
# ifdef DGRAM_DEBUG
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2790
	console_printf("sending %d bytes ...\n", nBytes);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2791
# endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2792
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2793
#ifdef DO_WRAP_CALLS
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2794
	if (extPtr) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2795
	    buffer = extPtr + offs;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2796
	} else {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2797
	    allocatedBuffer = buffer = (char *)malloc(objSize);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2798
	    memcpy(allocatedBuffer, (char *)__InstPtr(aDataBuffer) + offs, objSize);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2799
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2800
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2801
	do {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2802
	    __threadErrno = 0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2803
	    n = (INT)STX_WSA_NOINT_CALL4("send", send, sock, buffer, objSize, _flags);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2804
	} while ((n < 0) && (__threadErrno == EINTR));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2805
	if (n < 0) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2806
	    errno = __threadErrno;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2807
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2808
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2809
	if (allocatedBuffer) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2810
	    free(allocatedBuffer);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2811
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2812
#else
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2813
	__BEGIN_INTERRUPTABLE__
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2814
	do {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2815
	    if (extPtr) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2816
		n = send(sock, extPtr + offs, objSize, _flags);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2817
	    } else {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2818
		n = send(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, _flags);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2819
	    }
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2820
	} while ((n < 0) && (errno == EINTR));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2821
	__END_INTERRUPTABLE__
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2822
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2823
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2824
	if (n < 0) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2825
	    error = __INST(lastErrorNumber) = __MKSMALLINT(errno);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2826
	} else {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2827
	    RETURN (__MKSMALLINT(n));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2828
	}
915
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2829
    }
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2830
#endif
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2831
bad: ;
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2832
%}.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2833
    error notNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2834
	self writeError:error.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2835
    ].
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2836
915
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2837
    "
1485
d8578cf3504b gcc3.3.1 insn-scheduler bug workaround
Claus Gittinger <cg@exept.de>
parents: 1474
diff changeset
  2838
     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
  2839
    "
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2840
    self primitiveFailed
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2841
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2842
    "Modified: / 21-03-2018 / 19:22:52 / stefan"
915
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2843
!
3dc0e3cd838a added interface to send() and recv()
Claus Gittinger <cg@exept.de>
parents: 898
diff changeset
  2844
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2845
sendTo:aSocketAddress buffer:buffer
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2846
    "send datagramm data - fetch address of destination host from
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2847
     aSocketAddress, data from aDataBuffer.
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2848
     aDataBuffer must be ByteArray-like.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2849
     aSocketAddress must be a valid SocketAddress for my domain
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2850
     (i.e. for IPv4, an IPSocketAddress).
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2851
     For backward compatibility, a ByteArray is still supported in aSocketAddress
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2852
     (i.e. for IPv4, a 4-byte byteArray).
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2853
     Return the number of bytes transmitted."
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2854
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2855
    ^ self sendTo:aSocketAddress buffer:buffer start:1 for:buffer size flags:0
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2856
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2857
    "Modified (comment): / 21-03-2018 / 19:30:20 / stefan"
116
7789fb1c9fa2 more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents: 115
diff changeset
  2858
!
7789fb1c9fa2 more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents: 115
diff changeset
  2859
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2860
sendTo:aSocketAddress buffer:buffer start:startIndex for:count
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2861
    "send datagramm data - fetch address of destination host from
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2862
     aSocketAddress, data from aDataBuffer.
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2863
     aDataBuffer must be ByteArray-like.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2864
     aSocketAddress must be a valid SocketAddress for my domain
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2865
     (i.e. for IPv4, an IPSocketAddress).
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2866
     For backward compatibility, a ByteArray is still supported in aSocketAddress
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2867
     (i.e. for IPv4, a 4-byte byteArray).
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2868
     Return the number of bytes transmitted."
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2869
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2870
    ^ self sendTo:aSocketAddress buffer:buffer start:startIndex for:count flags:0
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2871
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2872
    "Modified (comment): / 21-03-2018 / 19:29:07 / stefan"
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2873
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2874
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2875
sendTo:aSocketAddress buffer:aDataBuffer start:startIndex for:nBytes flags:flags
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2876
    "send datagramm data - fetch address of destination host from
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2877
     aSocketAddress, data from aDataBuffer starting at startIndex,
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2878
     sending count bytes.
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2879
     aDataBuffer must be ByteArray-like.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2880
     aSocketAddress must be a valid SocketAddress for my domain
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2881
     (i.e. for IPv4, an IPSocketAddress).
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2882
     For backward compatibility, a ByteArray is still supported in aSocketAddress
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2883
     (i.e. for IPv4, a 4-byte byteArray).
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2884
     Return the number of bytes transmitted."
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2885
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2886
    |domainClass addr error|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2887
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  2888
    aSocketAddress isSocketAddress ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2889
	addr := aSocketAddress.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2890
    ] ifFalse:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2891
	aSocketAddress isByteArray ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2892
	    ^ self error:'bad socketAddress argument'
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2893
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2894
	domainClass := self class socketAddressClassForDomain:domain.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2895
	domainClass isNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2896
	    ^ self error:'invalid (unsupported) domain'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2897
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2898
	addr := domainClass hostAddress:aSocketAddress.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2899
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2900
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2901
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  2902
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2903
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2904
    if ((fp != nil)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2905
     && __isSmallInteger(startIndex)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2906
     && __isSmallInteger(nBytes)) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2907
	SOCKET sock;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2908
	INT objSize;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2909
	struct sockaddr *sockaddr_ptr;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2910
	union sockaddr_u sa;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2911
	socklen_t sockaddr_size, alen = 0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2912
	INT sockAddrOffs;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2913
	INT n, offs;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2914
	char *extPtr;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2915
	int _flags = __longIntVal(flags);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2916
	unsigned long norder;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2917
	unsigned char *buffer;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2918
	unsigned char *allocatedBuffer = NULL;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2919
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2920
	sock = SOCKET_FROM_FILE_OBJECT(fp);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2921
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2922
	if (! __isBytes(addr)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2923
	    sockaddr_size = 0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2924
	    sockaddr_ptr = (struct sockaddr *)0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2925
	} else {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2926
	    int nIndex;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2927
	    OBJ cls;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2928
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2929
	    sockAddrOffs = 0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2930
	    if ((cls = __qClass(addr)) != @global(ByteArray))
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2931
		sockAddrOffs += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2932
	    nIndex = __qSize(addr) - OHDR_SIZE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2933
	    sockaddr_size = nIndex - sockAddrOffs;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2934
	    if (sockaddr_size > sizeof(sa)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2935
		console_fprintf(stderr, "Socket [warning]: bad socketAddr\n");
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2936
		goto bad;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2937
	    }
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2938
	    memcpy(&sa, (__byteArrayVal(addr) + sockAddrOffs), sockaddr_size);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2939
	    sockaddr_ptr = (struct sockaddr *)(&sa);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2940
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2941
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2942
	if (! setupBufferParameters(aDataBuffer, startIndex, &extPtr, &offs, &objSize)) goto bad;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2943
	if (__isSmallInteger(nBytes)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2944
	    if (__intVal(nBytes) < objSize) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2945
		objSize = __intVal(nBytes);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2946
	    }
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2947
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2948
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2949
#ifdef DO_WRAP_CALLS
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2950
	if (extPtr) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2951
	    buffer = extPtr + offs;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2952
	} else {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2953
	    allocatedBuffer = buffer = (char *)malloc(objSize);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2954
	    memcpy(allocatedBuffer, (char *)__InstPtr(aDataBuffer) + offs, objSize);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2955
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2956
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2957
	do {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2958
	    __threadErrno = 0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2959
	    n = (INT)STX_WSA_NOINT_CALL6("sendto", sendto, sock, buffer, objSize, _flags, sockaddr_ptr, sockaddr_size);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2960
	} while ((n < 0) && (__threadErrno == EINTR));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2961
	if (n < 0) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2962
	    errno = __threadErrno;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2963
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2964
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2965
	if (allocatedBuffer) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2966
	    free(allocatedBuffer);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2967
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2968
#else
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2969
	__BEGIN_INTERRUPTABLE__
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2970
	do {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2971
	    if (extPtr) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2972
		n = sendto(sock, extPtr + offs, objSize, _flags, sockaddr_ptr, sockaddr_size);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2973
	    } else {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2974
		n = sendto(sock, (char *)__InstPtr(aDataBuffer) + offs, objSize, _flags, sockaddr_ptr, sockaddr_size);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2975
	    }
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2976
	} while ((n < 0) && (errno == EINTR));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2977
	__END_INTERRUPTABLE__
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2978
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2979
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2980
	if (n < 0) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2981
	    error = __INST(lastErrorNumber) = __MKSMALLINT(errno);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2982
	} else {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2983
	    RETURN (__MKSMALLINT(n));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2984
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2985
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2986
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2987
bad: ;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2988
%}.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2989
    error notNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  2990
	self writeError:error.
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2991
    ].
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  2992
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2993
    "
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2994
     arrive here if you try to send from an invalid buffer
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2995
     (i.e. not ByteArray-like),
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2996
     or if the addressBuffer is nonNil AND not a ByteArray/String
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2997
     or if the addressBuffer is nonNil AND too small.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2998
    "
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  2999
    self primitiveFailed
4626
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  3000
5e6419fb6699 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4587
diff changeset
  3001
    "Modified: / 21-03-2018 / 19:27:51 / stefan"
0
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  3002
! !
1cf8d1747859 Initial revision
claus
parents:
diff changeset
  3003
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3004
!Socket methodsFor:'error reporting'!
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3005
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3006
reportError:osErrorNumber
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3007
    "report an error to the initiator of a Socket operation.
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3008
     We raise a notification for newer code, and also
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3009
     return false for old code not catching the notification."
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3010
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3011
    |errorHolder|
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3012
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3013
    lastErrorNumber := osErrorNumber.
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3014
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3015
    errorHolder := OperatingSystem errorHolderForNumber:osErrorNumber.
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3016
    SocketErrorNotification raiseRequestWith:errorHolder.
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3017
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3018
    ^ false.
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3019
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3020
    "Created: / 19-01-2018 / 12:50:00 / stefan"
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3021
! !
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3022
4172
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  3023
!Socket methodsFor:'finalization'!
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  3024
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  3025
finalize
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  3026
    self linger:0.      "/ do an abortive release - discard buffered data
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  3027
    self closeFile.     "/ does not wait due to abortive release.
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  3028
! !
2a6544c030fc #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4091
diff changeset
  3029
3484
ccd8683e4e7e class: Socket
Claus Gittinger <cg@exept.de>
parents: 3481
diff changeset
  3030
!Socket methodsFor:'initialization'!
ccd8683e4e7e class: Socket
Claus Gittinger <cg@exept.de>
parents: 3481
diff changeset
  3031
ccd8683e4e7e class: Socket
Claus Gittinger <cg@exept.de>
parents: 3481
diff changeset
  3032
initialize
ccd8683e4e7e class: Socket
Claus Gittinger <cg@exept.de>
parents: 3481
diff changeset
  3033
    super initialize.
ccd8683e4e7e class: Socket
Claus Gittinger <cg@exept.de>
parents: 3481
diff changeset
  3034
    "/ transparent
ccd8683e4e7e class: Socket
Claus Gittinger <cg@exept.de>
parents: 3481
diff changeset
  3035
    eolMode := nil.
ccd8683e4e7e class: Socket
Claus Gittinger <cg@exept.de>
parents: 3481
diff changeset
  3036
! !
ccd8683e4e7e class: Socket
Claus Gittinger <cg@exept.de>
parents: 3481
diff changeset
  3037
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3038
!Socket methodsFor:'low level'!
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3039
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3040
getSocketAdress
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3041
    "BAD SPELLING, of #getSocketAddress, kept for compatibility with swazoo"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3042
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3043
    <resource: #obsolete>
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3044
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3045
    ^ self getSocketAddress
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3046
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3047
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3048
getSocketError
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3049
    "get the SO_ERROR form the socket, which indicates the
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3050
     result of an asynchronous operation"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3051
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3052
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3053
#ifndef NO_SOCKET
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3054
    OBJ fp;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3055
    int err;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3056
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3057
    fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3058
    if (fp == nil) {
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3059
	err = EBADF;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3060
    } else {
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3061
	unsigned int sz;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3062
	SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3063
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3064
	sock = SOCKET_FROM_FILE_OBJECT(fp);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3065
	sz = sizeof(err);
2909
aacef77a1697 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 2904
diff changeset
  3066
	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
  3067
# ifdef __win32__
1568
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3068
	    errno = WSAGetLastError();
1558
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  3069
# endif
1568
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3070
	    err = errno;
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  3071
	}
1558
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  3072
    }
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  3073
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  3074
    RETURN(__MKSMALLINT(err));
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  3075
#endif
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  3076
%}
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  3077
!
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  3078
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3079
listenWithBacklog:aNumber
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3080
    <resource: #obsolete>
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3081
    "same as listenFor: - backward compatibility with old ST/X"
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3082
    self obsoleteMethodWarning.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3083
    ^ self listenFor:aNumber
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3084
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3085
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3086
primAcceptOn:aServerSocket blocking:blocking
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3087
    "accept a connection on a server port (created with:'Socket>>newTCP:')
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3088
     usage is: (Socket basicNew acceptOn:(Socket newTCP:9999)).
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3089
     Return the true if ok; false if not.
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3090
     Notify with a SocketErrorNotification on error.
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3091
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3092
     If blocking is true, the accept() syscall (and the whole smalltalk image)
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3093
     will block, until a connection is accepted.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3094
     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
  3095
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3096
    |serverSocketHandle addr domainClass newHandle err|
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3097
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3098
    handle notNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3099
	^ self errorAlreadyOpen.
4180
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  3100
    ].
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  3101
    serverSocketHandle := aServerSocket fileHandle.
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  3102
    serverSocketHandle isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3103
	"socket is not open"
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3104
	^ self errorNotOpen
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3105
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3106
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3107
    domain := aServerSocket domain.
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3108
    socketType := aServerSocket type.
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3109
    handleType := aServerSocket handleType.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3110
    "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
  3111
    domainClass := self class socketAddressClassForDomain:domain.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3112
    domainClass isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3113
	^ self error:'invalid (unsupported) domain'.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3114
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3115
    addr := domainClass new.
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3116
    newHandle := OperatingSystem socketAccessor new.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3117
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3118
%{  /* STACK: 100000 */
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3119
#ifndef NO_SOCKET
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3120
    FILE *fp;
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3121
    int flags;
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3122
    SOCKET serverSocket, newSock;
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3123
    int _fd;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3124
    union sockaddr_u sa;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3125
    unsigned int alen;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3126
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3127
# ifdef __win32__
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3128
    serverSocket = SOCKET_FROM_FILE_OBJECT(serverSocketHandle);
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3129
# else
3247
259f71b89b6e class: Socket
Stefan Vogel <sv@exept.de>
parents: 3246
diff changeset
  3130
    serverSocket = __intVal(serverSocketHandle);
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3131
# endif
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3132
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3133
# 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
  3134
    if (blocking == false) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3135
	flags = fcntl(serverSocket, F_GETFL);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3136
	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
  3137
    }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3138
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3139
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3140
# ifdef DO_WRAP_CALLS
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3141
    do {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3142
	__threadErrno = 0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3143
	alen = sizeof(sa);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3144
	newSock = (SOCKET)STX_WSA_CALL3("accept", accept, serverSocket, &sa, &alen);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3145
    } while ((newSock < 0) && (__threadErrno == EINTR));
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3146
    if (newSock == -1) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3147
	err = __MKSMALLINT(__threadErrno);
3126
d95cc55a4fb5 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3123
diff changeset
  3148
    }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3149
# else
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3150
    __BEGIN_INTERRUPTABLE__
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3151
    do {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3152
	alen = sizeof(sa);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3153
	newSock = accept(serverSocket, (struct sockaddr *) &sa, &alen);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3154
    } while ((newSock < 0) && (errno == EINTR));
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3155
    __END_INTERRUPTABLE__
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3156
    if (newSock == -1) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3157
	err = __MKSMALLINT(errno);
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3158
    }
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3159
# endif
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3160
    DBGFPRINTF((stderr, "SOCKET: accept newSock=%d\n", newSock));
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3161
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3162
# if defined(O_NDELAY) && defined(SET_NDELAY)
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3163
    if (blocking == false) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3164
	fcntl(serverSocket, F_SETFL, flags);
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3165
    }
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3166
# endif
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3167
    if (newSock == -1) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3168
	DBGPRINTF(("SOCKET: accept call failed errno=%d\n", (int)(__intVal(err))));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3169
	goto out;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3170
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3171
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3172
    if (__isNonNilObject(addr)) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3173
	OBJ oClass = __qClass(addr);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3174
	int nInstVars, nInstBytes, objSize;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3175
	char *addrP;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3176
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3177
	if (! __isBytes(addr)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3178
	    DBGPRINTF(("SOCKET: bad addr\n"));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3179
	    closesocket(newSock);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3180
	    err = @symbol(badAddressArg);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3181
	    goto out;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3182
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3183
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3184
	nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3185
	nInstBytes = OHDR_SIZE + (nInstVars * sizeof(OBJ));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3186
	objSize = __qSize(addr) - nInstBytes;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3187
	addrP = (char *)__InstPtr(addr) + nInstBytes;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3188
	if (objSize < alen) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3189
	    DBGPRINTF(("SOCKET: bad addr\n"));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3190
	    closesocket(newSock);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3191
	    err = @symbol(badAddressArgLen);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3192
	    goto out;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3193
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3194
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3195
	/*
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3196
	 * extract the partners address
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3197
	 */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3198
	memcpy(addrP, (char *)&sa, alen);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3199
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3200
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3201
    /*
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3202
     * make it a FILE *
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3203
     */
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3204
# ifdef __win32__
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3205
#  if 0 && (defined( __BORLANDC__ ) || defined( __MINGW__ ))
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3206
    __stxWrapApiEnterCritical();
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3207
    _fd = _open_osfhandle((long)newSock, 0);
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3208
    __stxWrapApiLeaveCritical();
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3209
    DBGPRINTF(("SOCKET: sock=%d fd=%d\n", newSock, _fd));
2773
ab3049f4a351 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2748
diff changeset
  3210
#  else
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3211
    _fd = (int)newSock;
2773
ab3049f4a351 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2748
diff changeset
  3212
#  endif
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3213
# else // ! __win32__
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3214
    fp = fdopen(newSock, "r+");
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3215
    if (! fp) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3216
	DBGPRINTF(("SOCKET: fdopen call failed\n"));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3217
	err = __MKSMALLINT(errno);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3218
	closesocket(newSock);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3219
	DBGFPRINTF((stderr, "SOCKET: close (fdopen failed) (%d)\n", newSock));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3220
	goto out;
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3221
    }
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3222
# endif // ! __win32__
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3223
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3224
    if ((@global(FileOpenTrace) == true) || __debugging__) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3225
# ifdef __win32__
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3226
	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
  3227
# else
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3228
	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
  3229
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3230
    }
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3231
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3232
# ifdef __win32__
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3233
    __externalAddressVal(newHandle) = _fd;
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3234
    __INST(handleType) = @symbol(socketHandle);
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3235
# else
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3236
    __externalAddressVal(newHandle) = fp;
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3237
    __INST(handleType) = @symbol(socketFilePointer);
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3238
# endif
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3239
#endif /* not NO_SOCKET */
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3240
out:;
1558
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  3241
%}.
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3242
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3243
    err notNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3244
	err isSymbol ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3245
	    self error:err.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3246
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3247
	^ self reportError:err.
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3248
    ].
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3249
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3250
    handle := newHandle.
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3251
    buffered := false.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3252
    mode := #readwrite.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3253
    binary := false.
3620
c648d063bccc #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3617
diff changeset
  3254
    self registerForFinalization.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3255
    peer := addr.
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3256
    port := aServerSocket port.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3257
1558
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  3258
    ^ true
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3259
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  3260
    "Modified (comment): / 19-01-2018 / 13:35:57 / stefan"
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  3261
!
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3262
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3263
setSocketOption:option argument:arg1 argument:arg2
3513
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3264
    |ok error|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3265
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  3266
    handle isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3267
	^ self errorNotOpen
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  3268
    ].
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  3269
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3270
%{  /* STACK: 32000 */
2818
94d67df5c00d compilable with tcc
Claus Gittinger <cg@exept.de>
parents: 2773
diff changeset
  3271
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3272
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3273
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3274
    if (fp != nil) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3275
	SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3276
	int opt = -1;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3277
	int level = -1;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3278
	int usize = -1;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3279
	int ret;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3280
	union u {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3281
	    BOOL        u_bool;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3282
	    int         u_int;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3283
	    struct linger  u_linger;
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3284
# ifdef IP_ADD_MEMBERSHIP
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3285
	    struct ip_mreq u_ip_mreq;
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3286
# endif
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3287
# if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3288
	    struct timeval u_tv;
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3289
# endif
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3290
	} u;
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3291
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3292
# ifdef IP_ADD_MEMBERSHIP
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3293
	if (option == @symbol(IP_DROP_MEMBERSHIP)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3294
	    opt = IP_DROP_MEMBERSHIP;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3295
	    goto dropOrAdd;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3296
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3297
	if (option == @symbol(IP_ADD_MEMBERSHIP)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3298
	    /* add membership to a multicast group */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3299
	    opt = IP_ADD_MEMBERSHIP;
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3300
dropOrAdd:
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3301
	    level = IPPROTO_IP;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3302
	    usize = sizeof(u.u_ip_mreq);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3303
	    if (__isByteArrayLike(arg1) && __isByteArrayLike(arg2)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3304
		memcpy(&u.u_ip_mreq.imr_multiaddr, __byteArrayVal(arg1), sizeof(u.u_ip_mreq.imr_multiaddr));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3305
		memcpy(&u.u_ip_mreq.imr_interface, __byteArrayVal(arg2), sizeof(u.u_ip_mreq.imr_interface));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3306
		// once we use the struct ip_mreqn:
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3307
		// u.u_ip_mreqn.imr_ifindex = 0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3308
	    }
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3309
	    else
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3310
		goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3311
	}
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3312
# endif /* IP_ADD_MEMBERSHIP */
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3313
4738
f915f985a54c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4736
diff changeset
  3314
# ifdef IP_TTL
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3315
	if (option == @symbol(IP_TTL)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3316
	    opt = IP_TTL;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3317
	    level = IPPROTO_IP;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3318
	    usize = sizeof(u.u_int);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3319
	    if (__isSmallInteger(arg1))u.u_int = __intVal(arg1);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3320
	    else goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3321
	}
4738
f915f985a54c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4736
diff changeset
  3322
# endif /* IP_TTL */
f915f985a54c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4736
diff changeset
  3323
f915f985a54c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4736
diff changeset
  3324
# ifdef IP_MULTICAST_TTL
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3325
	if (option == @symbol(IP_MULTICAST_TTL)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3326
	    opt = IP_MULTICAST_TTL;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3327
	    level = IPPROTO_IP;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3328
	    usize = sizeof(u.u_int);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3329
	    if (__isSmallInteger(arg1))u.u_int = __intVal(arg1);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3330
	    else goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3331
	}
4738
f915f985a54c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4736
diff changeset
  3332
# endif /* IP_TTL */
f915f985a54c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4736
diff changeset
  3333
f915f985a54c #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4736
diff changeset
  3334
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3335
# ifdef SO_BROADCAST
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3336
	if (option == @symbol(SO_BROADCAST)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3337
	    /* Enables transmission and receipt of broadcast messages on the socket. */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3338
	    opt = SO_BROADCAST;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3339
	    level = SOL_SOCKET;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3340
	    usize = sizeof(u.u_bool);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3341
	    if (arg1 == true) u.u_bool = TRUE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3342
	    else if (arg1 == false) u.u_bool = FALSE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3343
	    else goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3344
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3345
# endif /* SO_BROADCAST */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3346
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3347
# ifdef SO_CONDITIONAL
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3348
#  if 0
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3349
	if (option == @symbol(SO_CONDITIONAL)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3350
	    /* Enables sockets to delay the acknowledgment of a connection until after the WSAAccept condition function is called. */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3351
	    opt = SO_CONDITIONAL;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3352
	    level = SOL_SOCKET;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3353
	    usize = sizeof(u.u_bool);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3354
	    if (arg1 == true) u.u_bool = TRUE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3355
	    else if (arg1 == false) u.u_bool = FALSE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3356
	    else goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3357
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3358
#  endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3359
# endif /* SO_CONDITIONAL */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3360
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3361
# ifdef SO_DEBUG
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3362
	if (option == @symbol(SO_DEBUG)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3363
	    /* Records debugging information. */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3364
	    opt = SO_DEBUG;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3365
	    level = SOL_SOCKET;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3366
	    usize = sizeof(u.u_bool);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3367
	    if (arg1 == true) u.u_bool = TRUE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3368
	    else if (arg1 == false) u.u_bool = FALSE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3369
	    else goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3370
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3371
# endif /* SO_DEBUG */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3372
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3373
# ifdef SO_DONTLINGER
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3374
	if (option == @symbol(SO_DONTLINGER)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3375
	    /* Does not block close waiting for unsent data to be sent.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3376
	       Setting this option is equivalent to setting SO_LINGER with l_onoff set to zero. */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3377
	    opt = SO_DONTLINGER;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3378
	    level = SOL_SOCKET;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3379
	    usize = sizeof(u.u_bool);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3380
	    if (arg1 == true) u.u_bool = TRUE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3381
	    else if (arg1 == false) u.u_bool = FALSE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3382
	    else goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3383
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3384
# endif /* SO_DONTLINGER */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3385
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3386
# ifdef SO_DONTROUTE
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3387
	if (option == @symbol(SO_DONTROUTE)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3388
	    /* Does not route: sends directly to interface.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3389
	       Succeeds but is ignored on AF_INET sockets;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3390
	       fails on AF_INET6 sockets with WSAENOPROTOOPT.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3391
	       Not supported on ATM sockets (results in an error). */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3392
	    opt = SO_DONTROUTE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3393
	    level = SOL_SOCKET;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3394
	    usize = sizeof(u.u_bool);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3395
	    if (arg1 == true) u.u_bool = TRUE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3396
	    else if (arg1 == false) u.u_bool = FALSE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3397
	    else goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3398
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3399
# endif /* SO_DONTROUTE */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3400
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3401
#if defined(IPPROTO_TCP) && defined(TCP_NODELAY)
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3402
	if (option == @symbol(TCP_NODELAY)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3403
	    /* enable/disable TCP_NODELAY (i.e. disable/enable the Nagle algorithm) */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3404
	    opt = TCP_NODELAY;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3405
	    level = IPPROTO_TCP;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3406
	    usize = sizeof(u.u_bool);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3407
	    if (arg1 == true) u.u_bool = TRUE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3408
	    else if (arg1 == false) u.u_bool = FALSE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3409
	    else goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3410
	}
2516
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3411
# endif /* TCP_NODELAY */
592619f82885 Add setsockopt for Multicast stuff
Stefan Vogel <sv@exept.de>
parents: 2465
diff changeset
  3412
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3413
# ifdef SO_KEEPALIVE
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3414
	if (option == @symbol(SO_KEEPALIVE)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3415
	    /* Sends keep-alives. Not supported on ATM sockets (results in an error). */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3416
	    opt = SO_KEEPALIVE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3417
	    level = SOL_SOCKET;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3418
	    usize = sizeof(u.u_bool);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3419
	    if (arg1 == true) u.u_bool = TRUE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3420
	    else if (arg1 == false) u.u_bool = FALSE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3421
	    else goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3422
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3423
# endif /* SO_KEEPALIVE */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3424
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3425
# ifdef SO_LINGER
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3426
	if (option == @symbol(SO_LINGER)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3427
	    /* Lingers on close if unsent data is present. */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3428
	    opt = SO_LINGER;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3429
	    level = SOL_SOCKET;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3430
	    usize = sizeof(u.u_linger);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3431
	    if (arg1 == true) u.u_linger.l_onoff = TRUE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3432
	    else if (arg1 == false) u.u_linger.l_onoff = FALSE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3433
	    else goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3434
	    if (arg2 == nil) u.u_linger.l_linger = 0;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3435
	    else if (__isSmallInteger(arg2))u.u_linger.l_linger = __intVal(arg2);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3436
	    else goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3437
	    DBGPRINTF(("SOCKET: SO_LINGER %d %d\n", u.u_linger.l_onoff, u.u_linger.l_linger));
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3438
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3439
# endif /* SO_LINGER */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3440
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3441
# ifdef SO_OOBINLINE
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3442
	if (option == @symbol(SO_OOBINLINE)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3443
	    /* Receives OOB data in the normal data stream. */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3444
	    opt = SO_OOBINLINE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3445
	    level = SOL_SOCKET;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3446
	    usize = sizeof(u.u_bool);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3447
	    if (arg1 == true) u.u_bool = TRUE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3448
	    else if (arg1 == false) u.u_bool = FALSE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3449
	    else goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3450
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3451
# endif /* SO_OOBINLINE */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3452
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3453
# ifdef SO_RCVBUF
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3454
	if (option == @symbol(SO_RCVBUF)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3455
	    /* Specifies the total per-socket buffer space reserved for receives.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3456
	       This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window. */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3457
	    opt = SO_RCVBUF;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3458
	    level = SOL_SOCKET;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3459
	    usize = sizeof(u.u_int);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3460
	    if (__isSmallInteger(arg1))u.u_int = __intVal(arg1);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3461
	    else goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3462
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3463
# endif /* SO_RCVBUF */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3464
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3465
# ifdef SO_SNDBUF
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3466
	if (option == @symbol(SO_SNDBUF)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3467
	    /* Specifies the total per-socket buffer space reserved for sends.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3468
	       This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window. */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3469
	    opt = SO_SNDBUF;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3470
	    level = SOL_SOCKET;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3471
	    usize = sizeof(u.u_int);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3472
	    if (__isSmallInteger(arg1))u.u_int = __intVal(arg1);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3473
	    else goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3474
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3475
# endif /* SO_SNDBUF */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3476
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3477
# ifdef SO_REUSEADDR
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3478
	if (option == @symbol(SO_REUSEADDR)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3479
	    /* Allows the socket to be bound to an address that is already in use.  */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3480
	    opt = SO_REUSEADDR;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3481
	    level = SOL_SOCKET;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3482
	    usize = sizeof(u.u_bool);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3483
	    if (arg1 == true) u.u_bool = TRUE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3484
	    else if (arg1 == false) u.u_bool = FALSE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3485
	    else goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3486
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3487
# endif /* SO_OOBINLINE */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3488
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3489
# ifdef SO_EXCLUSIVEADDRUSE
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3490
	if (option == @symbol(SO_EXCLUSIVEADDRUSE)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3491
	    /* Enables a socket to be bound for exclusive access.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3492
	       Does not require administrative privilege.  */
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3493
	    opt = SO_EXCLUSIVEADDRUSE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3494
	    level = SOL_SOCKET;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3495
	    usize = sizeof(u.u_bool);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3496
	    if (arg1 == true) u.u_bool = TRUE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3497
	    else if (arg1 == false) u.u_bool = FALSE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3498
	    else goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3499
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3500
# endif /* SO_OOBINLINE */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3501
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3502
# ifdef SO_RCVTIMEO
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3503
	if ((option == @symbol(SO_RCVTIMEO))
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3504
	 && __isSmallInteger(arg1)
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3505
	 && __isSmallInteger(arg2)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3506
	    opt = SO_RCVTIMEO;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3507
	    level = SOL_SOCKET;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3508
	    u.u_tv.tv_sec = __intVal(arg1);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3509
	    u.u_tv.tv_usec = __intVal(arg2);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3510
	    usize = sizeof(u.u_tv);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3511
	}
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3512
# endif /* SO_RCVTIMEO */
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3513
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3514
# ifdef SO_SNDTIMEO
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3515
	if ((option == @symbol(SO_SNDTIMEO))
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3516
	 && __isSmallInteger(arg1)
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3517
	 && __isSmallInteger(arg2)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3518
	    opt = SO_SNDTIMEO;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3519
	    level = SOL_SOCKET;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3520
	    u.u_tv.tv_sec = __intVal(arg1);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3521
	    u.u_tv.tv_usec = __intVal(arg2);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3522
	    usize = sizeof(u.u_tv);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3523
	}
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3524
# endif /* SO_SNDTIMEO */
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3525
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3526
# if !defined(IPV6_V6ONLY) && defined(__win32__)
3513
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3527
#  define IPPROTO_IPV6 41
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3528
#  define IPV6_V6ONLY 27
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3529
# endif
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3530
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3531
# if defined(IPV6_V6ONLY)
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3532
	if (option == @symbol(IPV6_V6ONLY)) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3533
	    opt = IPV6_V6ONLY;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3534
	    level = IPPROTO_IPV6;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3535
	    usize = sizeof(u.u_bool);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3536
	    if (arg1 == true) u.u_bool = TRUE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3537
	    else if (arg1 == false) u.u_bool = FALSE;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3538
	    else goto argError;
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3539
#  ifdef __win32__
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3540
	    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
  3541
#  endif
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3542
	}
3513
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3543
# endif /* IPV6_V6ONLY */
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3544
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3545
	if (usize == -1) goto argError;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3546
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3547
	ok = (setsockopt(sock, level, opt, (char *)&u, usize) >= 0) ? true : false;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3548
	if (ok == false) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3549
# ifdef __win32__
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3550
	    error = __mkSmallInteger(WSAGetLastError());
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3551
# else
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3552
	    error = __mkSmallInteger(errno);
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3553
# endif
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3554
	}
3513
8105bf370769 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3485
diff changeset
  3555
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3556
    }
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3557
argError: ;
2818
94d67df5c00d compilable with tcc
Claus Gittinger <cg@exept.de>
parents: 2773
diff changeset
  3558
#endif /* NO_SOCKET */
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3559
%}.
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3560
    ok isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3561
	self primitiveFailed
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3562
    ].
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3563
    ok ifFalse:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3564
	'++++ 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
  3565
    ].
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  3566
!
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3567
2823
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
  3568
shutdown:howNum
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3569
    "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
  3570
	 0 - read side   (no further reads)
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3571
	     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
  3572
	 1 - write side  (no further writes)
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3573
	     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
  3574
	     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
  3575
	     that we will not send more data.
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3576
	 2 - both read side and write side."
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3577
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3578
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3579
#ifndef NO_SOCKET
2823
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
  3580
    OBJ __handle = __INST(handle);
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3581
2823
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
  3582
    if ((__handle != nil) && __isSmallInteger(howNum)) {
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3583
	SOCKET sock = SOCKET_FROM_FILE_OBJECT(__handle);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3584
	INT how = __intVal(howNum);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3585
	INT ret;
2823
10179ccfae5d Default for close - linger in background instead 30s in foreground
Stefan Vogel <sv@exept.de>
parents: 2818
diff changeset
  3586
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3587
# ifdef DO_WRAP_CALLS
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3588
	do {
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3589
	    __threadErrno = 0;
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3590
	    DBGFPRINTF((stderr, "SOCKET: shutDown...\n"));
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3591
	    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
  3592
	    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
  3593
	} 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
  3594
# else
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3595
	__BEGIN_INTERRUPTABLE__
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3596
	shutdown(sock, how);
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  3597
	__END_INTERRUPTABLE__
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  3598
# endif
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  3599
    }
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  3600
#endif
1339
cc973a42aa54 shutdown: added
Claus Gittinger <cg@exept.de>
parents: 1338
diff changeset
  3601
%}.
781
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  3602
! !
4d61e7588ff3 added #waitForNewConnectionOrDataOnAny:timeout:
Claus Gittinger <cg@exept.de>
parents: 776
diff changeset
  3603
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3604
!Socket methodsFor:'printing & storing'!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3605
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3606
printOn:aStream
3530
ce2171c0fe57 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3514
diff changeset
  3607
    aStream nextPutAll:self className; nextPutAll:'('.
ce2171c0fe57 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3514
diff changeset
  3608
    domain printOn:aStream.
ce2171c0fe57 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3514
diff changeset
  3609
    aStream nextPutAll:' protocol='.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3610
    protocol printOn:aStream.
4736
c5a3e2dae276 #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4671
diff changeset
  3611
    aStream nextPutAll:' type='.
c5a3e2dae276 #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4671
diff changeset
  3612
    socketType printOn:aStream.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3613
    aStream nextPutAll:' port='.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3614
    self port printOn:aStream.
4636
7e1e0ce3e0b4 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 4630
diff changeset
  3615
    peer notNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3616
	aStream nextPutAll:' peer='.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3617
	peer printOn:aStream.
4636
7e1e0ce3e0b4 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 4630
diff changeset
  3618
    ].
7e1e0ce3e0b4 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 4630
diff changeset
  3619
    listening notNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3620
	aStream nextPutAll:' *listening('.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3621
	listening printOn:aStream.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  3622
	aStream nextPutAll:')*'.
4636
7e1e0ce3e0b4 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 4630
diff changeset
  3623
    ].
1842
88c04d1e9e6b changed #printOn:
Stefan Vogel <sv@exept.de>
parents: 1839
diff changeset
  3624
    aStream nextPut:$).
4636
7e1e0ce3e0b4 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 4630
diff changeset
  3625
7e1e0ce3e0b4 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 4630
diff changeset
  3626
    "Modified: / 23-04-2018 / 19:44:24 / stefan"
4736
c5a3e2dae276 #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4671
diff changeset
  3627
    "Modified: / 19-09-2018 / 18:30:20 / Claus Gittinger"
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3628
! !
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3629
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3630
!Socket methodsFor:'queries'!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3631
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3632
domain
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3633
    "return the sockets addressing domain (i.e. #AF_INET, #AF_INET6, #AF_UNIX, ...)"
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3634
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3635
    ^ domain
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3636
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3637
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3638
getFullPeerAddress
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3639
    "implemented for swazoo project (primitive code can't be loaded as extension)
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3640
     Answer my own address (I am bound to this address).
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3641
     Note that this address may change after a connect or accept."
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3642
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3643
    |error domainClass addr|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3644
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3645
    handle isNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3646
	^ self errorNotOpen
1885
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
    domainClass := self class socketAddressClassForDomain:domain.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3650
    domainClass isNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3651
	^ self error:'invalid (unsupported) domain'.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3652
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3653
    addr := domainClass new.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3654
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3655
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3656
#ifndef NO_SOCKET
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3657
    OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3658
    SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3659
    int ret;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3660
    union sockaddr_u sa;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3661
    unsigned int alen = sizeof(sa);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3662
    char *addrP;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3663
    int addrObjSize, nAddrInstBytes;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3664
    OBJ addrClass;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3665
    int nAddrInstVars;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3666
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3667
    if (!__isNonNilObject(addr) || !__isBytes(addr)) {
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3668
	DBGPRINTF(("SOCKET: bad addr\n"));
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3669
	error = @symbol(badArgument);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3670
	goto err;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3671
    }
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3672
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3673
    addrClass = __qClass(addr);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3674
    nAddrInstVars = __intVal(__ClassInstPtr(addrClass)->c_ninstvars);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3675
    nAddrInstBytes = OHDR_SIZE + (nAddrInstVars * sizeof(OBJ));
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3676
    addrObjSize = __qSize(addr) - nAddrInstBytes;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3677
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3678
    sock = SOCKET_FROM_FILE_OBJECT(fp);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3679
    ret = getpeername(sock, (struct sockaddr *)&sa, &alen);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3680
    if (ret < 0) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3681
# ifdef __win32__
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3682
	errno = WSAGetLastError();
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3683
# endif
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3684
	DBGPRINTF(("SOCKET: getsocketname failed ret=%d errno=%d\n", ret, errno));
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3685
	error = __MKSMALLINT(errno);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3686
	goto err;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3687
    }
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3688
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3689
    if (addrObjSize < alen) {
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3690
	DBGPRINTF(("SOCKET: bad addr\n"));
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3691
	error = @symbol(badArgument);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3692
	goto err;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3693
    }
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3694
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3695
    addrP = (char *)__InstPtr(addr) + nAddrInstBytes;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3696
    memcpy(addrP, (char *)&sa, alen);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3697
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3698
err:;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3699
#else /* NO_SOCKET */
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3700
    error = @symbol(notImplemented);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3701
#endif /* NO_SOCKET */
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3702
%}.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3703
    error notNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3704
	^ self errorReporter reportOn:error
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3705
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3706
    ^ addr
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3707
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3708
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3709
getFullSocketAddress
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3710
    "implemented for swazoo project (primitive code can't be loaded as extension)
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3711
     Answer my own address (I am bound to this address).
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3712
     Note that this address may change after a connect or accept."
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3713
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3714
    |error domainClass addr|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3715
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3716
    handle isNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3717
	^ self errorNotOpen
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3718
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3719
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3720
    domainClass := self class socketAddressClassForDomain:domain.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3721
    domainClass isNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3722
	^ self error:'invalid (unsupported) domain'.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3723
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3724
    addr := domainClass new.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3725
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3726
%{
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3727
#ifndef NO_SOCKET
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3728
    OBJ fp = __INST(handle);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3729
    SOCKET sock;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3730
    int ret;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3731
    union sockaddr_u sa;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3732
    unsigned int alen = sizeof(sa);
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3733
    char *addrP;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3734
    int addrObjSize, nAddrInstBytes;
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3735
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3736
    if (!__isNonNilObject(addr) || !__isBytes(addr)) {
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3737
	DBGPRINTF(("SOCKET: bad addr\n"));
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3738
	error = @symbol(badArgument);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3739
	goto err;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3740
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3741
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3742
    {
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3743
	OBJ addrClass;
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3744
	int nAddrInstVars;
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3745
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3746
	addrClass = __qClass(addr);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3747
	nAddrInstVars = __intVal(__ClassInstPtr(addrClass)->c_ninstvars);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3748
	nAddrInstBytes = OHDR_SIZE + (nAddrInstVars * sizeof(OBJ));
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3749
	addrObjSize = __qSize(addr) - nAddrInstBytes;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3750
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3751
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3752
    sock = SOCKET_FROM_FILE_OBJECT(fp);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3753
    ret = getsockname(sock, (struct sockaddr *)&sa, &alen);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3754
    if (ret < 0) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3755
# ifdef __win32__
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3756
	errno = WSAGetLastError();
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3757
# endif
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3758
	DBGPRINTF(("SOCKET: getsocketname failed ret=%d errno=%d\n", ret, errno));
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3759
	error = __MKSMALLINT(errno);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3760
	goto err;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3761
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3762
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3763
    if (addrObjSize < alen) {
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3764
	DBGPRINTF(("SOCKET: bad addr\n"));
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3765
	error = @symbol(badArgument);
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3766
	goto err;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3767
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3768
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3769
    addrP = (char *)__InstPtr(addr) + nAddrInstBytes;
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3770
    memcpy(addrP, (char *)&sa, alen);
1535
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3771
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3772
err:;
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3773
#else /* NO_SOCKET */
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3774
    error = @symbol(notImplemented);
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3775
#endif /* NO_SOCKET */
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3776
%}.
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3777
    error notNil ifTrue:[
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  3778
	^ self errorReporter reportOn:error
1535
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3779
    ].
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3780
    ^ addr
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3781
!
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3782
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3783
getName
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3784
    "return the name; here, we return the ports name"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3785
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3786
    ^ self port printString
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3787
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3788
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3789
getPeer
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3790
    "ST-80 compatibility: return an IPSocketAddress instance representing
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3791
     my hostname/port combination.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3792
     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
  3793
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
  3794
    ^ peer
102
5dc264007513 added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
  3795
!
5dc264007513 added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
  3796
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3797
getPeerName
4091
ff4070ceffad #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4039
diff changeset
  3798
    "return the peer name; that's the hostname (or dotted name) of the
25
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
  3799
     partners host after an accept."
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
  3800
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
  3801
    peerName isNil ifTrue:[
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3802
	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
  3803
    ].
25
996051271ce9 *** empty log message ***
claus
parents: 21
diff changeset
  3804
    ^ peerName
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3805
!
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  3806
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3807
getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3808
    "implemented for swazoo project primitive code cant load as extension
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3809
     answer my own address (I am bound to this address).
1146
71a9df78ba9e bad spelling
Stefan Vogel <sv@exept.de>
parents: 1136
diff changeset
  3810
     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
  3811
1535
f6aa0fc8c3cc getSocketAddress
Claus Gittinger <cg@exept.de>
parents: 1533
diff changeset
  3812
    ^ self getFullSocketAddress hostAddress
1146
71a9df78ba9e bad spelling
Stefan Vogel <sv@exept.de>
parents: 1136
diff changeset
  3813
!
71a9df78ba9e bad spelling
Stefan Vogel <sv@exept.de>
parents: 1136
diff changeset
  3814
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3815
isActive
4795
58f0486fe4ba #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4791
diff changeset
  3816
    "return true, if the receiver has a connection or is bound or listening"
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3817
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3818
    ^ handle notNil
4795
58f0486fe4ba #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4791
diff changeset
  3819
58f0486fe4ba #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4791
diff changeset
  3820
    "Modified (comment): / 05-02-2019 / 17:19:35 / Stefan Vogel"
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3821
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3822
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3823
isConnected
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3824
    "return true, if the receiver has a connection"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3825
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3826
    ^ self isActive
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3827
	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
  3828
!
a563aac90660 No need to ask Socket>>#networkXXXOrderIsMSB, because the return value is
Stefan Vogel <sv@exept.de>
parents: 1542
diff changeset
  3829
5196
f2b4c109c24f #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 5001
diff changeset
  3830
isListening
f2b4c109c24f #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 5001
diff changeset
  3831
    ^ listening notNil
f2b4c109c24f #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 5001
diff changeset
  3832
!
f2b4c109c24f #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 5001
diff changeset
  3833
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3834
port
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3835
    "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
  3836
     - so this is the local port."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3837
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3838
"/    port isNil ifTrue:[
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3839
"/        port := self getFullSocketAddress port.
1505
Stefan Vogel <sv@exept.de>
parents: 1504
diff changeset
  3840
"/    ].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  3841
71
claus
parents: 63
diff changeset
  3842
    ^ port
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3843
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3844
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3845
socketAddressClass
4288
00e575acf898 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4261
diff changeset
  3846
    "get the matching SocketAddress class for this socket"
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3847
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3848
    |domainClass|
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3849
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3850
    domainClass := self class socketAddressClassForDomain:domain.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3851
    domainClass isNil ifTrue:[
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3852
	^ self error:'invalid (unsupported) domain'.
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3853
    ].
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3854
    ^ domainClass.
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3855
!
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3856
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3857
type
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3858
    "return the sockets connection type (i.e. #datagram, #stream etc)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3859
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3860
    ^ socketType
81
claus
parents: 79
diff changeset
  3861
! !
claus
parents: 79
diff changeset
  3862
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3863
!Socket methodsFor:'socket setup'!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3864
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3865
domain:domainArg type:typeArg
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3866
    "set up socket with domain and type.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3867
     This is a low level entry; no binding, listening or connect
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3868
     is done. Both arguments must be symbols from one of
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3869
      #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
  3870
3137
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  3871
    ^ self domain:domainArg type:typeArg protocol:nil
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3872
!
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  3873
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3874
domain:domainArg type:typeArg protocol:protocolNumber
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3875
    "set up socket with domain, type and protocol number.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3876
     This is a low level entry; no binding, listening or connect
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3877
     is done. Both arguments must be symbols from one of
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  3878
     #AF_INET, #AF_INET6, #AF_UNIX ... and #stream, #datagram, #raw resp."
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3879
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3880
    |domainName domainCode typeCode error newHandle|
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3881
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  3882
    handle notNil ifTrue:[
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3883
	^ self errorAlreadyOpen
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3884
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3885
    domainName := SocketAddress domainCodeFromName:domainArg.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3886
    domainCode := OperatingSystem domainCodeOf:domainName.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3887
    typeCode := OperatingSystem socketTypeCodeOf:typeArg.
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3888
    newHandle := OperatingSystem socketAccessor new.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3889
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3890
#ifndef NO_SOCKET
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3891
    FILE *fp;
3137
df3e9cebf52c class: Socket
Stefan Vogel <sv@exept.de>
parents: 3127
diff changeset
  3892
    int dom, typ, proto = 0;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3893
    int on = 1;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3894
    SOCKET sock;
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3895
    int _fd;
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3896
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3897
# ifdef __win32__
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3898
#  ifndef WSA_FLAG_NO_HANDLE_INHERIT
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3899
#   define WSA_FLAG_NO_HANDLE_INHERIT 0x80
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3900
#  endif
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3901
    static int noInheritFlag = WSA_FLAG_NO_HANDLE_INHERIT;
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3902
# endif
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3903
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3904
    if (! __isSmallInteger(domainCode)) {
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3905
	error = @symbol(badArgument1);
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3906
	goto out;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3907
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3908
    if (! __isSmallInteger(typeCode)) {
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3909
	error = @symbol(badArgument2);
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3910
	goto out;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3911
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3912
    if (protocolNumber != nil) {
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3913
	if (!__isSmallInteger(protocolNumber)) {
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3914
	    error = @symbol(badArgument3);
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3915
	    goto out;
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3916
	}
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3917
	proto = __intVal(protocolNumber);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3918
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3919
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3920
    /*
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3921
     * get address and protocol-family
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3922
     */
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3923
    dom = __intVal(domainCode);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3924
    typ = __intVal(typeCode);
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3925
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3926
# ifdef __win32__
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3927
    sock = WSASocket(dom, typ, proto, 0, 0, noInheritFlag);
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3928
    if (sock == INVALID_SOCKET && noInheritFlag) {
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3929
	// tried to open socket with WSA_FLAG_NO_HANDLE_INHERIT
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3930
	// This fails on older windows versions, e.g. Windows XP
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3931
	sock = WSASocket(dom, typ, proto, 0, 0, 0);
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3932
	if (sock != INVALID_SOCKET) {
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3933
	    // no error without WSA_FLAG_NO_HANDLE_INHERIT,
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3934
	    // never use this flag again!
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3935
	    noInheritFlag = 0;
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3936
	}
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3937
    }
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3938
    if (sock == INVALID_SOCKET) {
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3939
	errno = WSAGetLastError();
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3940
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3941
# else  // !__win32__
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3942
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3943
    sock = socket(dom, typ, proto);
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3944
# if defined(EPROTONOSUPPORT) /* for SGI */
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3945
    if ((sock < 0) && (proto != 0) && (errno == EPROTONOSUPPORT)) {
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3946
	DBGPRINTF(("SOCKET: retry with UNSPEC protocol\n"));
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3947
	proto = 0;
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3948
	sock = socket(dom, typ, 0);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3949
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3950
# endif
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3951
    if (sock < 0) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3952
# endif // !__win32__
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3953
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3954
	DBGPRINTF(("SOCKET: socket(dom=%d typ=%d proto=%d) call failed errno=%d\n", dom, typ, proto, errno));
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3955
	error = __MKSMALLINT(errno);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3956
    } else {
3123
55ef9ca4f09f class: Socket
Stefan Vogel <sv@exept.de>
parents: 3077
diff changeset
  3957
# if defined(SET_LINGER_WHEN_CREATING_SOCKET) && defined(SO_LINGER)
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3958
	{
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3959
	    struct linger l;
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3960
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3961
	    l.l_onoff = 1;
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3962
	    l.l_linger = 30;
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3963
	    setsockopt(sock, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3964
	}
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  3965
# endif
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3966
# ifdef __win32__
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3967
	/*
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3968
	 * make it blocking
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3969
	 */
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3970
	{
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3971
	    unsigned long zero = 0;
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3972
	    ioctlsocket(sock, FIONBIO, &zero);
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3973
	}
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3974
	{
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3975
#  if 0 && (defined( __BORLANDC__ ) || defined( __MINGW__ ))
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3976
	    /*
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3977
	     * make it a FILE *
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3978
	     */
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3979
	    __stxWrapApiEnterCritical();
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3980
	    _fd = _open_osfhandle((long)sock, 0);
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3981
	    __stxWrapApiLeaveCritical();
2773
ab3049f4a351 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2748
diff changeset
  3982
#  else
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3983
	    _fd = (int)sock;
2773
ab3049f4a351 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2748
diff changeset
  3984
#  endif
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3985
	    DBGPRINTF(("SOCKET: sock=%d fd=%d\n", sock, _fd));
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3986
	}
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3987
# else  // !__win32__
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3988
	fp = fdopen(sock, "r+");
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3989
	if (! fp) {
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3990
	    DBGPRINTF(("SOCKET: fdopen call failed\n"));
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3991
	    error = __MKSMALLINT(errno);
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3992
	    __BEGIN_INTERRUPTABLE__
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3993
	    closesocket(sock);
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3994
	    DBGFPRINTF((stderr, "SOCKET: fdopen failed (%d)\n", sock));
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3995
	    __END_INTERRUPTABLE__
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3996
	    goto out;
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  3997
	}
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  3998
# endif // !__win32__
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  3999
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  4000
	if (@global(FileOpenTrace) == true) {
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  4001
# ifdef __win32__
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  4002
	    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
  4003
# else
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  4004
	    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
  4005
# endif
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  4006
	}
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4007
3843
fbced69597a7 oops - error check was disabled
Claus Gittinger <cg@exept.de>
parents: 3742
diff changeset
  4008
# ifdef __win32__
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  4009
	__externalAddressVal(newHandle) = _fd;
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  4010
	__INST(handleType) = @symbol(socketHandle);
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4011
# else
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  4012
	__externalAddressVal(newHandle) = fp;
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  4013
	__INST(handleType) = @symbol(socketFilePointer);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4014
# endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4015
    }
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4016
#endif
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4017
out:;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4018
%}.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4019
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4020
    "all ok?"
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4021
    handleType notNil ifTrue:[
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  4022
	handle := newHandle.
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  4023
	domain := domainArg.
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  4024
	socketType := typeArg.
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  4025
	self registerForFinalization.
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  4026
	^ self.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4027
    ].
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  4028
    error isInteger ifTrue:[
4414
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  4029
	lastErrorNumber := error.
42edc97b2323 bcopy -> memcpy
Claus Gittinger <cg@exept.de>
parents: 4341
diff changeset
  4030
	^ self openError:error.
3217
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  4031
    ].
6950d2d3aecf class: Socket
Stefan Vogel <sv@exept.de>
parents: 3215
diff changeset
  4032
    ^ self primitiveFailed:error.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  4033
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  4034
    "
3059
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  4035
     Socket new domain:#AF_INET type:#stream
8be3c1e8dc05 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3043
diff changeset
  4036
     Socket new domain:#AF_UNIX type:#stream
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  4037
    "
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  4038
! !
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  4039
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  4040
!Socket methodsFor:'specials'!
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  4041
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  4042
linger:anIntegerOrNil
3224
bad706c50347 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3223
diff changeset
  4043
    "set the linger behavior on close:
bad706c50347 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3223
diff changeset
  4044
      anIntegerOrNil == nil: close returns immediately, socket tries
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4045
			     to send buffered data in background.
3224
bad706c50347 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3223
diff changeset
  4046
      anIntegerOrNil == 0:   close returns immediately, bufferd data is discarded.
bad706c50347 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3223
diff changeset
  4047
      anIntegerOrNil > 0:    close waits this many seconds for buffered data
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4048
			     to be delivered, after this time buffered data is
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4049
			     discarded and close returns with an error.
4796
3d3c49ddabdb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4795
diff changeset
  4050
     (returns false, if unsupported)"
3467
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  4051
24d35ef42e17 socket options rcvtimeout and sndtimeout added
Claus Gittinger <cg@exept.de>
parents: 3462
diff changeset
  4052
    ^ self
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4053
	setSocketOption:#'SO_LINGER'
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4054
	argument:anIntegerOrNil notNil
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4055
	argument:anIntegerOrNil.
4796
3d3c49ddabdb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4795
diff changeset
  4056
3d3c49ddabdb #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4795
diff changeset
  4057
    "Modified (comment): / 08-02-2019 / 22:33:14 / Claus Gittinger"
3224
bad706c50347 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3223
diff changeset
  4058
!
bad706c50347 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3223
diff changeset
  4059
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4060
receiveBufferSize
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4061
    "get the send buffer size - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4062
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4063
     (returns nil, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4064
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4065
    handle isNil ifTrue:[
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4066
	^ self errorNotOpen
1335
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4067
    ].
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4068
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4069
#if defined(SO_RCVBUF) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4070
    {
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4071
	OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4072
	SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4073
	int opt;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4074
	unsigned int size;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4075
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4076
	sock = SOCKET_FROM_FILE_OBJECT(fp);
1568
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4077
	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
  4078
	    RETURN( __MKSMALLINT(opt) );
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4079
	}
1335
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4080
    }
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4081
#endif
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4082
%}.
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4083
    ^ nil
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4084
!
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4085
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4086
receiveBufferSize:size
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4087
    "set the receive buffer size - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4088
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4089
     (returns false, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4090
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4091
    handle isNil ifTrue:[
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4092
	^ self errorNotOpen
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4093
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4094
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4095
#if defined(SO_RCVBUF) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4096
    if (__isSmallInteger(size)) {
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4097
	OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4098
	SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4099
	int opt;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4100
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4101
	sock = SOCKET_FROM_FILE_OBJECT(fp);
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  4102
	opt = __intVal(size);
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  4103
	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
  4104
	    RETURN(true);
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  4105
	}
1335
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4106
    }
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4107
#endif
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4108
%}.
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4109
    ^ false
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4110
!
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4111
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4112
receiveTimeout
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4113
    "get the receive timeout in millis - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4114
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4115
     (returns nil, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4116
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4117
    |millis|
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4118
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4119
    handle isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4120
	^ self errorNotOpen
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4121
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4122
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4123
#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
  4124
    OBJ fp = __INST(handle);
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4125
    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
  4126
    int len;
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4127
    int __millis;
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4128
    struct timeval tv = {0, 0};
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4129
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4130
    len = sizeof(struct timeval);
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4131
    if (getsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, &len) == 0) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4132
	__millis = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4133
	millis = __mkSmallInteger(__millis);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4134
# if 0
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4135
	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
  4136
# endif
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4137
    } else {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4138
	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
  4139
    }
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4140
#endif
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4141
%}.
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4142
    ^ millis
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4143
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4144
    "
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4145
	Socket newTCP receiveTimeout
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4146
    "
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4147
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4148
    "Modified: / 19-01-2018 / 19:15:17 / stefan"
1568
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4149
!
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4150
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4151
receiveTimeout:secondsOrTimeDuration
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4152
    "set the receive timeout - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4153
     Not all operatingSystems offer this functionality
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4154
     (returns false, if unsupported).
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4155
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4156
     From linux manpage:
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4157
	  SO_RCVTIMEO and SO_SNDTIMEO
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4158
	      Specify  the  receiving  or  sending  timeouts  until reporting an error.  The argument is a
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4159
	      struct timeval.  If an input or output function blocks for this period of time, and data has
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4160
	      been  sent  or received, the return value of that function will be the amount of data trans-
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4161
	      ferred; if no data has been transferred and  the  timeout  has  been  reached,  then  -1  is
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4162
	      returned with errno set to EAGAIN or EWOULDBLOCK, or EINPROGRESS (for connect(2)) just as if
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4163
	      the socket was specified to be nonblocking.  If the timeout is set to  zero  (the  default),
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4164
	      then the operation will never timeout.  Timeouts only have effect for system calls that per-
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4165
	      form socket I/O (e.g., read(2), recvmsg(2), send(2), sendmsg(2)); timeouts  have  no  effect
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4166
	      for select(2), poll(2), epoll_wait(2), and so on."
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4167
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4168
    |millis|
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4169
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4170
    handle isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4171
	^ self errorNotOpen
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4172
    ].
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4173
    secondsOrTimeDuration isTimeDuration ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4174
	millis := secondsOrTimeDuration getMilliseconds.
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4175
    ] ifFalse:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4176
	millis := (secondsOrTimeDuration * 1000) rounded.
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4177
    ].
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4178
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4179
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4180
#if defined(SO_RCVTIMEO) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4181
    if (__isSmallInteger(millis)) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4182
	OBJ fp = __INST(handle);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4183
	SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4184
	int __millis = __intVal(millis);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4185
	struct timeval tv = {0, 0};
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4186
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4187
	tv.tv_sec = __millis / 1000;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4188
	tv.tv_usec = (__millis % 1000) * 1000;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4189
# if 0
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4190
	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
  4191
# endif
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4192
	if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(struct timeval)) == 0) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4193
	    RETURN(true);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4194
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4195
	console_fprintf(stderr, "Socket [warning]: setsockopt %d failed; errno=%d\n", sock, errno);
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  4196
    }
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  4197
#endif
71
claus
parents: 63
diff changeset
  4198
%}.
28
350f8e9493a4 *** empty log message ***
claus
parents: 25
diff changeset
  4199
    ^ false
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4200
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4201
    "
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4202
	Socket newTCP
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4203
	    receiveTimeout:5s;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4204
	    receiveTimeout
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4205
    "
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4206
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4207
    "Modified (comment): / 19-01-2018 / 19:14:49 / stefan"
81
claus
parents: 79
diff changeset
  4208
!
claus
parents: 79
diff changeset
  4209
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4210
sendBufferSize
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4211
    "get the send buffer size - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4212
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4213
     (returns nil, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4214
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4215
    handle isNil ifTrue:[
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4216
	^ self errorNotOpen
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4217
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4218
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4219
#if defined(SO_SNDBUF) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4220
    {
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4221
	OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4222
	SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4223
	int opt;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4224
	unsigned int size;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4225
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4226
	sock = SOCKET_FROM_FILE_OBJECT(fp);
1568
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4227
	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
  4228
	    RETURN( __MKSMALLINT(opt) );
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4229
	}
1335
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4230
    }
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4231
#endif
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4232
%}.
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4233
    ^ nil
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4234
!
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4235
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4236
sendBufferSize:size
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4237
    "set the send buffer size - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4238
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4239
     (returns false, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4240
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4241
    handle isNil ifTrue:[
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4242
	^ self errorNotOpen
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4243
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4244
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4245
#if defined(SO_SNDBUF) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4246
    if (__isSmallInteger(size)) {
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4247
	OBJ fp = __INST(handle);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4248
	SOCKET sock;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4249
	int opt;
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4250
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4251
	sock = SOCKET_FROM_FILE_OBJECT(fp);
1343
cc3bed115c0a definitions cleanup with stxOSDefs.h
penk
parents: 1342
diff changeset
  4252
	opt = __intVal(size);
1529
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  4253
	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
  4254
	    RETURN(true);
911273d090c1 Re-unified Win32 and Unix code into single Socket.st.
Claus Gittinger <cg@exept.de>
parents: 1505
diff changeset
  4255
	}
1335
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4256
    }
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4257
#endif
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4258
%}.
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4259
    ^ false
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4260
!
0fafb78d7a80 sockopt utilities added
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
  4261
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4262
sendTimeout
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4263
    "get the send timeout in millis - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4264
     Not all operatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4265
     (returns nil, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4266
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4267
    |millis|
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4268
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4269
    handle isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4270
	^ self errorNotOpen
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4271
    ].
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4272
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4273
#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
  4274
    OBJ fp = __INST(handle);
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4275
    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
  4276
    int len;
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4277
    int __millis;
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4278
    struct timeval tv = {0, 0};
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4279
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4280
    len = sizeof(struct timeval);
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4281
    if (getsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (void *)&tv, &len) == 0) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4282
	__millis = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4283
	millis = __mkSmallInteger(__millis);
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4284
# if 0
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4285
	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
  4286
# endif
3246
c75a006f9a7a Win32 sockets use handles instead of file pointers
Stefan Vogel <sv@exept.de>
parents: 3224
diff changeset
  4287
    } else {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4288
	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
  4289
    }
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4290
#endif
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4291
%}.
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4292
    ^ millis
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4293
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4294
    "
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4295
	Socket newTCP sendTimeout
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4296
    "
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4297
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4298
    "Modified (comment): / 19-01-2018 / 19:16:23 / stefan"
1568
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4299
!
3bca500e0c7a send and receiveTimeout fixed
Claus Gittinger <cg@exept.de>
parents: 1561
diff changeset
  4300
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4301
sendTimeout:secondsOrTimeDuration
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4302
    "set the send timeout - for special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4303
     Not all operatingSystems offer this functionality
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4304
     (returns false, if unsupported).
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4305
     From linux manpage:
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4306
	  SO_RCVTIMEO and SO_SNDTIMEO
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4307
	      Specify  the  receiving  or  sending  timeouts  until reporting an error.  The argument is a
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4308
	      struct timeval.  If an input or output function blocks for this period of time, and data has
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4309
	      been  sent  or received, the return value of that function will be the amount of data trans-
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4310
	      ferred; if no data has been transferred and  the  timeout  has  been  reached,  then  -1  is
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4311
	      returned with errno set to EAGAIN or EWOULDBLOCK, or EINPROGRESS (for connect(2)) just as if
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4312
	      the socket was specified to be nonblocking.  If the timeout is set to  zero  (the  default),
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4313
	      then the operation will never timeout.  Timeouts only have effect for system calls that per-
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4314
	      form socket I/O (e.g., read(2), recvmsg(2), send(2), sendmsg(2)); timeouts  have  no  effect
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4315
	      for select(2), poll(2), epoll_wait(2), and so on."
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4316
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4317
    |millis|
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4318
2321
6f4cc7b644db filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 2308
diff changeset
  4319
    handle isNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4320
	^ self errorNotOpen
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4321
    ].
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4322
    secondsOrTimeDuration isTimeDuration ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4323
	millis := secondsOrTimeDuration getMilliseconds.
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4324
    ] ifFalse:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4325
	millis := (secondsOrTimeDuration * 1000) rounded.
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4326
    ].
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4327
%{
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4328
#if defined(SO_SNDTIMEO) && defined(SOL_SOCKET)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4329
    if (__isSmallInteger(millis)) {
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4330
	OBJ fp = __INST(handle);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4331
	SOCKET sock = SOCKET_FROM_FILE_OBJECT(fp);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4332
	int __millis = __intVal(millis);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4333
	struct timeval tv = {0, 0};
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4334
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4335
	tv.tv_sec = __millis / 1000;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4336
	tv.tv_usec = (__millis % 1000) * 1000;
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4337
# if 0
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4338
	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
  4339
# endif
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4340
	if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (void *)&tv, sizeof(struct timeval)) == 0) {
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4341
	    RETURN(true);
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4342
	}
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4343
	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
  4344
    }
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  4345
#endif
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  4346
%}.
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  4347
    ^ false
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4348
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4349
    "
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4350
	Socket newTCP
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4351
	    sendTimeout:5s;
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4352
	    sendTimeout
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4353
    "
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4354
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4355
    "Modified (comment): / 19-01-2018 / 19:12:51 / stefan"
1340
eb9b1f1340e5 setTCP_NODELAY
Claus Gittinger <cg@exept.de>
parents: 1339
diff changeset
  4356
!
eb9b1f1340e5 setTCP_NODELAY
Claus Gittinger <cg@exept.de>
parents: 1339
diff changeset
  4357
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4358
setTCPCork:aBoolean
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4359
    "enable/disable TCP_CORK (do-not-send-partial-frames)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4360
     For special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4361
     Not all OperatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4362
     (returns false, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4363
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  4364
    ^ self setSocketOption:#'TCP_CORK' argument:aBoolean argument:nil.
1885
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4365
!
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4366
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4367
setTCPNoDelay:aBoolean
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4368
    "enable/disable TCP_NODELAY (i.e. disable/enable the Nagle algorithm)
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4369
     For special applications only.
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4370
     Not all OperatingSystems offer this functionality
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4371
     (returns false, if unsupported)"
Claus Gittinger <cg@exept.de>
parents: 1884
diff changeset
  4372
2551
6da1bc89b74a changed:
Stefan Vogel <sv@exept.de>
parents: 2516
diff changeset
  4373
    ^ self setSocketOption:#'TCP_NODELAY' argument:aBoolean argument:nil.
126
fca9404da9d4 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
  4374
! !
82
claus
parents: 81
diff changeset
  4375
5441
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4376
!Socket methodsFor:'support websocket'!
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4377
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4378
nonBlockingNextPutAll:someBytes
5445
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4379
    "using #primNonBlockingNextPutAll: 
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4380
     because #nextPutAll: handles WSAEWOULDBLOCK / EWOULDBLOCK incorrect (at least for windows)
5441
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4381
     only called by WebSocketStream #criticalSocketNextPutAll:"
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4382
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4383
    |bytes countRemainingBytesToWrite result|
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4384
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4385
    OperatingSystem isMSWINDOWSlike ifFalse:[
5445
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4386
        "this method supports non blocking socket for windows,
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4387
         for other os use the default behavior"
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4388
        self nextPutAll:someBytes.
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4389
        ^ self 
5441
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4390
    ].
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4391
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4392
    bytes := someBytes asByteArray.
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4393
    countRemainingBytesToWrite := bytes size.
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4394
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4395
    [
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4396
        result := self primNonBlockingNextPutAll:bytes. "/ may just writes a subset or may 0 -> would block
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4397
        result >= 0 "/ 0 or more bytes has been written
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4398
        and:[result ~= countRemainingBytesToWrite] "/ there are remaining bytes to write 
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4399
    ] whileTrue:[
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4400
        result > 0 ifTrue:[
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4401
            bytes := bytes copyFrom:result + 1.
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4402
            countRemainingBytesToWrite := bytes size.
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4403
        ].
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4404
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4405
        "/ timeout does not matter, we wait infinitiv (due to loop)   
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4406
        self writeWaitWithTimeoutMs:1000.
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4407
    ].
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4408
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4409
    "Created: / 30-01-2020 / 16:35:08 / Stefan Reise"
5445
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4410
    "Modified (format): / 06-02-2020 / 13:18:59 / Stefan Reise"
5441
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4411
!
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4412
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4413
primNonBlockingNextPutAll:someBytes
5445
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4414
    "using #primNonBlockingNextPutAll: 
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4415
     because #nextPutAll: handles WSAEWOULDBLOCK / EWOULDBLOCK incorrect (at least for windows)
5441
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4416
     only called by WebSocketStream #criticalSocketNextPutAll:"
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4417
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4418
    |bytes byteLength returnValue|
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4419
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4420
    OperatingSystem isMSWINDOWSlike ifFalse:[
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4421
        self error:'this method is for windows os only'.
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4422
    ].
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4423
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4424
    bytes := someBytes asExternalBytes.
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4425
    byteLength := bytes size.
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4426
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4427
    "
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4428
        -1      error
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4429
        0       0 bytes sent or would block -> try again after wait
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4430
        > 0     bytes sent (recall myself with the remaining bytes)
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4431
    "
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4432
    returnValue := -1. 
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4433
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4434
    %{
5443
3eac683cfb1d put #ifdef around win32 only code
Stefan Vogel <sv@exept.de>
parents: 5442
diff changeset
  4435
# ifdef __win32__
5441
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4436
        int sendResult;
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4437
        int wsaErrorNo;
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4438
        char *pBytes = __externalAddressVal(bytes);
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4439
        SOCKET socket = SOCKET_FROM_FILE_OBJECT(__INST(handle));
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4440
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4441
        sendResult = send(socket, pBytes, __intVal(byteLength), 0);
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4442
        if (sendResult == SOCKET_ERROR) {
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4443
            wsaErrorNo = WSAGetLastError();
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4444
            if (wsaErrorNo == WSAEWOULDBLOCK) {
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4445
                returnValue = __MKSMALLINT(0);
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4446
            } else {
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4447
                console_printf("send failed with: %d\n", wsaErrorNo);
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4448
            }
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4449
        } else {
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4450
            returnValue = __MKSMALLINT(sendResult);
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4451
        }
5443
3eac683cfb1d put #ifdef around win32 only code
Stefan Vogel <sv@exept.de>
parents: 5442
diff changeset
  4452
#endif // __win32__
5441
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4453
    %}.
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4454
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4455
    returnValue < 0 ifTrue:[
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4456
        self primitiveFailed.
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4457
    ].
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4458
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4459
    ^ returnValue
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4460
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4461
    "Created: / 30-01-2020 / 16:36:01 / Stefan Reise"
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4462
    "Modified: / 31-01-2020 / 11:53:01 / Stefan Reise"
5445
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4463
    "Modified (comment): / 06-02-2020 / 13:11:17 / Stefan Reise"
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4464
!
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4465
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4466
setNonBlocking
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4467
    "using #setNonBlocking 
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4468
     because implementing and using Win32OperatingSystem #setBlocking:fd: 
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4469
     would reset to blocking (see senders of #blocking:)
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4470
     and we need the socket to be non-blocking forever 
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4471
     only called by WebSocketStream #criticalSocketNextPutAll:"
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4472
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4473
    OperatingSystem isMSWINDOWSlike ifFalse:[
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4474
        "/ this method is for windows os only
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4475
        ^ self
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4476
    ].
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4477
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4478
    %{
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4479
# ifdef __win32__
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4480
        int result;
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4481
        u_long nonBlocking = 1;
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4482
        SOCKET socket = SOCKET_FROM_FILE_OBJECT(__INST(handle));
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4483
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4484
        result = ioctlsocket(socket, FIONBIO, &nonBlocking);
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4485
        if (result == SOCKET_ERROR) {
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4486
            console_fprintf(stderr, "Win32OS [info]: ioctlsocket failed with %d\n", WSAGetLastError());
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4487
        }
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4488
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4489
        RETURN(true);
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4490
#endif // __win32__
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4491
    %}.
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4492
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4493
    self primitiveFailed.
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4494
41990ba5aeff #FEATURE by Stefan Reise
sr
parents: 5443
diff changeset
  4495
    "Created: / 06-02-2020 / 13:09:42 / Stefan Reise"
5441
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4496
! !
bdf6b4e8c68b #BUGFIX by Stefan Reise
sr
parents: 5440
diff changeset
  4497
5450
7bc191b240c5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 5445
diff changeset
  4498
!Socket methodsFor:'testing'!
7bc191b240c5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 5445
diff changeset
  4499
7bc191b240c5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 5445
diff changeset
  4500
isSSLSocket
7bc191b240c5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 5445
diff changeset
  4501
    ^ false
7bc191b240c5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 5445
diff changeset
  4502
! !
7bc191b240c5 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 5445
diff changeset
  4503
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4504
!Socket methodsFor:'waiting'!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4505
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4506
waitForNewConnectionOrDataOnAny:otherConnections timeout:secondsOrTimeDurationOrNil
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4507
    "suspend the current process, until either a new connection comes
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4508
     in at the receiver, or data arrives on any of the otherConnections.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4509
     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
  4510
     For an old connection, that socket is returned.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4511
     In any case, the caller gets a socket to operate on as return value,
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4512
     or nil, if a timeout occurred.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4513
     This method implements the inner wait-primitive of a multi-connection
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4514
     server application."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4515
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4516
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4517
    |wasBlocked sema  timeoutMs|
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4518
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4519
    "first, a quick check if data is already available"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4520
    self canReadWithoutBlocking ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4521
	^ self accept.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4522
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4523
    otherConnections do:[:aConnection |
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4524
	aConnection canReadWithoutBlocking ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4525
	    ^ aConnection
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4526
	]
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4527
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4528
3281
bd9392932db1 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3247
diff changeset
  4529
    "check again - prevent incoming interrupts from disturbing our setup"
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4530
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4531
    secondsOrTimeDurationOrNil notNil ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4532
	secondsOrTimeDurationOrNil isTimeDuration ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4533
	    timeoutMs := secondsOrTimeDurationOrNil getMilliseconds.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4534
	] ifFalse:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4535
	    timeoutMs := secondsOrTimeDurationOrNil * 1000.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4536
	]
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4537
    ].
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4538
3281
bd9392932db1 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3247
diff changeset
  4539
    wasBlocked := OperatingSystem blockInterrupts.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4540
    [
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4541
	sema := Semaphore name:'multiReadWait'.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4542
	otherConnections do:[:aConnection |
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4543
	    Processor signal:sema onInput:(aConnection fileDescriptor).
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4544
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4545
	Processor signal:sema onInput:(self fileDescriptor).
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4546
	timeoutMs notNil ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4547
	    Processor signal:sema afterMilliseconds:timeoutMs
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4548
	].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4549
	Processor activeProcess state:#ioWait.
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4550
	sema wait.
3281
bd9392932db1 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3247
diff changeset
  4551
    ] ifCurtailed:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4552
	sema notNil ifTrue:[Processor disableSemaphore:sema].
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4553
	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4554
    ].
3281
bd9392932db1 class: Socket
Stefan Vogel <sv@exept.de>
parents: 3247
diff changeset
  4555
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4556
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4557
    "see who it was ..."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4558
    self canReadWithoutBlocking ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4559
	^ self accept.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4560
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4561
    otherConnections do:[:aConnection |
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4562
	aConnection canReadWithoutBlocking ifTrue:[
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4563
	    ^ aConnection
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4564
	]
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4565
    ].
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4566
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4567
    "none - a timeout"
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4568
    ^ nil
4497
bf5e9d5ec532 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4414
diff changeset
  4569
bf5e9d5ec532 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4414
diff changeset
  4570
    "Modified: / 09-08-2017 / 11:59:50 / cg"
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4571
    "Modified: / 19-01-2018 / 18:59:17 / stefan"
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4572
!
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4573
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4574
waitForNewConnectionWithTimeout:secondsOrTimeDurationOrNil
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4575
    "suspend the current process, until a new connection comes
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4576
     in at the receiver or a timeout occurs.
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4577
     For a new connection, an accept is performed and the new socket is returned.
4032
24df60dcd5f4 #OTHER by mawalch
mawalch
parents: 3989
diff changeset
  4578
     Returns nil, if a timeout occurred.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4579
     This method implements the inner wait-primitive of a single-connection
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4580
     server application."
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4581
3633
3a21f142432e #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3620
diff changeset
  4582
    |newSock|
3a21f142432e #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3620
diff changeset
  4583
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4584
    (self readWaitWithTimeout:secondsOrTimeDurationOrNil) ifTrue:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4585
	"a timeout occurred - no connection within timeout"
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4586
	self reportError:(OperatingSystem errorNumberFor:#ETIMEDOUT).
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4587
	^ nil.
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4588
    ].
4180
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  4589
    self isOpen ifFalse:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4590
	"I have been closed while waiting"
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4591
	^ self errorNotOpen.
4180
f2539f82b744 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4172
diff changeset
  4592
    ].
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  4593
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  4594
    "ok, a connection is present - accept it"
3633
3a21f142432e #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3620
diff changeset
  4595
    newSock := self class new.
3a21f142432e #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3620
diff changeset
  4596
    (newSock primAcceptOn:self blocking:false) ifFalse:[
5001
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4597
	"should raise an error here - primitive code raises a notification"
11cbbbcbc87e warnings
Claus Gittinger <cg@exept.de>
parents: 4961
diff changeset
  4598
	^ nil
3633
3a21f142432e #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3620
diff changeset
  4599
    ].
3a21f142432e #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3620
diff changeset
  4600
    ^ newSock
4568
c2b933093d63 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4506
diff changeset
  4601
4570
39e965ca8d06 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 4568
diff changeset
  4602
    "Modified (format): / 19-01-2018 / 18:53:15 / stefan"
1935
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4603
! !
326f8d31e7ca Clean up:
Stefan Vogel <sv@exept.de>
parents: 1911
diff changeset
  4604
464
600b101a7035 alen in accept
Claus Gittinger <cg@exept.de>
parents: 463
diff changeset
  4605
!Socket class methodsFor:'documentation'!
206
77166a6b3ee6 For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents: 203
diff changeset
  4606
77166a6b3ee6 For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents: 203
diff changeset
  4607
version
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  4608
    ^ '$Header$'
2292
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  4609
!
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  4610
c4b0b901278e added: #standardTimeout
Claus Gittinger <cg@exept.de>
parents: 1940
diff changeset
  4611
version_CVS
3590
3487165a9cdf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3550
diff changeset
  4612
    ^ '$Header$'
206
77166a6b3ee6 For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents: 203
diff changeset
  4613
! !
5196
f2b4c109c24f #FEATURE by exept
Claus Gittinger <cg@exept.de>
parents: 5001
diff changeset
  4614