Skip to content
This repository has been archived by the owner on May 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #204 from StevenC8661476/main
Browse files Browse the repository at this point in the history
Fix a bug about cauculating end position of bullet
  • Loading branch information
futrime authored May 20, 2024
2 parents fbd5d77 + 25b6d93 commit fc661a4
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,6 @@ private Task CreateTaskForParsingMessage(Guid socketId)
_logger.Debug($"{ex}");
}
}
});
}, cts.Token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,96 +7,121 @@ public partial class AgentServer
{
private void AddSocket(Guid socketId, IWebSocketConnection socket)
{
_sockets.TryAdd(socketId, socket);

_socketRawTextReceivingQueue.AddOrUpdate(
socketId,
new ConcurrentQueue<string>(),
(key, oldValue) => new ConcurrentQueue<string>()
);

_socketMessageSendingQueue.AddOrUpdate(
socketId,
new ConcurrentQueue<Message>(),
(key, oldValue) => new ConcurrentQueue<Message>()
);

// Cancel the previous task if it exists
_ctsForParsingMessage.TryGetValue(socketId, out CancellationTokenSource? ctsForParsingMessage);
ctsForParsingMessage?.Cancel();
_ctsForParsingMessage.TryRemove(socketId, out _);

_ctsForSendingMessage.TryGetValue(socketId, out CancellationTokenSource? ctsForSendingMessage);
ctsForSendingMessage?.Cancel();
_ctsForSendingMessage.TryRemove(socketId, out _);

// Create new tasks for parsing and sending messages
Task parsingTask = CreateTaskForParsingMessage(socketId);
parsingTask.Start();

_tasksForParsingMessage.AddOrUpdate(
socket.ConnectionInfo.Id,
parsingTask,
(key, oldValue) =>
{
oldValue?.Dispose();
return parsingTask;
}
);

Task sendingTask = CreateTaskForSendingMessage(socketId);
sendingTask.Start();

_tasksForSendingMessage.AddOrUpdate(
socketId,
sendingTask,
(key, oldValue) =>
{
oldValue?.Dispose();
return sendingTask;
}
);
try
{
_sockets.TryAdd(socketId, socket);

_socketRawTextReceivingQueue.AddOrUpdate(
socketId,
new ConcurrentQueue<string>(),
(key, oldValue) => new ConcurrentQueue<string>()
);

_socketMessageSendingQueue.AddOrUpdate(
socketId,
new ConcurrentQueue<Message>(),
(key, oldValue) => new ConcurrentQueue<Message>()
);

// Cancel the previous task if it exists
_ctsForParsingMessage.TryGetValue(socketId, out CancellationTokenSource? ctsForParsingMessage);
ctsForParsingMessage?.Cancel();
_ctsForParsingMessage.TryRemove(socketId, out _);

_ctsForSendingMessage.TryGetValue(socketId, out CancellationTokenSource? ctsForSendingMessage);
ctsForSendingMessage?.Cancel();
_ctsForSendingMessage.TryRemove(socketId, out _);

// Create new tasks for parsing and sending messages
Task parsingTask = CreateTaskForParsingMessage(socketId);
parsingTask.Start();

_tasksForParsingMessage.AddOrUpdate(
socket.ConnectionInfo.Id,
parsingTask,
(key, oldValue) =>
{
oldValue?.Dispose();
return parsingTask;
}
);

Task sendingTask = CreateTaskForSendingMessage(socketId);
sendingTask.Start();

_tasksForSendingMessage.AddOrUpdate(
socketId,
sendingTask,
(key, oldValue) =>
{
oldValue?.Dispose();
return sendingTask;
}
);
}
catch (Exception ex)
{
_logger.Error($"Failed to add {GetAddress(socket)}: {ex.Message}");
_logger.Debug($"{ex}");
}
}

private void RemoveSocket(Guid socketId)
{
_sockets.TryRemove(socketId, out _);
_socketTokens.TryRemove(socketId, out _);

_ctsForParsingMessage.TryGetValue(socketId, out CancellationTokenSource? ctsForParsingMessage);
ctsForParsingMessage?.Cancel();
_ctsForParsingMessage.TryRemove(socketId, out _);
try
{
_sockets.TryRemove(socketId, out _);
_socketTokens.TryRemove(socketId, out _);

_ctsForSendingMessage.TryGetValue(socketId, out CancellationTokenSource? ctsForSendingMessage);
ctsForSendingMessage?.Cancel();
_ctsForSendingMessage.TryRemove(socketId, out _);
_ctsForParsingMessage.TryGetValue(socketId, out CancellationTokenSource? ctsForParsingMessage);
ctsForParsingMessage?.Cancel();
_ctsForParsingMessage.TryRemove(socketId, out _);

_tasksForParsingMessage.TryGetValue(socketId, out Task? taskForParsingMessage);
taskForParsingMessage?.Dispose();
_tasksForParsingMessage.TryRemove(socketId, out _);
_ctsForSendingMessage.TryGetValue(socketId, out CancellationTokenSource? ctsForSendingMessage);
ctsForSendingMessage?.Cancel();
_ctsForSendingMessage.TryRemove(socketId, out _);

_tasksForSendingMessage.TryGetValue(socketId, out Task? taskForSendingMessage);
taskForSendingMessage?.Dispose();
_tasksForSendingMessage.TryRemove(socketId, out _);
_tasksForParsingMessage.TryRemove(socketId, out _);
_tasksForSendingMessage.TryRemove(socketId, out _);

_socketMessageSendingQueue.TryRemove(socketId, out _);
_socketRawTextReceivingQueue.TryRemove(socketId, out _);
_socketMessageSendingQueue.TryRemove(socketId, out _);
_socketRawTextReceivingQueue.TryRemove(socketId, out _);
}
catch (Exception ex)
{
_logger.Error($"Failed to remove {GetAddress(socketId)}: {ex.Message}");
_logger.Debug($"{ex}");
}
}

private string GetAddress(IWebSocketConnection socket)
{
return $"{socket.ConnectionInfo.ClientIpAddress}: {socket.ConnectionInfo.ClientPort}";
try
{
return $"{socket.ConnectionInfo.ClientIpAddress}: {socket.ConnectionInfo.ClientPort}";
}
catch (Exception)
{
return "[UNKNOWN]";
}
}

private string GetAddress(Guid socketId)
{
if (_sockets.TryGetValue(socketId, out IWebSocketConnection? socket) && socket is not null)
try
{
return GetAddress(socket);
if (_sockets.TryGetValue(socketId, out IWebSocketConnection? socket) && socket is not null)
{
return GetAddress(socket);
}
else
{
return "[UNKNOWN]";
}
}
else
catch (Exception)
{
return "UNKNOWN";
return "[UNKNOWN]";
}
}
}
18 changes: 6 additions & 12 deletions server/src/GameServer/Connection/AgentServer/AgentServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,19 @@ private void StartWsServer()
if (_socketRawTextReceivingQueue[socket.ConnectionInfo.Id].Count
> MAXIMUM_MESSAGE_QUEUE_SIZE)
{
_logger.Debug(
$"Received too many messages from {GetAddress(socket)}."
_logger.Warning(
$"Received too many messages from {GetAddress(socket)}. Messages in queue will be cleared."
);
_logger.Debug("Messages in queue will be cleared.");
_socketRawTextReceivingQueue[socket.ConnectionInfo.Id].Clear();
}

_socketRawTextReceivingQueue[socket.ConnectionInfo.Id].Enqueue(text);
_logger.Debug(
$"Received text message from {GetAddress(socket)}."
);
_logger.Debug($"Received text message from {GetAddress(socket)}.");
_logger.Verbose(text.Length > 65536 ? string.Concat(text.AsSpan(0, 65536), "...") : text);
}
catch (Exception exception)
{
_logger.Error(
$"Failed to receive message from {GetAddress(socket)}: {exception.Message}"
);
_logger.Error($"Failed to receive message from {GetAddress(socket)}: {exception.Message}");
_logger.Debug($"{exception}");
}
};
Expand All @@ -159,10 +154,9 @@ private void StartWsServer()
if (_socketRawTextReceivingQueue[socket.ConnectionInfo.Id].Count
> MAXIMUM_MESSAGE_QUEUE_SIZE)
{
_logger.Debug(
$"Received too many messages from {GetAddress(socket)}."
_logger.Warning(
$"Received too many messages from {GetAddress(socket)}. Messages in queue will be cleared."
);
_logger.Debug("Messages in queue will be cleared.");
_socketRawTextReceivingQueue[socket.ConnectionInfo.Id].Clear();
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/GameServer/GameLogic/Map/Map.Geometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public Position GetRealEndPositon(
{
Position a = new(startPosition.x, startPosition.y);
Position b = new(expectedEndPosition.x, expectedEndPosition.y);
Position firstHitX = new(a.x, a.y);
Position firstHitY = new(a.x, a.y);
Position firstHitX = new(b.x, b.y);
Position firstHitY = new(b.x, b.y);

int deltaX = (direction.x > 0) ? 0 : -1;
int deltaY = (direction.y > 0) ? 0 : -1;
Expand Down

0 comments on commit fc661a4

Please sign in to comment.