begining tibia protocol

This commit is contained in:
Andre Antunes 2011-04-09 22:25:13 -03:00
parent 02c58f16cd
commit 2805a34267
3 changed files with 73 additions and 27 deletions

View File

@ -12,6 +12,7 @@ FIND_PACKAGE(Lua51 REQUIRED)
FIND_PACKAGE(YamlCpp REQUIRED) FIND_PACKAGE(YamlCpp REQUIRED)
FIND_PACKAGE(PhysFS REQUIRED) FIND_PACKAGE(PhysFS REQUIRED)
FIND_PACKAGE(PNG REQUIRED) FIND_PACKAGE(PNG REQUIRED)
FIND_PACKAGE(GMP REQUIRED)
# choose a default build type if not specified # choose a default build type if not specified
IF(NOT CMAKE_BUILD_TYPE) IF(NOT CMAKE_BUILD_TYPE)
@ -33,6 +34,7 @@ INCLUDE_DIRECTORIES(
${LUA_INCLUDE_DIRS} ${LUA_INCLUDE_DIRS}
${YAMLCPP_INCLUDE_DIRS} ${YAMLCPP_INCLUDE_DIRS}
${PHYSFS_INCLUDE_DIRS} ${PHYSFS_INCLUDE_DIRS}
${GMP_INCLUDE_DIR}
${PNG_INCLUDE_DIRS}) ${PNG_INCLUDE_DIRS})
LINK_DIRECTORIES( LINK_DIRECTORIES(
@ -84,7 +86,10 @@ SET(SOURCES
src/framework/net/connection.cpp src/framework/net/connection.cpp
src/framework/net/connections.cpp src/framework/net/connections.cpp
src/framework/net/protocol.cpp src/framework/net/protocol.cpp
src/framework/net/networkmessage.cpp) src/framework/net/networkmessage.cpp
# util
src/framework/util/rsa.cpp)
IF(WIN32) IF(WIN32)
SET(SOURCES ${SOURCES} src/framework/win32platform.cpp) SET(SOURCES ${SOURCES} src/framework/win32platform.cpp)
@ -106,4 +111,5 @@ TARGET_LINK_LIBRARIES(otclient
${YAMLCPP_LIBRARY} ${YAMLCPP_LIBRARY}
${PHYSFS_LIBRARY} ${PHYSFS_LIBRARY}
${PNG_LIBRARY} ${PNG_LIBRARY}
${GMP_LIBRARIES}
${ADDITIONAL_LIBRARIES}) ${ADDITIONAL_LIBRARIES})

View File

