Files
Ragon/Ragon.Server.ENetServer/Sources/ENetServer.cs
T

143 lines
3.9 KiB
C#
Raw Normal View History

2023-03-06 10:06:43 +04:00
/*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using ENet;
using NLog;
using Ragon.Protocol;
2023-04-09 11:06:52 +04:00
using Ragon.Server.IO;
2023-03-06 10:06:43 +04:00
2023-10-08 21:13:31 +03:00
namespace Ragon.Server.ENetServer
2023-03-06 10:06:43 +04:00
{
2023-10-04 14:42:59 +03:00
public sealed class ENetServer : INetworkServer
2023-03-06 10:06:43 +04:00
{
public Executor Executor => _executor;
2023-10-04 14:42:59 +03:00
2023-10-08 21:13:31 +03:00
private readonly Host _host = new();
2023-03-06 10:06:43 +04:00
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
2023-10-04 14:42:59 +03:00
2023-10-08 21:13:31 +03:00
private ENetConnection[] _connections = Array.Empty<ENetConnection>();
2023-03-06 10:06:43 +04:00
private INetworkListener _listener;
private uint _protocol;
2023-10-08 21:13:31 +03:00
private ENet.Event _event;
private Executor _executor = new();
2023-03-06 10:06:43 +04:00
public void Start(INetworkListener listener, NetworkConfiguration configuration)
{
Library.Initialize();
2023-10-04 14:42:59 +03:00
2023-03-06 10:06:43 +04:00
_connections = new ENetConnection[configuration.LimitConnections];
2023-10-04 14:42:59 +03:00
2023-03-06 10:06:43 +04:00
_listener = listener;
_protocol = configuration.Protocol;
2023-10-04 14:42:59 +03:00
2023-03-06 10:06:43 +04:00
var address = new Address
{
2023-10-04 14:42:59 +03:00
Port = (ushort)configuration.Port,
2023-03-06 10:06:43 +04:00
};
_host.Create(address, _connections.Length, 2, 0, 0, 1024 * 1024);
var protocolDecoded = RagonVersion.Parse(_protocol);
_logger.Info($"Listen at 127.0.0.1:{configuration.Port}");
_logger.Info($"Protocol: {protocolDecoded}");
}
public void Update()
{
bool polled = false;
while (!polled)
{
if (_host.CheckEvents(out _event) <= 0)
{
if (_host.Service(0, out _event) <= 0)
break;
polled = true;
}
2023-10-04 14:42:59 +03:00
2023-03-06 10:06:43 +04:00
switch (_event.Type)
{
case EventType.None:
{
_logger.Trace("None event");
break;
}
case EventType.Connect:
{
if (!IsValidProtocol(_event.Data))
{
_logger.Warn($"Mismatched protocol Server: {RagonVersion.Parse(_protocol)} Client: {RagonVersion.Parse(_event.Data)}, close connection");
_event.Peer.DisconnectNow(0);
break;
}
2023-10-04 14:42:59 +03:00
2023-03-06 10:06:43 +04:00
var connection = new ENetConnection(_event.Peer);
2023-10-04 14:42:59 +03:00
2023-03-06 10:06:43 +04:00
_connections[_event.Peer.ID] = connection;
_listener.OnConnected(connection);
break;
}
case EventType.Disconnect:
{
var connection = _connections[_event.Peer.ID];
_listener.OnDisconnected(connection);
break;
}
case EventType.Timeout:
{
var connection = _connections[_event.Peer.ID];
_listener.OnTimeout(connection);
break;
}
case EventType.Receive:
{
2023-10-04 14:42:59 +03:00
var peerId = (ushort)_event.Peer.ID;
2023-03-06 10:06:43 +04:00
var connection = _connections[peerId];
var dataRaw = new byte[_event.Packet.Length];
2023-10-04 14:42:59 +03:00
2023-03-06 10:06:43 +04:00
_event.Packet.CopyTo(dataRaw);
_event.Packet.Dispose();
2023-10-04 14:42:59 +03:00
2023-10-08 21:13:31 +03:00
_listener.OnData(connection, (NetworkChannel)_event.ChannelID, dataRaw);
2023-03-06 10:06:43 +04:00
break;
}
}
}
}
2023-10-11 19:37:50 +03:00
public void Broadcast(byte[] data, NetworkChannel channel)
2023-10-04 14:42:59 +03:00
{
var packet = new Packet();
2023-10-11 19:37:50 +03:00
var flag = channel == NetworkChannel.RELIABLE? PacketFlags.Reliable: PacketFlags.None;
packet.Create(data, flag);
2023-10-08 21:13:31 +03:00
2023-10-11 19:37:50 +03:00
_host.Broadcast((byte)channel, ref packet);
2023-10-04 14:42:59 +03:00
}
2023-10-11 19:37:50 +03:00
2023-03-06 10:06:43 +04:00
public void Stop()
{
_host?.Dispose();
2023-10-04 14:42:59 +03:00
2023-03-06 10:06:43 +04:00
Library.Deinitialize();
}
private bool IsValidProtocol(uint protocol)
{
return protocol == _protocol;
}
}
2022-12-16 00:05:46 +04:00
}