Author SHA1 Message Date
ftc 37dc90a65a Disable logging. Version uses TCP_CORK option 2023-03-14 23:30:58 +03:00
6 changed files with 21 additions and 10 deletions
+4 -2
View File
@@ -3,6 +3,7 @@
abstract class Socket abstract class Socket
{ {
const RECEIVE_TIMEOUT = 10; const RECEIVE_TIMEOUT = 10;
const TCP_CORK = 3;
protected $sock; protected $sock;
protected $throw_exception_on_timeout; protected $throw_exception_on_timeout;
@@ -107,8 +108,10 @@ abstract class Socket
{ {
$len = strlen($s); $len = strlen($s);
$lens = chr(($len >> 0) & 0xFF) . chr(($len >> 8) & 0xFF) . chr(($len >> 16) & 0xFF) . chr(($len >> 24) & 0xFF); $lens = chr(($len >> 0) & 0xFF) . chr(($len >> 8) & 0xFF) . chr(($len >> 16) & 0xFF) . chr(($len >> 24) & 0xFF);
socket_set_option($this->sock, SOL_TCP, self::TCP_CORK, 1);
$this->iterativeSend($lens); $this->iterativeSend($lens);
$this->iterativeSend($s); $this->iterativeSend($s);
socket_set_option($this->sock, SOL_TCP, self::TCP_CORK, 0);
} }
/* /*
@@ -141,8 +144,7 @@ class IPSocket extends Socket
if ($this->sock === FALSE) if ($this->sock === FALSE)
{ {
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;
+2 -2
View File
@@ -13,9 +13,9 @@ $log_start = hrtime(true);
function logTime($message) function logTime($message)
{ {
global $log_start; /*global $log_start;
$current = (hrtime(true) - $log_start) / 1000000.0; $current = (hrtime(true) - $log_start) / 1000000.0;
print(sprintf("%.3f", $current)." ".$message."\n"); print(sprintf("%.3f", $current)." ".$message."\n");*/
} }
function testSocket($amount, $reconnect, $size) function testSocket($amount, $reconnect, $size)
+11
View File
@@ -1,5 +1,6 @@
#include "SynchronousSocket.h" #include "SynchronousSocket.h"
#include <netinet/tcp.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netdb.h> #include <netdb.h>
@@ -162,8 +163,18 @@ void SynchronousSocket::SendMessage(const string & m)
{ {
len_s += (char)((len >> (8 * i)) & 0xFF); len_s += (char)((len >> (8 * i)) & 0xFF);
} }
int result = setsockopt(socket, IPPROTO_TCP, TCP_CORK, (char*)&YES, sizeof(int));
if (result < 0)
{
throw (string)("Error setting socket option");
}
Send(len_s); Send(len_s);
Send(m); Send(m);
result = setsockopt(socket, IPPROTO_TCP, TCP_CORK, (char*)&NO, sizeof(int));
if (result < 0)
{
throw (string)("Error setting socket option");
}
} }
+2
View File
@@ -10,6 +10,8 @@ class SynchronousSocket
private: private:
const int TIMEOUT = 10; // in seconds const int TIMEOUT = 10; // in seconds
const int BUFFER_SIZE = 1048576; const int BUFFER_SIZE = 1048576;
const int YES = 1;
const int NO = 0;
protected: protected:
// собственно сокет // собственно сокет
int socket; int socket;
+2
View File
@@ -6,6 +6,7 @@ using namespace std;
void LogTime(const string &message) void LogTime(const string &message)
{ {
#ifdef LOGGING_ENABLED
static auto start = chrono::high_resolution_clock::now(); static auto start = chrono::high_resolution_clock::now();
static auto prev = start; static auto prev = start;
auto current = chrono::high_resolution_clock::now(); auto current = chrono::high_resolution_clock::now();
@@ -13,4 +14,5 @@ void LogTime(const string &message)
auto nsec_prev = chrono::duration_cast<chrono::nanoseconds>(current - prev).count(); auto nsec_prev = chrono::duration_cast<chrono::nanoseconds>(current - prev).count();
cerr << (nsec / 1000000.f) << " " << message << " delta " << (nsec_prev / 1000000.0f) << "\n"; cerr << (nsec / 1000000.f) << " " << message << " delta " << (nsec_prev / 1000000.0f) << "\n";
prev = current; prev = current;
#endif
} }
-6
View File
@@ -97,12 +97,6 @@ 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);
} }