if you get disconnected sometimes check your ping ^^
Last edited:
if (noPongTime >= 60000) {
if(canLogout()){
if (client) {
client->logout(true);
} else {
g_game.removeCreature(this, true);
}
}
othire already comes with a ping system but it is different from the tfs or otclient onesI am not really into OTHire but as far as I can see in the changes, there's not pongBack therefore the ping system proceeds to kick you. I'd suggest to take a look how the pong is being received.
Code:if (noPongTime >= 60000) { if(canLogout()){ if (client) { client->logout(true); } else { g_game.removeCreature(this, true); } }
if(OTSYS_TIME() - last_pong >= 60000){
if(canLogout()){
if(client){
client->logout(true);
}
else{
g_game.removeCreature(this, true);
}
}
}
Add to game.h somewhere beside protected.my final recommendation is don't use it i been testing it and some times you get disconnected without any rason or atleast it is happening to me... i was looking this post Ping is not working [OThire 0.0.3 - 7.72] · Issue #979 · edubart/otclient and i wanted to implement but don't get it i dont know where or in wich part of game.h i have to register this getNewPing(const std::string& ip)
int getNewPing(const std::string& ip)
Thank you i'm compiling right now but @ruth i still have to put this in modules/game_things/things.luaAdd to game.h somewhere beside protected.
Code:int getNewPing(const std::string& ip)
g_game.enableFeature(GameClientPing)
Thank you i'm compiling right now but @ruth i still have to put this in modules/game_things/things.lua
?Code:g_game.enableFeature(GameClientPing)
EDIT
i compile it everything fine but the ping doesnt show up :/ should i add something more somewhere else? i have to put the ip of the vps where it says ip? because i tried to put the ip but i cant compile :O
thank you so much =) it workedUsage print(g_game.getNewPing("Server IP"))
true i just tested it by adding this to things.luaThe part above this message "Now we start with otclient sources we need to add some codes here too... Thanks to @ruth or Anastacia from github for this code of otclient" is not needed. As i said before this ping system is only client side.
g_game.enableFeature(GameClientPing)
here you go guys the code is working and tested by me i make the tutorial easy for you guys so follow it and you wont have any problems
Player.h around line 900
Code:void sendPing(); void sendPingBack() const { if (client) { client->sendPingBack(); } }
Protocolgame.h look for this
and add this belowCode:void parseReceivePing(NetworkMessage& msg);
Code:void parseReceivePingBack(NetworkMessage& msg);
Protocolgame.h around line 192
Code:void sendPing(); void sendPingBack();
Game.h around line 505
Code:bool playerReceivePing(uint32_t playerId); bool playerReceivePingBack(uint32_t playerId);
Protocolgame.cpp around line 516
Code:case 0x1D: addGameTask(&Game::playerReceivePingBack, player->getID()); break;
Protocolgame.cpp around line 553
Code:case 0x1D: // keep alive / ping response parseReceivePingBack(msg); break;
Protocolgame.cpp around line 1090
Code:void ProtocolGame::parseReceivePingBack(NetworkMessage& msg) { addGameTask(&Game::playerReceivePingBack, player->getID()); }
Protocolgame.cpp around line 1905
Code:void ProtocolGame::sendPing() { NetworkMessage_ptr msg = getOutputBuffer(); if(msg){ TRACK_MESSAGE(msg); msg->AddByte(0x1E); } } void ProtocolGame::sendPingBack() { NetworkMessage_ptr msg = getOutputBuffer(); if(msg){ TRACK_MESSAGE(msg); msg->AddByte(0x1D); } }
Game.cpp around line 2250
Code:bool Game::playerReceivePing(uint32_t playerId) { Player* player = getPlayerByID(playerId); if(!player || player->isRemoved()) return false; player->receivePing(); return true; } bool Game::playerReceivePingBack(uint32_t playerId) { Player* player = getPlayerByID(playerId); if(!player || player->isRemoved()) return false; player->sendPingBack(); return true; }
Otclient/modules/game_things/things.lua
Code:g_game.enableFeature(GameClientPing)
Otclient/modules/game_interface/interface.lua
Code:function onGameStart() show() g_game.setPingDelay(4000) end
In case you want the ping system only in the client side only you can edit this files in otclient sources... Thanks to @ruth or Anastacia from github for this code of otclient
add this to Luafunctions.cpp
Code:g_lua.bindSingletonFunction("g_game", "getNewPing", &Game::getNewPing, &g_game);
in game.cpp after this
we add thisCode:#include <framework/platform/platformwindow.h>
Code:#include <winsock2.h> #include <iphlpapi.h> #include <icmpapi.h> #include <stdio.h> #pragma comment(lib, "iphlpapi.lib") #pragma comment(lib, "ws2_32.lib") int Game::getNewPing(const std::string& ip) { HANDLE hIcmpFile; unsigned long ipaddr = inet_addr(ip.c_str()); DWORD dwRetVal = 0; char SendData[32] = "Data Buffer"; LPVOID ReplyBuffer = NULL; DWORD ReplySize = 0; // Validate the parameters if (ipaddr == INADDR_NONE) { return 0; } hIcmpFile = IcmpCreateFile(); if (hIcmpFile == INVALID_HANDLE_VALUE) { g_logger.info(stdext::format("\tUnable to open handle.\n")); g_logger.info(stdext::format("IcmpCreatefile returned error: %ld\n", GetLastError())); return 0; } ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData); ReplyBuffer = (VOID*)malloc(ReplySize); if (ReplyBuffer == NULL) { g_logger.info(stdext::format("\tUnable to allocate memory\n")); return 0; } dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, 5000); if (dwRetVal != 0) { PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer; struct in_addr ReplyAddr; ReplyAddr.S_un.S_addr = pEchoReply->Address; return pEchoReply->RoundTripTime; } else { g_logger.info(stdext::format("\tCall to IcmpSendEcho failed.\n")); g_logger.info(stdext::format("\tIcmpSendEcho returned error: %ld\n", GetLastError())); return 0; } return 0; }
in game.h we should add this somewhere before Protected
Code:int getNewPing(const std::string& ip);
Otclient/modules/game_things/things.lua
Code:print(g_game.getNewPing("YOUR_NUMERIC_IP"))
Otclient/modules/game_interface/interface.lua
Code:function onGameStart() show() g_game.setPingDelay(4000) end
#include <framework/core/application.h>
#include "luavaluecasts.h"
#include "protocolgame.h"
#include "protocolcodes.h"
//ping related
#include <winsock2.h>
#include <iphlpapi.h>
#include <icmpapi.h>
#include <stdio.h>
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
int Game::getNewPing(const std::string& ip)
{
HANDLE hIcmpFile;
unsigned long ipaddr = inet_addr(ip.c_str());
DWORD dwRetVal = 0;
char SendData[32] = "Data Buffer";
LPVOID ReplyBuffer = NULL;
DWORD ReplySize = 0;
// Validate the parameters
if (ipaddr == INADDR_NONE) {
return 0;
}
hIcmpFile = IcmpCreateFile();
if (hIcmpFile == INVALID_HANDLE_VALUE) {
g_logger.info(stdext::format("\tUnable to open handle.\n"));
g_logger.info(stdext::format("IcmpCreatefile returned error: %ld\n", GetLastError()));
return 0;
}
ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData);
ReplyBuffer = (VOID*)malloc(ReplySize);
if (ReplyBuffer == NULL) {
g_logger.info(stdext::format("\tUnable to allocate memory\n"));
return 0;
}
dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData),
NULL, ReplyBuffer, ReplySize, 5000);
if (dwRetVal != 0) {
PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
struct in_addr ReplyAddr;
ReplyAddr.S_un.S_addr = pEchoReply->Address;
return pEchoReply->RoundTripTime;
}
else {
g_logger.info(stdext::format("\tCall to IcmpSendEcho failed.\n"));
g_logger.info(stdext::format("\tIcmpSendEcho returned error: %ld\n", GetLastError()));
return 0;
}
return 0;
}
shouldnt be that hard to understand it is in the top of the file... but if u dont have it just search for thisI DONT HAVE THIS #include <framework/platform/platformwindow.h> IN GAME.CPP OTC :/ ARE YOU SURE that's where we should put the code its there? could you tell me the lines please?
Game g_game;
Wasn't hard to understand but i were suffering some compilation errorsshouldnt be that hard to understand it is in the top of the file... but if u dont have it just search for this
then add it before that codeCode:Game g_game;
--1first line
print(g_game.getNewPing("127.0.0.1"))
filename = nil
loaded = false
function onGameStart()
show()
g_game.setPingDelay(4000)
end
look for this in things.luaI followed tutorial OTClient Ping client side
but when i enable ping at optio nit doesnt appear
no errors on terminal
things.lua
Code:--1first line print(g_game.getNewPing("127.0.0.1")) filename = nil loaded = false
gameinterface.lua
any idea why?Code:function onGameStart() show() g_game.setPingDelay(4000) end
g_game.setProtocolVersion(0)
print(g_game.getNewPing("127.0.0.1"))
disconnect(g_game, { onClientVersionChange = load })
g_game.setClientVersion(0)
g_game.setProtocolVersion(0)
print(g_game.getNewPing("127.0.0.1"))
connect(g_game, { onClientVersionChange = load })
end
end
did you compile the otc sources with the req modifications? also make sure the ping is enabled in the otclientCode:disconnect(g_game, { onClientVersionChange = load }) g_game.setClientVersion(0) g_game.setProtocolVersion(0) print(g_game.getNewPing("127.0.0.1")) connect(g_game, { onClientVersionChange = load }) end end
the ping still wont appear :/