add tcp client; test tcp server and client;

This commit is contained in:
eric 2020-07-09 02:30:27 +08:00
parent dd1e30d4ea
commit 9c6ba70c45
13 changed files with 158 additions and 25 deletions

View File

@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 14)
include_directories(include/)
include_directories(utils/)
find_package(Boost REQUIRED)
find_package(Boost REQUIRED thread)
find_package(OpenSSL 1.1.1 REQUIRED)
include_directories(${Boost_INCLUDE_DIRS} ${OPENSSL_INCLUDE_DIR} ${SQLiteCpp_INCLUDE_DIRS})

View File

@ -0,0 +1,45 @@
//
// Created by 胡宇 on 2020/7/9.
//
#ifndef NET_TCP_CLIENT_H
#define NET_TCP_CLIENT_H
#include <cstring>
#include <string>
#include <stdexcept>
#include <sstream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <boost/thread/thread.hpp>
namespace Net {
class TCPClient {
public:
TCPClient(const std::string &ip, int port);
int sendData(const std::string &data);
private:
int fd{};
struct sockaddr_in client_addr{};
std::stringstream recv_buff;
void create_socket_and_connection();
void recv_cycle();
static void recv_data(TCPClient *client);
};
}
#endif //NET_TCP_CLIENT_H

View File

