add namespace

This commit is contained in:
ackimixs
2024-03-28 08:40:46 +01:00
parent 8e94cda16f
commit a4a5e56daf
7 changed files with 18 additions and 25 deletions

View File

@@ -3,9 +3,6 @@ project(TCPSocket VERSION 0.1.3 DESCRIPTION "TCP Socket" LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
# Specify C++ Standard
set(CMAKE_CXX_STANDARD 17)
# Add the source files
set(SOURCES
src/TCPClient.cpp

View File

@@ -8,4 +8,4 @@ pkg_check_modules(TCPSocket REQUIRED TCPSocket)
add_executable(client client.test.cpp)
target_link_libraries(client TCPSocket)
target_link_libraries(client TCPSocket)

View File

@@ -1,7 +1,7 @@
#include <TCPSocket/TCPClient.hpp>
int main() {
TCPClient client("127.0.0.1", 8080); // Replace "127.0.0.1" with the IP address of your server and 8080 with the port number
TCPClient client("127.0.0.1", 8082); // Replace "127.0.0.1" with the IP address of your server and 8080 with the port number
client.start();

View File

@@ -12,12 +12,11 @@ class TCPClient {
int clientSocket;
sockaddr_in serverAddress;
bool running;
//void (*callback)(const std::string& message);
public:
TCPClient(const char* serverIP = "127.0.0.1", int port = 8080/*, void (*callback)(const std::string& message) = [](const std::string& message) { std::cout << message << std::endl; }*/);
TCPClient(const char* serverIP = "127.0.0.1", int port = 8080);
void sendMessage(const char* message);
void sendMessage(const char* message) const;
void receiveMessages();
@@ -29,5 +28,4 @@ public:
virtual void handleMessage(const std::string& message);
//void setCallback(void (*callback)(const std::string& message));
};

View File

@@ -2,11 +2,13 @@
#include <vector>
#include <string>
namespace TCPSocket
{
bool startWith(const std::string& str, const std::string& start);
inline bool startWith(const std::string& str, const std::string& start);
bool endsWith(const std::string& str, const std::string& end);
inline bool endsWith(const std::string& str, const std::string& end);
bool contains(const std::string& str, const std::string& sub);
inline bool contains(const std::string& str, const std::string& sub);
inline std::vector<std::string> split(const std::string& str, const std::string& delimiter);
std::vector<std::string> split(const std::string& str, const std::string& delimiter);
}

View File

@@ -1,6 +1,6 @@
#include "TCPSocket/TCPClient.hpp"
TCPClient::TCPClient(const char* serverIP, int port/*, void (*callback)(const std::string& message)*/) : running(true)/*, callback(callback)*/ {
TCPClient::TCPClient(const char* serverIP, int port) {
clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket == -1) {
std::cerr << "Socket creation failed" << std::endl;
@@ -21,7 +21,7 @@ TCPClient::TCPClient(const char* serverIP, int port/*, void (*callback)(const st
}
}
void TCPClient::sendMessage(const char* message) {
void TCPClient::sendMessage(const char* message) const {
send(clientSocket, message, strlen(message), 0);
}
@@ -59,8 +59,4 @@ void TCPClient::start() {
void TCPClient::stop() {
running = false;
close(clientSocket);
}
/*void TCPClient::setCallback(void (*callback)(const std::string& message)) {
this->callback = callback;
}*/
}

View File

@@ -1,11 +1,11 @@
#include "TCPSocket/TCPUtils.hpp"
inline bool startWith(const std::string& str, const std::string& start)
bool TCPSocket::startWith(const std::string& str, const std::string& start)
{
return str.rfind(start, 0) == 0;
}
inline bool endsWith(const std::string& str, const std::string& end)
bool TCPSocket::endsWith(const std::string& str, const std::string& end)
{
if (str.length() >= end.length())
{
@@ -14,12 +14,12 @@ inline bool endsWith(const std::string& str, const std::string& end)
return false;
}
inline bool contains(const std::string& str, const std::string& sub)
bool TCPSocket::contains(const std::string& str, const std::string& sub)
{
return str.find(sub) != std::string::npos;
}
inline std::vector<std::string> split(const std::string& str, const std::string& delimiter)
std::vector<std::string> TCPSocket::split(const std::string& str, const std::string& delimiter)
{
std::vector<std::string> tokens;
size_t prev = 0, pos = 0;