@ -71,7 +71,7 @@ void Connection::onResolveDns(const boost::system::error_code& error, boost::asi
{ {
if(error){ if(error){
m_connecting = false; m_connecting = false;
m_connectCallback(error); m_errorCallback(error, __FUNCTION__);
return; return;
} }
@ -81,22 +81,24 @@ void Connection::onResolveDns(const boost::system::error_code& error, boost::asi
void Connection::onConnect(const boost::system::error_code& error) void Connection::onConnect(const boost::system::error_code& error)
{ {
if(!error){ if(error){
m_connected = true; m_connecting = false;
m_errorCallback(error, __FUNCTION__);
return;
} }
m_connecting = false; m_connected = true;
m_connectCallback(error); m_connectCallback();
} }
void Connection::handleError(const boost::system::error_code& error) void Connection::handleError(const boost::system::error_code& error)
{ {
stop();
if(isConnected()){ if(isConnected()){
closeSocket(); closeSocket();
} }
stop();
} }
void Connection::closeSocket() void Connection::closeSocket()
@ -117,39 +119,64 @@ void Connection::closeSocket()
void Connection::send(NetworkMessagePtr networkMessage, ConnectionCallback onSend) void Connection::send(NetworkMessagePtr networkMessage, ConnectionCallback onSend)
{ {
m_socket.async_send( boost::asio::async_write(m_socket,
boost::asio::buffer(networkMessage->getBuffer(), NetworkMessage::header_length), boost::asio::buffer(networkMessage->getBuffer(), NetworkMessage::header_length),
boost::bind(&Connection::onSendHeader, shared_from_this(), networkMessage, onSend, boost::asio::placeholders::error) boost::bind(&Connection::onSendHeader, shared_from_this(), networkMessage, onSend, boost::asio::placeholders::error));
); }
void Connection::recv(RecvCallback onRecv)
{
NetworkMessagePtr networkMessage(new NetworkMessage);
boost::asio::async_read(m_socket,
boost::asio::buffer(networkMessage->getBuffer(), NetworkMessage::header_length),
boost::bind(&Connection::onRecvHeader, shared_from_this(), networkMessage, onRecv, boost::asio::placeholders::error));
}
void Connection::onRecvHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error)
{
if(error){
connection->handleError(error);
connection->onError(error, __FUNCTION__);
return;
}
boost::asio::async_read(connection->getSocket(),
boost::asio::buffer(networkMessage->getBodyBuffer(), networkMessage->getMessageLength()),
boost::bind(&Connection::onRecvBody, connection, networkMessage, onRecv, boost::asio::placeholders::error));
}
void Connection::onRecvBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error)
{
if(error){
connection->handleError(error);
connection->onError(error, __FUNCTION__);
return;
}
onRecv(networkMessage);
} }
void Connection::onSendHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error) void Connection::onSendHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error)
{ {
if(!connection->isConnected()){
return;
}
if(error){ if(error){
connection->handleError(error); connection->handleError(error);
onSend(error); connection->onError(error, __FUNCTION__);
return; return;
} }
connection->getSocket().async_send( boost::asio::async_write(connection->getSocket(),
boost::asio::buffer(networkMessage->getBodyBuffer(), networkMessage->getMessageLength()), boost::asio::buffer(networkMessage->getBodyBuffer(), networkMessage->getMessageLength()),
boost::bind(&Connection::onSendBody, connection, networkMessage, onSend, boost::asio::placeholders::error) boost::bind(&Connection::onSendBody, connection, networkMessage, onSend, boost::asio::placeholders::error));
);
} }
void Connection::onSendBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error) void Connection::onSendBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error)
{ {
if(!connection->isConnected()){ if(error){
connection->handleError(error);
connection->onError(error, __FUNCTION__);
return; return;
} }
if(error){ onSend();
connection->handleError(error);
}
onSend(error);
} }

View File

@ -37,7 +37,10 @@ class Connections;
class Connection : public std::enable_shared_from_this<Connection> class Connection : public std::enable_shared_from_this<Connection>
{ {
public: public:
typedef std::function<void(const boost::system::error_code&)> ConnectionCallback; typedef std::function<void()> ConnectionCallback;
typedef std::function<void(NetworkMessagePtr)> RecvCallback;
typedef std::function<void(const boost::system::error_code&, const std::string&)> ErrorCallback;
typedef std::shared_ptr<Connection> ConnectionPtr; typedef std::shared_ptr<Connection> ConnectionPtr;
private: private:
@ -45,17 +48,26 @@ private:
bool connect(const std::string& ip, uint16 port, ConnectionCallback onConnect); bool connect(const std::string& ip, uint16 port, ConnectionCallback onConnect);
void stop(); void stop();
void send(NetworkMessagePtr networkMessage, ConnectionCallback onSend);
void setErrorCallback(ErrorCallback c) { m_errorCallback = c; }
void recv(RecvCallback onSend);
void send(NetworkMessagePtr networkMessage, ConnectionCallback onRecv);
bool isConnecting() const { return m_connecting; } bool isConnecting() const { return m_connecting; }
bool isConnected() const { return m_connected; } bool isConnected() const { return m_connected; }
boost::asio::ip::tcp::socket& getSocket() { return m_socket; } boost::asio::ip::tcp::socket& getSocket() { return m_socket; }
void onError(const boost::system::error_code& error, const std::string& msg) { m_errorCallback(error, msg); }
private: private:
static void onSendHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error); static void onSendHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error);
static void onSendBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error); static void onSendBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error);
static void onRecvHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error);
static void onRecvBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error);
private: private:
void onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt); void onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt);
void onConnect(const boost::system::error_code& error); void onConnect(const boost::system::error_code& error);
@ -76,6 +88,7 @@ private:
uint16_t m_port; uint16_t m_port;
ConnectionCallback m_connectCallback; ConnectionCallback m_connectCallback;
ErrorCallback m_errorCallback;
friend class Protocol; friend class Protocol;
friend class Connections; friend class Connections;