Added NODELAY option to both server and client

This commit is contained in:
ftc
2023-03-14 23:17:02 +03:00
parent 0a83c1a447
commit 9bf8c97ebf
7 changed files with 45 additions and 1 deletions
+3
View File
@@ -17,6 +17,9 @@ task.cpp
mutex.h
mutex.cpp
log.h
log.cpp
SynchronousSocket.cpp
SynchronousSocket.h
)
+8
View File
@@ -7,6 +7,8 @@
#include <unistd.h>
#include <errno.h>
#include "log.h"
SynchronousSocket::SynchronousSocket()
{
socket = -1;
@@ -63,6 +65,7 @@ void SynchronousSocket::Send(const string & data)
{
int pos = 0;
time_t start = time(NULL);
LogTime("Send() start");
while (pos < (int)data.length())
{
int res = send(socket, &((data.data())[pos]), data.length() - pos, 0);
@@ -87,17 +90,21 @@ void SynchronousSocket::Send(const string & data)
pos += res;
}
}
LogTime("Send() finish");
}
string SynchronousSocket::Receive(int amount)
{
string r = "";
time_t start = time(NULL);
LogTime("Receive() start");
while ((int)r.length() < amount)
{
char buf[BUFFER_SIZE];
int to_recv = min((int)sizeof(buf), amount - (int)r.length());
LogTime("Receive(), before recv");
int res = recv(socket, buf, to_recv, 0);
LogTime("Receive(), after recv");
if (res == 0)
{
// бросаем специфический exception, означающий, что сокет закрыт
@@ -125,6 +132,7 @@ string SynchronousSocket::Receive(int amount)
r += string(buf, res);
}
}
LogTime("Receive() finish");
return r;
}
+16
View File
@@ -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;
}
+8
View File
@@ -0,0 +1,8 @@
#ifndef LOG_HEADER_FILE_INCLUDED
#define LOG_HEADER_FILE_INCLUDED
#include <string>
void LogTime(const std::string &message);
#endif
+7
View File
@@ -1,4 +1,5 @@
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <string>
#include <cstring>
@@ -96,6 +97,12 @@ void* IPSocketThread(void* arg)
cerr << "Error accepting connection, errno = " << errno << "\n";
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";
CreateThread(&ProcessRequests, &recsock);
}