Skip to content

Commit

Permalink
fix error after merge
Browse files Browse the repository at this point in the history
  • Loading branch information
gukj-spel committed Aug 14, 2024
1 parent 1acf057 commit c012159
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
17 changes: 7 additions & 10 deletions src/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace pikiwidb {

const ClientInfo ClientInfo::invalidClientInfo = {-1, "", -1, -1};
const ClientInfo ClientInfo::invalidClientInfo = {-1, "", -1};

void CmdRes::RedisAppendLen(std::string& str, int64_t ori, const std::string& prefix) {
str.append(prefix);
Expand Down Expand Up @@ -453,15 +453,15 @@ void PClient::OnConnect() {

std::string PClient::PeerIP() const {
if (!addr_.IsValid()) {
ERROR("Invalid address detected for client {}", uniqueID());
ERROR("Invalid address detected for client {}", GetUniqueID());
return "";
}
return addr_.GetIP();
}

int PClient::PeerPort() const {
if (!addr_.IsValid()) {
ERROR("Invalid address detected for client {}", uniqueID());
ERROR("Invalid address detected for client {}", GetUniqueID());
return 0;
}
return addr_.GetPort();
Expand Down Expand Up @@ -508,13 +508,10 @@ bool PClient::isClusterCmdTarget() const {
return PRAFT.GetClusterCmdCtx().GetPeerIp() == PeerIP() && PRAFT.GetClusterCmdCtx().GetPort() == PeerPort();
}

uint64_t PClient::uniqueID() const { return GetConnId(); }
uint64_t PClient::GetUniqueID() const { return GetConnId(); }

ClientInfo PClient::GetClientInfo() const {
if (auto c = getTcpConnection(); c) {
return {GetUniqueId(), PeerIP().c_str(), PeerPort(), GetFd()};
}
return ClientInfo::invalidClientInfo;
return {GetUniqueID(), PeerIP().c_str(), PeerPort()};
}

bool PClient::Watch(int dbno, const std::string& key) {
Expand All @@ -524,12 +521,12 @@ bool PClient::Watch(int dbno, const std::string& key) {

bool PClient::NotifyDirty(int dbno, const std::string& key) {
if (IsFlagOn(kClientFlagDirty)) {
INFO("client is already dirty {}", GetUniqueId());
INFO("client is already dirty {}", GetUniqueID());
return true;
}

if (watch_keys_[dbno].contains(key)) {
INFO("{} client become dirty because key {} in db {}", GetUniqueId(), key, dbno);
INFO("{} client become dirty because key {} in db {}", GetUniqueID(), key, dbno);
SetFlag(kClientFlagDirty);
return true;
} else {
Expand Down
7 changes: 3 additions & 4 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ class DB;
struct PSlaveInfo;

struct ClientInfo {
int client_id;
int64_t client_id;
std::string ip;
int port;
int fd;
static const ClientInfo invalidClientInfo;
bool operator==(ClientInfo& ci) const { return client_id == ci.client_id; }
};

class PClient : public std::enable_shared_from_this<PClient>, public CmdRes {
public:
// PClient() = delete;
Expand All @@ -137,7 +137,6 @@ class PClient : public std::enable_shared_from_this<PClient>, public CmdRes {
std::string PeerIP() const;
int PeerPort() const;
const int GetFd() const;
int GetUniqueId() const;
ClientInfo GetClientInfo() const;

// bool SendPacket(const std::string& buf);
Expand Down Expand Up @@ -227,6 +226,7 @@ class PClient : public std::enable_shared_from_this<PClient>, public CmdRes {

void SetAuth() { auth_ = true; }
bool GetAuth() const { return auth_; }
uint64_t GetUniqueID() const;
void RewriteCmd(std::vector<std::string>& params) { parser_.SetParams(params); }
void Reexecutecommand() { this->executeCommand(); }

Expand Down Expand Up @@ -254,7 +254,6 @@ class PClient : public std::enable_shared_from_this<PClient>, public CmdRes {
int processInlineCmd(const char*, size_t, std::vector<std::string>&);
void reset();
bool isPeerMaster() const;
uint64_t uniqueID() const;

bool isClusterCmdTarget() const;

Expand Down
10 changes: 5 additions & 5 deletions src/cmd_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ CmdClientId::CmdClientId(const std::string& name, int16_t arity)

bool CmdClientId::DoInitial(PClient* client) { return true; }

void CmdClientId::DoCmd(PClient* client) { client->AppendInteger(client->GetUniqueId()); }
void CmdClientId::DoCmd(PClient* client) { client->AppendInteger(client->GetUniqueID()); }

CmdClientKill::CmdClientKill(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsAdmin, kAclCategoryAdmin) {}
Expand Down Expand Up @@ -573,8 +573,8 @@ void CmdClientList::DoCmd(PClient* client) {
char buf[128];
for (auto& client_info : client_infos) {
// client->
snprintf(buf, sizeof(buf), "ID=%d IP=%s PORT=%d FD=%d\n", client_info.client_id, client_info.ip.c_str(),
client_info.port, client_info.fd);
snprintf(buf, sizeof(buf), "ID=%ld IP=%s PORT=%d\n", client_info.client_id, client_info.ip.c_str(),
client_info.port);
client->AppendString(std::string(buf));
}
break;
Expand All @@ -591,8 +591,8 @@ void CmdClientList::DoCmd(PClient* client) {
return;
}
char buf[128];
snprintf(buf, sizeof(buf), "ID=%d IP=%s PORT=%d FD=%d\n", client_info.client_id, client_info.ip.c_str(),
client_info.port, client_info.fd);
snprintf(buf, sizeof(buf), "ID=%ld IP=%s PORT=%d\n", client_info.client_id, client_info.ip.c_str(),
client_info.port);
client->AppendString(std::string(buf));
} catch (const std::exception& e) {
client->SetRes(CmdRes::kErrOther, "Invalid client id");
Expand Down
4 changes: 3 additions & 1 deletion src/pikiwidb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void PikiwiDB::OnNewConnection(uint64_t connId, std::shared_ptr<pikiwidb::PClien
client->SetSocketAddr(addr);
client->OnConnect();
// add new PClient to clients
ClientMap::getInstance().AddClient(client->GetUniqueId(), client);
ClientMap::getInstance().AddClient(client->GetUniqueID(), client);
}

bool PikiwiDB::Init() {
Expand Down Expand Up @@ -159,6 +159,7 @@ bool PikiwiDB::Init() {
event_server_->SetOnCreate([](uint64_t connID, std::shared_ptr<PClient>& client, const net::SocketAddr& addr) {
client->SetSocketAddr(addr);
client->OnConnect();
ClientMap::getInstance().AddClient(client->GetUniqueID(), client);
INFO("New connection from fd:{} IP:{} port:{}", connID, addr.GetIP(), addr.GetPort());
});

Expand All @@ -169,6 +170,7 @@ bool PikiwiDB::Init() {
event_server_->SetOnClose([](std::shared_ptr<PClient>& client, std::string&& msg) {
INFO("Close connection id:{} msg:{}", client->GetConnId(), msg);
client->OnClose();
ClientMap::getInstance().RemoveClientById(client->GetUniqueID());
});

event_server_->InitTimer(10);
Expand Down

0 comments on commit c012159

Please sign in to comment.