From b7b25f15bf622b2bafe563eb7d283119a663e930 Mon Sep 17 00:00:00 2001 From: hama Date: Thu, 19 Mar 2015 23:41:56 +0100 Subject: [PATCH] dirtool tcp --- base/ReThread.cpp | 2 +- net/ReTCP.cpp | 46 ++++++++++++++++++++++++++++++---------------- os/ReDirTools.cpp | 10 +++++----- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/base/ReThread.cpp b/base/ReThread.cpp index 95706a9..b7f638a 100644 --- a/base/ReThread.cpp +++ b/base/ReThread.cpp @@ -108,12 +108,12 @@ bool ReThread::prepareToRun(int id, ReLogger* masterLogger, */ void ReThread::runAndFinish() { run(); + m_isStopped = true; #if defined __linux__ pthread_exit(NULL); #elif defined __WIN32__ // Nothing to do #endif - m_isStopped = true; } /** diff --git a/net/ReTCP.cpp b/net/ReTCP.cpp index 6374762..cd938e7 100644 --- a/net/ReTCP.cpp +++ b/net/ReTCP.cpp @@ -12,7 +12,7 @@ enum LOCATION_DIRTOOL { LC_LISTEN_FOR_ALL_1 = LC_TCP + 1, // 50501 LC_LISTEN_FOR_ALL_2, // 50502 LC_LISTEN_FOR_ALL_3, // 50503 - LC_LISTEN_FOR_ALL_4, // 50504 + LC_RECEIVE_5, // 50504 LC_LISTEN_FOR_ALL_5, // 50505 LC_LISTEN_FOR_ALL_6, // 50506 LC_WRITE_1, // 50507 @@ -28,7 +28,9 @@ enum LOCATION_DIRTOOL { LC_CONNECT_3, // 50517 LC_TCP_CONNECTION_1, // 50518 LC_WRITE_2, // 50519 - LC_RECEIVE_5, // 50520 + LC_SERVER_CONNECTION_RUN_1, // 50520 + LC_SERVER_CONNECTION_RUN_2, // 50521 + LC_SERVER_CONNECTION_RUN_3, // 50522 }; #if defined __WIN32__ @@ -290,9 +292,12 @@ void ReTCPConnection::receive(ReByteBuffer& command, ReByteBuffer& data) { LC_RECEIVE_4, i18n("too few bytes read: $1/$2 [$3]")).arg( readBytes).arg(received).arg(m_peerName).end(); } else { - m_loggerOwner->logger()->sayF(LOG_DEBUG | CAT_NETWORK, LC_RECEIVE_5, - i18n("received: $1 bytes in $2 round(s) [$3]: $4")).arg(length).arg(rounds).arg( - m_peerName).arg(ReByteBuffer().appendDump(data.str(), data.length(), 80).str()).end(); + m_loggerOwner->logger()->sayF(LOG_DEBUG | CAT_NETWORK, + LC_RECEIVE_5, + i18n("received: $1 bytes in $2 round(s) [$3]: $4")).arg( + length).arg(rounds).arg(m_peerName).arg( + ReByteBuffer().appendDump(data.str(), data.length(), 80).str()) + .end(); } command.setLength(0); if (readBytes >= 8) { @@ -317,11 +322,11 @@ void ReTCPConnection::send(const char* command, const char* data, int length) { ++m_noSent; m_toSend.setLength(0); int flags = 0x7b; + // add command length: int rest = length + 8; - m_toSend.appendInt(length | (flags << 24), "%08x"); + m_toSend.appendInt(rest | (flags << 24), "%08x"); m_toSend.appendFix(command, -1, 8, 8, NULL); m_toSend.append(data, length); - rest += 8; int sent = 0; const char* buf = m_toSend.str(); int rounds = 0; @@ -338,8 +343,9 @@ void ReTCPConnection::send(const char* command, const char* data, int length) { } if (rest == 0) m_loggerOwner->logger()->sayF(LOG_DEBUG | CAT_NETWORK, LC_WRITE_2, - i18n("sent: $1 bytes in $2 round(s): $3 $4")).arg(length).arg(rounds) - .arg(command).arg(ReByteBuffer().appendDump(data, length, 80).str()).end(); + i18n("sent: $1 bytes in $2 round(s): $3 $4")).arg(length).arg( + rounds).arg(command).arg( + ReByteBuffer().appendDump(data, length, 80).str()).end(); } /** @@ -369,6 +375,9 @@ ReTCPServerConnection::~ReTCPServerConnection() { void ReTCPServerConnection::run() { ReByteBuffer command; ReNetCommandHandler::ProcessingState rc = ReNetCommandHandler::PS_UNDEF; + m_loggerOwner->logger()->sayF(LOG_INFO | CAT_NETWORK, + LC_SERVER_CONNECTION_RUN_1, i18n("new connection to $1")) + .arg(m_name).end(); do { receive(command, m_received); rc = m_server->handler().handleNetCommand(command, m_received, this); @@ -377,10 +386,16 @@ void ReTCPServerConnection::run() { i18n("unknown command: $1 length: $2")).arg(command).arg( m_received.length()).end(); } - } while (rc != ReNetCommandHandler::PS_STOP && ! m_shouldStop); - if (rc != ReNetCommandHandler::PS_STOP) - m_pool->setShouldStop(); + } while (rc != ReNetCommandHandler::PS_STOP && !m_shouldStop); + m_pool->setShouldStop(); close(); + if (rc == ReNetCommandHandler::PS_STOP) + m_loggerOwner->logger()->sayF(LOG_INFO | CAT_NETWORK, + LC_SERVER_CONNECTION_RUN_2, i18n("stop signal received from $1")) + .arg(m_name).end(); + else + m_loggerOwner->logger()->say(LOG_INFO | CAT_NETWORK, + LC_SERVER_CONNECTION_RUN_3, i18n("stop order received")); m_id = -1; } @@ -509,9 +524,7 @@ bool ReTCPServer::listenForAll() { socklen_t lengthAddr = sizeof(struct sockaddr_in); while ((clientSocket = accept(m_handleSocket, (struct sockaddr *) &addrClient, &lengthAddr)) != 0) { - if (! pool.shouldStop()) { - m_logger->sayF(LOG_INFO | CAT_NETWORK, LC_LISTEN_FOR_ALL_4, - i18n("accepted: $1")).arg(m_port).end(); + if (!pool.shouldStop()) { if (m_countConnections >= m_maxConnections) { // close the connection at once: m_logger->sayF(LOG_WARNING | CAT_NETWORK, @@ -523,7 +536,6 @@ bool ReTCPServer::listenForAll() { } else { ReTCPServerConnection* connection = createConnection( nextId++, clientSocket, addrClient); - if (!pool.startThread(connection)) { m_logger->sayF(LOG_ERROR | CAT_PROCESS, LC_LISTEN_FOR_ALL_6, @@ -531,6 +543,8 @@ bool ReTCPServer::listenForAll() { getLastOSError()).end(); reCloseSocket(clientSocket); clientSocket = -1; + } else { + } } } diff --git a/os/ReDirTools.cpp b/os/ReDirTools.cpp index 553c9b4..b054fae 100644 --- a/os/ReDirTools.cpp +++ b/os/ReDirTools.cpp @@ -144,8 +144,8 @@ const char* s_tcpUsage[] = { " : 'upload', 'download' or 'mixed'", NULL }; const char* s_tcpExamples[] = { "dirtool tcp -p 5555 server", - "dirtool tcp -p 5555 client localhost 10000 10", - "dirtool tcp -p 5555 --buffer-size=1024 client 192.168.7.3 10 25", + "dirtool tcp -p 5555 client localhost download 10000 10", + "dirtool tcp -p 5555 --buffer-size=1024 client 192.168.7.3 upload 10 25", NULL }; const char* s_touchUsage[] = @@ -2405,7 +2405,6 @@ void ReDirTCP::doIt() { tolower(direction.at(0)) == 'u'); } else help("unknown subcommand: $1", command.str()); - } void ReDirTCP::runMixedClient(const char* ip, int port, int rounds, int interval, int bufferSize) { @@ -2419,9 +2418,9 @@ void ReDirTCP::runOneThreadClient(const char* ip, int port, int rounds, const char* command = upload ? "strlen" : "filldata"; ReByteBuffer message; if (upload) - message.appendInt(bufferSize); - else message.appendChar('x', bufferSize); + else + message.appendInt(bufferSize); time_t lastPrint = start; int64_t size = 0; int duration = 0; @@ -2634,6 +2633,7 @@ int ReDirTools::main(int argc, char* orgArgv[]) { testAll(); } else tools.usage("unknown command: ", argv[1]); + ReTCPServer::globalClose(); return 0; } -- 2.39.5