@@ -48,30 +48,23 @@ using namespace LibMCDriver_BuR::Impl;
48
48
**************************************************************************************************************************/
49
49
50
50
CDriver_BuR::CDriver_BuR (const std::string& sName , LibMCEnv::PDriverEnvironment pDriverEnvironment)
51
- : m_sName(sName ), m_pDriverEnvironment (pDriverEnvironment)
51
+ : m_sName(sName ), m_pDriverEnvironment (pDriverEnvironment), m_nWorkerThreadCount ( 1 ), m_nMaxReceiveBufferSize ( 1024 * 1024 )
52
52
{
53
53
if (pDriverEnvironment.get () == nullptr )
54
54
throw ELibMCDriver_BuRInterfaceException (LIBMCDRIVER_BUR_ERROR_INVALIDPARAM);
55
55
}
56
56
57
+
58
+
57
59
void CDriver_BuR::Configure (const std::string& sConfigurationString )
58
60
{
59
- uint32_t workerThreadCount = 1 ;
60
- uint32_t maxReceiveBufferSize = 1024 * 1024 ;
61
61
62
62
m_pTcpService = brynet::net::TcpService::Create ();
63
- m_pTcpService->startWorkerThread (workerThreadCount );
63
+ m_pTcpService->startWorkerThread (m_nWorkerThreadCount );
64
64
65
65
m_pAsyncConnector = brynet::net::AsyncConnector::Create ();
66
66
m_pAsyncConnector->startWorkerThread ();
67
67
68
- brynet::net::wrapper::ConnectionBuilder connectionBuilder;
69
-
70
- connectionBuilder.WithService (m_pTcpService)
71
- .WithConnector (m_pAsyncConnector)
72
- .WithMaxRecvBufferSize (maxReceiveBufferSize)
73
- .AddEnterCallback ([this ](const brynet::net::TcpConnection::Ptr& session) { enterCallback (session); });
74
-
75
68
}
76
69
77
70
std::string CDriver_BuR::GetName ()
@@ -100,14 +93,75 @@ void CDriver_BuR::GetHeaderInformation(std::string& sNameSpace, std::string& sBa
100
93
void CDriver_BuR::QueryParameters ()
101
94
{
102
95
brynet::base::app_kbhit ();
96
+
97
+ if (!m_pCurrentConnection.expired ()) {
98
+ auto pConnection = m_pCurrentConnection.lock ();
99
+ // pConnection->send ();
100
+ }
101
+
103
102
}
104
103
105
- void CDriver_BuR::enterCallback (const std::shared_ptr <brynet::net::TcpConnection> session)
104
+ void CDriver_BuR::enterCallback (const std::shared_ptr <brynet::net::TcpConnection> session, const std::string& sIPAddress , const uint32_t nPort )
106
105
{
106
+
107
+ std::lock_guard<std::mutex> lockGuard (m_ConnectionMutex);
108
+
109
+ if (!m_pCurrentConnection.expired ()) {
110
+ auto pConnection = m_pCurrentConnection.lock ();
111
+ pConnection->postDisConnect ();
112
+ }
113
+
114
+
115
+ session->setDataCallback ([session](brynet::base::BasePacketReader& reader)
116
+ {
117
+
118
+
119
+ });
120
+
121
+ m_pCurrentConnection = session->weak_from_this ();
122
+ std::cout << " Connected!" << std::endl;
107
123
124
+
108
125
}
109
126
110
127
void CDriver_BuR::failedCallback ()
111
128
{
129
+ if (!m_pCurrentConnection.expired ()) {
130
+ std::lock_guard<std::mutex> lockGuard (m_ConnectionMutex);
131
+ auto pConnection = m_pCurrentConnection.lock ();
132
+ pConnection->postDisConnect ();
133
+ }
134
+ m_pCurrentConnection.reset ();
135
+
136
+ }
137
+
138
+
139
+ void CDriver_BuR::Connect (const std::string& sIPAddress , const LibMCDriver_BuR_uint32 nPort, const LibMCDriver_BuR_uint32 nTimeout)
140
+ {
141
+ brynet::net::wrapper::ConnectionBuilder connectionBuilder;
112
142
113
- }
143
+ connectionBuilder.WithService (m_pTcpService)
144
+ .WithConnector (m_pAsyncConnector)
145
+ .WithMaxRecvBufferSize (m_nMaxReceiveBufferSize)
146
+ .AddEnterCallback ([this , sIPAddress , nPort](const brynet::net::TcpConnection::Ptr& session) { enterCallback (session, sIPAddress , nPort); });
147
+
148
+ connectionBuilder
149
+ .WithAddr (sIPAddress , nPort)
150
+ .WithTimeout (std::chrono::milliseconds (nTimeout))
151
+ .WithFailedCallback ([this ]() { failedCallback (); })
152
+ .AddSocketProcessCallback ([](brynet::net::TcpSocket& socket) {
153
+ socket.setNodelay ();
154
+ })
155
+ .asyncConnect ();
156
+
157
+ }
158
+
159
+ void CDriver_BuR::Disconnect ()
160
+ {
161
+ if (!m_pCurrentConnection.expired ()) {
162
+ std::lock_guard<std::mutex> lockGuard (m_ConnectionMutex);
163
+ auto pConnection = m_pCurrentConnection.lock ();
164
+ pConnection->postDisConnect ();
165
+ }
166
+ m_pCurrentConnection.reset ();
167
+ }
0 commit comments