Net: handle in-game GC_PHASE like the Windows client

PHASE_SELECT returns the connection to LoginSession (cached roster,
SelectReady, later LOGIN_SUCCESS refreshes), PHASE_CLOSE goes to the
login phase, LOADING clears the player, GAME refreshes the status and
DEAD no longer leaves the game phase.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-13 13:24:12 +09:00
co-authored by Claude Opus 5.5
parent 189da4c269
commit 40cac5cf52
9 changed files with 113 additions and 24 deletions
+1
View File
@@ -1717,6 +1717,7 @@ P0, the real-server flow stops:
1. `RecvWarpPacket` reads and drops GC_WARP. Windows sets DirectEnter for the selected slot and reconnects to `lAddr:wPort`. Map teleports and channel moves hang.
- **Fixed:** `RecvWarpPacket` stops processing the old socket and `pump_python_net` calls `LoginClient::warp`. It reconnects like DirectEnter: same login key, the remembered selected slot, a new generation so the server clock is re-synced, and `SetNewLoadingPhase` (ENABLE_NEW_LOADING). A connect failure under DirectEnter is ClosePhase (`SetLoginPhase`). Covered in `login_oracle_test`.
2. In-game `RecvPhasePacket` only stores the phase. Windows calls `SetSelectPhase` (plus `__DownloadMark`), `SetLoginPhase`, `SetLoadingPhase`, `SetGamePhase` or `ClosePhase`. Going back to character select and reloading after a warp do not work.
- **Fixed:** `RecvPhasePacket` follows the Windows switch: CLOSE → SetLoginPhase; SELECT → leave the game phase and hand the unread bytes back to `LoginSession::return_to_select`, which shows the cached roster at once (SelectReady); a later LOGIN_SUCCESS raises SelectRefresh → select window `Refresh`; LOADING → player clear; GAME → status refresh; DEAD → no-op (it used to make `IsGamePhase()` false). `__DownloadMark` waits on item 8.
3. `LoginSession` (select phase) only accepts headers 0/7/32/44/90/150-153/253-255 and drops the connection on anything else. The following are missing:
- GC 6 LOGIN_SUCCESS3;
- GC 8/9 PLAYER_CREATE_SUCCESS/FAILURE;
+30 -2
View File
@@ -88,7 +88,7 @@ void PythonNetworkStream::StartGame() {
}
void PythonNetworkStream::ResetConnection() {
m_recvBuffer.clear();m_unportedPackets.clear();m_unknownPacketQuit=false;m_isObserverMode=m_bMobileFlag=false;std::memset(m_lastPacket,0,sizeof(m_lastPacket));ClearSendQueue();m_startGameRequested=m_refreshInventory=m_refreshStatus=m_refreshSkill=m_refreshCharacter=m_refreshShopSearch=m_stopGamePhase=m_warpRequested=false;
m_recvBuffer.clear();m_unportedPackets.clear();m_unknownPacketQuit=false;m_isObserverMode=m_bMobileFlag=false;std::memset(m_lastPacket,0,sizeof(m_lastPacket));ClearSendQueue();m_startGameRequested=m_refreshInventory=m_refreshStatus=m_refreshSkill=m_refreshCharacter=m_refreshShopSearch=m_stopGamePhase=m_warpRequested=m_selectRequested=false;m_selectLeftover.clear();
m_refreshGuildInfo=m_refreshGuildBoard=m_refreshGuildMember=m_refreshGuildCombo=m_refreshGuildSkill=m_refreshGuildGrade=m_refreshMessenger=false;
m_guildID=0;
m_changeServerTime=m_changeClientTime=0;m_dungeonDestination={};
@@ -445,7 +445,35 @@ bool PythonNetworkStream::DispatchPortedGamePhasePacket(uint8_t header) {
bool PythonNetworkStream::RecvPhasePacket() {
TPacketGCPhase p;
if (!Read(sizeof(p), &p)) return false;
SetPhase(static_cast<EPhase>(p.phase));
switch(p.phase) {
case PHASE_CLOSE: // ClosePhase
inventory_callback(m_poHandler,"SetLoginPhase",nullptr);
break;
case PHASE_HANDSHAKE:
case PHASE_LOGIN:
// SetHandShakePhase/SetLoginPhase leave the game phase; the login transport owns these phases.
NetworkActorManager::Instance().Clear();
SetPhase(static_cast<EPhase>(p.phase));
break;
case PHASE_SELECT:
// SetSelectPhase leaves the game phase (__LeaveGamePhase). The select-phase packets that follow
// (LOGIN_SUCCESS, EMPIRE, PLAYER_CREATE/DELETE, CHANGE_NAME) belong to LoginSession, so the
// unprocessed bytes go back to the transport. __DownloadMark: guild marks are not ported.
m_selectRequested=true;
m_selectLeftover.assign(m_recvBuffer.begin(),m_recvBuffer.end());
m_recvBuffer.clear();m_stopGamePhase=true;
break;
case PHASE_LOADING: // SetLoadingPhase: CPythonPlayer::Clear
ResetPlayerState();
SetPhase(PHASE_LOADING);
break;
case PHASE_GAME: // SetGamePhase: __RefreshStatus
SetPhase(PHASE_GAME);
m_refreshStatus=true;
break;
case PHASE_DEAD: // no-op on Windows; m_strPhase stays "Game"
break;
}
return true;
}
+7
View File
@@ -221,6 +221,11 @@ public:
if(!m_warpRequested) return false;
m_warpRequested=false;address=m_warpAddress;port=m_warpPort;return true;
}
// GC_PHASE(SELECT) in game: returns the bytes after it for the login transport.
bool TakeSelectRequest(std::vector<uint8_t>& leftover) {
if(!m_selectRequested) return false;
m_selectRequested=false;leftover=std::move(m_selectLeftover);m_selectLeftover.clear();return true;
}
private:
bool CheckPacket(uint8_t* retHeader);
@@ -267,6 +272,8 @@ private:
// Handshake/HandshakeOK/HybridCrypt return from GamePhase() before the refresh block.
bool m_stopGamePhase=false;
bool m_warpRequested=false;
bool m_selectRequested=false;
std::vector<uint8_t> m_selectLeftover;
uint32_t m_warpAddress=0;
uint16_t m_warpPort=0;
uint32_t m_changeServerTime=0,m_changeClientTime=0; // CPythonNetworkStream::m_kServerTimeSync
+36 -19
View File
@@ -119,28 +119,45 @@ void LoginClient::pump() {
if(n<0 && (errno==EAGAIN || errno==EWOULDBLOCK)) break;
if(n<=0) { fail("server disconnected");return; }
session_->feed({buffer,static_cast<std::size_t>(n)});enqueue();if(status_==Status::Failed) return;
if(session_->state()==LoginSession::State::Failed) { fail(session_->failure());return; }
if(session_->state()==LoginSession::State::Authenticated && !on_game_) {
on_game_=true;session_->begin_game();start_endpoint(game_);return;
}
if(on_game_ && session_->handshake_phase_entered() && !handshake_notified_) { handshake_notified_=true;if(!direct_enter_) notify(EventType::Handshake); }
if(on_game_ && session_->game_login_sent() && !login_notified_) { login_notified_=true;if(!direct_enter_) notify(EventType::LoginStart); }
if(session_->state()==LoginSession::State::Selecting && !selection_notified_) {
selection_notified_=true;status_=Status::Selecting;
if(direct_enter_) {
select(direct_enter_slot_);
// Do not notify DirectEnterReady here before the server actually transitions to Loading!
} else {
notify(EventType::SelectReady);
}
}
if(session_->state()==LoginSession::State::Loading && !loading_notified_) {
loading_notified_=true;status_=Status::Loading;
notify(EventType::Loading);return;
}
if(!advance()) return;
flush();if(status_==Status::Failed) return;
}
}
// Session state transitions after new input; false when pump() must stop for this frame.
bool LoginClient::advance() {
if(session_->state()==LoginSession::State::Failed) { fail(session_->failure());return false; }
if(session_->state()==LoginSession::State::Authenticated && !on_game_) {
on_game_=true;session_->begin_game();start_endpoint(game_);return false;
}
if(on_game_ && session_->handshake_phase_entered() && !handshake_notified_) { handshake_notified_=true;if(!direct_enter_) notify(EventType::Handshake); }
if(on_game_ && session_->game_login_sent() && !login_notified_) { login_notified_=true;if(!direct_enter_) notify(EventType::LoginStart); }
// __RecvLoginSuccessPacket4 in SelectPhase: PHASE_WINDOW_SELECT.Refresh.
if(session_->state()==LoginSession::State::Selecting && selection_notified_ && !direct_enter_ && session_->roster_version()!=roster_version_)
notify(EventType::SelectRefresh);
if(session_->state()==LoginSession::State::Selecting && !selection_notified_) {
selection_notified_=true;status_=Status::Selecting;
if(direct_enter_) {
select(direct_enter_slot_);
// Do not notify DirectEnterReady here before the server actually transitions to Loading!
} else {
notify(EventType::SelectReady);
}
}
roster_version_=session_->roster_version();
if(session_->state()==LoginSession::State::Loading && !loading_notified_) {
loading_notified_=true;status_=Status::Loading;
notify(EventType::Loading);return false;
}
return true;
}
void LoginClient::return_to_select(std::span<const std::uint8_t> leftover) {
if(!session_ || status_!=Status::Loading) throw std::logic_error("select phase requires an entered game session");
session_->return_to_select(leftover);
// The next character choice is a fresh DirectEnter/select from the select window.
direct_enter_=warp_=false;selection_notified_=loading_notified_=false;
status_=Status::Entering;deadline_=std::chrono::steady_clock::now()+std::chrono::seconds(15);
enqueue();advance();flush();
}
wire::Bytes LoginClient::take_game_input(std::size_t max) { return session_?session_->take_game_input(max):wire::Bytes{}; }
void LoginClient::send_game_packet(wire::Bytes packet,bool sequence) {
if(status_!=Status::Loading || !session_) throw std::logic_error("game transport is not ready");
+5 -1
View File
@@ -9,7 +9,7 @@ class LoginClient {
public:
struct Endpoint { std::string host; int port=0; };
enum class Status { Idle, Resolving, Connecting, Authenticating, Selecting, Entering, Loading, Failed };
enum class EventType { Handshake, RemoteDisconnect, LoginStart, SelectReady, DirectEnterReady, Loading, Failure };
enum class EventType { Handshake, RemoteDisconnect, LoginStart, SelectReady, SelectRefresh, DirectEnterReady, Loading, Failure };
struct Event { EventType type; std::string reason; bool connection_failure=false; std::uint64_t generation=0; };
LoginClient()=default;
~LoginClient();
@@ -23,6 +23,8 @@ public:
void direct_enter(std::uint8_t slot);
// CPythonNetworkStream::RecvWarpPacket: __DirectEnterMode_Set(m_dwSelectedCharacterIndex, true) + Connect(lAddr, wPort).
void warp(std::uint32_t address,std::uint16_t port);
// CPythonNetworkStream::RecvPhasePacket(PHASE_SELECT) in game: back to SelectReady on the same connection.
void return_to_select(std::span<const std::uint8_t> leftover);
wire::Bytes take_game_input(std::size_t max=LoginSession::kRecvBufferSize);
void send_game_packet(wire::Bytes packet,bool sequence=true);
std::vector<Event> take_events();
@@ -35,6 +37,7 @@ public:
private:
struct Resolution;
void start_endpoint(const Endpoint& endpoint);
bool advance();
void reconnect(std::uint32_t address,std::uint16_t port,std::uint8_t slot,bool warp);
bool try_next_address();
void close_socket();
@@ -50,6 +53,7 @@ private:
bool direct_enter_=false;
std::uint8_t direct_enter_slot_=0, selected_slot_=0;
bool warp_=false;
std::uint32_t roster_version_=0;
bool handshake_notified_=false;
bool on_game_=false, login_notified_=false, selection_notified_=false, loading_notified_=false;
std::uint64_t generation_=0;
+10 -1
View File
@@ -32,6 +32,14 @@ void LoginSession::reconnect_game() {
tx_=rx_=locale_key(); state_=State::Handshake;
roster_received_=select_phase_received_=game_login_sent_=false;
}
void LoginSession::return_to_select(std::span<const std::uint8_t> leftover) {
if(state_!=State::Loading) throw std::logic_error("select phase requires an entered game session");
// Same socket and cipher. Windows SetSelectPhase shows the cached m_akSimplePlayerInfo at once;
// a later LOGIN_SUCCESS in SelectPhase refreshes it.
state_=State::Selecting;select_phase_received_=true;
plain_.insert(plain_.begin(),leftover.begin(),leftover.end());
while(state_!=State::Failed && process_packet()) {}
}
void LoginSession::emit(Bytes bytes,bool sequence) {
if(sequence) { bytes.push_back(sequences[sequence_]); sequence_=(sequence_+1)%std::size(sequences); }
output_.push_back(encrypted_?encrypt(bytes,tx_):std::move(bytes));
@@ -109,7 +117,8 @@ bool LoginSession::process_packet() {
else if(!packet[5] || read32(packet,1)==0) fail("BESAMEKEY");
else { login_key_=read32(packet,1);state_=State::Authenticated; }
} else if(header==32) {
if(!game_ || state_!=State::LoggingIn) { fail("character roster in wrong state"); return false; }
if(!game_ || (state_!=State::LoggingIn && state_!=State::Selecting)) { fail("character roster in wrong state"); return false; }
++roster_version_;
characters_.clear();
for(std::size_t i=0;i<5;++i) {
std::size_t o=1+i*70; Character c;
+4
View File
@@ -19,6 +19,8 @@ public:
~LoginSession();
void begin_game();
void reconnect_game();
// GC_PHASE(SELECT) while in game: the game stream hands back the bytes it has not processed.
void return_to_select(std::span<const std::uint8_t> leftover);
void feed(std::span<const std::uint8_t> input);
std::vector<wire::Bytes> take_output();
void update_player_guild(const std::string& player,uint32_t id,const std::string& name) {
@@ -36,6 +38,7 @@ public:
const std::vector<Character>& characters() const { return characters_; }
bool handshake_phase_entered() const { return handshake_phase_entered_; }
bool game_login_sent() const { return game_login_sent_; }
std::uint32_t roster_version() const { return roster_version_; }
// Game bootstrap owns the first handshake before native Game-phase processing.
bool server_milliseconds(std::uint32_t& value) const {
if(!game_ || !server_clock_ready_) return false;
@@ -57,6 +60,7 @@ private:
std::chrono::steady_clock::time_point server_clock_received_;
bool game_=false, encrypted_=false, roster_received_=false, select_phase_received_=false, game_login_sent_=false;
std::uint8_t empire_=0;
std::uint32_t roster_version_=0;
std::string account_,password_,failure_;
wire::Key client_key_,receive_key_,tx_,rx_;
std::uint32_t login_key_=0;
+11 -1
View File
@@ -1409,7 +1409,8 @@ void pump_python_net() {
else if(event.type == LoginClient::EventType::SelectReady) {
callback(handler, client.session()->empire() ? "SetSelectCharacterPhase" : "SetSelectEmpirePhase");
if(event.generation == client.generation()) callback(phases[2], "Refresh");
} else if(event.type == LoginClient::EventType::DirectEnterReady) callback(handler, "SetLoadingPhase");
} else if(event.type == LoginClient::EventType::SelectRefresh) callback(phases[2], "Refresh");
else if(event.type == LoginClient::EventType::DirectEnterReady) callback(handler, "SetLoadingPhase");
else if(event.type == LoginClient::EventType::Loading) {
PythonNetworkStream::Instance().SetPhase(PHASE_LOADING);
// SetSelectPhase under DirectEnter: ENABLE_NEW_LOADING picks SetNewLoadingPhase for a warp.
@@ -1427,6 +1428,15 @@ void pump_python_net() {
auto& stream=PythonNetworkStream::Instance();
stream.PushRecvData(client.take_game_input(stream.RecvBufferSpace()));
stream.ProcessRecvData();
std::vector<uint8_t> selectLeftover;
if(stream.TakeSelectRequest(selectLeftover) && client.status()==LoginClient::Status::Loading) {
// RecvPhasePacket(PHASE_SELECT): SetSelectPhase. __LeaveGamePhase clears actors and items; the
// transport resumes select-phase parsing and raises SelectReady (SetSelectCharacterPhase).
stream.ResetConnection();stream.SetPhase(PHASE_SELECT);
try { client.return_to_select(selectLeftover); }
catch(const std::exception& e) { SDL_Log("Select phase failed: %s",e.what());client.cancel();callback(handler,"SetLoginPhase"); }
return;
}
uint32_t warpAddress=0;uint16_t warpPort=0;
if(stream.TakeWarpRequest(warpAddress,warpPort) && client.status()==LoginClient::Status::Loading) {
// RecvWarpPacket: __DirectEnterMode_Set(selected slot, true) + Connect. Leaving the game phase
+9
View File
@@ -70,5 +70,14 @@ int main() {
valid.select(0);valid.take_output();
ava2::wire::Bytes warped{253,5};valid.feed(ava2::wire::encrypt(warped,rx));
if(valid.state()!=ava2::LoginSession::State::Loading) return 16;
// GC_PHASE(SELECT) in game (/phase_select): the cached roster is selectable at once and a later
// LOGIN_SUCCESS refreshes it (SelectPhase accepts LOGIN_SUCCESS4).
auto version=valid.roster_version();
valid.return_to_select({});
if(valid.state()!=ava2::LoginSession::State::Selecting || valid.characters()[0].id!=11) return 17;
valid.feed(ava2::wire::encrypt(ava2::wire::Bytes(payload.begin()+2,payload.end()),rx));
if(valid.state()!=ava2::LoginSession::State::Selecting || valid.roster_version()!=version+1) return 18;
valid.select(0);
if(valid.state()!=ava2::LoginSession::State::Entering) return 19;
std::cout<<"Original AVA2 client/server layout, cipher and character selection oracle passed\n";
}