-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpsocketwriter.cpp
More file actions
47 lines (38 loc) · 1.28 KB
/
Copy pathtcpsocketwriter.cpp
File metadata and controls
47 lines (38 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "tcpsocketwriter.h"
#include "tcpclient.h"
#include <boost/bind.hpp>
#include <iostream>
TcpSocketWriter::TcpSocketWriter(const std::string& Message, boost::asio::ip::tcp::socket& Socket, TcpClient& Client) :
m_Message(Message),
m_Socket(Socket),
m_ClientSharedLock(Client.GetMutex()),
m_Client(Client)
{
m_Message.append("\n");
}
void TcpSocketWriter::AsyncWrite(const std::string& Message, boost::asio::ip::tcp::socket& Socket, TcpClient& Client)
{
auto Writer = std::make_shared<TcpSocketWriter>(Message, Socket, Client);
Writer->Write();
}
void TcpSocketWriter::Write()
{
boost::asio::async_write(m_Socket,
boost::asio::buffer(m_Message),
boost::bind(&TcpSocketWriter::HandleWrite,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::iterator)
);
}
void TcpSocketWriter::HandleWrite(const boost::system::error_code &ErrorCode, std::size_t BytesTransferred)
{
if(ErrorCode)
{
m_Client.OnTcpWriteError(ErrorCode.message());
}
else
{
m_Client.OnTcpWriteSuccess(BytesTransferred);
}
}