SpiecsEngine
 
Loading...
Searching...
No Matches
Socket.h
Go to the documentation of this file.
1/**
2* @file Socket.h.
3* @brief The Socket Class Definitions.
4* @author Spices & Muduo.
5*/
6
7#pragma once
8#include "InetAddress.h"
9
10namespace Spices {
11
12namespace Net {
13
14 /**
15 * @brief This class is Wrapper of socket.
16 */
17 class Socket
18 {
19 public:
20
21 /**
22 * @brief Constructor Function.
23 */
24 Socket() = default;
25
26 /**
27 * @brief Constructor Function.
28 * @param[in] socketFd Socket fd.
29 */
30 explicit Socket(SOCKET socketFd)
32 {}
33
34 /**
35 * @brief Destructor Function.
36 */
37 virtual ~Socket();
38
39 /**
40 * @brief Copy Constructor Function.
41 * @note This Class not allowed copy behaves.
42 */
43 Socket(const Socket&) = delete;
44
45 /**
46 * @brief Copy Assignment Operation.
47 * @note This Class not allowed copy behaves.
48 */
49 Socket& operator=(const Socket&) = delete;
50
51 /**
52 * @brief Create Non Blocking Socket.
53 */
54 void Create();
55
56 /**
57 * @brief Get this socket fd.
58 * @return Returns this socket fd.
59 */
60 const SOCKET Fd() const { return m_SocketFd; }
61
62 /**
63 * @brief Get this socket fd.
64 * @return Returns this socket fd.
65 */
66 SOCKET Fd() { return m_SocketFd; }
67
68 /**
69 * @brief Bind address to socket.
70 * @param localAddress The address to bind.
71 */
72 void BindAddress(const InetAddress& localAddress) const;
73
74 /**
75 * @brief Listen on socket.
76 */
77 void Listen() const;
78
79 /**
80 * @brief Connect to socket
81 * @param connectAddress Connection address.
82 */
83 void Connect(InetAddress* connectAddress) const;
84
85 /**
86 * @brief Accept connection on socket.
87 * @param peerAddress The address of peer.
88 * @return Returns the socket fd of peer.
89 */
91
92 /**
93 * @brief Send data to server.
94 * @param[in] data Send data.
95 * @note Usually not call Send directly, but use Buffer::WriteFd.
96 */
97 void Send(const std::string& data) const;
98
99 /**
100 * @brief Receive data from server.
101 * @return Returns received data.
102 * @note Usually not call Receive directly, but use Buffer::ReadFd.
103 */
104 std::string Receive() const;
105
106 /**
107 * @brief Disable writen in socket.
108 */
109 void ShutDownWrite() const;
110
111 /**
112 * @brief Set socket Tcp no delay option.
113 * @param on True if want enable such option.
114 */
115 void SetTcpNoDelay(bool on) const;
116
117 /**
118 * @brief Set socket reuse address option.
119 * @param on True if want enable such option.
120 */
121 void SetReuseAddress(bool on) const;
122
123 /**
124 * @brief Set socket reuse port option.
125 * @param on True if want enable such option.
126 * @attention This option is only available on Linux.
127 * use SetReuseAddress instead
128 */
129 [[deprecated]] void SetReusePort(bool on) const;
130
131 /**
132 * @brief Set socket keep alive option.
133 * @param on True if want enable such option.
134 */
135 void SetKeepAlive(bool on) const;
136
137 private:
138
139 /**
140 * @brief This socket fd.
141 */
143 };
144
145}
146
147}
#define SPICES_PROFILE_ZONE
Wrapper of readwrite buffer.
Definition Buffer.h:19
Wrapper of SOCKET'S Event.
Definition Channel.h:27
ThreadPool of Multiple threading EventLoop.
EventLoopThreadWrapper()
Constructor Function.
Definition EventLoop.h:186
virtual ~EventLoopThreadWrapper()
Destructor Function.
static EventLoop *& GetInst(InetAddress *address=nullptr)
Get EventLoop Instance. @reutrn Returns EventLoop Instance.
EventLoop * instance
This thread EventLoop instance.
Definition EventLoop.h:204
Wrapper of Instance/Delete ThreadCache in thread.
Definition EventLoop.h:180
EventLoop & operator=(const EventLoop &)=delete
Copy Assignment Operation.
void HandleWakeUp()
Handle WakeUp notify by read one byte data.
std::vector< Functor > m_PendingFunctors
Delay functors.
Definition EventLoop.h:168
void DoPendingFunctors()
Execute all pending functors.
std::shared_ptr< Poller > m_Poller
Poller instance.
Definition EventLoop.h:148
std::atomic_bool m_IsCallingPendingFunctors
True if is execute pending functors.
Definition EventLoop.h:138
std::atomic_bool m_IsLooping
True if is in Looping.
Definition EventLoop.h:128
bool HasChannel(Channel *channel) const
Determine if channel is inside poller.
std::mutex m_Mutex
Mutex for PendingFunctors.
Definition EventLoop.h:173
EventLoop(const EventLoop &)=delete
Copy Constructor Function.
std::atomic_bool m_IsQuit
True if is quit from Looping.
Definition EventLoop.h:133
EventLoop(InetAddress *address=nullptr)
Constructor Function.
Definition EventLoop.cpp:22
void WakeUp()
WakeUp a thread, which wait on recv, by sending one byte data.
std::unique_ptr< Channel > m_WeakupChannel
Wakeup Channel.
Definition EventLoop.h:158
void Loop()
Start Event Loop.
Definition EventLoop.cpp:48
bool IsInLoopThread() const
Determine if current thread is in eventloop thread.
Definition EventLoop.h:109
void RunInLoop(Functor cb)
Push functor to pending functors.
Definition EventLoop.cpp:79
~EventLoop()
Destructor Function.
Definition EventLoop.cpp:42
Socket m_WakeupFd
Wakeup Socket.
Definition EventLoop.h:153
DWORD m_ThreadId
Thread's identify, which is running this EventLoop.
Definition EventLoop.h:143
void QueueInLoop(Functor cb)
Execute functor in EventLoop thread.
Definition EventLoop.cpp:91
void UpdateChannel(Channel *channel) const
Update channel state with poller.
void Quit()
Quit from Event Loop.
Definition EventLoop.cpp:69
ChannelList m_ActiveChannels
Channels with events.
Definition EventLoop.h:163
void RemoveChannel(Channel *channel) const
Remove channel from poller.
Wrapper of Poller and wakeup socket to acceptor(SubLoop).
Definition EventLoop.h:28
const Version GetVersion() const
Get Http Version.
Definition HttpRequest.h:89
std::string GetHeader(const std::string &name) const
Get a Header from this HttpRequest.
Http Request body.
Definition HttpRequest.h:18
HttpResponse(bool close)
Constructor Function.
void AppendToBuffer(Buffer *output)
Append this response to Buffer.
bool CloseConnection() const
Get CloseConnection.
HttpServer & operator=(const HttpServer &)=delete
Copy Assignment Operation.
HttpServer(const HttpServer &)=delete
Copy Constructor Function.
void OnRequest(const TcpConnectionPtr &connection, const HttpRequest &request)
HttpCallback m_HttpCallback
Definition HttpServer.h:75
virtual ~HttpServer()=default
Destructor Function.
void OnConnection(const TcpConnectionPtr &connection)
HttpServer(const InetAddress &listenAddress, int idleSeconds, TcpServer::Option option=TcpServer::Option::NoReusePort)
Constructor Function.
void Start(int threadSize) const
Start ThreadPool and call listen on acceptor.
WeakConnectionList m_ConnectionList
Definition HttpServer.h:78
void OnMessage(const TcpConnectionPtr &connection, Buffer *buf)
This class is Wrapper of current socket address.
Definition InetAddress.h:22
Inherit from this and Implement Specific Poller.
Definition Poller.h:25
Socket(const Socket &)=delete
Copy Constructor Function.
void SetReuseAddress(bool on) const
Set socket reuse address option.
Definition Socket.cpp:188
void SetTcpNoDelay(bool on) const
Set socket Tcp no delay option.
Definition Socket.cpp:149
void Connect(InetAddress *connectAddress) const
Connect to socket.
Definition Socket.cpp:62
Socket & operator=(const Socket &)=delete
Copy Assignment Operation.
SOCKET m_SocketFd
This socket fd.
Definition Socket.h:142
void Send(const std::string &data) const
Send data to server.
Definition Socket.cpp:106
SOCKET Accept(InetAddress *peerAddress) const
Accept connection on socket.
Definition Socket.cpp:83
Socket()=default
Constructor Function.
void BindAddress(const InetAddress &localAddress) const
Bind address to socket.
Definition Socket.cpp:36
void Create()
Create Non Blocking Socket.
Definition Socket.cpp:27
virtual ~Socket()
Destructor Function.
Definition Socket.cpp:14
std::string Receive() const
Receive data from server.
Definition Socket.cpp:119
SOCKET Fd()
Get this socket fd.
Definition Socket.h:66
Socket(SOCKET socketFd)
Constructor Function.
Definition Socket.h:30
const SOCKET Fd() const
Get this socket fd.
Definition Socket.h:60
void SetKeepAlive(bool on) const
Set socket keep alive option.
Definition Socket.cpp:175
void SetReusePort(bool on) const
Set socket reuse port option.
Definition Socket.cpp:162
void ShutDownWrite() const
Disable writen in socket.
Definition Socket.cpp:136
void Listen() const
Listen on socket.
Definition Socket.cpp:49
This class is Wrapper of socket.
Definition Socket.h:18
Combine of Socket Connection data.
void SetThreadInitCallback(const ThreadInitCallback &cb)
Set Thread initialize Call back.
Definition TcpServer.h:71
void AddMessageCallback(const DelegateMessageCallback::Agent &cb)
Add Message Call back.
Definition TcpServer.h:89
void Start(int threadSize) const
Start ThreadPool and call listen on acceptor.
Definition TcpServer.cpp:46
std::shared_ptr< EventLoopThreadPool > m_ThreadPool
ThreadPool.
Definition TcpServer.h:145
void NewConnection(SOCKET socketFd, const InetAddress &peerAddress)
Handle New Connection to Acceptor.
Definition TcpServer.cpp:57
void RemoveConnectionInLoop(const TcpConnectionPtr &connection)
Remove a TcpConnection InLoop.
Definition TcpServer.cpp:97
TcpServer(const TcpServer &)=delete
Copy Constructor Function.
ConnectionMap m_Connections
All TcpConnections.
Definition TcpServer.h:175
DelegateWriteCompleteCallback m_WriteCompleteCallback
WriteCompleteCallback.
Definition TcpServer.h:160
void RemoveConnection(const TcpConnectionPtr &connection)
Remove a TcpConnection.
Definition TcpServer.cpp:92
TcpServer(const InetAddress &listenAddress, Option option=Option::NoReusePort)
Constructor Function.
Definition TcpServer.cpp:16
TcpServer & operator=(const TcpServer &)=delete
Copy Assignment Operation.
ThreadInitCallback m_ThreadInitCallback
ThreadInitCallback.
Definition TcpServer.h:165
std::unique_ptr< Acceptor > m_Acceptor
Acceptor.
Definition TcpServer.h:140
std::string m_IpPort
TcpServer Ip and Port.
Definition TcpServer.h:135
void AddConnectionCallback(const DelegateConnectionCallback::Agent &cb)
Add Connection Call back.
Definition TcpServer.h:80
virtual ~TcpServer()
Destructor Function.
Definition TcpServer.cpp:33
DelegateConnectionCallback m_ConnectionCallback
ConnectionCallback.
Definition TcpServer.h:150
int m_NextConnectedId
Next Connection id.
Definition TcpServer.h:170
DelegateMessageCallback m_MessageCallback
MessageCallback.
Definition TcpServer.h:155
void AddWriteCompleteCallback(const DelegateWriteCompleteCallback::Agent &cb)
Add WriteComplete Call back.
Definition TcpServer.h:98
Sample of a TcpServer.
Definition TcpServer.h:29