aboutsummaryrefslogtreecommitdiff
path: root/src/stable/serversockettcp.h
blob: 8e43c76698eda57835ed3b9958f2ea5ea06f13f1 (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
/*
 * Copyright (C) 2007-2019 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.
 */

#ifndef BU_SERVER_SOCKET_TCP_H
#define BU_SERVER_SOCKET_TCP_H

#include <stdint.h>
#include "bu/string.h"
#include "bu/exceptionbase.h"
#include "bu/serversocket.h"

#ifdef WIN32
 #include <Winsock2.h>
#else
 #include <sys/select.h>
#endif

namespace Bu
{
    subExceptionDecl( ServerSocketTcpException );

    /**
     * A single tcp/ip server socket.  When created the server socket will bind
     * to the specified interface and port, and immediately begin listening for
     * connections.  When connections come in they are pooled by the networking
     * drivers in the kernel until they are accepted, this means that failure
     * to keep space in the connection pool will result in connection refusals.
     *
     * Although the accept function returns an integral file descriptor, it is
     * designed to be used with the Socket class.
     *
     *@ingroup Serving
     */
    class ServerSocketTcp : public ServerSocket
    {
    public:
#ifdef WIN32
        typedef unsigned int socket_t;
#else
        typedef int socket_t;
#endif
        ServerSocketTcp( int iPort, int nPoolSize=40 );
        ServerSocketTcp( const String &sAddr, int iPort, int nPoolSize=40 );
        ServerSocketTcp( socket_t nSocket, bool bInit, int nPoolSize=40 );
        ServerSocketTcp( const ServerSocketTcp &rSrc );
        virtual ~ServerSocketTcp();

        virtual Bu::Socket *accept( int nTimeoutSec=0, int nTimeoutUSec=0 );
        virtual bool getFd( int &rFdOut ) const;

    private:
        void startServer( struct sockaddr_in &name, int nPoolSize );
        void initServer( struct sockaddr_in &name, int nPoolSize );

        fd_set fdActive;
        socket_t iSocket;
        int iPort;
    };
}

#endif