Try to add comments and fix some bugs.

This commit is contained in:
Saturneric 2020-11-25 03:14:32 +08:00
parent 31aa47cac6
commit bccaca82fa
2 changed files with 38 additions and 15 deletions

View File

@ -13,13 +13,21 @@
namespace Net { 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 { class TCPServer {
public: 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() { ~TCPServer() {
stop(); stop();
@ -27,8 +35,8 @@ public:
} }
void stop() { void stop() {
if (p_accept_manager_thread != nullptr) if (p_conn_mgr_thrd != nullptr)
p_accept_manager_thread->interrupt(); p_conn_mgr_thrd->interrupt();
this->status = -1; this->status = -1;
} }
@ -40,7 +48,7 @@ private:
struct sockaddr_in server_addr; struct sockaddr_in server_addr;
std::queue<uint8_t> recv_buff; std::queue<uint8_t> recv_buff;
boost::mutex buff_mutex; boost::mutex buff_mutex;
boost::thread *p_accept_manager_thread; boost::thread *p_conn_mgr_thrd;
void cycle(); void cycle();
@ -49,6 +57,12 @@ private:
static void accept(int fd, boost::mutex *buff_mutex, static void accept(int fd, boost::mutex *buff_mutex,
std::queue<uint8_t> *recv_buff, const int *status); std::queue<uint8_t> *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); void create_socket(int port);
}; };

View File

@ -9,7 +9,7 @@ extern int errno;
void Net::TCPServer::cycle() { void Net::TCPServer::cycle() {
boost::thread accept_manager_thread(TCPServer::connection_manager, this); 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(); 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_family = AF_INET;
this->server_addr.sin_addr.s_addr = htonl(INADDR_ANY); this->server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
this->server_addr.sin_port = htons(port); 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); create_socket(port);
if (bind(fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) if (listen(fd, max_connection) < 0) {
throw std::runtime_error(strerror(errno)); // Close the socket created
close(fd);
if (listen(fd, max_connection) < 0)
throw std::runtime_error(strerror(errno)); throw std::runtime_error(strerror(errno));
}
// Start running the engine cycle
cycle(); cycle();
} }