Skip to content

Commit

Permalink
Prevent inviting an IP address twice, which may cause some issues.
Browse files Browse the repository at this point in the history
Also fixes a memory leak of unconnected connections.
  • Loading branch information
IntegratedQuantum committed May 28, 2024
1 parent 9d432fb commit ae0972c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/network.zig
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,12 @@ pub const ConnectionManager = struct {
}
}

pub fn addConnection(self: *ConnectionManager, conn: *Connection) void {
pub fn addConnection(self: *ConnectionManager, conn: *Connection) error{AlreadyConnected}!void {
self.mutex.lock();
defer self.mutex.unlock();

for(self.connections.items) |other| {
if(other.remoteAddress.ip == conn.remoteAddress.ip and other.remoteAddress.port == conn.remoteAddress.port) return error.AlreadyConnected;
}
self.connections.append(conn);
}

Expand Down Expand Up @@ -1324,7 +1326,7 @@ pub const Connection = struct {
break :blk settings.defaultPort;
};

result.manager.addConnection(result);
try result.manager.addConnection(result);
return result;
}

Expand Down
6 changes: 3 additions & 3 deletions src/server/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ fn init(name: []const u8) void {
}

fn deinit() void {
for(users.items) |user| {
user.decreaseRefCount();
}
users.clearAndFree();
for(userDeinitList.items) |user| {
user.deinit();
}
userDeinitList.clearAndFree();
for(connectionManager.connections.items) |conn| {
conn.user.?.decreaseRefCount();
}
connectionManager.deinit();
connectionManager = undefined;

Expand Down

0 comments on commit ae0972c

Please sign in to comment.