Skip to content

Commit

Permalink
P2PService fix;
Browse files Browse the repository at this point in the history
Room voice disabling fix;
  • Loading branch information
Nirklav committed Nov 11, 2017
1 parent 6418028 commit 684af41
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 42 deletions.
4 changes: 2 additions & 2 deletions ConsoleServer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("4.1.0.0")]
[assembly: AssemblyFileVersion("4.1.0.0")]
[assembly: AssemblyVersion("4.1.1.0")]
[assembly: AssemblyFileVersion("4.1.1.0")]
13 changes: 9 additions & 4 deletions Engine/Api/Client/P2P/ClientConnectToPeerAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ public ClientConnectToPeerAction(string nick)
public void Perform()
{
if (ClientModel.Peer.IsConnected(_nick))
return;

var sendingContent = new ServerP2PConnectRequestCommand.MessageContent { Nick = _nick };
ClientModel.Client.SendMessage(ServerP2PConnectRequestCommand.CommandId, sendingContent);
{
ClientModel.Logger.WriteDebug("Client already connected to {0}", _nick);
}
else
{
var sendingContent = new ServerP2PConnectRequestCommand.MessageContent { Nick = _nick };
ClientModel.Client.SendMessage(ServerP2PConnectRequestCommand.CommandId, sendingContent);
ClientModel.Logger.WriteDebug("Connecting directly to {0}...", _nick);
}
}
}
}
1 change: 1 addition & 0 deletions Engine/Api/Client/P2P/ClientConnectToPeerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected override void OnRun(MessageContent content, CommandArgs args)

var peerPoint = new IPEndPoint(new IPAddress(content.IPAddress), content.Port);
ClientModel.Peer.ConnectToPeer(content.RemoteInfo.Nick, peerPoint);
ClientModel.Logger.WriteDebug("ClientConnectToPeerCommand: {0}|{1}", content.RemoteInfo.Nick, peerPoint.ToString());
}

[Serializable]
Expand Down
1 change: 1 addition & 0 deletions Engine/Api/Client/P2P/ClientWaitPeerConnectionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected override void OnRun(MessageContent content, CommandArgs args)
sendingContent.RemoteInfo = new UserDto(client.Chat.User);

ClientModel.Client.SendMessage(ServerP2PReadyAcceptCommand.CommandId, sendingContent);
ClientModel.Logger.WriteDebug("ClientWaitPeerConnectionCommand: {0}|{1}|{2}|{3}", sendingContent.PeerIPAddress, sendingContent.PeerPort, sendingContent.ReceiverNick, sendingContent.RemoteInfo.Nick);
}

[Serializable]
Expand Down
2 changes: 1 addition & 1 deletion Engine/Model/Client/ClientModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static void Init(ClientInitializer initializer)

Api = new ClientApi();
Client = new AsyncClient(initializer.Nick, Api, _notifier, _logger);
Peer = new AsyncPeer(Api);
Peer = new AsyncPeer(Api, _notifier, _logger);

Plugins = new ClientPluginManager(initializer.PluginsPath);
Plugins.LoadPlugins(initializer.ExcludedPlugins);
Expand Down
2 changes: 1 addition & 1 deletion Engine/Model/Client/Entities/ClientVoiceRoom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public override void Enable()
[SecuritySafeCritical]
public override void Disable()
{
if (!Enabled)
if (Enabled)
{
foreach (var nick in _users)
DecVoiceCoutner(nick);
Expand Down
31 changes: 18 additions & 13 deletions Engine/Network/AsyncPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public WaitingCommandContainer(IPackage package, byte[] rawData, bool unreliable
[SecurityCritical] private readonly Dictionary<string, Packer> _packers;
[SecurityCritical] private readonly SynchronizationContext _syncContext;
[SecurityCritical] private readonly RequestQueue _requestQueue;
[SecurityCritical] private readonly IApi _api;
[SecurityCritical] private readonly ECDiffieHellmanCng _diffieHellman;
[SecurityCritical] private readonly IClientNotifier _notifier;
[SecurityCritical] private readonly Logger _logger;

[SecurityCritical] private NetConnection _serviceConnection;
[SecurityCritical] private NetPeer _handler;
Expand All @@ -75,16 +78,18 @@ public PeerState State

#region constructor
[SecurityCritical]
internal AsyncPeer(IApi api)
internal AsyncPeer(IApi api, IClientNotifier notifier, Logger logger)
{
_waitingCommands = new Dictionary<string, List<WaitingCommandContainer>>();
_packers = new Dictionary<string, Packer>();
_syncContext = new EngineSyncContext();
_requestQueue = new RequestQueue(api);

_api = api;
_diffieHellman = new ECDiffieHellmanCng(KeySize);
_diffieHellman.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
_diffieHellman.HashAlgorithm = CngAlgorithm.Sha256;
_notifier = notifier;
_logger = logger;
}
#endregion

Expand Down Expand Up @@ -127,7 +132,7 @@ internal void ConnectToService(IPEndPoint remotePoint)

_serviceConnection = _handler.Connect(remotePoint, hail);

ClientModel.Logger.WriteDebug("AsyncPeer.ConnectToService({0})", remotePoint);
_logger.WriteDebug("AsyncPeer.ConnectToService({0})", remotePoint);
}

[SecurityCritical]
Expand Down Expand Up @@ -159,7 +164,7 @@ internal void WaitConnection(IPEndPoint waitingPoint)

DisconnectFromService();

ClientModel.Logger.WriteDebug("AsyncPeer.WaitConnection({0})", waitingPoint);
_logger.WriteDebug("AsyncPeer.WaitConnection({0})", waitingPoint);
}

/// <summary>
Expand All @@ -184,7 +189,7 @@ internal void ConnectToPeer(string peerId, IPEndPoint remotePoint)

DisconnectFromService();

ClientModel.Logger.WriteDebug("AsyncPeer.ConnectToPeer({0}, {1})", peerId, remotePoint);
_logger.WriteDebug("AsyncPeer.ConnectToPeer({0}, {1})", peerId, remotePoint);
}

