Skip to content

Commit

Permalink
Bla
Browse files Browse the repository at this point in the history
  • Loading branch information
RenNagasaki committed Sep 3, 2024
1 parent 542673e commit fd449f4
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 93 deletions.
11 changes: 11 additions & 0 deletions Echosync-Server/Echosync-Server.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Echosync_Server</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
2 changes: 2 additions & 0 deletions Echosync-Server/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
16 changes: 16 additions & 0 deletions Echosync.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,32 @@ VisualStudioVersion = 17.10.34916.146
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Echosync", "Echosync\Echosync.csproj", "{13C812E9-0D42-4B95-8646-40EEBF30636F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Echosync-Server", "Echosync-Server\Echosync-Server.csproj", "{879FC7FB-5B24-4775-BC34-073A30C95A57}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{13C812E9-0D42-4B95-8646-40EEBF30636F}.Debug|Any CPU.ActiveCfg = Debug|x64
{13C812E9-0D42-4B95-8646-40EEBF30636F}.Debug|Any CPU.Build.0 = Debug|x64
{13C812E9-0D42-4B95-8646-40EEBF30636F}.Debug|x64.ActiveCfg = Debug|x64
{13C812E9-0D42-4B95-8646-40EEBF30636F}.Debug|x64.Build.0 = Debug|x64
{13C812E9-0D42-4B95-8646-40EEBF30636F}.Release|Any CPU.ActiveCfg = Release|x64
{13C812E9-0D42-4B95-8646-40EEBF30636F}.Release|Any CPU.Build.0 = Release|x64
{13C812E9-0D42-4B95-8646-40EEBF30636F}.Release|x64.ActiveCfg = Release|x64
{13C812E9-0D42-4B95-8646-40EEBF30636F}.Release|x64.Build.0 = Release|x64
{879FC7FB-5B24-4775-BC34-073A30C95A57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{879FC7FB-5B24-4775-BC34-073A30C95A57}.Debug|Any CPU.Build.0 = Debug|Any CPU
{879FC7FB-5B24-4775-BC34-073A30C95A57}.Debug|x64.ActiveCfg = Debug|Any CPU
{879FC7FB-5B24-4775-BC34-073A30C95A57}.Debug|x64.Build.0 = Debug|Any CPU
{879FC7FB-5B24-4775-BC34-073A30C95A57}.Release|Any CPU.ActiveCfg = Release|Any CPU
{879FC7FB-5B24-4775-BC34-073A30C95A57}.Release|Any CPU.Build.0 = Release|Any CPU
{879FC7FB-5B24-4775-BC34-073A30C95A57}.Release|x64.ActiveCfg = Release|Any CPU
{879FC7FB-5B24-4775-BC34-073A30C95A57}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 3 additions & 1 deletion Echosync/DataClasses/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public static class Constants
public static readonly Vector4 INFOLOGCOLOR = new Vector4(.3f, 1.0f, 1.0f, 1f);
public static readonly Vector4 DEBUGLOGCOLOR = new Vector4(0.0f, 1.0f, 0.0f, 1f);
public static readonly Vector4 ERRORLOGCOLOR = new Vector4(1.0f, 0.0f, 0.0f, 1f);
public static string RabbitMQConnectionUrl = "rabbit.hogwarts-library.de";
public static string RabbitMQConnectionUrl = "https://echosync.hogwarts-library.de";
public static string RabbitMQUserName = "Echosync-User";
public static string RabbitMQPassword = "1234432112344321";
}
}
2 changes: 1 addition & 1 deletion Echosync/Echosync.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
<PackageReference Include="Apache.NMS" Version="2.1.0" />
</ItemGroup>
</Project>
127 changes: 60 additions & 67 deletions Echosync/Helper/RabbitMQHelper.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
using Dalamud.Plugin.Services;
using FFXIVClientStructs.FFXIV.Client.Game.Event;
using Echosync.DataClasses;
using Echosync.Enums;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.Intrinsics.X86;
using System.Net.Security;
using Apache.NMS;
using Apache.NMS.Util;
using FFXIVClientStructs.FFXIV.Client.Game.Event;

