rodecomp/Source/Network/Connection/Connection.cpp
ragnarok-clanker 5ad8e0c58c Maintenance: Resubmission of 16340 over main 047268c0 (conflict in UICashEmotionPack::GetPriceText resolved keeping main's new body).
Batch #16351 by ragnarok-clanker: no function's compiled code changes; checked with 784 compiled unit(s), including every includer of Source/Archive/GPak.h, Source/Client/Browser/BrowserCommandTarget.h, Source/Client/Browser/BrowserDocumentUI.h, Source/Client/Browser/BrowserInPlace.h, Source/Client/Browser/WebBrowser.h, Source/Client/Input/Language.h, Source/Client/Input/LanguageTC.h, Source/Client/Input/Mouse.h, Source/Client/Mode/CGameMode.h, Source/Client/Mode/CLoginMode.h, Source/Client/Replay.h, Source/Client/Session.h, Source/Client/Stove/StovePCSDK.h, Source/Contents/AchievementMgr.h, Source/Contents/Actors/CharacterRender.h, Source/Contents/Actors/EffectActors.h, Source/Contents/Actors/GameActor.h, Source/Contents/Actors/GndRiser.h, Source/Contents/Actors/GrannyGameActor.h, Source/Contents/Actors/MousePointer.h, Source/Contents/Actors/Npc.h, Source/Contents/Actors/Pc.h, Source/Contents/Actors/RenderObjectState.h, Source/Contents/Actors/Skill.h, Source/Contents/AdventureGuideMgr.h, Source/Contents/CashEmotionMgr.h, Source/Contents/ClanInfo.h, Source/Contents/Configuration/GameSettingsMgr.h, Source/Contents/EffectResourceSwitcher.h, Source/Contents/Effects/AcidifiedThrow.h, Source/Contents/Effects/ArrowEffect.h, Source/Contents/Effects/AxeBoomerang.h, Source/Contents/Effects/BlastForge.h, Source/Contents/Effects/CharacterParticleBundle.h, Source/Contents/Effects/DummyEffectLauncher.h, Source/Contents/Effects/EZ2STREffectEx.h, Source/Contents/Effects/Effect.h, Source/Contents/Effects/EffectActor.h, Source/Contents/Effects/EffectEmitter.h, Source/Contents/Effects/EffectPrimitiveState.h, Source/Contents/Effects/EffectTypes.h, Source/Contents/Effects/ElectricEffect.h, Source/Contents/Effects/EmitterEffect.h, Source/Contents/Effects/FortuneKissEffect.h, Source/Contents/Effects/MagicFloorEffect.h, Source/Contents/Effects/MandraAttackEffect.h, Source/Contents/Effects/MonsterSkillEffects.h, Source/Contents/Effects/MsgEffect.h, Source/Contents/Effects/Particle.h, Source/Contents/Effects/RagEffectState.h, Source/Contents/Effects/RichManKim.h, Source/Contents/Effects/SpriteParticle.h, Source/Contents/Effects/StarEffect.h, Source/Contents/Effects/WhirlwindEffect.h, Source/Contents/EnchantMgr.h, Source/Contents/FlyingShipRouteInfo.h, Source/Contents/Ground/GroundSpatial.h, Source/Contents/GuildInfo.h, Source/Contents/Hotkeys/InterfaceHotkey.h, Source/Contents/Items/ItemInfo.h, Source/Contents/Items/RuneSystem.h, Source/Contents/MapSkyMgr.h, Source/Contents/Models/ModelActor.h, Source/Contents/Movies/BinkMovie.h, Source/Contents/NavigationManager.h, Source/Contents/Party/PartyBookingMgr.h, Source/Contents/PetEvolutionMgr.h, Source/Contents/RenewQuestMgr.h, Source/Contents/RodexMailStore.h, Source/Contents/Schedule/Schedule.h, Source/Contents/Schedule/ShutDownSchedule.h, Source/Contents/SignBoard/SignBoard.h, Source/Contents/Skills/PlayerSkillBundle.h, Source/Contents/Sound/SoundMgr.h, Source/Contents/SpriteLayerDrawing.h, Source/Contents/Store/MCStoreInfoBackupAsyncWork.h, Source/Contents/UserData/AccountLinkedUserDataLoadAsyncWork.h, Source/Contents/UserData/NormalItemSlots.h, Source/Contents/WorldView/WorldViewData.h, Source/Network/Connection/Connection.h, Source/Render/DX7/DeviceOwnerDX7.h, Source/Render/DX7/DirectDrawEnumeration.h, Source/Render/DX9/DeviceOwnerDX9.h, Source/Render/Primitives/LightmappedPrimitive.h, Source/Render/Primitives/RenderPrimitive.h, Source/Render/Renderer.h, Source/Render/RendererDX7.h, Source/Render/Text/EmbedFont.h, Source/Render/Text/TextContext.h, Source/Render/Textures/Texture.h, Source/Resource/BinkRuntime.h, Source/Resource/GifResource.h, Source/Resource/Models/AnimatedModelResource.h, Source/Resource/Models/GrannyModelResource.h, Source/Resource/Models/ModelMesh.h, Source/Resource/ResourceMgr.h, Source/Resource/World/GroundResource.h, Source/Resource/World/WorldResource.h, Source/Security/NProtectGameGuardMgr.h, Source/Security/ReassemblyPacketMgr.h, Source/UI/Buttons/UIBmpRadioBtn.h, Source/UI/Buttons/UIBmpTextButton2.h, Source/UI/Buttons/UIDescriptRace.h, Source/UI/Buttons/UIItemTagButton.h, Source/UI/Buttons/UITVTextButton.h, Source/UI/Buttons/UITextureButton.h, Source/UI/Chat/UINewChatWnd.h, Source/UI/Chat/UISubChatHisWnd.h, Source/UI/Dialogs/UILoadingWnd.h, Source/UI/Dialogs/UIMiniMapWnd.h, Source/UI/Dialogs/UIPasswordWnd.h, Source/UI/Dialogs/UIRestartWnd.h, Source/UI/Dialogs/UISayDialogWnd.h, Source/UI/Edit/UIEditCtrl.h, Source/UI/Enchant/EnchantUI.h, Source/UI/GameSettings/GameSettingsUI.h, Source/UI/Guild/UIGuildWnd.h, Source/UI/Item/UIItemStoreFindWnd.h, Source/UI/Item/UIItemStoreSubWnd.h, Source/UI/Item/UIItemStoreWnd.h, Source/UI/Items/UIEquipWnd.h, Source/UI/Items/UIExchangeWnd.h, Source/UI/Items/UIItemCollectionWnd.h, Source/UI/Items/UIItemRepairWnd.h, Source/UI/Items/UISwapEquipmentWnd.h, Source/UI/Lists/TableBox.h, Source/UI/Lists/UIListBox.h, Source/UI/Lists/UIListBoxEx.h, Source/UI/Lists/UIListBoxID.h, Source/UI/Lists/UIListBoxTemplate.h, Source/UI/Lists/UITabControl.h, Source/UI/Lists/UITreeView.h, Source/UI/Lists/UIVScrollBar.h, Source/UI/Navigation/UINavigationV4Wnd.h, Source/UI/Navigation/UIPrivateAirplaneWnd.h, Source/UI/Party/UIAdvenPartyBoardWnd.h, Source/UI/Party/UIRequestJoinPartyWnd.h, Source/UI/ProbabilityTable/UIProbabilityTable.h, Source/UI/Quest/UIRenewQuestWnd.h, Source/UI/Rodex/UIRodexComposeWnd.h, Source/UI/RuneSystem/UIRuneSystemWindows.h, Source/UI/RuneSystem/UIRuneSystemWnd.h, Source/UI/Shop/UIMerchantItemMyShopWnd.h, Source/UI/Shop/UIMerchantShopMakeWnd.h, Source/UI/Shop/UIMerchantShopTitle.h, Source/UI/StylingShop/UIStylingShopWnd.h, Source/UI/Text/CreateLineInfo.h, Source/UI/Text/TextMarkup.h, Source/UI/Text/UIString.h, Source/UI/Text/UITextViewer.h, Source/UI/Text/UITransBalloonText.h, Source/UI/UIChangeMaterialWnd.h, Source/UI/UIMacroDetectorWnd.h, Source/UI/UIMacroUserInfoWnd.h, Source/UI/Window/UIAccountLimitedToolWnd.h, Source/UI/Window/UIBalloonOpenBtn.h, Source/UI/Window/UIBasicInfoWnd.h, Source/UI/Window/UIBattleMsgOptionWnd.h, Source/UI/Window/UICollectionSystemWnd.h, Source/UI/Window/UIControl.h, Source/UI/Window/UIHotKeyWnd.h, Source/UI/Window/UIJobListWnd.h, Source/UI/Window/UILoginWnd.h, Source/UI/Window/UIMakeCharWnd.h, Source/UI/Window/UIMerInfoWnd.h, Source/UI/Window/UIMessageBox.h, Source/UI/Window/UIMonsterDisplayWnd.h, Source/UI/Window/UINewMakeCharWnd.h, Source/UI/Window/UINewSkillListWnd.h, Source/UI/Window/UINoticeWnd.h, Source/UI/Window/UIReplayRECControlWnd.h, Source/UI/Window/UIRoMapWnd.h, Source/UI/Window/UISecondCostumeWnd.h, Source/UI/Window/UISeekPartyListWnd.h, Source/UI/Window/UISeekPartyMBWnd.h, Source/UI/Window/UISeekPartyWnd.h, Source/UI/Window/UISerachStoreInfoWnd.h, Source/UI/Window/UIWindow.h, Source/UI/Window/UIWindowMgr.h, Source/UI/Window/WindowId.h, Source/Utility/Async/NAsyncWork.h, Source/Utility/Async/NRecycleObjectPoolMgr.h, Source/Utility/JsonCppLoader.h, Source/Utility/Lexicon/LexiconSerializer.h, Source/Utility/NFileLog.h, Source/Utility/Threading/NThread.h.
2026-11-10 17:38:25 +00:00

375 lines
9 KiB
C++

#include "Client/Application/ErrorDialog.h"
#include "Client/Session.h"
#include "Network/Connection/Connection.h"
#include "Network/Connection/ConnectionRuntime.h"
#include "Network/Packets/PacketLengths.h"
#include "Render/DeviceContext/DCSurface.h"
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <windows.h>
#include <timeapi.h>
#include <memory>
#include <string>
// OFFSET: 0x00C7C110
// FUNCTION: fn_20382
CConnection::CConnection() : m_socket(INVALID_SOCKET), m_deferred(0), m_sendDeadline(0) {
this->m_input.Init(0xA000);
this->m_output.Init(0xA000);
this->m_pending.Init(0xA000);
}
// OFFSET: 0x00C7C280
// FUNCTION: fn_20384
CConnection::~CConnection() = default;
// OFFSET: 0x00C7C1C0
// FUNCTION: fn_20383
CRagConnection::CRagConnection() : padding_006C(0) {
}
// INLINED
CRagConnection::~CRagConnection() {
}
namespace {
// INLINED
unsigned long ResolveConnectionAddress(std::string overrideAddress, const char* defaultAddress) {
std::string address = defaultAddress;
if (!overrideAddress.empty()) {
const std::size_t colon = overrideAddress.rfind(":");
if (colon != std::string::npos) {
const std::string host = overrideAddress.substr(0, colon);
if (hostent* resolved = gethostbyname(host.c_str())) {
address = inet_ntoa(*reinterpret_cast<in_addr*>(resolved->h_addr_list[0]));
}
}
}
return inet_addr(address.c_str());
}
// INLINED
u_short ResolveConnectionPort(std::string overrideAddress, int port) {
if (!overrideAddress.empty()) {
const std::size_t colon = overrideAddress.rfind(":");
if (colon != std::string::npos) {
port = std::atoi(overrideAddress.substr(colon + 1).c_str());
}
}
return htons(static_cast<u_short>(port));
}
}
// OFFSET: 0x00C7C4D0
// FUNCTION: fn_20389
bool CConnection::Connect(const ConnectionEndpoint& endpoint) {
this->m_deferred = 0;
while (this->m_pending.GetSize() != 0) {
const int bytes = this->m_pending.GetSize();
this->m_output.InsertData(bytes, this->m_pending.Data());
this->m_pending.RemoveData(bytes);
this->Flush();
}
this->m_input.Init(0xA000);
this->m_output.Init(0xA000);
this->m_pending.Init(0xA000);
this->m_socket = socket(AF_INET, SOCK_STREAM, 0);
if (this->m_socket == INVALID_SOCKET) {
return false;
}
u_long nonblocking = 1;
if (ioctlsocket(this->m_socket, FIONBIO, &nonblocking) == SOCKET_ERROR) {
ShowErrorDialog("Failed to setup select mode", 0);
return false;
}
std::memset(&this->m_address, 0, sizeof(this->m_address));
this->m_address.sin_addr.s_addr = ResolveConnectionAddress(endpoint.overrideAddress, endpoint.address);
this->m_address.sin_family = AF_INET;
this->m_address.sin_port = ResolveConnectionPort(endpoint.overrideAddress, endpoint.port);
if (connect(this->m_socket, reinterpret_cast<const sockaddr*>(&this->m_address), sizeof(this->m_address)) != SOCKET_ERROR ||
WSAGetLastError() == WSAEWOULDBLOCK) {
this->m_status = 0;
return true;
}
this->Close();
return false;
}
// OFFSET: 0x00C7C830
// FUNCTION: fn_20390
void CConnection::Close() {
if (this->m_socket != INVALID_SOCKET) {
closesocket(this->m_socket);
this->m_socket = INVALID_SOCKET;
this->m_output.Init(0xA000);
this->m_pending.Init(0xA000);
}
this->m_status = 0;
g_session.m_options.Values()[5] = 0;
}
// OFFSET: 0x00C7C9C0
// FUNCTION: fn_20392
short CRagConnection::GetPacketType(const char* packet) {
return *reinterpret_cast<const short*>(packet);
}
// OFFSET: 0x00C7CA40
// FUNCTION: fn_20397
bool CConnection::Receive() {
for (;;) {
fd_set readable;
FD_ZERO(&readable);
FD_SET(this->m_socket, &readable);
timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
const int ready = select(0, &readable, nullptr, nullptr, &timeout);
if (ready == SOCKET_ERROR || ready <= 0) {
return true;
}
char data[2048];
const int bytes = g_connectionReceive(this->m_socket, data, sizeof(data), 0);
if (bytes == SOCKET_ERROR) {
const int error = WSAGetLastError();
if (error == WSAEWOULDBLOCK || error == WSAENOTCONN) {
continue;
}
} else if (bytes > 0) {
this->m_input.InsertData(bytes, data);
continue;
}
this->Close();
return false;
}
}
// OFFSET: 0x00C7CB30
// FUNCTION: fn_20398
bool CConnection::Flush() {
while (this->m_sendDeadline < timeGetTime()) {
if (this->m_output.GetSize() == 0) {
return true;
}
fd_set writable;
FD_ZERO(&writable);
FD_SET(this->m_socket, &writable);
timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
const int ready = select(0, nullptr, &writable, nullptr, &timeout);
if (ready != SOCKET_ERROR) {
if (ready <= 0) {
return true;
}
const int sent = g_connectionSend(this->m_socket, this->m_output.Data(), this->m_output.GetSize(), 0);
if (sent != SOCKET_ERROR) {
this->m_output.RemoveData(sent);
} else {
const int error = WSAGetLastError();
if (error != WSAEWOULDBLOCK && error != WSAENOTCONN) {
this->Close();
return false;
}
}
}
}
return true;
}
// OFFSET: 0x00C7CC30
// FUNCTION: fn_20399
bool CConnection::Update() {
return this->m_socket == INVALID_SOCKET || (this->Receive() && this->Flush());
}
// OFFSET: 0x00C7CC60
// FUNCTION: fn_20400
void CConnection::UpdateStatus() {
if (this->m_socket != INVALID_SOCKET) {
if (this->Receive() && this->Flush()) {
this->m_status = 0;
} else {
this->m_status = 1;
}
}
}
// OFFSET: 0x00C7CA10
// FUNCTION: fn_20394
int CConnection::Status() const {
return this->m_status;
}
// OFFSET: 0x00C7CA20
// FUNCTION: fn_20395
void __stdcall Nullsub435() {
}
// OFFSET: 0x00C7CA30
// FUNCTION: fn_20396
void __stdcall Nullsub436() {
}
// OFFSET: 0x00C7CE30
// FUNCTION: fn_20405
void CConnection::Send(int bytes, const void* data) {
timeGetTime();
if (this->m_socket == INVALID_SOCKET) {
return;
}
this->m_output.InsertData(bytes, data);
this->Flush();
}
// OFFSET: 0x00C7CC90
// FUNCTION: fn_20401
int CConnection::Read(void* output, int bytes, unsigned char exact) {
if (exact != 0) {
return this->m_input.GetData(bytes, output) ? bytes : 0;
}
const int available = std::min(bytes, this->m_input.GetSize());
this->m_input.GetData(available, output);
return available;
}
// OFFSET: 0x00C7CCE0
// FUNCTION: fn_20402
bool CConnection::ReadPacket(void* output, unsigned short* bytes, const PacketLengths& lengths) {
unsigned short id;
if (!this->m_input.PeekData(2, &id)) {
return false;
}
unsigned short size;
if (lengths.Lookup(id).kind == 1) {
size = static_cast<unsigned short>(lengths.FixedLength(id));
} else {
unsigned header;
if (!this->m_input.PeekData(4, &header)) {
return false;
}
size = static_cast<unsigned short>(header >> 16);
}
if (!this->m_input.GetData(size, output)) {
return false;
}
if (bytes) {
*bytes = size;
}
return true;
}
// OFFSET: 0x00C7CEC0
// FUNCTION: fn_20406
void CConnection::Prepend(int bytes, const void* data) {
this->m_input.Prepend(bytes, data);
}
// OFFSET: 0x00C7CDB0
// FUNCTION: fn_20403
void CConnection::ResetDeferred() {
this->m_pending.Init(0xA000);
}
// OFFSET: 0x00C7CDC0
// FUNCTION: fn_20404
void CConnection::ResetQueue() {
this->m_input.Init(0xA000);
this->m_output.Init(0xA000);
this->m_pending.Init(0xA000);
}
// OFFSET: 0x00C7CED0
// FUNCTION: fn_20407
void CConnection::SetBlock(unsigned char value) {
this->m_deferred = value;
if (value != 0) {
return;
}
while (this->m_pending.GetSize() != 0) {
const int bytes = this->m_pending.GetSize();
this->m_output.InsertData(bytes, this->m_pending.Data());
this->m_pending.RemoveData(bytes);
this->Flush();
}
}
// OFFSET: 0x00C7D7D0
// FUNCTION: fn_20409
CRagConnection& GetRagConnection() {
// STATIC: 0x016FDC98
// GLOBAL: gl_24490
static CRagConnection connection;
return connection;
}
struct ConnectionBlock {
/* 00000 */ ConnectionBlock* next;
/* 00004 */ unsigned char data[0x41004];
};
struct ConnectionBlockList {
/* 0000 */ ConnectionBlock* head;
/* 0004 */ int* count;
};
namespace {
// GLOBAL: gl_25135
constexpr unsigned char g_codeMarker[6] = {0x33, 0xC0, 0x33, 0xDB, 0x33, 0xC9};
}
// OFFSET: 0x00C7D930
// FUNCTION: fn_20410
void ReleaseConnectionBlocks(int, ConnectionBlockList& list) {
*list.count = 0;
ConnectionBlock* block = list.head;
while (block) {
ConnectionBlock* next = block->next;
std::allocator<ConnectionBlock>().deallocate(block, 1);
block = next;
}
}
// OFFSET: 0x00C7DF30
// FUNCTION: fn_20411
int PacketCodecStub(void* data, std::size_t size, std::size_t* outSize, int) {
unsigned char buffer[0x400];
std::memcpy(buffer, data, size);
std::memcpy(data, buffer, size);
*outSize = size;
return 0;
}
// OFFSET: 0x00C7E030
// FUNCTION: fn_20412
const unsigned char* __stdcall FindCodeMarker(const unsigned char* code) {
while (std::memcmp(code, g_codeMarker, sizeof(g_codeMarker)) != 0) {
++code;
}
return code;
}