Skip to content

Commit

Permalink
refactor(chat): distribution logic
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojiuwo1993 committed Oct 18, 2023
1 parent e267ef7 commit 0b7a17a
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,21 @@ public override void Handle()
base.Handle();
try
{
// we do not publish message when the message is received from remote client
if (_client.IsRemoteClient)
{
return;
}
if (_channel is null)
{
return;
}
if (_request.RawRequest is null)
{
return;
}
PublishMessage();
UpdateChannelCache();
}
catch (Exception ex)
{
Expand All @@ -62,35 +76,26 @@ public override void Handle()
/// </summary>
protected virtual void PublishMessage()
{
// we do not publish message when the message is received from remote client
if (_client.IsRemoteClient)
{
return;
}
if (_channel is null)
{
return;
}
if (_request.RawRequest is null)
{
return;
}

var msg = new RemoteMessage(_request, _client.GetRemoteClient());
_channel.Broker.PublishMessage(msg);
}

protected virtual void UpdateChannelCache()
{
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,9 @@ protected override void Response()
break;
}
}
protected override void UpdateChannelCache()
{
// we do nothing here, when there is a channel message
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public interface IStorageOperation
Channel GetChannel(ChannelCache key);
void UpdateChannel(Channel channel);
void RemoveChannel(Channel channel);
void UpdateClient(Client client);
void RemoveClient(Client client);
void UpdateClient(IShareClient client);
void RemoveClient(IShareClient client);
bool IsClientExist(ClientInfoCache key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ public sealed partial class Channel
/// </summary>
[JsonIgnore]
public static readonly ConcurrentDictionary<string, Channel> LocalChannels = new();
[JsonIgnore]
public static readonly ConcurrentDictionary<string, ChannelMessageBroker> MessageBrokers = new();
/// <summary>
/// You need to manually check channel existance then get channel
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static Channel GetLocalChannel(string name)
{
LocalChannels.TryGetValue(name, out var channel);
Expand Down
20 changes: 10 additions & 10 deletions src/Servers/Chat/src/Application/Client.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Threading.Tasks;
using System;
using UniSpy.Server.Chat.Abstraction.Interface;
using UniSpy.Server.Chat.Aggregate;
using UniSpy.Server.Chat.Aggregate.Misc;
Expand All @@ -8,6 +8,7 @@
using UniSpy.Server.Core.Abstraction.BaseClass;
using UniSpy.Server.Core.Abstraction.Interface;
using UniSpy.Server.Core.Encryption;
using UniSpy.Server.Core.Extension;
using UniSpy.Server.Core.Logging;

namespace UniSpy.Server.Chat.Application
Expand All @@ -29,22 +30,21 @@ public Client(IConnection connection, IServer server, ClientInfo info) : this(co
Info = info;
_remoteClient = new RemoteClient(this);
}

protected override void EventBinding()
{
base.EventBinding();
_timer = new EasyTimer(TimeSpan.FromMinutes(1));

Check failure on line 36 in src/Servers/Chat/src/Application/Client.cs

View workflow job for this annotation

GitHub Actions / unispy (ubuntu-latest)

There is no argument given that corresponds to the required formal parameter 'intervalTimeSpan' of 'EasyTimer.EasyTimer(TimeSpan, TimeSpan)'

Check failure on line 36 in src/Servers/Chat/src/Application/Client.cs

View workflow job for this annotation

GitHub Actions / unispy (ubuntu-latest)

There is no argument given that corresponds to the required formal parameter 'intervalTimeSpan' of 'EasyTimer.EasyTimer(TimeSpan, TimeSpan)'
_timer.Elapsed += (s, e) => Application.StorageOperation.Persistance.UpdateClient(this);
_timer.Start();
}
protected override void OnReceived(object buffer)
{
var message = DecryptMessage((byte[])buffer);
if (_bufferCache.ProcessBuffer(message, out var completeBuffer))
{
this.LogNetworkReceiving(completeBuffer);
var switcher = CreateSwitcher(completeBuffer);
if (System.Diagnostics.Debugger.IsAttached)
{
switcher.Handle();
}
else
{
Task.Run(() => switcher.Handle());
}
switcher.Handle();
}
}
protected override void OnDisconnected()
Expand Down
2 changes: 0 additions & 2 deletions src/Servers/Chat/src/Application/ClientManager.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Net;
using System.Collections.Generic;
using System.Linq;
using UniSpy.Server.Core.Abstraction.BaseClass;
using UniSpy.Server.Chat.Abstraction.Interface;
using UniSpy.Server.Core.Abstraction.Interface;

namespace UniSpy.Server.Chat.Application
{
Expand Down
4 changes: 2 additions & 2 deletions src/Servers/Chat/src/Application/StorageOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void RemoveChannel(Channel channel)
ChannelCacheClient.DeleteKeyValue(data);
}

public void UpdateClient(Client client)
public void UpdateClient(IShareClient client)
{
var data = new ClientInfoCache
{
Expand All @@ -123,7 +123,7 @@ public void UpdateClient(Client client)
ClientCacheClient.SetValue(data);
}

public void RemoveClient(Client client)
public void RemoveClient(IShareClient client)
{
var data = new ClientInfoCache
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected override void ResponseConstruct()
protected override void Response()
{
base.Response();
if (!_client.IsRemoteClient)
if (!_client.IsRemoteClient && _client.GetType() == typeof(Client))
{
((Client)_client).Crypto = _crypto;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected override void DataOperation()
if (!Application.StorageOperation.Persistance.IsClientExist(key))
{
_client.Info.NickName = _request.NickName;
Application.StorageOperation.Persistance.UpdateClient((Client)_client);
Application.StorageOperation.Persistance.UpdateClient(_client);
}
else
{
Expand Down

0 comments on commit 0b7a17a

Please sign in to comment.