forked from nefarius/sm-ext-socket
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathSocket.h
72 lines (54 loc) · 2.9 KB
/
Socket.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
63
64
65
66
67
68
69
70
71
72
#pragma once
#include <cstdint>
#include <string>
#include <queue>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/thread/shared_mutex.hpp>
#include "sdk/smsdk_ext.h"
#include "Define.h"
class SocketHandler;
template <class SocketType>
class Socket {
public:
Socket(SM_SocketType st, typename SocketType::socket *asioSocket = nullptr);
~Socket();
bool IsOpen();
bool Bind(const char *hostname, uint16_t port, bool async = true);
bool Connect(const char *hostname, uint16_t port, bool async = true);
bool Disconnect();
bool Listen();
bool Send(const std::string &data, bool async = true);
bool SendTo(const std::string &data, const char *hostname, uint16_t port, bool async = true);
bool SetOption(SM_SocketOption so, int value, bool lock = true);
IPluginFunction *connectCallback;
IPluginFunction *incomingCallback;
IPluginFunction *receiveCallback;
IPluginFunction *sendqueueEmptyCallback;
IPluginFunction *disconnectCallback;
IPluginFunction *errorCallback;
int32_t smHandle;
int32_t smCallbackArg;
volatile unsigned int sendQueueLength;
private:
void ReceiveHandler(char *buf, size_t bufferSize, size_t bytes, const boost::system::error_code &, boost::shared_lock<boost::shared_mutex> *);
void BindPostResolveHandler(typename SocketType::resolver *, typename SocketType::resolver::iterator, const boost::system::error_code &, boost::shared_lock<boost::shared_mutex> *);
void ConnectPostResolveHandler(typename SocketType::resolver *, typename SocketType::resolver::iterator, const boost::system::error_code &, boost::shared_lock<boost::shared_mutex> *);
void ConnectPostConnectHandler(typename SocketType::resolver *, typename SocketType::resolver::iterator, const boost::system::error_code &, boost::shared_lock<boost::shared_mutex> *);
void ListenIncomingHandler(boost::asio::ip::tcp::socket *newAsioSocket, const boost::system::error_code &, boost::shared_lock<boost::shared_mutex> *);
void SendPostSendHandler(char *buf, size_t bytes, const boost::system::error_code &err, boost::shared_lock<boost::shared_mutex> *);
void SendToPostResolveHandler(typename SocketType::resolver *, typename SocketType::resolver::iterator, char *buf, size_t bufLen, const boost::system::error_code &, boost::shared_lock<boost::shared_mutex> *);
void SendToPostSendHandler(typename SocketType::resolver *, typename SocketType::resolver::iterator, char *buf, size_t bufLen, size_t bytesTransferred, const boost::system::error_code &, boost::shared_lock<boost::shared_mutex> *);
//void InitializeResolver();
void InitializeSocket();
SM_SocketType sm_sockettype;
std::queue<SocketOption *> socketOptionQueue;
typename SocketType::socket *socket;
boost::mutex socketMutex;
//typename SocketType::resolver* resolver;
typename SocketType::endpoint *localEndpoint;
boost::mutex *localEndpointMutex;
boost::asio::ip::tcp::acceptor *tcpAcceptor;
boost::mutex *tcpAcceptorMutex;
boost::shared_mutex handlerMutex;
};