namespace Echosync.Helper
{
public static class RabbitMQHelper
{
public static Dictionary<string, bool> ConnectedPlayers = new Dictionary<string, bool>();
private static ConnectionFactory? Factory;
private static IConnectionFactory Factory;
private static IConnection Connection;
private static IModel? Channel;
private static EventingBasicConsumer Consumer;
private static ISession Session;
private static IDestination Destination;
private static IMessageConsumer Consumer;
private static IMessageProducer Producer;
private static Configuration Configuration;
private static string ActiveChannel = "";
private static IClientState ClientState;
Expand All @@ -32,14 +29,17 @@ public static void Setup(Configuration configuration, IClientState clientState)
Configuration = configuration;
ClientState = clientState;
EntityId = ClientState.LocalPlayer?.EntityId.ToString();
Factory = new ConnectionFactory { HostName = DataClasses.Constants.RabbitMQConnectionUrl };
Factory.RequestedConnectionTimeout = new TimeSpan(0, 0, 5);
Factory.Port = 5672;
Factory.UserName = "Echosync-User";
Factory.Password = "1234432112344321";

if (!string.IsNullOrWhiteSpace(configuration.SyncChannel) && configuration.ConnectAtStart)
Connect(new EKEventId(0, Enums.TextSource.Sync));
try
{
IConnectionFactory factory = new NMSConnectionFactory(DataClasses.Constants.RabbitMQConnectionUrl);

if (!string.IsNullOrWhiteSpace(configuration.SyncChannel) && configuration.ConnectAtStart)
Connect(new EKEventId(0, Enums.TextSource.Sync));
}
catch (Exception ex)
{
LogHelper.Error(MethodBase.GetCurrentMethod().Name, $"Error while connecting: {ex}", new EKEventId(0, TextSource.Sync));
}
}

public static void Test(EKEventId eventId)
Expand All @@ -49,21 +49,49 @@ public static void Test(EKEventId eventId)
CreateMessage(RabbitMQMessages.Test, eventId);
}

private static void ConsumerReceived(object? model, BasicDeliverEventArgs ea)
public static void Connect(EKEventId eventId)
{
try
{
if (!string.IsNullOrWhiteSpace(ActiveChannel))
Disconnect(eventId);

ActiveChannel = Configuration.SyncChannel;
LogHelper.Info(MethodBase.GetCurrentMethod().Name, $"Connecting to channel: {ActiveChannel}", eventId);

if (Connection == null || !Connection.IsStarted)
Connection = Factory.CreateConnection(DataClasses.Constants.RabbitMQUserName, DataClasses.Constants.RabbitMQPassword);

Session = Connection.CreateSession();
Destination = SessionUtil.GetDestination(Session, $"queue://{ActiveChannel}");
Consumer = Session.CreateConsumer(Destination);
Consumer.Listener += Consumer_Listener;
Producer = Session.CreateProducer(Destination);
Producer.DeliveryMode = MsgDeliveryMode.Persistent;

CreateMessage(RabbitMQMessages.Connect, eventId);
}
catch (Exception ex)
{
LogHelper.Error(MethodBase.GetCurrentMethod().Name, $"Error while connecting: {ex}", eventId);
}
}

