mirror of
https://github.com/modelec/TCPSocketServer.git
synced 2026-01-18 16:37:29 +01:00
use c++ file
This commit is contained in:
@@ -5,4 +5,5 @@ set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
add_executable(socketServer main.cpp
|
||||
TCPServer.cpp
|
||||
utils.cpp
|
||||
)
|
||||
|
||||
80
utils.cpp
Normal file
80
utils.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include "utils.h"
|
||||
|
||||
bool TCPUtils::startWith(const std::string& str, const std::string& start)
|
||||
{
|
||||
return str.rfind(start, 0) == 0;
|
||||
}
|
||||
|
||||
bool TCPUtils::endsWith(const std::string& str, const std::string& end)
|
||||
{
|
||||
if (str.length() >= end.length())
|
||||
{
|
||||
return (0 == str.compare(str.length() - end.length(), end.length(), end));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool TCPUtils::contains(const std::string& str, const std::string& sub)
|
||||
{
|
||||
return str.find(sub) != std::string::npos;
|
||||
}
|
||||
|
||||
std::vector<std::string> TCPUtils::split(const std::string& str, const std::string& delimiter)
|
||||
{
|
||||
std::vector<std::string> tokens;
|
||||
size_t prev = 0, pos = 0;
|
||||
do
|
||||
{
|
||||
pos = str.find(delimiter, prev);
|
||||
if (pos == std::string::npos) pos = str.length();
|
||||
std::string token = str.substr(prev, pos - prev);
|
||||
if (!token.empty()) tokens.push_back(token);
|
||||
prev = pos + delimiter.length();
|
||||
} while (pos < str.length() && prev < str.length());
|
||||
return tokens;
|
||||
}
|
||||
|
||||
|
||||
ArucoTag::ArucoTag(int id, std::string name, std::pair<float[2], float[3]> pos) : _id(id), _name(std::move(name)), _pos(pos) {}
|
||||
|
||||
int ArucoTag::id() const {
|
||||
return _id;
|
||||
}
|
||||
|
||||
std::string ArucoTag::name() const {
|
||||
return _name;
|
||||
}
|
||||
|
||||
std::pair<float[2], float[3]> ArucoTag::pos() const {
|
||||
return _pos;
|
||||
}
|
||||
|
||||
void ArucoTag::setId(int id) {
|
||||
_id = id;
|
||||
}
|
||||
|
||||
void ArucoTag::setName(const std::string& name) {
|
||||
_name = name;
|
||||
}
|
||||
|
||||
void ArucoTag::setPos(float x, float y) {
|
||||
_pos.first[0] = x;
|
||||
_pos.first[1] = y;
|
||||
}
|
||||
|
||||
void ArucoTag::setRot(float x, float y, float z) {
|
||||
_pos.second[0] = x;
|
||||
_pos.second[1] = y;
|
||||
_pos.second[2] = z;
|
||||
}
|
||||
|
||||
ArucoTag& ArucoTag::operator=(const ArucoTag& tag) {
|
||||
_id = tag.id();
|
||||
_name = tag.name();
|
||||
_pos.first[0] = tag.pos().first[0];
|
||||
_pos.first[1] = tag.pos().first[1];
|
||||
_pos.second[0] = tag.pos().second[0];
|
||||
_pos.second[1] = tag.pos().second[1];
|
||||
_pos.second[2] = tag.pos().second[2];
|
||||
return *this;
|
||||
}
|
||||
91
utils.h
91
utils.h
@@ -1,57 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
inline bool 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)
|
||||
{
|
||||
if (str.length() >= end.length())
|
||||
{
|
||||
return (0 == str.compare(str.length() - end.length(), end.length(), end));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool 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> tokens;
|
||||
size_t prev = 0, pos = 0;
|
||||
do
|
||||
{
|
||||
pos = str.find(delimiter, prev);
|
||||
if (pos == std::string::npos) pos = str.length();
|
||||
std::string token = str.substr(prev, pos - prev);
|
||||
if (!token.empty()) tokens.push_back(token);
|
||||
prev = pos + delimiter.length();
|
||||
} while (pos < str.length() && prev < str.length());
|
||||
return tokens;
|
||||
}
|
||||
|
||||
struct ClientTCP
|
||||
{
|
||||
std::string name;
|
||||
int socket = -1;
|
||||
bool isReady = false;
|
||||
enum PinceState {
|
||||
WHITE_FLOWER,
|
||||
PURPLE_FLOWER,
|
||||
NONE
|
||||
};
|
||||
|
||||
struct ArucoTag
|
||||
{
|
||||
int id;
|
||||
std::string name;
|
||||
};
|
||||
namespace TCPUtils {
|
||||
bool startWith(const std::string& str, const std::string& start);
|
||||
|
||||
struct ArucoTagPos
|
||||
{
|
||||
ArucoTag tag;
|
||||
std::pair<float[2], float[3]> pos;
|
||||
};
|
||||
bool endsWith(const std::string& str, const std::string& end);
|
||||
|
||||
bool contains(const std::string& str, const std::string& sub);
|
||||
|
||||
std::vector<std::string> split(const std::string& str, const std::string& delimiter);
|
||||
}
|
||||
|
||||
class ArucoTag {
|
||||
|
||||
public:
|
||||
ArucoTag(int id, std::string name, std::pair<float[2], float[3]> pos);
|
||||
|
||||
ArucoTag() = default;
|
||||
|
||||
[[nodiscard]] int id() const;
|
||||
|
||||
[[nodiscard]] std::string name() const;
|
||||
|
||||
[[nodiscard]] std::pair<float[2], float[3]> pos() const;
|
||||
|
||||
void setId(int id);
|
||||
|
||||
void setName(const std::string& name);
|
||||
|
||||
void setPos(float x, float y);
|
||||
|
||||
void setRot(float x, float y, float z);
|
||||
|
||||
ArucoTag& operator=(const ArucoTag& tag);
|
||||
|
||||
private:
|
||||
int _id = 0;
|
||||
std::string _name;
|
||||
std::pair<float[2], float[3]> _pos;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user