Added NODELAY option to both server and client
This commit is contained in:
@@ -142,6 +142,7 @@ class IPSocket extends Socket
|
|||||||
{
|
{
|
||||||
throw new Exception("Error creating socket: ".socket_last_error()." ".socket_strerror(socket_last_error()));
|
throw new Exception("Error creating socket: ".socket_last_error()." ".socket_strerror(socket_last_error()));
|
||||||
}
|
}
|
||||||
|
//socket_set_option($this->sock, SOL_TCP, TCP_NODELAY, 1);
|
||||||
$this->connected = false;
|
$this->connected = false;
|
||||||
$this->host = $ip;
|
$this->host = $ip;
|
||||||
$this->port = $port;
|
$this->port = $port;
|
||||||
|
|||||||
@@ -36,9 +36,10 @@ function testSocket($amount, $reconnect, $size)
|
|||||||
$m = makeRequest($size);
|
$m = makeRequest($size);
|
||||||
logTime("begin sending");
|
logTime("begin sending");
|
||||||
$s->sendMessage($m);
|
$s->sendMessage($m);
|
||||||
|
$ts = hrtime(true);
|
||||||
logTime("sent, receiving");
|
logTime("sent, receiving");
|
||||||
$r = $s->receiveMessage();
|
$r = $s->receiveMessage();
|
||||||
logTime("received");
|
logTime("received, time = ".sprintf("%.3f", (hrtime(true) - $ts) / 1000000.0));
|
||||||
$current++;
|
$current++;
|
||||||
if ($current >= $reconnect)
|
if ($current >= $reconnect)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ task.cpp
|
|||||||
mutex.h
|
mutex.h
|
||||||
mutex.cpp
|
mutex.cpp
|
||||||
|
|
||||||
|
log.h
|
||||||
|
log.cpp
|
||||||
|
|
||||||
SynchronousSocket.cpp
|
SynchronousSocket.cpp
|
||||||
SynchronousSocket.h
|
SynchronousSocket.h
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -7,6 +7,8 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
SynchronousSocket::SynchronousSocket()
|
SynchronousSocket::SynchronousSocket()
|
||||||
{
|
{
|
||||||
socket = -1;
|
socket = -1;
|
||||||
@@ -63,6 +65,7 @@ void SynchronousSocket::Send(const string & data)
|
|||||||
{
|
{
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
time_t start = time(NULL);
|
time_t start = time(NULL);
|
||||||
|
LogTime("Send() start");
|
||||||
while (pos < (int)data.length())
|
while (pos < (int)data.length())
|
||||||
{
|
{
|
||||||
int res = send(socket, &((data.data())[pos]), data.length() - pos, 0);
|
int res = send(socket, &((data.data())[pos]), data.length() - pos, 0);
|
||||||
@@ -87,17 +90,21 @@ void SynchronousSocket::Send(const string & data)
|
|||||||
pos += res;
|
pos += res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
LogTime("Send() finish");
|
||||||
}
|
}
|
||||||
|
|
||||||
string SynchronousSocket::Receive(int amount)
|
string SynchronousSocket::Receive(int amount)
|
||||||
{
|
{
|
||||||
string r = "";
|
string r = "";
|
||||||
time_t start = time(NULL);
|
time_t start = time(NULL);
|
||||||
|
LogTime("Receive() start");
|
||||||
while ((int)r.length() < amount)
|
while ((int)r.length() < amount)
|
||||||
{
|
{
|
||||||
char buf[BUFFER_SIZE];
|
char buf[BUFFER_SIZE];
|
||||||
int to_recv = min((int)sizeof(buf), amount - (int)r.length());
|
int to_recv = min((int)sizeof(buf), amount - (int)r.length());
|
||||||
|
LogTime("Receive(), before recv");
|
||||||
int res = recv(socket, buf, to_recv, 0);
|
int res = recv(socket, buf, to_recv, 0);
|
||||||
|
LogTime("Receive(), after recv");
|
||||||
if (res == 0)
|
if (res == 0)
|
||||||
{
|
{
|
||||||
// бросаем специфический exception, означающий, что сокет закрыт
|
// бросаем специфический exception, означающий, что сокет закрыт
|
||||||
@@ -125,6 +132,7 @@ string SynchronousSocket::Receive(int amount)
|
|||||||
r += string(buf, res);
|
r += string(buf, res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
LogTime("Receive() finish");
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#include "log.h"
|
||||||
|
#include <chrono>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void LogTime(const string &message)
|
||||||
|
{
|
||||||
|
static auto start = chrono::high_resolution_clock::now();
|
||||||
|
static auto prev = start;
|
||||||
|
auto current = chrono::high_resolution_clock::now();
|
||||||
|
auto nsec = chrono::duration_cast<chrono::nanoseconds>(current - start).count();
|
||||||
|
auto nsec_prev = chrono::duration_cast<chrono::nanoseconds>(current - prev).count();
|
||||||
|
cerr << (nsec / 1000000.f) << " " << message << " delta " << (nsec_prev / 1000000.0f) << "\n";
|
||||||
|
prev = current;
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef LOG_HEADER_FILE_INCLUDED
|
||||||
|
#define LOG_HEADER_FILE_INCLUDED
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
void LogTime(const std::string &message);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <netinet/tcp.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@@ -96,6 +97,12 @@ void* IPSocketThread(void* arg)
|
|||||||
cerr << "Error accepting connection, errno = " << errno << "\n";
|
cerr << "Error accepting connection, errno = " << errno << "\n";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
int yes = 1;
|
||||||
|
int result = setsockopt(recsock, IPPROTO_TCP, TCP_NODELAY, (char*)&yes, sizeof(int));
|
||||||
|
if (result < 0)
|
||||||
|
{
|
||||||
|
cerr << "Error setting socket option, errno = " << errno << "\n";
|
||||||
|
}
|
||||||
cout << "Accepted connection from " << inet_ntoa(peeraddr.sin_addr) << ":" << ntohs(peeraddr.sin_port) << "\n";
|
cout << "Accepted connection from " << inet_ntoa(peeraddr.sin_addr) << ":" << ntohs(peeraddr.sin_port) << "\n";
|
||||||
CreateThread(&ProcessRequests, &recsock);
|
CreateThread(&ProcessRequests, &recsock);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user