Skip to content

Commit ed0682a

Browse files
committed
Fixed delay in keep-alive connections
1 parent 88dea46 commit ed0682a

8 files changed

Lines changed: 113 additions & 16 deletions

File tree

apps.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
server {
22
listen 2280;
3-
server_name servertest www.servertest;
4-
server_module /media/projects/sites/servertest/module/appgameserver_debug.so;
3+
server_name servertest www.servertest 192.168.1.35;
4+
server_module /media/projects/appgameserver/build/Release/libappgameserver.so;
55
server_module_update /media/projects/httpserverapp/httpserverapp/bin/Release/libhttpserverapp.so;
66
root_dir /media/projects/sites/servertest/www/;
77
request_max_size 10485760;

httpserver.pro

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ CONFIG(debug, debug|release):DEFINES += DEBUG
99

1010
QMAKE_CXXFLAGS += -std=c++11
1111

12-
QMAKE_CFLAGS_RELEASE -= -O
13-
QMAKE_CFLAGS_RELEASE -= -O1
14-
QMAKE_CFLAGS_RELEASE -= -O2
15-
QMAKE_CFLAGS_RELEASE *= -O3
12+
CONFIG(release, debug|release)
13+
{
14+
QMAKE_CFLAGS_RELEASE -= -O
15+
QMAKE_CFLAGS_RELEASE -= -O1
16+
QMAKE_CFLAGS_RELEASE -= -O2
17+
QMAKE_CFLAGS_RELEASE *= -O3
18+
}
1619

1720
LIBS += -ldl -pthread
1821

httpserver.pro.user

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 3.2.2, 2015-03-27T18:07:52. -->
3+
<!-- Written by QtCreator 3.2.1, 2015-06-19T14:11:06. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>
@@ -61,7 +61,7 @@
6161
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
6262
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
6363
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{c77ee4a2-1c2a-4bac-9185-8378ec4ebf5d}</value>
64-
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
64+
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
6565
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
6666
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
6767
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">

httpserver/Event.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,37 @@ namespace HttpServer
2222
}
2323
}
2424

