From: Maxim Mamontov Date: Tue, 10 May 2011 15:04:08 +0000 (+0300) Subject: Implement connecting and disconnecting users X-Git-Tag: 2.407-p1~10 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/3cc6c36fcf3f0c6449debeb56e53c2ede03efc26?ds=inline Implement connecting and disconnecting users --- diff --git a/projects/sgauthstress/proto.cpp b/projects/sgauthstress/proto.cpp index b89681f6..d4624594 100644 --- a/projects/sgauthstress/proto.cpp +++ b/projects/sgauthstress/proto.cpp @@ -5,12 +5,21 @@ #include #include #include +#include #include "stg/common.h" #include "stg/ia_packets.h" #include "proto.h" +class HasIP : public std::unary_function, bool> { + public: + explicit HasIP(uint32_t i) : ip(i) {} + bool operator()(const std::pair & value) { return value.first == ip; } + private: + uint32_t ip; +}; + PROTO::PROTO(const std::string & server, uint16_t port, uint16_t localPort, @@ -111,38 +120,45 @@ if (pthread_join(tid, NULL)) return true; } -void PROTO::AddUser(const USER & user) +void PROTO::AddUser(const USER & user, bool connect) { - users.push_back(std::make_pair(user.GetIP(), user)); - struct pollfd pfd; - pfd.fd = user.GetSocket(); - pfd.events = POLLIN; - pfd.revents = 0; - pollFds.push_back(pfd); +users.push_back(std::make_pair(user.GetIP(), user)); +struct pollfd pfd; +pfd.fd = user.GetSocket(); +pfd.events = POLLIN; +pfd.revents = 0; +pollFds.push_back(pfd); + +users.back().second.InitNetwork(); + +if (connect) + { + RealConnect(&users.back().second); + } } bool PROTO::Connect(uint32_t ip) { -/*std::vector >::const_iterator it; -it = users.find(ip); +std::vector >::iterator it; +it = std::find_if(users.begin(), users.end(), HasIP(ip)); if (it == users.end()) - return false;*/ + return false; // Do something -return true; +return RealConnect(&it->second); } bool PROTO::Disconnect(uint32_t ip) { -/*std::vector >::const_iterator it; -it = users.find(ip); +std::vector >::iterator it; +it = std::find_if(users.begin(), users.end(), HasIP(ip)); if (it == users.end()) - return false;*/ + return false; // Do something -return true; +return RealDisconnect(&it->second); } void PROTO::Run() @@ -235,6 +251,8 @@ user->SetAliveTimeout(aliveTimeout); user->SetUserTimeout(userTimeout); user->SetRnd(rnd); +printfd(__FILE__, "PROTO::CONN_SYN_ACK_Proc() - user '%s' successfully logged in from IP %s\n", user->GetLogin().c_str(), inet_ntostring(user->GetIP()).c_str()); + return true; } @@ -468,7 +486,7 @@ if (res < 0) errorStr = "Failed to send packet: '"; errorStr += strerror(errno); errorStr += "'"; - printfd(__FILE__, "PROTO::SendPacket() - %s\n", errorStr.c_str()); + printfd(__FILE__, "PROTO::SendPacket() - %s, fd: %d\n", errorStr.c_str(), user->GetSocket()); return false; } @@ -481,3 +499,28 @@ if (res < sizeof(buffer)) return true; } + +bool PROTO::RealConnect(USER * user) +{ +if (user->GetPhase() != 1 && + user->GetPhase() != 5) + { + errorStr = "Unexpected connect"; + printfd(__FILE__, "PROTO::RealConnect() - wrong phase: %d\n", user->GetPhase()); + } +user->SetPhase(2); + +return Send_CONN_SYN(user); +} + +bool PROTO::RealDisconnect(USER * user) +{ +if (user->GetPhase() != 3) + { + errorStr = "Unexpected disconnect"; + printfd(__FILE__, "PROTO::RealDisconnect() - wrong phase: %d\n", user->GetPhase()); + } +user->SetPhase(4); + +return Send_DISCONN_SYN(user); +} diff --git a/projects/sgauthstress/proto.h b/projects/sgauthstress/proto.h index 05fe4722..a63a4f90 100644 --- a/projects/sgauthstress/proto.h +++ b/projects/sgauthstress/proto.h @@ -31,7 +31,7 @@ class PROTO { const std::string GetStrError() const { return errorStr; } - void AddUser(const USER & user); + void AddUser(const USER & user, bool connect = false); bool Connect(uint32_t ip); bool Disconnect(uint32_t ip); @@ -74,6 +74,9 @@ class PROTO { bool Send_DISCONN_SYN(USER * user); bool Send_DISCONN_ACK(USER * user); bool Send_ALIVE_ACK(USER * user); + + bool RealConnect(USER * user); + bool RealDisconnect(USER * user); }; #endif