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:
co-authored by
Claude Opus 5.5
parent
189da4c269
commit
40cac5cf52
@@ -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");
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user