25-
void Event::notify()
25+
bool Event::wait_for(const std::chrono::milliseconds &ms)
2626
{
2727
std::unique_lock<std::mutex> lck(mtx);
28+
29+
auto status = cv.wait_for(lck, ms);
30+
31+
if (false == manualy)
32+
{
33+
signaled = false;
34+
}
35+
36+
return std::cv_status::timeout == status;
37+
}
38+
39+
bool Event::wait_until(const std::chrono::high_resolution_clock::time_point &tp)
40+
{
41+
std::unique_lock<std::mutex> lck(mtx);
42+
43+
auto status = cv.wait_until(lck, tp);
44+
45+
if (false == manualy)
46+
{
47+
signaled = false;
48+
}
49+
50+
return std::cv_status::timeout == status;
51+
}
52+
53+
void Event::notify()
54+
{
55+
// std::unique_lock<std::mutex> lck(mtx);
2856
signaled = true;
2957
cv.notify_all();
3058
}
@@ -33,7 +61,7 @@ namespace HttpServer
3361
{
3462
if (threadsCount)
3563
{
36-
std::unique_lock<std::mutex> lck(mtx);
64+
// std::unique_lock<std::mutex> lck(mtx);
3765

3866
signaled = true;
3967

@@ -46,14 +74,14 @@ namespace HttpServer
4674

4775
void Event::reset()
4876
{
49-
std::unique_lock<std::mutex> lck(mtx);
77+
// std::unique_lock<std::mutex> lck(mtx);
5078

5179
signaled = false;
5280
}
5381

5482
bool Event::notifed()
5583
{
56-
std::unique_lock<std::mutex> lck(mtx);
84+
// std::unique_lock<std::mutex> lck(mtx);
5785

5886
return signaled;
5987
}

httpserver/Event.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <mutex>
44
#include <condition_variable>
5+
#include <atomic>
56

67
namespace HttpServer
78
{
@@ -10,7 +11,7 @@ namespace HttpServer
1011
private:
1112
std::mutex mtx;
1213
std::condition_variable cv;
13-
bool signaled;
14+
std::atomic<bool> signaled;
1415
bool manualy;
1516

1617
public:
@@ -19,8 +20,12 @@ namespace HttpServer
1920

2021
public:
2122
void wait();
23+
bool wait_for(const std::chrono::milliseconds &);
24+
bool wait_until(const std::chrono::high_resolution_clock::time_point &);
25+
2226
void notify();
2327
void notify(const size_t);
28+
2429
void reset();
2530

2631
bool notifed();

httpserver/Server.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ namespace HttpServer
340340
if (false == headersOnly && file_size)
341341
{
342342
std::vector<std::string::value_type> buf(file_size < 512 * 1024 ? file_size : 512 * 1024);
343+
// buf.assign(headers.cbegin(), headers.cend() );
344+
345+
// file.read(reinterpret_cast<char *>(buf.data() + headers.length() ), buf.size() - headers.length() );
346+
// size_t send_size = clientSocket.nonblock_send(buf, file.gcount(), timeout);
343347

344348
size_t send_size;
345349

@@ -452,7 +456,7 @@ namespace HttpServer
452456
std::string uri_reference;
453457

454458
// Подготовить параметры для получения данных
455-
std::chrono::milliseconds timeout(5000);
459+
std::chrono::milliseconds timeout(500);
456460

457461
// Получить данные запроса от клиента
458462
const size_t recv_size = clientSocket.nonblock_recv(buf, timeout);
@@ -863,6 +867,9 @@ namespace HttpServer
863867

864868
if (false == connection_upgrade)
865869
{
870+
// TODO: wait for send all data to client
871+
clientSocket.nonblock_send_sync();
872+
866873
clientSocket.shutdown();
867874
clientSocket.close();
868875
}
@@ -1439,6 +1446,7 @@ namespace HttpServer
14391446
if (sock.is_open() )
14401447
{
14411448
sock.nonblock(true);
1449+
sock.tcp_nodelay(true);
14421450
sockets.emplace(std::move(sock) );
14431451
}
14441452
}

httpserver/Socket.cpp

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@ namespace HttpServer
210210
#endif
211211
}*/
212212

213+
bool Socket::tcp_nodelay(const bool nodelay) const
214+
{
215+
#ifdef WIN32
216+
int flags = nodelay ? 1 : 0;
217+
return 0 == setsockopt(socket_handle, IPPROTO_TCP, TCP_NODELAY, (char *)&flags, sizeof(flags) );
218+
#elif POSIX
219+
int flags = nodelay ? 1 : 0;
220+
return 0 == setsockopt(socket_handle, IPPROTO_TCP, TCP_NODELAY, (char *)&flags, sizeof(flags) );
221+
#else
222+
#error "Undefine platform"
223+
#endif
224+
}
225+
213226
size_t Socket::recv(std::vector<std::string::value_type> &buf) const
214227
{
215228
#ifdef WIN32
@@ -328,17 +341,50 @@ namespace HttpServer
328341

329342
if (1 == ::poll(&event, 1, timeWait.count() ) && event.revents & POLLOUT)
330343
{
331-
send_len = ::send(socket_handle, buf.data(), length, MSG_WAITALL | MSG_NOSIGNAL);
344+
send_len = ::send(socket_handle, buf.data(), length, MSG_NOSIGNAL);
332345
}
333346
#else
334347
#error "Undefine platform"
335348
#endif
336349
return send_len;
337350
}
338351

352+
void Socket::nonblock_send_sync() const
353+
{
354+
#ifdef WIN32
355+
WSAPOLLFD event = {
356+
socket_handle,
357+
POLLWRNORM,
358+
0
359+
};
360+
361+
::WSAPoll(&event, 1, ~0);
362+
#elif POSIX
363+
struct ::pollfd event = {
364+
socket_handle,
365+
POLLOUT,
366+
0
367+
};
368+
369+
::poll(&event, 1, ~0);
370+
#else
371+
#error "Undefine platform"
372+
#endif
373+
}
374+
339375
Socket &Socket::operator=(const Socket s)
340376
{
341377
socket_handle = s.socket_handle;
342378
return *this;
343379
}
344-
};
380+
381+
bool Socket::operator ==(const Socket &sock) const
382+
{
383+
return this->socket_handle == sock.socket_handle;
384+
}
385+
386+
bool Socket::operator !=(const Socket &sock) const
387+
{
388+
return this->socket_handle != sock.socket_handle;
389+
}
390+
};

httpserver/Socket.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <sys/epoll.h>
1111
#include <poll.h>
1212
#include <netinet/in.h>
13+
#include <netinet/tcp.h>
1314
#include <unistd.h>
1415
#include <fcntl.h>
1516
#else
@@ -69,6 +70,7 @@ namespace HttpServer
6970

7071
bool nonblock(const bool = true) const;
7172
// bool is_nonblock() const;
73+
bool tcp_nodelay(const bool = true) const;
7274

7375
size_t recv(std::vector<std::string::value_type> &) const;
7476
size_t nonblock_recv(std::vector<std::string::value_type> &, const std::chrono::milliseconds &) const;
@@ -79,11 +81,16 @@ namespace HttpServer
7981
size_t nonblock_send(const std::string &, const std::chrono::milliseconds &) const;
8082
size_t nonblock_send(const std::vector<std::string::value_type> &, const size_t, const std::chrono::milliseconds &) const;
8183

84+
void nonblock_send_sync() const;
85+
8286
inline System::native_socket_type get_handle() const
8387
{
8488
return socket_handle;
8589
}
8690

8791
Socket &operator =(const Socket);
92+
93+
bool operator ==(const Socket &sock) const;
94+
bool operator !=(const Socket &sock) const;
8895
};
8996
};

0 commit comments

Comments
 (0)