summaryrefslogtreecommitdiff
path: root/src/3rd_party/apache-log4cxx-0.10.0/src/main/cpp/serversocket.cpp
blob: e5c1b29be9dafb121776ec588d82fbf8fbd72849 (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
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Note: This file has been modified from its original form.
 */
 
#include <log4cxx/helpers/serversocket.h>
#include <log4cxx/helpers/synchronized.h>
#include "apr_network_io.h"
#include "apr_pools.h"
#include "apr_poll.h"

using namespace log4cxx::helpers;

/**  Creates a server socket on a specified port.
*/
ServerSocket::ServerSocket(int port) : pool(), mutex(pool), socket(0), timeout(0)
{
  apr_status_t status =
    apr_socket_create(&socket, APR_INET, SOCK_STREAM, 
                      APR_PROTO_TCP, pool.getAPRPool());
  if (status != APR_SUCCESS) {
    throw SocketException(status);
  }
  
  status = apr_socket_opt_set(socket, APR_SO_NONBLOCK, 1);
  if (status != APR_SUCCESS) {
    throw SocketException(status);
  }
  
  // AKirov: Added SO_REUSEADDR option to fix APPLINK-15273
  status = apr_socket_opt_set(socket, APR_SO_REUSEADDR, 1);
  if (status != APR_SUCCESS) {
    throw SocketException(status);
  }

        // Create server socket address (including port number)
  apr_sockaddr_t *server_addr;
  status =
      apr_sockaddr_info_get(&server_addr, NULL, APR_INET,
                                  port, 0, pool.getAPRPool());
  if (status != APR_SUCCESS) {
      throw ConnectException(status);
  }

  // bind the socket to the address
  status = apr_socket_bind(socket, server_addr);
  if (status != APR_SUCCESS) {
      throw BindException(status);
  }
  
  
  status = apr_socket_listen(socket, 50);
  if (status != APR_SUCCESS) {
    throw SocketException(status);
  }
}


ServerSocket::~ServerSocket()
{
}

void ServerSocket::close() {
    synchronized sync(mutex);
    if (socket != 0) {
        apr_status_t status = apr_socket_close(socket);
        if (status != APR_SUCCESS) {
            throw SocketException(status);
        }
        socket = 0;
    }
}

/** Listens for a connection to be made to this socket and
accepts it
*/
SocketPtr ServerSocket::accept() {
    synchronized sync(mutex);
    if (socket == 0) {
        throw IOException();
    }
    
    apr_pollfd_t poll;
    poll.p = pool.getAPRPool();
    poll.desc_type = APR_POLL_SOCKET;
    poll.reqevents = APR_POLLIN;
    poll.rtnevents = 0;
    poll.desc.s = socket;
    poll.client_data = NULL;
  
    apr_int32_t signaled;
    apr_interval_time_t to = timeout * 1000;
    apr_status_t status = apr_poll(&poll, 1, &signaled, to);  
  
    if (APR_STATUS_IS_TIMEUP(status)) {
        throw SocketTimeoutException();
    } else if (status != APR_SUCCESS) {
        throw SocketException(status);
    }
    
    apr_pool_t* newPool;
    status = apr_pool_create(&newPool, 0);
    if (status != APR_SUCCESS) {
        throw PoolException(status);
    }
    apr_socket_t* newSocket;
    status = apr_socket_accept(&newSocket, socket, newPool);
    if (status != APR_SUCCESS) {
        apr_pool_destroy(newPool);
        throw SocketException(status);
    }
    
    status = apr_socket_opt_set(newSocket, APR_SO_NONBLOCK, 0);
    if (status != APR_SUCCESS) {
        apr_pool_destroy(newPool);
        throw SocketException(status);
    }
    
    // Added 5 seconds timeout to fix endless blocking on write() when the client is not reading
    status = apr_socket_timeout_set(newSocket, 5000000);
    if (status != APR_SUCCESS) {
        apr_pool_destroy(newPool);
        throw SocketException(status);
    }

    return new Socket(newSocket, newPool);
}

/** Retrive setting for SO_TIMEOUT.
*/
int ServerSocket::getSoTimeout() const
{
   return timeout;
}

/** Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
*/
void ServerSocket::setSoTimeout(int newVal)
{
   timeout = newVal;
}