SpiecsEngine
 
Loading...
Searching...
No Matches

◆ SendInLoop()

void Spices::Net::TcpConnection::SendInLoop ( const char * message,
size_t len )
private

Send message to socket.

Parameters
[in]messageData pointer.
[in]lenmessage bytes.

ShutDown has been called before.

First writing.

Send message separatly.

ShutDown has been called before.

First writing.

Send message separatly.

Definition at line 177 of file TcpConnection.cpp.

178 {
179 int nWrote = 0;
180 size_t remaining = len;
181 bool faultError = false;
182
186 if (m_State.load() == State::Disconnected)
187 {
188 SPICES_CORE_ERROR("Disconnected, Can bot send message")
189 return;
190 }
191
195 if (!m_Channel->IsWriting() && m_OutputBuffer.ReadableBytes() == 0)
196 {
197 nWrote = ::send(m_Channel->Fd(), message, len, 0);
198 if (nWrote >= 0)
199 {
200 remaining = len - nWrote;
201 if (remaining == 0 && m_WriteCompleteCallback.size() > 0)
202 {
203 m_IoLoop->QueueInLoop([=]() { m_WriteCompleteCallback.Broadcast(shared_from_this()); });
204 }
205 }
206 else
207 {
208 nWrote = 0;
209 const int err = WSAGetLastError();
210
211 if (err != WSAEWOULDBLOCK)
212 {
213 SPICES_CORE_ERROR(" TcpConnection::SendInLoop")
214
215 if (err == WSAECONNRESET || err == WSAECONNRESET)
216 {
217 faultError = true;
218 }
219 }
220 }
221 }
222
226 if (!faultError && remaining > 0)
227 {
228 const size_t oldLen = m_OutputBuffer.ReadableBytes();
229 if (oldLen + remaining >= HighWaterMark && oldLen < HighWaterMark && m_HighWaterMarkCallback.size() > 0)
230 {
231 m_IoLoop->QueueInLoop([=]() { m_HighWaterMarkCallback.Broadcast(shared_from_this(), oldLen + remaining); });
232 }
233 m_OutputBuffer.Append((char*)message + nWrote, remaining);
234 if (!m_Channel->IsWriting())
235 {
236 m_Channel->EnableWriting();
237 }
238 }
239 }
size_t ReadableBytes() const
Get readable area bytes.
Definition Buffer.h:46
void Append(const std::string &msg)
Append Message to this buffer.
Definition Buffer.cpp:41
void QueueInLoop(Functor cb)
Execute functor in EventLoop thread.
Definition EventLoop.cpp:91
std::atomic< State > m_State
TcpConnection state.
DelegateHighWaterMarkCallback m_HighWaterMarkCallback
DelegateHighWaterMarkCallback.
Buffer m_OutputBuffer
Output Buffer.
DelegateWriteCompleteCallback m_WriteCompleteCallback
DelegateWriteCompleteCallback.
std::unique_ptr< Channel > m_Channel
TcpConnection channel.
EventLoop * m_IoLoop
io Loop from TcpServer::NewConnection.
constexpr size_t HighWaterMark

References m_IoLoop.