Otclient  14/8/2020
protocolhttp.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "protocolhttp.h"
24 #include "connection.h"
26 
28 {
29 }
30 
32 {
33 #ifndef NDEBUG
34  assert(!g_app.isTerminated());
35 #endif
36  disconnect();
37 }
38 
39 void ProtocolHttp::connect(const std::string& host, uint16 port)
40 {
41  m_connection = ConnectionPtr(new Connection);
42  m_connection->setErrorCallback(std::bind(&ProtocolHttp::onError, asProtocolHttp(), std::placeholders::_1));
43  m_connection->connect(host, port, std::bind(&ProtocolHttp::onConnect, asProtocolHttp()));
44 }
45 
47 {
48  if(m_connection) {
49  m_connection->close();
50  m_connection.reset();
51  }
52 }
53 
54 void ProtocolHttp::send(const std::string& message)
55 {
56  if(m_connection)
57  m_connection->write((uint8*)message.c_str(), message.length());
58 }
59 
61 {
62  if(m_connection)
63  m_connection->read_until("\r\n\r\n", std::bind(&ProtocolHttp::onRecv, asProtocolHttp(), std::placeholders::_1, std::placeholders::_2));
64 }
65 
67 {
68  callLuaField("onConnect");
69 }
70 
71 void ProtocolHttp::onRecv(uint8* buffer, uint16 size)
72 {
73  std::string string = std::string((char*)buffer, (size_t)size);
74  callLuaField("onRecv", string);
75 }
76 
77 void ProtocolHttp::onError(const boost::system::error_code& err)
78 {
79  callLuaField("onError", err.message(), err.value());
80  disconnect();
81 }
ConnectionPtr
stdext::shared_object_ptr< Connection > ConnectionPtr
Definition: declarations.h:41
ProtocolHttp::ProtocolHttp
ProtocolHttp()
Definition: protocolhttp.cpp:27
ProtocolHttp::onConnect
void onConnect()
Definition: protocolhttp.cpp:66
ProtocolHttp::connect
void connect(const std::string &host, uint16 port)
Definition: protocolhttp.cpp:39
ProtocolHttp::~ProtocolHttp
virtual ~ProtocolHttp()
Definition: protocolhttp.cpp:31
ProtocolHttp::onRecv
void onRecv(uint8 *buffer, uint16 size)
Definition: protocolhttp.cpp:71
ProtocolHttp::asProtocolHttp
ProtocolHttpPtr asProtocolHttp()
Definition: protocolhttp.h:44
protocolhttp.h
uint16
uint16_t uint16
Definition: types.h:36
LuaObject::callLuaField
R callLuaField(const std::string &field, const T &... args)
Definition: luaobject.h:172
Connection
Definition: connection.h:31
stdext::shared_object_ptr::reset
void reset()
Definition: shared_object.h:79
Connection::close
void close()
Definition: connection.cpp:66
ProtocolHttp::onError
void onError(const boost::system::error_code &err)
Definition: protocolhttp.cpp:77
g_app
ConsoleApplication g_app
Definition: consoleapplication.cpp:32
Connection::write
void write(uint8 *buffer, size_t size)
Definition: connection.cpp:117
Connection::connect
void connect(const std::string &host, uint16 port, const std::function< void()> &connectCallback)
Definition: connection.cpp:93
Connection::read_until
void read_until(const std::string &what, const RecvCallback &callback)
Definition: connection.cpp:173
connection.h
Application::isTerminated
bool isTerminated()
Definition: application.h:50
Connection::setErrorCallback
void setErrorCallback(const ErrorCallback &errorCallback)
Definition: connection.h:58
ProtocolHttp::disconnect
void disconnect()
Definition: protocolhttp.cpp:46
ProtocolHttp::send
void send(const std::string &message)
Definition: protocolhttp.cpp:54
uint8
uint8_t uint8
Definition: types.h:37
ProtocolHttp::recv
void recv()
Definition: protocolhttp.cpp:60
application.h