Skip to content

Commit ca905a5

Browse files
committed
misc updates
1 parent 9456f75 commit ca905a5

File tree

16 files changed

+93
-62
lines changed

16 files changed

+93
-62
lines changed

include/simple_socket/SimpleConnection.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
#define SIMPLE_SOCKET_SIMPLE_CONNECTION_HPP
44

55
#include <cstddef>
6+
#include <cstdint>
67

78
namespace simple_socket {
89

910
class SimpleConnection {
1011
public:
11-
virtual int read(unsigned char* buffer, size_t size) = 0;
12-
virtual bool readExact(unsigned char* buffer, size_t size) = 0;
13-
virtual bool write(const unsigned char* data, size_t size) = 0;
12+
virtual int read(uint8_t* buffer, size_t size) = 0;
13+
virtual bool readExact(uint8_t* buffer, size_t size) = 0;
14+
virtual bool write(const uint8_t* data, size_t size) = 0;
1415

1516
template<class Container>
1617
int read(Container& buffer) {
@@ -28,12 +29,12 @@ namespace simple_socket {
2829
}
2930

3031
bool write(const char* data, size_t size) {
31-
return write(reinterpret_cast<const unsigned char*>(data), size);
32+
return write(reinterpret_cast<const uint8_t*>(data), size);
3233
}
3334

3435
template<size_t N>
3536
bool write(const char (&data)[N]) {
36-
return write(reinterpret_cast<const unsigned char*>(data), N - 1);// N - 1 to exclude null terminator if it's a C-string
37+
return write(reinterpret_cast<const uint8_t*>(data), N - 1);// N - 1 to exclude null terminator if it's a C-string
3738
}
3839

3940
virtual void close() = 0;

include/simple_socket/TCPSocket.hpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "simple_socket/SimpleConnection.hpp"
66

7+
#include <cstdint>
78
#include <memory>
89
#include <string>
910

@@ -13,7 +14,12 @@ namespace simple_socket {
1314
public:
1415
TCPClientContext();
1516

16-
std::unique_ptr<SimpleConnection> connect(const std::string& ip, int port);
17+
TCPClientContext(const TCPClientContext& other) = delete;
18+
TCPClientContext& operator=(const TCPClientContext& other) = delete;
19+
TCPClientContext(TCPClientContext&& other) = delete;
20+
TCPClientContext& operator=(TCPClientContext&& other) = delete;
21+
22+
std::unique_ptr<SimpleConnection> connect(const std::string& ip, uint16_t port);
1723

1824
~TCPClientContext();
1925

@@ -24,7 +30,12 @@ namespace simple_socket {
2430

2531
class TCPServer {
2632
public:
27-
explicit TCPServer(int port, int backlog = 1);
33+
explicit TCPServer(uint16_t port, int backlog = 1);
34+
35+
TCPServer(const TCPServer&) = delete;
36+
TCPServer& operator=(const TCPServer&) = delete;
37+
TCPServer(TCPServer&&) = delete;
38+
TCPServer& operator=(TCPServer&&) = delete;
2839

2940
std::unique_ptr<SimpleConnection> accept();
3041

include/simple_socket/UDPSocket.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ namespace simple_socket {
2020

2121
bool sendTo(const std::string& address, uint16_t remotePort, const std::string& data);
2222

23-
bool sendTo(const std::string& address, uint16_t remotePort, const std::vector<unsigned char>& data);
23+
bool sendTo(const std::string& address, uint16_t remotePort, const std::vector<uint8_t>& data);
2424

25-
bool sendTo(const std::string& address, uint16_t remotePort, const unsigned char* data, size_t size);
25+
bool sendTo(const std::string& address, uint16_t remotePort, const uint8_t* data, size_t size);
2626

27-
int recvFrom(const std::string& address, uint16_t remotePort, std::vector<unsigned char>& buffer);
27+
int recvFrom(const std::string& address, uint16_t remotePort, std::vector<uint8_t>& buffer);
2828

29-
int recvFrom(const std::string& address, uint16_t remotePort, unsigned char* buffer, size_t size);
29+
int recvFrom(const std::string& address, uint16_t remotePort, uint8_t* buffer, size_t size);
3030

3131
[[nodiscard]] std::string recvFrom(const std::string& address, uint16_t remotePort);
3232

include/simple_socket/UnixDomainSocket.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace simple_socket {
1313
public:
1414
UnixDomainClientContext();
1515

16-
std::unique_ptr<SimpleConnection> connect(const std::string& domain);
16+
[[nodiscard]] std::unique_ptr<SimpleConnection> connect(const std::string& domain);
1717

1818
~UnixDomainClientContext();
1919

@@ -26,7 +26,7 @@ namespace simple_socket {
2626
public:
2727
explicit UnixDomainServer(const std::string& domain, int backlog = 1);
2828

29-
std::unique_ptr<SimpleConnection> accept();
29+
[[nodiscard]] std::unique_ptr<SimpleConnection> accept();
3030

3131
void close();
3232

include/simple_socket/WebSocket.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ namespace simple_socket {
3232

3333
explicit WebSocket(uint16_t port);
3434

35-
void stop() const;
35+
void start();
36+
37+
void stop();
3638

3739
~WebSocket();
3840

include/simple_socket/util/port_query.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
#ifndef SIMPLE_SOCKET_PORT_QUERY_HPP
33
#define SIMPLE_SOCKET_PORT_QUERY_HPP
44

5+
#include <cstdint>
6+
#include <optional>
57
#include <vector>
68

79
namespace simple_socket {
810

9-
int getAvailablePort(int startPort, int endPort, const std::vector<int>& excludePorts = {});
11+
std::optional<uint16_t> getAvailablePort(uint16_t startPort, uint16_t endPort, const std::vector<uint16_t>& excludePorts = {});
1012

1113
}
1214

src/simple_socket/TCPSocket.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ struct TCPServer::Impl {
7777
Socket socket;
7878
};
7979

80-
TCPServer::TCPServer(int port, int backlog)
80+
TCPServer::TCPServer(uint16_t port, int backlog)
8181
: pimpl_(std::make_unique<Impl>(port, backlog)) {}
8282

83-
std::unique_ptr<SimpleConnection> TCPServer::accept() {
83+
[[nodiscard]] std::unique_ptr<SimpleConnection> TCPServer::accept() {
8484

8585
return pimpl_->accept();
8686
}
@@ -92,10 +92,11 @@ void TCPServer::close() {
9292

9393
TCPServer::~TCPServer() = default;
9494

95+
9596
TCPClientContext::TCPClientContext()
9697
: pimpl_(std::make_unique<Impl>()) {}
9798

98-
std::unique_ptr<SimpleConnection> TCPClientContext::connect(const std::string& ip, int port) {
99+
[[nodiscard]] std::unique_ptr<SimpleConnection> TCPClientContext::connect(const std::string& ip, uint16_t port) {
99100

100101
SOCKET sock = createSocket();
101102

src/simple_socket/UDPSocket.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ void UDPSocket::close() {
168168
}
169169

170170
std::unique_ptr<SimpleConnection> UDPSocket::makeConnection(const std::string& address, uint16_t remotePort) {
171+
171172
struct UDPConnection: SimpleConnection {
172173

173174
UDPSocket* socket;

src/simple_socket/modbus/ModbusClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace {
1818
request_[2] = 0x00; // Protocol ID (always 0 for Modbus TCP)
1919
request_[3] = 0x00;
2020

21-
const uint8_t length = data.size() + 2;
21+
const auto length = data.size() + 2;
2222
request_[4] = length >> 8;// Length field (2 bytes: unitID + functionCode + data)
2323
request_[5] = length & 0xFF;
2424
request_[6] = unitID;// Unit ID

src/simple_socket/util/port_query.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
#include "simple_socket/socket_common.hpp"
55

6+
#include <optional>
67
#include <algorithm>
78

89
using namespace simple_socket;
910

10-
int simple_socket::getAvailablePort(int startPort, int endPort, const std::vector<int>& excludePorts) {
11+
std::optional<uint16_t> simple_socket::getAvailablePort(uint16_t startPort, uint16_t endPort, const std::vector<uint16_t>& excludePorts) {
1112

1213
#ifdef _WIN32
1314
WSASession session;
@@ -27,7 +28,7 @@ int simple_socket::getAvailablePort(int startPort, int endPort, const std::vecto
2728

2829
for (int port = startPort; port <= endPort; ++port) {
2930

30-
if (std::find(excludePorts.begin(), excludePorts.end(), port) != excludePorts.end()) {
31+
if (std::ranges::find(excludePorts, port) != excludePorts.end()) {
3132
continue;
3233
}
3334

0 commit comments

Comments
 (0)