@ -11,6 +11,7 @@
#include <vector>
#include <sstream>
#include <string>
#include <queue>
#include <sys/types.h>
#include <sys/socket.h>
@ -43,7 +44,7 @@ namespace Net {
int fd;
int status = 0;
struct sockaddr_in server_addr;
std::stringstream recv_buff;
std::queue<uint8_t> recv_buff;
boost::mutex buff_mutex;
boost::thread *p_accept_manager_thread;
@ -51,7 +52,7 @@ namespace Net {
static void accept_manager(TCPServer *server);
static void accept(int fd, boost::mutex *buff_mutex, std::stringstream *recv_buff, const int *status);
static void accept(int fd, boost::mutex *buff_mutex, std::queue<uint8_t> *recv_buff, const int *status);
void create_socket(int port);
};

View File

@ -19,13 +19,11 @@ namespace Net {
using FormalItem = pair<string, string>;
void printError(string error_info);
void debugPrintError(const string &error_info);
void printWarning(string warning_info);
void debugPrintWarning(const string &warning_info);
void printSuccess(string success_info);
void printRed(string red_info);
void debugPrintSuccess(const string &success_info);
void printInfo(const string& info, const string& tag);

View File

@ -1,5 +1,3 @@
file(GLOB commuSrc *.cpp)
add_library(commu STATIC ${commuSrc})
target_link_libraries(commu utils debugTools)
add_library(commu STATIC ${commuSrc})

View File

@ -0,0 +1,41 @@
//
// Created by 胡宇 on 2020/7/9.
//
#include "communicate/tcp_client.h"
void Net::TCPClient::recv_data(Net::TCPClient *client) {
u_char buff[1024];
int len;
while ((len = recv(client->fd, buff, sizeof(buff), 0)) > 0) {
client->recv_buff.write(reinterpret_cast<const char *>(buff), len);
}
}
void Net::TCPClient::recv_cycle() {
boost::thread recv_thread(TCPClient::recv_data,
this);
recv_thread.detach();
}
void Net::TCPClient::create_socket_and_connection() {
if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
throw std::runtime_error("create socket failed.");
}
connect(fd, (struct sockaddr *) &client_addr, sizeof(client_addr));
}
int Net::TCPClient::sendData(const std::string &data) {
create_socket_and_connection();
recv_cycle();
int len = send(fd, data.data(), data.size(), 0);
close(fd);
return len;
}
Net::TCPClient::TCPClient(const std::string &ip, int port) {
std::memset(&client_addr, 0, sizeof(client_addr));
client_addr.sin_family = AF_INET;
client_addr.sin_addr.s_addr = inet_addr(ip.c_str());
client_addr.sin_port = htons(port);
}

View File

@ -2,17 +2,21 @@
// Created by 胡宇 on 2020/7/7.
//
#include "debug_tools/print_tools.h"
#include "communicate/tcp_server.h"
void Net::TCPServer::cycle() {
boost::thread accept_manager_thread(TCPServer::accept_manager, this);
this->p_accept_manager_thread = &accept_manager_thread;
accept_manager_thread.join();
accept_manager_thread.detach();
}
void Net::TCPServer::accept_manager(Net::TCPServer *server) {
Net::PrintTools::debugPrintSuccess("AcceptManager Started.");
while(true){
int connect_fd = ::accept(server->fd, nullptr, nullptr);
Net::PrintTools::debugPrintSuccess("New Connection.");
if(connect_fd == -1) throw std::runtime_error("accept tcp connection error");
else{
boost::thread accept_thread(TCPServer::accept, connect_fd, &server->buff_mutex, &server->recv_buff, &server->status);
@ -21,19 +25,23 @@ void Net::TCPServer::accept_manager(Net::TCPServer *server) {
}
}
void Net::TCPServer::accept(int fd, boost::mutex *buff_mutex, std::stringstream *recv_buff, const int *status) {
void Net::TCPServer::accept(int fd, boost::mutex *buff_mutex, std::queue<uint8_t> *recv_buff, const int *status) {
Net::PrintTools::debugPrintSuccess("Try Getting Data From Connection.");
uint8_t buff[1024];
int len;
std::vector<uint8_t> accept_buff;
while((len = recv(fd, buff, sizeof(buff), 0)) > 0 && *status == 0){
Net::PrintTools::debugPrintSuccess("Received.");
for(int i = 0; i < len; ++i) accept_buff.push_back(buff[i]);
}
if(*status == 0) {
buff_mutex->lock();
recv_buff->write(reinterpret_cast<const char *>(accept_buff.data()), accept_buff.size());
for(unsigned char & i : accept_buff)
recv_buff->push(i);
buff_mutex->unlock();
}
close(fd);
Net::PrintTools::debugPrintSuccess("Connection Closed.");
}
void Net::TCPServer::create_socket(int port) {
@ -61,5 +69,12 @@ Net::TCPServer::TCPServer(int port, int max_connection) {
}
uint8_t Net::TCPServer::readByte() {
return recv_buff.get();
uint8_t byte = '\0';
buff_mutex.try_lock();
if(!recv_buff.empty()) {
byte = recv_buff.front();
recv_buff.pop();
}
buff_mutex.unlock();
return byte;
}

View File

@ -8,22 +8,18 @@ using std::string;
namespace Net {
namespace PrintTools {
void printError(const string &error_info) {
void debugPrintError(const string &error_info) {
printf("\033[31mError: %s\033[0m\n", error_info.data());
}
void printWarning(const string &warning_info) {
void debugPrintWarning(const string &warning_info) {
printf("\033[33mWarning: %s\033[0m\n", warning_info.data());
}
void printSuccess(const string &success_info) {
void debugPrintSuccess(const string &success_info) {
printf("\033[32m%s\033[0m\n", success_info.data());
}
void printRed(const string &red_info) {
printf("\033[31m%s\n\033[0m", red_info.data());
}
void printInfo(const string& info, const string& tag) {
printf("[DEBUG INFO] %s ", info.data());
if(!tag.empty())

View File

@ -1,5 +1,3 @@
file(GLOB utilSrc *.cpp)
add_library(utils STATIC ${utilSrc})
target_link_libraries(utils debugTools ssl crypto)

View File

@ -6,3 +6,4 @@ set(GTEST_LIBS gtest gmock pthread dl)
link_directories(${GTEST_LIB})
add_subdirectory(test_util)
add_subdirectory(test_commu)

View File

@ -0,0 +1,9 @@
file(GLOB testSrc *.cpp)
add_compile_definitions(DEBUG)
add_executable(testCommu ${testSrc})
target_link_libraries(testCommu commu debugTools Boost::thread)

31
test/test_commu/main.cpp Normal file
View File

@ -0,0 +1,31 @@
//
// Created by 胡宇 on 2020/7/9.
//
#include <unistd.h>
#include <cstdio>
#include "debug_tools/print_tools.h"
#include "communicate/tcp_server.h"
#include "communicate/tcp_client.h"
using namespace Net;
int main(int argc, char *argv[]){
if(fork() == 0) {
PrintTools::debugPrintSuccess("Child Started.");
TCPClient client("127.0.0.1", 9048);
for(int i = 0; i < 32; i++, usleep(1e4))
client.sendData("Hello");
PrintTools::debugPrintSuccess("Child Exited.");
}
else{
PrintTools::debugPrintSuccess("Father Started.");
TCPServer server(9048, 100);
while (true){
std::putchar(server.readByte());
usleep(1e3);
}
}
return 0;
}

View File

@ -5,7 +5,7 @@ add_executable(testUtils ${testSrc})
message(${GTEST_LIBS})
target_link_libraries(testUtils utils debugTools ${GTEST_LIBS})
target_link_libraries(testUtils utils debugTools ssl crypto ${GTEST_LIBS})
add_test(Name testUtils
COMMAND testUtils)