/// <summary>
Expand Down Expand Up @@ -379,7 +384,7 @@ private void SaveCommandAndConnect(string peerId, IPackage package, byte[] rawDa
commands.Add(new WaitingCommandContainer(package, rawData, unreliable));
}

ClientModel.Api.Perform(new ClientConnectToPeerAction(peerId));
_api.Perform(new ClientConnectToPeerAction(peerId));
}

[SecurityCritical]
Expand Down Expand Up @@ -431,7 +436,7 @@ private void OnReceive(object obj)
case NetIncomingMessageType.ErrorMessage:
case NetIncomingMessageType.WarningMessage:
var error = new NetException(message.ReadString());
ClientModel.Notifier.AsyncError(new AsyncErrorEventArgs(error));
_notifier.AsyncError(new AsyncErrorEventArgs(error));
break;

case NetIncomingMessageType.ConnectionApproval:
Expand Down Expand Up @@ -464,13 +469,13 @@ private void OnReceive(object obj)
private void OnApprove(NetIncomingMessage message)
{
message.SenderConnection.Approve(CreateHailMessage());
ClientModel.Logger.WriteDebug("AsyncPeer.Approve()");
_logger.WriteDebug("AsyncPeer.Approve()");
}

[SecurityCritical]
private void OnServiceConnected(NetIncomingMessage message)
{
ClientModel.Logger.WriteDebug("AsyncPeer.ServiceConnect()");
_logger.WriteDebug("AsyncPeer.ServiceConnect()");
}

[SecurityCritical]
Expand All @@ -480,7 +485,7 @@ private void OnPeerConnected(NetIncomingMessage message)
if (hailMessage == null)
{
message.SenderConnection.Deny();
ClientModel.Logger.WriteWarning("ConnectionId is null [Message: {0}, SenderEndPoint: {1}]", message.ToString(), message.SenderEndPoint);
_logger.WriteWarning("ConnectionId is null [Message: {0}, SenderEndPoint: {1}]", message.ToString(), message.SenderEndPoint);
return;
}

Expand Down Expand Up @@ -510,7 +515,7 @@ private void OnPeerConnected(NetIncomingMessage message)
}
}

ClientModel.Logger.WriteDebug("AsyncPeer.PeerConnected({0})", connectionId);
_logger.WriteDebug("AsyncPeer.PeerConnected({0})", connectionId);
}

[SecurityCritical]
Expand All @@ -536,8 +541,8 @@ private void OnPackageReceived(NetIncomingMessage message)
}
catch (Exception exc)
{
ClientModel.Notifier.AsyncError(new AsyncErrorEventArgs(exc));
ClientModel.Logger.Write(exc);
_notifier.AsyncError(new AsyncErrorEventArgs(exc));
_logger.Write(exc);
}
}
#endregion
Expand Down
2 changes: 1 addition & 1 deletion Engine/Network/AsyncServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void Start(int serverPort, int p2pServicePort, bool usingIPv6)
if (!Connection.TcpPortIsAvailable(serverPort))
throw new ArgumentException("port not available", "serverPort");

_p2pService = new P2PService(p2pServicePort, usingIPv6);
_p2pService = new P2PService(_api, _logger, p2pServicePort, usingIPv6);
_systemTimer = new Timer(OnTimer, null, SystemTimerInterval, -1);

var address = usingIPv6 ? IPAddress.IPv6Any : IPAddress.Any;
Expand Down
35 changes: 19 additions & 16 deletions Engine/Network/P2PService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Engine.Api.Server.P2P;
using Engine.Api;
using Engine.Api.Server.P2P;
using Engine.Helpers;
using Engine.Model.Server;
using Lidgren.Network;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -45,6 +45,9 @@ public RequestPair(string requestId, string senderId)
[SecurityCritical] private readonly Dictionary<string, ClientDescription> _clientsEndPoints;
[SecurityCritical] private readonly HashSet<string> _connectingClients;
[SecurityCritical] private readonly List<RequestPair> _requests;

