StringTokenizer 복구
This commit is contained in:
@@ -11,5 +11,4 @@ int main() {
|
||||
|
||||
sock.connect(serveraddr);
|
||||
spdlog::info("Connection established from {}, {}", sock, (std::string)serveraddr);
|
||||
sleep(10);
|
||||
}
|
||||
2
Server/include/ServerController.hpp
Normal file
2
Server/include/ServerController.hpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
|
||||
@@ -16,23 +16,20 @@ int main() {
|
||||
|
||||
struct Chattr::TCPSocket sock;
|
||||
struct Chattr::Address serveraddr;
|
||||
struct Chattr::TCPSocket clientSock;
|
||||
struct Chattr::Address clientAddr;
|
||||
if (config.ipVersion == 4) {
|
||||
sock.init(AF_INET);
|
||||
serveraddr.set(AF_INET, INADDR_ANY, config.listenPort);
|
||||
clientAddr.setType(AF_INET);
|
||||
}
|
||||
else if (config.ipVersion == 6) {
|
||||
sock.init(AF_INET6);
|
||||
serveraddr.set(AF_INET6, in6addr_any, config.listenPort);
|
||||
clientAddr.setType(AF_INET6);
|
||||
}
|
||||
sock.bind(&serveraddr.addr, serveraddr.length);
|
||||
sock.listen(SOMAXCONN);
|
||||
struct Chattr::TCPSocket clientSock;
|
||||
struct Chattr::Address clientAddr;
|
||||
|
||||
if (config.ipVersion == 4)
|
||||
clientAddr.length = sizeof(sockaddr_in);
|
||||
else
|
||||
clientAddr.length = sizeof(sockaddr_in6);
|
||||
|
||||
while (true) {
|
||||
spdlog::info("Waiting for connection...");
|
||||
|
||||
109
impl/Socket/Address.cpp
Normal file
109
impl/Socket/Address.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
#include "Socket/Address.hpp"
|
||||
#include "precomp.hpp"
|
||||
#include <format>
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
Address::Address() {
|
||||
zeroFill();
|
||||
}
|
||||
|
||||
Address::Address(int type, gsl::czstring presentationAddr, std::uint16_t port) {
|
||||
set(type, presentationAddr, port);
|
||||
}
|
||||
|
||||
void Address::zeroFill() {
|
||||
memset(&addr_in6, 0, sizeof(addr_in6));
|
||||
}
|
||||
|
||||
void Address::set(int type, gsl::czstring presentationAddr, std::uint16_t port) {
|
||||
zeroFill();
|
||||
|
||||
if (type == AF_INET) {
|
||||
addr_in.sin_family = AF_INET;
|
||||
::inet_pton(AF_INET, presentationAddr, &addr_in.sin_addr);
|
||||
addr_in.sin_port = htons(port);
|
||||
length = sizeof(sockaddr_in);
|
||||
}
|
||||
else if (type == AF_INET6) {
|
||||
addr_in6.sin6_family = AF_INET6;
|
||||
::inet_pton(AF_INET6, presentationAddr, &addr_in6.sin6_addr);
|
||||
addr_in6.sin6_port = htons(port);
|
||||
length = sizeof(sockaddr_in6);
|
||||
}
|
||||
}
|
||||
|
||||
void Address::set(int type, in_addr_t addr, std::uint16_t port) {
|
||||
zeroFill();
|
||||
|
||||
if (type == AF_INET) {
|
||||
addr_in.sin_family = AF_INET;
|
||||
addr_in.sin_addr.s_addr = htonl(addr);
|
||||
addr_in.sin_port = htons(port);
|
||||
length = sizeof(sockaddr_in);
|
||||
}
|
||||
}
|
||||
|
||||
void Address::set(int type, in_addr addr, std::uint16_t port) {
|
||||
zeroFill();
|
||||
|
||||
if (type == AF_INET) {
|
||||
addr_in.sin_family = AF_INET;
|
||||
addr_in.sin_addr = addr;
|
||||
addr_in.sin_port = htons(port);
|
||||
length = sizeof(sockaddr_in);
|
||||
}
|
||||
}
|
||||
|
||||
void Address::set(int type, in6_addr addr, std::uint16_t port) {
|
||||
zeroFill();
|
||||
|
||||
if (type == AF_INET6) {
|
||||
addr_in6.sin6_family = AF_INET6;
|
||||
addr_in6.sin6_addr = addr;
|
||||
addr_in6.sin6_port = htons(port);
|
||||
length = sizeof(sockaddr_in6);
|
||||
}
|
||||
}
|
||||
|
||||
void Address::setType(int type) {
|
||||
zeroFill();
|
||||
|
||||
if (type == AF_INET)
|
||||
length = sizeof(sockaddr_in);
|
||||
else if (type == AF_INET6)
|
||||
length = sizeof(sockaddr_in6);
|
||||
}
|
||||
|
||||
Address::operator std::string() {
|
||||
std::optional<std::uint16_t> port = getPort();
|
||||
|
||||
if (!port)
|
||||
return std::string();
|
||||
|
||||
if (length == sizeof(addr_in)) {
|
||||
char addrStr[INET_ADDRSTRLEN];
|
||||
::inet_ntop(AF_INET, &addr_in.sin_addr, addrStr, sizeof(addrStr));
|
||||
|
||||
return std::format("{}:{}", addrStr, port.value());
|
||||
}
|
||||
else if (length == sizeof(addr_in6)) {
|
||||
char addrStr[INET6_ADDRSTRLEN];
|
||||
::inet_ntop(AF_INET6, &addr_in6.sin6_addr, addrStr, sizeof(addrStr));
|
||||
|
||||
return std::format("{}:{}", addrStr, port.value());
|
||||
}
|
||||
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::optional<std::uint16_t> Address::getPort() {
|
||||
if (length == sizeof(addr_in))
|
||||
return ntohs(addr_in.sin_port);
|
||||
else if (length == sizeof(addr_in6))
|
||||
return ntohs(addr_in6.sin6_port);
|
||||
else
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
}
|
||||
21
impl/Socket/WSAManager.cpp
Normal file
21
impl/Socket/WSAManager.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "Socket/WSAManager.hpp"
|
||||
#include "Socket/Log.hpp"
|
||||
|
||||
#include "precomp.hpp"
|
||||
|
||||
namespace Chattr {
|
||||
WSAManager::WSAManager() {
|
||||
#ifdef _WIN32
|
||||
WSADATA wsa;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
|
||||
log::critical("WSAStartup()");
|
||||
#endif
|
||||
}
|
||||
|
||||
WSAManager::~WSAManager() {
|
||||
#ifdef _WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
82
impl/Utils/StringTokenizer.cpp
Normal file
82
impl/Utils/StringTokenizer.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
#pragma once
|
||||
#include "Utils/StringTokenizer.hpp"
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
StringTokenizer::StringTokenizer(gsl::czstring string, std::uint32_t size) {
|
||||
set(string, size);
|
||||
}
|
||||
|
||||
void StringTokenizer::set(gsl::czstring string, std::uint32_t size) {
|
||||
int length = 0;
|
||||
bool token = false;
|
||||
bool quote = false, doubleQuote = false;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (string[i] == ' ' && token != true)
|
||||
continue;
|
||||
|
||||
if (token == false)
|
||||
tokens.push(&string[i]);
|
||||
token = true;
|
||||
|
||||
switch (string[i]) {
|
||||
case '\"':
|
||||
doubleQuote = !doubleQuote;
|
||||
length++;
|
||||
break;
|
||||
case '\'':
|
||||
quote = !quote;
|
||||
length++;
|
||||
break;
|
||||
case '\t':
|
||||
case '\n':
|
||||
case ' ':
|
||||
if (quote || doubleQuote) {
|
||||
length++;
|
||||
break;
|
||||
}
|
||||
tokens_length.push(length);
|
||||
length = 0;
|
||||
token = false;
|
||||
break;
|
||||
case '\0':
|
||||
tokens_length.push(length);
|
||||
length = 0;
|
||||
break;
|
||||
default:
|
||||
length++;
|
||||
}
|
||||
}
|
||||
|
||||
if (quote || doubleQuote) {
|
||||
spdlog::error("quote or doublequote is not closed");
|
||||
tokens = std::queue<gsl::czstring>();
|
||||
tokens_length = std::queue<std::uint32_t>();
|
||||
}
|
||||
|
||||
if (length != 0)
|
||||
tokens_length.push(length);
|
||||
}
|
||||
|
||||
std::vector<std::string> StringTokenizer::get() {
|
||||
std::vector<std::string> retValue;
|
||||
|
||||
while (!tokens.empty()) {
|
||||
if (tokens_length.front() != 0)
|
||||
retValue.push_back(std::string(tokens.front(), tokens_length.front()));
|
||||
tokens.pop();
|
||||
tokens_length.pop();
|
||||
}
|
||||
|
||||
while (!tokens_length.empty())
|
||||
tokens_length.pop();
|
||||
|
||||
return retValue;
|
||||
}
|
||||
|
||||
StringTokenizer::operator std::vector<std::string>() {
|
||||
return get();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,100 +1,20 @@
|
||||
#pragma once
|
||||
#include "precomp.hpp"
|
||||
#include <format>
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
struct Address {
|
||||
Address() {
|
||||
zeroFill();
|
||||
}
|
||||
Address();
|
||||
Address(int type, gsl::czstring presentationAddr, std::uint16_t port);
|
||||
|
||||
Address(int type, gsl::czstring presentationAddr, std::uint16_t port) {
|
||||
set(type, presentationAddr, port);
|
||||
}
|
||||
void zeroFill();
|
||||
void set(int type, gsl::czstring presentationAddr, std::uint16_t port);
|
||||
void set(int type, in_addr_t addr, std::uint16_t port);
|
||||
void set(int type, in_addr addr, std::uint16_t port);
|
||||
void set(int type, in6_addr addr, std::uint16_t port);
|
||||
void setType(int type);
|
||||
|
||||
void zeroFill() {
|
||||
memset(&addr_in6, 0, sizeof(addr_in6));
|
||||
}
|
||||
|
||||
void set(int type, gsl::czstring presentationAddr, std::uint16_t port) {
|
||||
zeroFill();
|
||||
|
||||
if (type == AF_INET) {
|
||||
addr_in.sin_family = AF_INET;
|
||||
::inet_pton(AF_INET, presentationAddr, &addr_in.sin_addr);
|
||||
addr_in.sin_port = htons(port);
|
||||
length = sizeof(sockaddr_in);
|
||||
} else if (type == AF_INET6) {
|
||||
addr_in6.sin6_family = AF_INET6;
|
||||
::inet_pton(AF_INET6, presentationAddr, &addr_in6.sin6_addr);
|
||||
addr_in6.sin6_port = htons(port);
|
||||
length = sizeof(sockaddr_in6);
|
||||
}
|
||||
}
|
||||
|
||||
void set(int type, in_addr_t addr, std::uint16_t port) {
|
||||
zeroFill();
|
||||
|
||||
if (type == AF_INET) {
|
||||
addr_in.sin_family = AF_INET;
|
||||
addr_in.sin_addr.s_addr = htonl(addr);
|
||||
addr_in.sin_port = htons(port);
|
||||
length = sizeof(sockaddr_in);
|
||||
}
|
||||
}
|
||||
|
||||
void set(int type, in_addr addr, std::uint16_t port) {
|
||||
zeroFill();
|
||||
|
||||
if (type == AF_INET) {
|
||||
addr_in.sin_family = AF_INET;
|
||||
addr_in.sin_addr = addr;
|
||||
addr_in.sin_port = htons(port);
|
||||
length = sizeof(sockaddr_in);
|
||||
}
|
||||
}
|
||||
|
||||
void set(int type, in6_addr addr, std::uint16_t port) {
|
||||
zeroFill();
|
||||
|
||||
if (type == AF_INET6) {
|
||||
addr_in6.sin6_family = AF_INET6;
|
||||
addr_in6.sin6_addr = addr;
|
||||
addr_in6.sin6_port = htons(port);
|
||||
length = sizeof(sockaddr_in6);
|
||||
}
|
||||
}
|
||||
|
||||
operator std::string() {
|
||||
std::optional<std::uint16_t> port = getPort();
|
||||
|
||||
if (!port)
|
||||
return std::string();
|
||||
|
||||
if (length == sizeof(addr_in)) {
|
||||
char addrStr[INET_ADDRSTRLEN];
|
||||
::inet_ntop(AF_INET, &addr_in.sin_addr, addrStr, sizeof(addrStr));
|
||||
|
||||
return std::format("{}:{}", addrStr, port.value());
|
||||
} else if (length == sizeof(addr_in6)) {
|
||||
char addrStr[INET6_ADDRSTRLEN];
|
||||
::inet_ntop(AF_INET6, &addr_in6.sin6_addr, addrStr, sizeof(addrStr));
|
||||
|
||||
return std::format("{}:{}", addrStr, port.value());
|
||||
}
|
||||
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::optional<std::uint16_t> getPort() {
|
||||
if (length == sizeof(addr_in))
|
||||
return ntohs(addr_in.sin_port);
|
||||
else if (length == sizeof(addr_in6))
|
||||
return ntohs(addr_in6.sin6_port);
|
||||
else
|
||||
return std::nullopt;
|
||||
}
|
||||
operator std::string();
|
||||
std::optional<std::uint16_t> getPort();
|
||||
|
||||
union {
|
||||
struct sockaddr addr;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#pragma once
|
||||
#include "precomp.hpp"
|
||||
|
||||
namespace Chattr::log {
|
||||
void setDefaultLogger(spdlog::level::level_enum logLevel, gsl::czstring logFileName, std::uint32_t logFileSize, std::uint32_t logFileCount);
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
#pragma once
|
||||
#include "precomp.hpp"
|
||||
#include "Address.hpp"
|
||||
#include "Socket/Address.hpp"
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
struct Address;
|
||||
|
||||
class Socket {
|
||||
public:
|
||||
Socket() = default;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
#include "Socket.hpp"
|
||||
#include "precomp.hpp"
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
|
||||
@@ -1,24 +1,10 @@
|
||||
#pragma once
|
||||
#include "precomp.hpp"
|
||||
#include "Socket/Log.hpp"
|
||||
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
struct WSAManager {
|
||||
|
||||
WSAManager() {
|
||||
#ifdef _WIN32
|
||||
WSADATA wsa;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
|
||||
log::critical("WSAStartup()");
|
||||
#endif
|
||||
}
|
||||
|
||||
~WSAManager() {
|
||||
#ifdef _WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
}
|
||||
|
||||
WSAManager();
|
||||
~WSAManager();
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
#pragma once
|
||||
#include "precomp.hpp"
|
||||
#include <queue>
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
Reference in New Issue
Block a user