From bccaca82fa4040d404423ef51b58da0211473094 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Wed, 25 Nov 2020 03:14:32 +0800 Subject: [PATCH] Try to add comments and fix some bugs. --- include/communicate/tcp_server.h | 30 ++++++++++++++++++++++-------- src/communicate/tcp_server.cpp | 23 ++++++++++++++++------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/include/communicate/tcp_server.h b/include/communicate/tcp_server.h index 83234ca..9c8e3df 100644 --- a/include/communicate/tcp_server.h +++ b/include/communicate/tcp_server.h @@ -13,13 +13,21 @@ namespace Net { -/** - * TCP Server using - * -*/ +// +// Provide stable, reliable, easy-to-use, many-to-one TCP communication. +// Requirement: +// 1. It must be bound to an available port before use. +// Features: +// 1. Support communication concurrency based on multithreading. +// class TCPServer { public: - TCPServer(int port, int max_connection); + + // + // Please provide an avaliable port(1~65535) first + // The default maximum connection limit is 1024 + // + TCPServer(uint16_t port, uint32_t max_connection); ~TCPServer() { stop(); @@ -27,8 +35,8 @@ public: } void stop() { - if (p_accept_manager_thread != nullptr) - p_accept_manager_thread->interrupt(); + if (p_conn_mgr_thrd != nullptr) + p_conn_mgr_thrd->interrupt(); this->status = -1; } @@ -40,7 +48,7 @@ private: struct sockaddr_in server_addr; std::queue recv_buff; boost::mutex buff_mutex; - boost::thread *p_accept_manager_thread; + boost::thread *p_conn_mgr_thrd; void cycle(); @@ -49,6 +57,12 @@ private: static void accept(int fd, boost::mutex *buff_mutex, std::queue *recv_buff, const int *status); + // + // Using the standard UNIX way to create a new socket + // Requirement: + // the given port need to be free for the binding + // Return: + // a new and initialized socket which can be given to listen() void create_socket(int port); }; diff --git a/src/communicate/tcp_server.cpp b/src/communicate/tcp_server.cpp index 5e90441..7e0fc9f 100644 --- a/src/communicate/tcp_server.cpp +++ b/src/communicate/tcp_server.cpp @@ -9,7 +9,7 @@ extern int errno; void Net::TCPServer::cycle() { boost::thread accept_manager_thread(TCPServer::connection_manager, this); - this->p_accept_manager_thread = &accept_manager_thread; + this->p_conn_mgr_thrd = &accept_manager_thread; accept_manager_thread.detach(); } @@ -59,19 +59,28 @@ void Net::TCPServer::create_socket(int port) { this->server_addr.sin_family = AF_INET; this->server_addr.sin_addr.s_addr = htonl(INADDR_ANY); this->server_addr.sin_port = htons(port); + + // Try binding the socket to the given port + if (bind(fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0){ + // Close the socket created + close(fd); + throw std::runtime_error(strerror(errno)); + } + } -Net::TCPServer::TCPServer(int port, int max_connection) { +Net::TCPServer::TCPServer(uint16_t port, uint32_t max_connection=1024) { - // 创建 socket + // Create a new socket binding certain port create_socket(port); - if (bind(fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) - throw std::runtime_error(strerror(errno)); - - if (listen(fd, max_connection) < 0) + if (listen(fd, max_connection) < 0) { + // Close the socket created + close(fd); throw std::runtime_error(strerror(errno)); + } + // Start running the engine cycle cycle(); }