27 #include <type_traits>
28 #include <boost/asio/ip/tcp.hpp>
30 using boost::asio::ip::tcp;
32 #define READ_BLOCK_SIZE 4096
33 #ifdef BOOST_ASIO_HAS_IOCP
34 #define TC_SOCKET_USE_IOCP
64 template<
class T,
class Stream = tcp::socket>
65 class Socket :
public std::enable_shared_from_this<T>
77 boost::system::error_code error;
81 virtual void Start() = 0;
88 #ifndef TC_SOCKET_USE_IOCP
128 std::bind(callback, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
135 #ifdef TC_SOCKET_USE_IOCP
147 boost::system::error_code shutdownError;
148 _socket.shutdown(boost::asio::socket_base::shutdown_send, shutdownError);
151 shutdownError.value(), shutdownError.message().c_str());
173 #ifdef TC_SOCKET_USE_IOCP
176 this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
179 this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
187 boost::system::error_code err;
188 _socket.set_option(tcp::no_delay(enable), err);
190 TC_LOG_DEBUG(
"network",
"Socket::SetNoDelay: failed to set_option(boost::asio::ip::tcp::no_delay) for %s - %d (%s)",
212 #ifdef TC_SOCKET_USE_IOCP
214 void WriteHandler(boost::system::error_code error, std::size_t transferedBytes)
219 _writeQueue.front().ReadCompleted(transferedBytes);
249 boost::system::error_code error;
250 std::size_t bytesSent =
_socket.write_some(boost::asio::buffer(queuedMessage.
GetReadPointer(), bytesToSend), error);
254 if (error == boost::asio::error::would_block || error == boost::asio::error::try_again)
262 else if (bytesSent == 0)
269 else if (bytesSent < bytesToSend)
297 #endif // __SOCKET_H__
void WriteHandlerWrapper(boost::system::error_code, std::size_t)
Definition: Socket.h:234
Socket(tcp::socket &&socket)
Definition: Socket.h:68
void AsyncReadWithCallback(void(T::*callback)(boost::system::error_code, std::size_t))
Definition: Socket.h:120
size_type GetRemainingSpace() const
Definition: MessageBuffer.h:68
boost::asio::ip::address _remoteAddress
Definition: Socket.h:285
bool _isWritingAsync
Definition: Socket.h:294
virtual bool Update()
Definition: Socket.h:83
boost::asio::ip::address GetRemoteIpAddress() const
Definition: Socket.h:99
#define false
Definition: CascPort.h:18
#define TC_LOG_DEBUG(filterType__,...)
Definition: Log.h:198
bool AsyncProcessQueue()
Definition: Socket.h:166
MessageBuffer _readBuffer
Definition: Socket.h:288
uint8 * GetWritePointer()
Definition: MessageBuffer.h:60
bool HandleQueue()
Definition: Socket.h:240
void SetNoDelay(bool enable)
Definition: Socket.h:185
void WriteCompleted(size_type bytes)
Definition: MessageBuffer.h:64
bool IsOpen() const
Definition: Socket.h:140
virtual ~Socket()
Definition: Socket.h:74
void EnsureFreeSpace()
Definition: MessageBuffer.h:85
void ReadCompleted(size_type bytes)
Definition: MessageBuffer.h:62
void DelayedCloseSocket()
Marks the socket for closing after write buffer becomes empty.
Definition: Socket.h:157
uint16 _remotePort
Definition: Socket.h:286
uint16_t uint16
Definition: Define.h:151
void QueuePacket(MessageBuffer &&buffer)
Definition: Socket.h:131
#define READ_BLOCK_SIZE
Definition: Socket.h:32
uint16 GetRemotePort() const
Definition: Socket.h:104
size_type GetActiveSize() const
Definition: MessageBuffer.h:66
void Resize(size_type bytes)
Definition: MessageBuffer.h:51
std::atomic< bool > _closing
Definition: Socket.h:292
std::queue< MessageBuffer > _writeQueue
Definition: Socket.h:289
virtual void ReadHandler()=0
Stream _socket
Definition: Socket.h:283
MessageBuffer & GetReadBuffer()
Definition: Socket.h:159
void Normalize()
Definition: MessageBuffer.h:73
void AsyncRead()
Definition: Socket.h:109
virtual void OnClose()
Definition: Socket.h:162
uint8 * GetReadPointer()
Definition: MessageBuffer.h:58
std::atomic< bool > _closed
Definition: Socket.h:291
void CloseSocket()
Definition: Socket.h:142
Stream & underlying_stream()
Definition: Socket.h:194
Definition: MessageBuffer.h:24
void ReadHandlerInternal(boost::system::error_code error, size_t transferredBytes)
Definition: Socket.h:200