1#ifndef ASYNC_SOCKET_BASE_HXX
2#define ASYNC_SOCKET_BASE_HXX
24class AsyncSocketBaseHandler;
28 public std::enable_shared_from_this<AsyncSocketBase>
41 virtual asio::error_code
bind(
const asio::ip::address& address,
unsigned short port) = 0;
42 virtual void connect(
const std::string& address,
unsigned short port) = 0;
44 virtual void send(
const StunTuple& destination,
const std::shared_ptr<DataBuffer>& data);
45 virtual void send(
const StunTuple& destination,
unsigned short channel,
const std::shared_ptr<DataBuffer>& data);
58 virtual void doSend(
const StunTuple& destination,
unsigned short channel,
const std::shared_ptr<DataBuffer>& data,
const std::size_t
bufferStartPos = 0);
66 virtual void onReceiveSuccess(
const asio::ip::address& address,
unsigned short port,
const std::shared_ptr<DataBuffer>& data) = 0;
105 asio::error_code
openAndBind(asio::ip::tcp::socket& socket);
106 void asyncConnect(asio::ip::tcp::socket& socket,
const asio::ip::tcp::resolver::results_type&
endpoints);
114 void noteReceived() { mLastReceived = std::chrono::steady_clock::now(); }
137 void asyncConnectFrom(asio::ip::tcp::socket& socket, asio::ip::tcp::resolver::results_type
endpoints,
138 asio::ip::tcp::resolver::results_type::const_iterator next, asio::error_code
lastError);
140 void waitIdle(std::chrono::steady_clock::duration wait);
141 std::unique_ptr<asio::steady_timer> mIdleTimer;
142 std::chrono::steady_clock::duration mIdleTimeout;
143 std::function<
bool()> mInUse;
144 std::chrono::steady_clock::time_point mLastReceived;
146 virtual void transportSend(
const StunTuple& destination, std::vector<asio::const_buffer>&
buffers) = 0;
147 virtual void transportReceive() = 0;
148 virtual void transportFramedReceive() = 0;
149 virtual void transportClose() = 0;
151 virtual asio::ip::address getSenderEndpointAddress() = 0;
152 virtual unsigned short getSenderEndpointPort() = 0;
154 virtual void sendFirstQueuedData();
161 std::shared_ptr<DataBuffer> mFrameData;
162 std::shared_ptr<DataBuffer> mData;
163 size_t mBufferStartPos;
167 typedef std::deque<SendData> SendDataQueue;
168 SendDataQueue mSendDataQueue;
constexpr size_t RECEIVE_BUFFER_SIZE
#define resip_assert(x)
This file needs to work for both C and C++ code.
virtual void handleConnect(const asio::error_code &ec, const asio::ip::tcp::endpoint &endpoint)
virtual void handleReceive(const asio::error_code &e, size_t bytesTransferred)
asio::ip::address & getConnectedAddress() noexcept
asio::ip::tcp::endpoint mLocalTcpEndpoint
asio::error_code openAndBind(asio::ip::tcp::socket &socket)
For TCP and TLS client sockets.
virtual void onReceiveSuccess(const asio::ip::address &address, unsigned short port, const std::shared_ptr< DataBuffer > &data)=0
virtual ~AsyncSocketBase()
virtual void handleTcpResolve(const asio::error_code &ec, const asio::ip::tcp::resolver::results_type &endpoints)
BeforeClosedHandler mOnBeforeSocketCloseFp
Provides an opportunity for the app to clean up, e.g., QoS-related data or resources just before the ...
static size_t channelDataPadding(size_t length)
Over TCP and TLS, a TURN ChannelData message is padded to a multiple of 4 bytes, the padding not coun...
virtual void handleClientHandshake(const asio::error_code &ec, const asio::ip::tcp::endpoint &endpoint)
virtual void onConnectFailure(const asio::error_code &e)
virtual void onConnectSuccess()
Class override callbacks.
unsigned short mConnectedPort
virtual void doFramedReceive()
virtual void handleSend(const asio::error_code &e)
Handle completion of a sendData operation.
static std::shared_ptr< DataBuffer > allocateBuffer(size_t size)
Utility API.
unsigned short getConnectedPort() const noexcept
std::function< void(unsigned int)> BeforeClosedHandler
virtual void handleReadHeader(const asio::error_code &e)
virtual void onReceiveFailure(const asio::error_code &e)=0
virtual void framedReceive()
void asyncConnect(asio::ip::tcp::socket &socket, const asio::ip::tcp::resolver::results_type &endpoints)
virtual void onSendFailure(const asio::error_code &e)=0
virtual void onSendSuccess()=0
asio::io_context & mIOService
The io_context used to perform asynchronous operations.
virtual void setOnBeforeSocketClosedFp(BeforeClosedHandler fp)
virtual void doSend(const StunTuple &destination, unsigned short channel, const std::shared_ptr< DataBuffer > &data, const std::size_t bufferStartPos=0)
Use these if you already operating within the ioService thread.
std::shared_ptr< DataBuffer > mReceiveBuffer
Receive Buffer and state.
bool isConnected() const noexcept
AsyncSocketBaseHandler * mAsyncSocketBaseHandler
Handlers.
virtual void send(const StunTuple &destination, const std::shared_ptr< DataBuffer > &data)
Note: destination is ignored for TCP and TLS connections.
virtual void handleServerHandshake(const asio::error_code &e)
void startIdleTimer(unsigned int timeoutSeconds, std::function< bool()> inUse)
For server connections (TcpConnectionIdleTimeout): the connection is closed, with close(),...
static void setExclusiveAddressUse(asio::ip::udp::socket &socket)
For a UDP socket, between open() and bind().
asio::ip::address mConnectedAddress
Connected Info and State.
virtual unsigned int getSocketDescriptor()=0
virtual void receive()
Overlapped calls to receive functions have no effect.
virtual asio::error_code bind(const asio::ip::address &address, unsigned short port)=0
Note: The following API's are thread safe and queue the request to be handled by the ioService thread...
virtual void handleUdpResolve(const asio::error_code &ec, const asio::ip::udp::resolver::results_type &endpoints)
virtual void registerAsyncSocketBaseHandler(AsyncSocketBaseHandler *handler)
virtual void connect(const std::string &address, unsigned short port)=0
std::shared_ptr< AsyncSocketBase > ConnectionPtr