private static void Consumer_Listener(IMessage message)
{
var bodyString = "";
var eventId = new EKEventId(0, TextSource.Sync);
try
{
var body = ea.Body.ToArray();
bodyString = Encoding.UTF8.GetString(body);
var textMessage = (ITextMessage)message;
bodyString = textMessage.Text;
var bodyStringSplit = bodyString.Split('|');
var message = (RabbitMQMessages)Convert.ToInt32(bodyStringSplit[0]);
var messageEnum = (RabbitMQMessages)Convert.ToInt32(bodyStringSplit[0]);
var sendingPlayer = bodyStringSplit[1];
eventId = LogHelper.EventId(MethodBase.GetCurrentMethod().Name, TextSource.Sync);
LogHelper.Info(MethodBase.GetCurrentMethod().Name, $"Received '{message}' from '{sendingPlayer}'", eventId);

switch (message)
switch (messageEnum)
{
case RabbitMQMessages.Connect:
if (!ConnectedPlayers.ContainsKey(sendingPlayer))
Expand Down Expand Up @@ -95,49 +123,16 @@ private static void ConsumerReceived(object? model, BasicDeliverEventArgs ea)
LogHelper.End(MethodBase.GetCurrentMethod().Name, eventId);
}

public static void Connect(EKEventId eventId)
{
try
{
if (!string.IsNullOrWhiteSpace(ActiveChannel))
Disconnect(eventId);

if (Connection == null || !Connection.IsOpen)
Connection = Factory.CreateConnection();

ActiveChannel = Configuration.SyncChannel;
LogHelper.Info(MethodBase.GetCurrentMethod().Name, $"Connecting to channel: {ActiveChannel}", eventId);
Connection = Factory.CreateConnection();
Channel = Connection.CreateModel();
var res = Channel.QueueDeclare(queue: ActiveChannel,
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
Consumer = new EventingBasicConsumer(Channel);
Consumer.Received += ConsumerReceived;
Channel.BasicConsume(queue: ActiveChannel,
autoAck: true,
consumer: Consumer);

CreateMessage(RabbitMQMessages.Connect, eventId);
}
catch (Exception ex)
{
LogHelper.Error(MethodBase.GetCurrentMethod().Name, $"Error while connecting: {ex}", eventId);
}
}

public static void Disconnect(EKEventId eventId)
{
try
{
LogHelper.Info(MethodBase.GetCurrentMethod().Name, $"Disconnecting from channel: {ActiveChannel}", eventId);
CreateMessage(RabbitMQMessages.Disconnect, eventId);
Consumer.Received -= ConsumerReceived;
Consumer.OnCancel();
Channel?.Dispose();
Connection?.Dispose();
Consumer.Dispose();
Producer.Dispose();
Session.Dispose();
Connection.Dispose();
}
catch (Exception ex)
{
Expand All @@ -149,17 +144,15 @@ public static void CreateMessage(RabbitMQMessages message, EKEventId eventId)
{
try
{
if (Connection == null || !Connection.IsOpen)
if (Connection == null || !Connection.IsStarted)
{
Connect(eventId);
}

var bodyString = $"{((int)message)}|{EntityId}";
var body = Encoding.UTF8.GetBytes(bodyString);
Channel.BasicPublish(exchange: string.Empty,
routingKey: ActiveChannel,
basicProperties: null,
body: body);
var request = Session.CreateTextMessage(bodyString);

Producer.Send(request);
LogHelper.Info(MethodBase.GetCurrentMethod().Name, $"Sending '{message.ToString()}' to channel: {ActiveChannel}", eventId);
}
catch (Exception ex)
Expand Down
6 changes: 2 additions & 4 deletions Echosync/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,11 @@ public Plugin(

public void Dispose()
{
addonTalkHelper.Dispose();
RabbitMQHelper.Dispose();
WindowSystem.RemoveAllWindows();

ConfigWindow.Dispose();

CommandManager.RemoveHandler(CommandName);
addonTalkHelper.Dispose();
RabbitMQHelper.Dispose();
}

private void OnCommand(string command, string args)
Expand Down
26 changes: 6 additions & 20 deletions Echosync/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,17 @@
"version": 1,
"dependencies": {
"net8.0-windows7.0": {
"Apache.NMS": {
"type": "Direct",
"requested": "[2.1.0, )",
"resolved": "2.1.0",
"contentHash": "oJb3UdF+IrbpKoe1EEbhxq/MrpEG6thvGsz857HT1JjptUqI7JiA8UZ0payOqpyHjJfb20QphKv+Q+0T3nlPMw=="
},
"DalamudPackager": {
"type": "Direct",
"requested": "[2.1.13, )",
"resolved": "2.1.13",
"contentHash": "rMN1omGe8536f4xLMvx9NwfvpAc9YFFfeXJ1t4P4PE6Gu8WCIoFliR1sh07hM+bfODmesk/dvMbji7vNI+B/pQ=="
},
"RabbitMQ.Client": {
"type": "Direct",
"requested": "[6.8.1, )",
"resolved": "6.8.1",
"contentHash": "jNsmGgmCNw2S/NzskeN2ijtGywtH4Sk/G6jWUTD5sY9SrC27Xz6BsLIiB8hdsfjeyWCa4j4GvCIGkpE8wrjU1Q==",
"dependencies": {
"System.Memory": "4.5.5",
"System.Threading.Channels": "7.0.0"
}
},
"System.Memory": {
"type": "Transitive",
"resolved": "4.5.5",
"contentHash": "XIWiDvKPXaTveaB7HVganDlOCRoj03l+jrwNvcge/t8vhGYKvqV+dMv6G4SAX2NoNmN0wZfVPTAlFwZcZvVOUw=="
},
"System.Threading.Channels": {
"type": "Transitive",
"resolved": "7.0.0",
"contentHash": "qmeeYNROMsONF6ndEZcIQ+VxR4Q/TX/7uIVLJqtwIWL7dDWeh0l1UIqgo4wYyjG//5lUNhwkLDSFl+pAWO6oiA=="
}
}
}
Expand Down

0 comments on commit fd449f4

Please sign in to comment.