SpiecsEngine
 
Loading...
Searching...
No Matches
Socket.cpp
Go to the documentation of this file.
1/**
2* @file Socket.h.
3* @brief The Socket Class Implementation.
4* @author Spices & Muduo.
5*/
6
7#include "Pchheader.h"
8#include "Socket.h"
9
10namespace Spices {
11
12namespace Net {
13
15 {
17
18 if(::closesocket(m_SocketFd) < 0)
19 {
20 std::stringstream ss;
21 ss << "Socket::Close failed, socket fd: " << m_SocketFd << " Error: " << WSAGetLastError();
22
23 SPICES_CORE_CRITICAL(ss.str())
24 }
25 }
26
27 void Socket::Create()
28 {
30
31 SOCKET sockfd = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
32
33 m_SocketFd = sockfd;
34 }
35
36 void Socket::BindAddress(const InetAddress& localAddress) const
37 {
39
40 if (::bind(m_SocketFd, (sockaddr*)localAddress.GetSockAddress(), sizeof(sockaddr_in)) < 0)
41 {
42 std::stringstream ss;
43 ss << "Socket::BindAddress failed, socket fd: " << m_SocketFd << " Error: " << WSAGetLastError();
44
45 SPICES_CORE_CRITICAL(ss.str())
46 }
47 }
48
49 void Socket::Listen() const
50 {
52
53 if (::listen(m_SocketFd, SOMAXCONN) < 0)
54 {
55 std::stringstream ss;
56 ss << "Socket::Listen failed, socket fd: " << m_SocketFd << " Error: " << WSAGetLastError();
57
58 SPICES_CORE_CRITICAL(ss.str())
59 }
60 }
61
62 void Socket::Connect(InetAddress* connectAddress) const
63 {
65
66 int r = ::connect(m_SocketFd, reinterpret_cast<sockaddr*>(connectAddress->GetSockAddress()), sizeof(sockaddr_in));
67
68 if (r < 0)
69 {
70 // Non-Blocking usually return this error code.
71 if (WSAGetLastError() == 10035)
72 {
73 return;
74 }
75
76 std::stringstream ss;
77 ss << "Socket::Connect failed, socket fd: " << m_SocketFd << " Error: " << WSAGetLastError();
78
79 SPICES_CORE_CRITICAL(ss.str())
80 }
81 }
82
84 {
86
87 sockaddr_in address = {};
88 socklen_t len = sizeof(address);
89 const SOCKET connectFd = ::accept(m_SocketFd, reinterpret_cast<sockaddr*>(&address), &len);
90
91 // Set socket non-blocking.
92 u_long mode = 1;
93 if(ioctlsocket(m_SocketFd, FIONBIO, &mode) == SOCKET_ERROR)
94 {
95 std::stringstream ss;
96 ss << "Socket::Accept failed, socket fd: " << m_SocketFd << " Error: " << WSAGetLastError();
97
98 SPICES_CORE_CRITICAL(ss.str())
99 }
100
101 peerAddress->SetSockAddress(address);
102
103 return connectFd;
104 }
105
106 void Socket::Send(const std::string& data) const
107 {
109
110 if (::send(m_SocketFd, data.c_str(), data.size(), 0) < 0)
111 {
112 std::stringstream ss;
113 ss << "Socket::Send error, socket fd: " << m_SocketFd << " Error: " << WSAGetLastError();
114
115 SPICES_CORE_CRITICAL(ss.str())
116 }
117 }
118
119 std::string Socket::Receive() const
120 {
122
123 char buffer[1024];
124 int bytes = ::recv(m_SocketFd, buffer, sizeof(buffer), 0);
125 if(bytes < 0)
126 {
127 std::stringstream ss;
128 ss << "Socket::Receive error, socket fd: " << m_SocketFd << " Error: " << WSAGetLastError();
129
130 SPICES_CORE_CRITICAL(ss.str())
131 }
132
133 return std::string(buffer, bytes);
134 }
135
136 void Socket::ShutDownWrite() const
137 {
139
140 if (::shutdown(m_SocketFd, SD_SEND) < 0)
141 {
142 std::stringstream ss;
143 ss << "Socket::ShutDownWrite error, socket fd: " << m_SocketFd << " Error: " << WSAGetLastError();
144
145 SPICES_CORE_CRITICAL(ss.str())
146 }
147 }
148
149 void Socket::SetTcpNoDelay(bool on) const
150 {
152
153 if (::setsockopt(m_SocketFd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char*>(&on), sizeof(bool)) < 0)
154 {
155 std::stringstream ss;
156 ss << "Socket::SetTcpNoDelay error, socket fd: " << m_SocketFd << " Error: " << WSAGetLastError();
157
158 SPICES_CORE_CRITICAL(ss.str())
159 }
160 }
161
162 void Socket::SetReusePort(bool on) const
163 {
165
166 /**
167 * @note windows not support such option.
168 * Use SetKeepAlive instead.
169 */
170 //::setsockopt(m_SocketFd, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<const char*>(&on), sizeof(bool));
171
172 SPICES_CORE_ERROR("Windows not support SO_REUSEPORT, use SetReuseAddress instead.")
173 }
174
175 void Socket::SetKeepAlive(bool on) const
176 {
178
179 if (::setsockopt(m_SocketFd, SOL_SOCKET, SO_KEEPALIVE, reinterpret_cast<const char*>(&on), sizeof(bool)) < 0)
180 {
181 std::stringstream ss;
182 ss << "Socket::SetKeepAlive error, socket fd: " << m_SocketFd << " Error: " << WSAGetLastError();
183
184 SPICES_CORE_CRITICAL(ss.str())
185 }
186 }
187
188 void Socket::SetReuseAddress(bool on) const
189 {
191
192 if(::setsockopt(m_SocketFd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char*>(&on), sizeof(bool)) < 0)
193 {
194 std::stringstream ss;
195 ss << "Socket::SetReuseAddress error, socket fd: " << m_SocketFd << " Error: " << WSAGetLastError();
196
197 SPICES_CORE_CRITICAL(ss.str())
198 }
199 }
200
201}
202
203}
#define SPICES_PROFILE_ZONE
This class is Wrapper of current socket address.
Definition InetAddress.h:22
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
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
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
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