Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class TClient final {
[[nodiscard]] const ip::tcp::socket& GetTCPSock() const { return mSocket; }
[[nodiscard]] std::string GetRoles() const { return mRole; }
[[nodiscard]] std::string GetName() const { return mName; }
[[nodiscard]] const std::string& GetIP() const { return mIP; }
void SetUnicycleID(int ID) { mUnicycleID = ID; }
void SetID(int ID) { mID = ID; }
[[nodiscard]] int GetOpenCarID() const;
Expand Down Expand Up @@ -121,6 +122,7 @@ class TClient final {
TSetOfVehicleData mVehicleData;
SparseArray<std::string> mVehiclePosition;
std::string mName = "Unknown Client";
std::string mIP;
ip::tcp::socket mSocket;
ip::udp::endpoint mUDPAddress {};
int mUnicycleID = -1;
Expand Down
5 changes: 5 additions & 0 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ TClient::TClient(TServer& Server, ip::tcp::socket&& Socket)
: mServer(Server)
, mSocket(std::move(Socket))
, mLastPingTime(std::chrono::high_resolution_clock::now()) {
boost::system::error_code ec;
auto ep = mSocket.remote_endpoint(ec);
if (!ec) {
mIP = ep.address().to_string();
}
Comment on lines +152 to +156
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why the error code isn't handled here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'ec' beeing the error code I don't see what you're saying, could you please explain ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is an error the ip is skipped and then the client construction is finished. However if the remote_endpoint call failed I don't see why we'd need to keep the client. The error also isn't logged making things harder to debug.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, well this is something I did not think about, do you want me to change it ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be preferred.

}

TClient::~TClient() {
Expand Down
17 changes: 9 additions & 8 deletions src/TNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,15 +661,16 @@ void TNetwork::DisconnectClient(const std::weak_ptr<TClient> &c, const std::stri
void TNetwork::DisconnectClient(TClient &c, const std::string &R)
{
if (c.IsDisconnected()) return;
std::string ClientIP = c.GetTCPSock().remote_endpoint().address().to_string();
mClientMapMutex.lock();
if (mClientMap[ClientIP] > 0) {
mClientMap[ClientIP]--;
}
if (mClientMap[ClientIP] == 0) {
mClientMap.erase(ClientIP);
const std::string& ClientIP = c.GetIP();
if (!ClientIP.empty()) {
std::lock_guard<std::mutex> lock(mClientMapMutex);
if (mClientMap[ClientIP] > 0) {
mClientMap[ClientIP]--;
}
if (mClientMap[ClientIP] == 0) {
mClientMap.erase(ClientIP);
}
}
mClientMapMutex.unlock();
c.Disconnect(R);
}

Expand Down
Loading