-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAsioSocketConnection.cpp
More file actions
141 lines (126 loc) · 4.67 KB
/
AsioSocketConnection.cpp
File metadata and controls
141 lines (126 loc) · 4.67 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
AsioQuickfix (c) by Michiel van Slobbe
AsioQuickfix is licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License.
You should have received a copy of the license along with this
work. If not, see <http://creativecommons.org/licenses/by-sa/3.0/>.
*/
#include "AsioSocketConnection.hpp"
namespace FIX
{
AsioSocketConnection::AsioSocketConnection ( boost::shared_ptr < boost::asio::ip::tcp::socket > const & socket,
SessionID const & sessionID,
Session * session ) :
m_socket ( socket ),
m_session_id ( sessionID ),
m_session ( session ),
m_sending ( false )
{
assert ( m_socket.get() != 0 && m_socket->is_open() );
assert ( session != 0 );
session->setResponder ( this );
session->next(); // should send logon
Session::registerSession( m_session->getSessionID() );
StartReadAsync();
}
AsioSocketConnection::~AsioSocketConnection()
{
Session::unregisterSession( m_session->getSessionID() );
if ( m_socket.get() != 0 && m_socket->is_open() )
m_socket->cancel();
disconnect();
}
bool AsioSocketConnection::send( const std::string& msg )
{
size_t sz ( msg.size() );
const char * buf ( &msg[0] );
if ( !m_sending && sz <= OUTGOING_BUFFER_SIZE )
{
StartSendAsync( buf, sz );
}
else
{
m_queued_outgoing_buffer.insert ( m_queued_outgoing_buffer.end(), msg.begin(), msg.end() );
if ( !m_sending )
{
m_sending = true;
StartSendAsync();
}
}
return true;
}
void AsioSocketConnection::disconnect()
{
if ( m_socket.get() != 0 && m_socket->is_open() )
m_socket->close();
}
void AsioSocketConnection::StartSendAsync( const char * buf, size_t sz )
{
assert ( m_socket.get() != 0 && m_socket->is_open() );
assert ( !m_sending );
m_sending = true;
std::copy ( buf, buf + sz, m_outgoing_buffer.begin() );
m_socket->async_write_some ( boost::asio::buffer ( m_outgoing_buffer, sz ), boost::bind ( &AsioSocketConnection::AsyncSentSocket,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred ) );
}
void AsioSocketConnection::StartSendAsync()
{
assert ( m_sending );
assert ( m_socket.get() != 0 && m_socket->is_open() );
if ( m_queued_outgoing_buffer.size() == 0 )
m_sending = false;
else
{
size_t buffer_size ( m_queued_outgoing_buffer.size() > OUTGOING_BUFFER_SIZE ? OUTGOING_BUFFER_SIZE : m_queued_outgoing_buffer.size() );
std::copy ( m_queued_outgoing_buffer.begin(), m_queued_outgoing_buffer.begin() + buffer_size, m_outgoing_buffer.begin() );
m_queued_outgoing_buffer.erase( m_queued_outgoing_buffer.begin(), m_queued_outgoing_buffer.begin() + buffer_size );
m_socket->async_write_some ( boost::asio::buffer ( m_outgoing_buffer, buffer_size ), boost::bind ( &AsioSocketConnection::AsyncSentSocket,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred ) );
}
}
void AsioSocketConnection::StartReadAsync()
{
assert ( m_socket.get() != 0 && m_socket->is_open() );
m_socket->async_read_some ( boost::asio::buffer ( m_incoming_buffer ),
boost::bind ( &AsioSocketConnection::AsyncReadSocket,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred )
);
}
void AsioSocketConnection::AsyncSentSocket( boost::system::error_code const & error_code,
size_t len )
{
if ( error_code == 0 && len != 0 )
{
StartSendAsync();
}
else
{
std::cerr << "socket disconnected or error received!" << std::endl;
}
}
void AsioSocketConnection::AsyncReadSocket( boost::system::error_code const & error_code,
size_t len )
{
if ( error_code == 0 && len != 0 )
{
assert ( m_socket.get() != 0 && m_socket->is_open() );
const char* buffer ( m_incoming_buffer.begin() );
m_parser.addToStream( buffer, len );
assert ( m_session != 0 );
std::string msg;
while ( m_parser.readFixMessage( msg ) )
m_session->next( msg, UtcTimeStamp() );
StartReadAsync();
}
else
{
std::cerr << "socket disconnected or error received!" << std::endl;
}
}
}