Skip to content

Commit

Permalink
fix(chat): message publish logic
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojiuwo1993 committed Oct 11, 2023
1 parent 2eb9b0a commit 24d8c0c
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using UniSpy.Server.Core.Abstraction.Interface;
using UniSpy.Server.Chat.Abstraction.Interface;
using UniSpy.Server.Chat.Aggregate.Redis.Contract;
using System;
using UniSpy.Server.Chat.Aggregate.Redis;

namespace UniSpy.Server.Chat.Abstraction.BaseClass
{
Expand Down Expand Up @@ -33,7 +35,10 @@ protected override void RequestCheck()
{
throw new NoSuchChannelException($"No such channel {_request.ChannelName}", _request.ChannelName);
}
_user = _channel.GetUser(_client);
if (_user is null)
{
_user = _channel.GetUser(_client);
}
if (_user is null)
{
throw new NoSuchNickException($"Can not find user with nickname: {_client.Info.NickName} username: {_client.Info.UserName}");
Expand All @@ -55,7 +60,7 @@ public override void Handle()
/// <summary>
/// publish message to redis channel, only localclient can publish message
/// </summary>
protected void PublishMessage()
protected virtual void PublishMessage()
{
// we do not publish message when the message is received from remote client
if (_client.IsRemoteClient)
Expand All @@ -70,7 +75,20 @@ protected void PublishMessage()
{
return;
}
Aggregate.Channel.UpdateChannelCache(_user, _channel);

var key = new ChannelCache
{
ChannelName = _channel.Name,
GameName = _channel.GameName
};
using (var locker = new LinqToRedis.RedisLock(TimeSpan.FromSeconds(10), Application.StorageOperation.Persistance.ChannelCacheClient.Db, key))
{
if (locker.LockTake())
{
Aggregate.Channel.UpdateChannelCache(_user, _channel);
}
}

var msg = new RemoteMessage(_request, _client.GetRemoteClient());
_channel.Broker.PublishMessage(msg);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Newtonsoft.Json;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using UniSpy.Server.Chat.Abstraction.Interface;
using UniSpy.Server.Chat.Contract.Request.Channel;
Expand Down Expand Up @@ -212,7 +211,8 @@ public void MultiCast(IClient sender, IResponse message, bool isSkipSender = fal
var users = Users.Values.Where(u => !u.IsRemoteClient).ToList();
foreach (var user in users)
{
if (isSkipSender)
if (isSkipSender
&& sender.Connection.RemoteIPEndPoint.Equals(user.RemoteIPEndPoint))
{
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Servers/Chat/src/Aggregate/Redis/RedisChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public ChannelMessageBroker(string chatChannelName) : base($"{RedisChannelName.C

public override void ReceivedMessage(RemoteMessage message)
{
// we are uint testing
if (ServerLauncher.Server is null)
// we are uint testing so we skip publish message here
if (ServerLauncher.ServerInstances.Count == 0)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ protected override void ResponseConstruct()
{
_response = new GetCKeyResponse(_request, _result);
}

/// <summary>
/// We do not publish message in GetCKey
/// </summary>
protected override void PublishMessage() { }
private void WaittingForKey(ChannelUser user)
{
// if user did not contains all key and value we wait for it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ protected override void DataOperation()
_result.Values = _channel.KeyValues.GetValueString(_request.Keys);
_result.ChannelName = _channel.Name;
}

/// <summary>
/// We do not publish message in GetChanKey
/// </summary>
protected override void PublishMessage() { }
protected override void ResponseConstruct()
{
_response = new GetChannelKeyResponse(_request, _result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ protected override void Response()
{
ChannelName = _request.ChannelName
};
new NamesHandler(_client, namesRequest).Handle();

var nameHandler = new NamesHandler(_client, namesRequest, _channel, _user);
nameHandler.Handle();
var userModeRequest = new ModeRequest
{
RequestType = ModeRequestType.GetChannelAndUserModes,
Expand All @@ -136,7 +136,8 @@ protected override void Response()
UserName = _user.Client.Info.UserName,
Password = _request.Password is null ? null : _request.Password
};
new ModeHandler(_client, userModeRequest).Handle();
var modeHandler = new ModeHandler(_client, userModeRequest, _channel, _user);
modeHandler.Handle();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ protected override void Response()
{
_channel.MultiCast(_client, _response);
_channel.RemoveUser(_kickee);
Aggregate.Channel.UpdateChannelCache(_user,_channel);
// Aggregate.Channel.UpdatePeerRoomInfo(_user);
}
}
}
13 changes: 12 additions & 1 deletion src/Servers/Chat/src/Handler/CmdHandler/Channel/ModeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public sealed class ModeHandler : ChannelHandlerBase
private new ModeRequest _request => (ModeRequest)base._request;
private new ModeResult _result { get => (ModeResult)base._result; set => base._result = value; }
public ModeHandler(IShareClient client, ModeRequest request) : base(client, request) { }
public ModeHandler(IShareClient client, ModeRequest request, Aggregate.Channel channel, Aggregate.ChannelUser user) : base(client, request)
{
_user = user;
_channel = channel;
}
protected override void DataOperation()
{
_result = new ModeResult();
Expand All @@ -33,7 +38,13 @@ protected override void DataOperation()
break;
}
}

protected override void PublishMessage()
{
if (_request.RequestType == ModeRequestType.SetChannelModes)
{
base.PublishMessage();
}
}
protected override void ResponseConstruct()
{
// we only response to get channel modes
Expand Down
27 changes: 9 additions & 18 deletions src/Servers/Chat/src/Handler/CmdHandler/Channel/NamesHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using UniSpy.Server.Chat.Abstraction.BaseClass;
using UniSpy.Server.Chat.Error.IRC.Channel;
using UniSpy.Server.Chat.Error.IRC.General;
using UniSpy.Server.Chat.Contract.Request.General;
using UniSpy.Server.Chat.Contract.Response.Channel;
using UniSpy.Server.Chat.Contract.Result.Channel;
Expand All @@ -14,31 +12,24 @@ public sealed class NamesHandler : ChannelHandlerBase
private new NamesRequest _request => (NamesRequest)base._request;
private new NamesResult _result { get => (NamesResult)base._result; set => base._result = value; }
public NamesHandler(IShareClient client, NamesRequest request) : base(client, request) { }
protected override void RequestCheck()
public NamesHandler(IShareClient client, NamesRequest request, Aggregate.Channel channel, Aggregate.ChannelUser user) : base(client, request)
{
if (_request.RawRequest is null)
{
_channel = _client.Info.GetLocalJoinedChannel(_request.ChannelName);
if (_channel is null)
{
throw new NoSuchChannelException($"No such channel {_request.ChannelName}", _request.ChannelName);
}
_user = _channel.GetUser(_client);
if (_user is null)
{
throw new NoSuchNickException($"Can not find user with nickname: {_client.Info.NickName} username: {_client.Info.UserName}");
}
return;
}
base.RequestCheck();
_user = user;
_channel = channel;
}

protected override void DataOperation()
{
_result = new NamesResult();
_result.AllChannelUserNicks = _channel.GetAllUsersNickString();
_result.ChannelName = _channel.Name;
_result.RequesterNickName = _user.Client.Info.NickName;
}

/// <summary>
/// We do not publish message in names handler
/// </summary>
protected override void PublishMessage() { }
protected override void ResponseConstruct()
{
_response = new NamesResponse(_request, _result);
Expand Down
17 changes: 13 additions & 4 deletions src/Servers/Chat/src/Handler/CmdHandler/Channel/PartHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ protected override void DataOperation()
case PeerRoomType.Normal:
case PeerRoomType.Staging:
Aggregate.Channel.RemoveLocalChannel(_channel);
Aggregate.Channel.RemoveChannelCache(_user, _channel);
break;
}
foreach (var user in _channel.Users.Values)
Expand All @@ -79,12 +78,22 @@ protected override void DataOperation()
default:
// we need always remove the connection in leaver and channel
_channel.RemoveUser(_user);
Aggregate.Channel.UpdateChannelCache(_user, _channel);
// Aggregate.Channel.UpdatePeerRoomInfo(_user);
break;
}
}

protected override void PublishMessage()
{
switch (_channel.RoomType)
{
case PeerRoomType.Normal:
case PeerRoomType.Staging:
Aggregate.Channel.RemoveChannelCache(_user, _channel);
break;
default:
base.PublishMessage();
break;
}
}
protected override void ResponseConstruct()
{
_response = new PartResponse(_request, _result);
Expand Down
5 changes: 0 additions & 5 deletions src/Servers/Chat/test/Game/GameTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ public void TcpMessageSplitingTest()
{
((ITestClient)client).TestReceived(raw);
}
// Given

// When

// Then
}
[Fact]
public void Worms3dTest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public sealed class InitHandler : CmdHandlerBase
/// <summary>
/// Local NatInitInfo storage, after all init packets are received we send all into redis database
/// </summary>
private InitPacketCache _addressInfo;
private InitPacketCache _cache;
public InitHandler(Client client, InitRequest request) : base(client, request)
{
_result = new InitResult();
}
protected override void DataOperation()
{
_addressInfo = new InitPacketCache()
_cache = new InitPacketCache()
{
ServerID = _client.Server.Id,
Cookie = (uint)_request.Cookie,
Expand All @@ -38,7 +38,7 @@ protected override void DataOperation()
PublicIPEndPoint = _client.Connection.RemoteIPEndPoint,
PrivateIPEndPoint = _request.PrivateIPEndPoint
};
_client.LogInfo($"Received init request with private ip: [{_addressInfo.PrivateIPEndPoint}], cookie: {_addressInfo.Cookie}, client index: {_addressInfo.ClientIndex}.");
_client.LogInfo($"Received init request with private ip: [{_cache.PrivateIPEndPoint}], cookie: {_cache.Cookie}, client index: {_cache.ClientIndex}.");
_result.RemoteIPEndPoint = _client.Connection.RemoteIPEndPoint;
}
protected override void ResponseConstruct()
Expand All @@ -62,8 +62,7 @@ protected override void Response()
}

// todo make the code do not block and redis do not have thread theaf problem
// Task.Run(() => StorageOperation.Persistance.UpdateInitInfo(_addressInfo));
StorageOperation.Persistance.UpdateInitInfo(_addressInfo);
StorageOperation.Persistance.UpdateInitInfo(_cache);
// init packet nn3 is the last one client send, although receiving nn3 does not mean we received other init packets, but we can use this as a flag to prevent start multiple connect handler
switch (_request.Version)
{
Expand Down

0 comments on commit 24d8c0c

Please sign in to comment.