aboutsummaryrefslogtreecommitdiff
path: root/src/stable/serversockettcp.cpp
blob: 5f4f3c3057e2e971b36e9789bd53f53e944fc08a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * Copyright (C) 2007-2023 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/config.h"

#ifndef WIN32
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <netdb.h>
 #include <arpa/inet.h>
#endif

#include <time.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
//#include <termios.h>
#include <fcntl.h>
#include "bu/serversockettcp.h"
#include "bu/sockettcp.h"

namespace Bu { subExceptionDef( ServerSocketTcpException ) }

Bu::ServerSocketTcp::ServerSocketTcp( int iPort, int nPoolSize ) :
    iPort( iPort )
{
#ifdef WIN32
    Bu::Winsock2::getInstance();
#endif
    
    /* Create the socket and set it up to accept connections. */
    struct sockaddr_in name;

    /* Give the socket a name. */
    name.sin_family = AF_INET;
    name.sin_port = bu_htons( iPort );

    // I think this specifies who we will accept connections from,
    // a good thing to make configurable later on
    name.sin_addr.s_addr = bu_htonl( INADDR_ANY );

    startServer( name, nPoolSize );
}

Bu::ServerSocketTcp::ServerSocketTcp(const String &sAddr,int iPort, int nPoolSize) :
    iPort( iPort )
{
#ifdef WIN32
    Bu::Winsock2::getInstance();
#endif

    /* Create the socket and set it up to accept connections. */
    struct sockaddr_in name;

    /* Give the socket a name. */
    name.sin_family = AF_INET;

    name.sin_port = bu_htons( iPort );

#ifdef WIN32
    name.sin_addr.s_addr = bu_inet_addr( sAddr.getStr() );
#else
    inet_aton( sAddr.getStr(), &name.sin_addr );
#endif

    startServer( name, nPoolSize );
}

Bu::ServerSocketTcp::ServerSocketTcp( socket_t iSocket, bool bInit, int nPoolSize ) :
    iSocket( iSocket ),
    iPort( 0 )
{
#ifdef WIN32
    Bu::Winsock2::getInstance();
#endif
    
    if( bInit )
    {
        struct sockaddr name;
        socklen_t namelen = sizeof(name);
        getpeername( iSocket, &name, &namelen );

        initServer( *((sockaddr_in *)&name), nPoolSize );
    }
    else
    {
        FD_ZERO( &fdActive );
        FD_SET( iSocket, &fdActive );
    }
}

Bu::ServerSocketTcp::ServerSocketTcp( const ServerSocketTcp &rSrc )
{
#ifdef WIN32
    Bu::Winsock2::getInstance();
#endif

    iSocket = dup( rSrc.iSocket );
    iPort = rSrc.iPort;
    FD_ZERO( &fdActive );
    FD_SET( iSocket, &fdActive );
}

Bu::ServerSocketTcp::~ServerSocketTcp()
{
#ifdef WIN32
    if( iSocket != INVALID_SOCKET )
#else
    if( iSocket > -1 )
#endif
        ::close( iSocket );
}

void Bu::ServerSocketTcp::startServer( struct sockaddr_in &name, int nPoolSize )
{
    /* Create the socket. */
    iSocket = bu_socket( PF_INET, SOCK_STREAM, 0 );

#ifdef WIN32
    if( iSocket == INVALID_SOCKET )
#else
    if( iSocket < 0 )
#endif
    {
        throw Bu::ServerSocketTcpException("Couldn't create a listen socket.");
    }
    
    int opt = 1;
    bu_setsockopt(
            iSocket,
            SOL_SOCKET,
            SO_REUSEADDR,
            (char *)&opt,
            sizeof( opt )
            );

    initServer( name, nPoolSize );
}

void Bu::ServerSocketTcp::initServer( struct sockaddr_in &name, int nPoolSize )
{
    if( bu_bind( iSocket, (struct sockaddr *) &name, sizeof(name) ) < 0 )
    {
        throw Bu::ServerSocketTcpException("Couldn't bind to the listen socket.");
    }

    if( bu_listen( iSocket, nPoolSize ) < 0 )
    {
        throw Bu::ServerSocketTcpException(
            "Couldn't begin listening to the server socket."
            );
    }

    FD_ZERO( &fdActive );
    /* Initialize the set of active sockets. */
    FD_SET( iSocket, &fdActive );
}

Bu::Socket *Bu::ServerSocketTcp::accept( int nTimeoutSec, int nTimeoutUSec )
{
    fd_set fdRead = fdActive;

    struct timeval xT;

    xT.tv_sec = nTimeoutSec;
    xT.tv_usec = nTimeoutUSec;  

    if( TEMP_FAILURE_RETRY(
        bu_select( iSocket+1, &fdRead, NULL, NULL, &xT )) < 0 )
    {
        throw Bu::ServerSocketTcpException(
            "Error scanning for new connections: %s", strerror( errno )
            );
    }

    if( FD_ISSET( iSocket, &fdRead ) )
    {
        struct sockaddr_in clientname;
        socklen_t size;
        int nClient;

        size = sizeof( clientname );
#ifdef WIN32
        nClient = bu_accept( iSocket, (struct sockaddr *)&clientname, &size);
#else /* not-WIN32 */
#ifdef __CYGWIN__
        nClient = ::accept( iSocket, (struct sockaddr *)&clientname,
                (int *)&size
                );
#else /* not-cygwin */
#ifdef __APPLE__
        nClient = ::accept( iSocket, (struct sockaddr *)&clientname, (socklen_t*)&size );
#else /* linux */
        nClient = ::accept( iSocket, (struct sockaddr *)&clientname, &size );
#endif /* __APPLE__ */
#endif /* __CYGWIN__ */
#endif /* WIN32 */
        if( nClient < 0 )
        {
            throw Bu::ServerSocketTcpException(
                "Error accepting a new connection: %s", strerror( errno )
                );
        }

#ifndef WIN32
        char tmpa[20];
        inet_ntop( AF_INET, (void *)&clientname.sin_addr, tmpa, 20 );
        //"New connection from host %s, port %hd.",
        //  tmpa, ntohs (clientname.sin_port) );
#endif
        
        {
#ifndef WIN32
            int flags;
            flags = fcntl( nClient, F_GETFL, 0 );
            flags |= O_NONBLOCK;
            if( fcntl( nClient, F_SETFL, flags ) < 0)
            {
                throw Bu::ServerSocketTcpException(
                    "Error setting option on client socket: %s",
                    strerror( errno )
                    );
            }
#else
            //-------------------------
            // Set the socket I/O mode: In this case FIONBIO
            // enables or disables the blocking mode for the 
            // socket based on the numerical value of iMode.
            // If iMode = 0, blocking is enabled; 
            // If iMode != 0, non-blocking mode is enabled.
            u_long iMode = 1;
            bu_ioctlsocket(nClient, FIONBIO, &iMode);           
#endif
        }

        return new SocketTcp( nClient );
    }

    return NULL;
}

bool Bu::ServerSocketTcp::getFd( int &rFdOut ) const
{
    rFdOut = iSocket;
    return true;
}