[SecurityCritical] private readonly IApi _api;
[SecurityCritical] private readonly Logger _logger;

[SecurityCritical] private readonly NetServer _server;
[SecurityCritical] private readonly SynchronizationContext _syncContext;
Expand All @@ -59,23 +62,23 @@ public RequestPair(string requestId, string senderId)
/// <param name="port">UDP порт сервиса. Для входящих данных.</param>
/// <param name="usingIPv6">Использовать IPv6.</param>
[SecurityCritical]
public P2PService(int port, bool usingIPv6)
public P2PService(IApi api, Logger logger, int port, bool usingIPv6)
{
_disposed = false;
_requests = new List<RequestPair>();
_clientsEndPoints = new Dictionary<string, ClientDescription>();
_connectingClients = new HashSet<string>();
_requests = new List<RequestPair>();

_api = api;
_logger = logger;

NetPeerConfiguration config = new NetPeerConfiguration(AsyncPeer.NetConfigString);
config.MaximumConnections = 100;
config.Port = port;

if (usingIPv6)
config.LocalAddress = IPAddress.IPv6Any;

_syncContext = new EngineSyncContext();

_server = new NetServer(config);
_syncContext = new EngineSyncContext();
_syncContext.Send(RegisterReceived, _server);
_server.Start();
}
Expand Down Expand Up @@ -114,9 +117,6 @@ public void Introduce(string senderId, string requestId)
{
ThrowIfDisposed();

if (ServerModel.Api == null)
throw new InvalidOperationException("Api not initialized");

if (requestId == null)
throw new ArgumentNullException("requestId");

Expand Down Expand Up @@ -150,14 +150,17 @@ private void TrySendConnectRequest(string connectionId)
}

if (needSend)
ServerModel.Api.Perform(new ServerSendP2PConnectRequestAction(connectionId, Port));
_api.Perform(new ServerSendP2PConnectRequestAction(connectionId, Port));
}

[SecurityCritical]
internal void RemoveEndPoint(string id)
{
lock (_syncObject)
{
_connectingClients.Remove(id);
_clientsEndPoints.Remove(id);
}
}
#endregion

Expand All @@ -175,7 +178,7 @@ private void OnReceive(object obj)
{
case NetIncomingMessageType.ErrorMessage:
case NetIncomingMessageType.WarningMessage:
ServerModel.Logger.Write(new NetException(message.ReadString()));
_logger.Write(new NetException(message.ReadString()));
break;

case NetIncomingMessageType.StatusChanged:
Expand Down Expand Up @@ -203,7 +206,7 @@ private void OnReceive(object obj)
}
catch (Exception e)
{
ServerModel.Logger.Write(e);
_logger.Write(e);
}
}
}
Expand Down Expand Up @@ -262,7 +265,7 @@ private bool TryDoneRequest(string senderId, string requestId)
_connectingClients.Remove(requestId);
}

ServerModel.Api.Perform(new ServerIntroduceConnectionsAction(senderId, senderEndPoint, requestId, requestEndPoint));
_api.Perform(new ServerIntroduceConnectionsAction(senderId, senderEndPoint, requestId, requestEndPoint));
return true;
}

Expand All @@ -288,7 +291,7 @@ private void TryDoneAllRequest()
(removedIds ?? (removedIds = new List<string>())).Add(request.SenderId);
(removedIds ?? (removedIds = new List<string>())).Add(request.RequestId);

ServerModel.Api.Perform(new ServerIntroduceConnectionsAction(request.SenderId, senderEndPoint, request.RequestId, requestEndPoint));
_api.Perform(new ServerIntroduceConnectionsAction(request.SenderId, senderEndPoint, request.RequestId, requestEndPoint));
_requests.RemoveAt(i);
}
}
Expand Down
4 changes: 2 additions & 2 deletions Engine/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// Можно задать все значения или принять номер построения и номер редакции по умолчанию,
// используя "*", как показано ниже:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("4.0.1.0")]
[assembly: AssemblyFileVersion("4.0.1.0")]
[assembly: AssemblyVersion("4.1.1.0")]
[assembly: AssemblyFileVersion("4.1.1.0")]

[assembly: AllowPartiallyTrustedCallers]
4 changes: 2 additions & 2 deletions UI/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@
// Можно задать все значения или принять номер построения и номер редакции по умолчанию,
// используя "*", как показано ниже:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("4.1.0.0")]
[assembly: AssemblyFileVersion("4.1.0.0")]
[assembly: AssemblyVersion("4.1.1.0")]
[assembly: AssemblyFileVersion("4.1.1.0")]

0 comments on commit 684af41

Please sign in to comment.