Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unexpected behavior on connection send and disconnect #408

Merged
merged 1 commit into from
Jun 6, 2022
Merged
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
21 changes: 14 additions & 7 deletions src/server/network/protocol/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class Protocol : public std::enable_shared_from_this<Protocol>
{
public:
explicit Protocol(Connection_ptr initConnection) : connection(initConnection) {}
explicit Protocol(Connection_ptr initConnection) : connectionPtr(initConnection) {}
virtual ~Protocol();

// non-copyable
Expand All @@ -44,11 +44,11 @@ class Protocol : public std::enable_shared_from_this<Protocol>
virtual void onConnect() {}

bool isConnectionExpired() const {
return connection.expired();
return connectionPtr.expired();
}

Connection_ptr getConnection() const {
return connection.lock();
return connectionPtr.lock();
}

uint32_t getIP() const;
Expand All @@ -61,12 +61,18 @@ class Protocol : public std::enable_shared_from_this<Protocol>
}

void send(OutputMessage_ptr msg) const {
getConnection()->send(msg);
if (auto connection = getConnection();
connection != nullptr) {
connection->send(msg);
}
}

protected:
void disconnect() const {
getConnection()->close();
if (auto connection = getConnection();
connection != nullptr) {
connection->close();
}
}
void enableXTEAEncryption() {
encryptionEnabled = true;
Expand All @@ -92,19 +98,20 @@ class Protocol : public std::enable_shared_from_this<Protocol>
bool XTEA_decrypt(NetworkMessage& msg) const;
bool compression(OutputMessage& msg) const;

friend class Connection;

OutputMessage_ptr outputBuffer;
std::unique_ptr<z_stream> defStream;

const ConnectionWeak_ptr connection;
const ConnectionWeak_ptr connectionPtr;
std::array<uint32_t, 4> key = {};
uint32_t serverSequenceNumber = 0;
uint32_t clientSequenceNumber = 0;
std::underlying_type_t<ChecksumMethods_t> checksumMethod = CHECKSUM_METHOD_NONE;
bool encryptionEnabled = false;
bool rawMessages = false;
bool compreesionEnabled = false;

friend class Connection;
};

#endif // SRC_SERVER_NETWORK_PROTOCOL_PROTOCOL_H_