-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlatformEasySocket.h
63 lines (49 loc) · 1.55 KB
/
PlatformEasySocket.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* PlatformEasySocket for Windows/MacOS
*
* Created by lihuanqian on 12/14/2024
*
* Copyright (c) lihuanqian. All rights reserved.
*/
#pragma once
#include "IEasySocket.h"
#include <string>
struct SocketSetupOptions
{
/** socket setup */
int af = 0;
int type = 0;
int protocol = 0;
int send_timeout = 5000; // send timeout (ms)
int recv_timeout = 5000; // send timeout (ms)
int port = 0; // port
std::string ip; // ip
SocketSetupOptions() {}
SocketSetupOptions(const SocketSetupOptions& opts) = default;
bool operator==(const SocketSetupOptions& opts) const = default;
bool operator!=(const SocketSetupOptions& opts) const = default;
};
class PlatformEasySocket : public IEasySocket
{
public:
explicit PlatformEasySocket();
explicit PlatformEasySocket(const SocketSetupOptions& opts);
~PlatformEasySocket();
bool setupSocket(const SocketSetupOptions& opts);
SocketSetupOptions getCurrentSocketOptions();
bool connect() override;
bool close() override;
bool shutdown();
std::optional<int> sendData(const std::vector<uint8_t>& sendData) override;
std::optional<std::vector<uint8_t>> recvData(int recvLen = 1024) override;
std::optional<int> sendMessage(const std::string& message) override;
std::optional<std::string> recvMessage(int recvLen = 1024) override;
std::optional<int> sendRaw(const char* byte, int len) override;
std::optional<int> recvRaw(char* byte, int len) override;
int error() const;
std::string getErrorString() const;
private:
using socket_t = int;
socket_t m_socket;
SocketSetupOptions m_opts;
};