2018-11-30 23:37:08 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using NetMQ;
|
|
|
|
|
using NetMQ.Sockets;
|
|
|
|
|
using OpenDiablo2.Common.Attributes;
|
|
|
|
|
using OpenDiablo2.Common.Enums;
|
|
|
|
|
using OpenDiablo2.Common.Interfaces;
|
2018-12-01 02:44:40 -05:00
|
|
|
|
using OpenDiablo2.Common.Interfaces.MessageBus;
|
|
|
|
|
using OpenDiablo2.ServiceBus.Message_Frames.Server;
|
2018-11-30 23:37:08 -05:00
|
|
|
|
|
|
|
|
|
namespace OpenDiablo2.ServiceBus
|
|
|
|
|
{
|
|
|
|
|
public sealed class SessionServer : ISessionServer, ISessionEventProvider
|
|
|
|
|
{
|
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
|
|
|
|
private readonly eSessionType sessionType;
|
2018-12-01 02:44:40 -05:00
|
|
|
|
private readonly IGameServer gameServer;
|
2018-11-30 23:37:08 -05:00
|
|
|
|
private readonly Func<eMessageFrameType, IMessageFrame> getMessageFrame;
|
2018-12-01 02:44:40 -05:00
|
|
|
|
|
2018-11-30 23:37:08 -05:00
|
|
|
|
private AutoResetEvent resetEvent = new AutoResetEvent(false);
|
|
|
|
|
public AutoResetEvent WaitServerStartEvent { get; set; } = new AutoResetEvent(false);
|
|
|
|
|
private bool running = false;
|
|
|
|
|
private ResponseSocket responseSocket;
|
|
|
|
|
|
|
|
|
|
public OnJoinGameEvent OnJoinGame { get; set; }
|
2018-12-01 15:22:55 -05:00
|
|
|
|
public OnMoveRequest OnMoveRequest { get; set; }
|
|
|
|
|
|
|
|
|
|
// TODO: Fix interface so we don't need this in the session server
|
|
|
|
|
public OnSetSeedEvent OnSetSeed { get; set; }
|
2018-12-01 02:44:40 -05:00
|
|
|
|
public OnLocatePlayersEvent OnLocatePlayers { get; set; }
|
2018-12-01 12:10:16 -05:00
|
|
|
|
public OnPlayerInfoEvent OnPlayerInfo { get; set; }
|
|
|
|
|
public OnFocusOnPlayer OnFocusOnPlayer { get; set; }
|
2018-11-30 23:37:08 -05:00
|
|
|
|
|
2018-12-01 02:44:40 -05:00
|
|
|
|
public SessionServer(
|
|
|
|
|
eSessionType sessionType,
|
|
|
|
|
IGameServer gameServer,
|
|
|
|
|
Func<eMessageFrameType, IMessageFrame> getMessageFrame
|
|
|
|
|
)
|
2018-11-30 23:37:08 -05:00
|
|
|
|
{
|
|
|
|
|
this.sessionType = sessionType;
|
|
|
|
|
this.getMessageFrame = getMessageFrame;
|
2018-12-01 02:44:40 -05:00
|
|
|
|
this.gameServer = gameServer;
|
2018-11-30 23:37:08 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
2018-12-01 02:44:40 -05:00
|
|
|
|
// TODO: Loading existing games...
|
|
|
|
|
gameServer.InitializeNewGame();
|
|
|
|
|
|
2018-11-30 23:37:08 -05:00
|
|
|
|
Task.Run(() => Serve());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Serve()
|
|
|
|
|
{
|
|
|
|
|
log.Info("Session server is starting.");
|
|
|
|
|
responseSocket = new ResponseSocket();
|
|
|
|
|
|
|
|
|
|
switch (sessionType)
|
|
|
|
|
{
|
|
|
|
|
case eSessionType.Local:
|
|
|
|
|
responseSocket.Bind("inproc://opendiablo2-session");
|
|
|
|
|
break;
|
|
|
|
|
case eSessionType.Server:
|
|
|
|
|
case eSessionType.Remote:
|
|
|
|
|
default:
|
|
|
|
|
throw new ApplicationException("This session type is currently unsupported.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OnJoinGame += OnJoinGameHandler;
|
2018-12-01 15:22:55 -05:00
|
|
|
|
OnMoveRequest += OnMovementRequestHandler;
|
2018-11-30 23:37:08 -05:00
|
|
|
|
|
|
|
|
|
var proactor = new NetMQProactor(responseSocket, (socket, message) =>
|
|
|
|
|
{
|
|
|
|
|
var bytes = message.First().ToByteArray();
|
|
|
|
|
var frameType = (eMessageFrameType)bytes[0];
|
|
|
|
|
var frameData = bytes.Skip(1).ToArray(); // TODO: Can we maybe use pointers? This seems wasteful
|
|
|
|
|
var messageFrame = getMessageFrame(frameType);
|
|
|
|
|
messageFrame.Data = frameData;
|
2018-12-01 02:44:40 -05:00
|
|
|
|
messageFrame.Process(socket.GetHashCode(), this);
|
2018-11-30 23:37:08 -05:00
|
|
|
|
});
|
|
|
|
|
running = true;
|
|
|
|
|
WaitServerStartEvent.Set();
|
|
|
|
|
resetEvent.WaitOne();
|
|
|
|
|
proactor.Dispose();
|
|
|
|
|
running = false;
|
|
|
|
|
responseSocket.Dispose();
|
|
|
|
|
log.Info("Session server has stopped.");
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-01 15:22:55 -05:00
|
|
|
|
private void OnMovementRequestHandler(int clientHash, int direction, eMovementType movementType)
|
|
|
|
|
{
|
|
|
|
|
// TODO: Actually move the player ....
|
|
|
|
|
var player = gameServer.Players.FirstOrDefault(x => x.ClientHash == clientHash);
|
|
|
|
|
if (player == null)
|
|
|
|
|
return;
|
|
|
|
|
log.Info($"Player {player.Id} requested to move in {direction} direction: {movementType.ToString()}");
|
|
|
|
|
|
|
|
|
|
NoOp();
|
|
|
|
|
}
|
2018-11-30 23:37:08 -05:00
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
|
|
|
|
if (!running)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
resetEvent.Set();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Stop();
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-01 15:22:55 -05:00
|
|
|
|
private void NoOp()
|
|
|
|
|
{
|
|
|
|
|
responseSocket.SendFrame(new byte[] { (byte)eMessageFrameType.None });
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-01 02:44:40 -05:00
|
|
|
|
private void Send(IMessageFrame messageFrame, bool more = false)
|
2018-11-30 23:37:08 -05:00
|
|
|
|
{
|
|
|
|
|
var attr = messageFrame.GetType().GetCustomAttributes(true).First(x => typeof(MessageFrameAttribute).IsAssignableFrom(x.GetType())) as MessageFrameAttribute;
|
2018-12-01 02:44:40 -05:00
|
|
|
|
responseSocket.SendFrame(new byte[] { (byte)attr.FrameType }.Concat(messageFrame.Data).ToArray(), more);
|
2018-11-30 23:37:08 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-01 02:44:40 -05:00
|
|
|
|
private void OnJoinGameHandler(int clientHash, eHero heroType, string playerName)
|
2018-11-30 23:37:08 -05:00
|
|
|
|
{
|
2018-12-01 02:44:40 -05:00
|
|
|
|
gameServer.SpawnNewPlayer(clientHash, playerName, heroType);
|
|
|
|
|
Send(new MFSetSeed(gameServer.Seed), true);
|
2018-12-01 12:10:16 -05:00
|
|
|
|
Send(new MFPlayerInfo(gameServer.Players.Select(x => x.ToPlayerInfo())), true);
|
|
|
|
|
Send(new MFLocatePlayers(gameServer.Players.Select(x => x.ToPlayerLocationDetails())), true);
|
|
|
|
|
Send(new MFFocusOnPlayer(gameServer.Players.First(x => x.ClientHash == clientHash).Id));
|
2018-11-30 23:37:08 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|