Example #1
0
 public DiffieHellmanCrypter(P2PClient server, ushort countPrimeBits = 2048)
 {
     CountPrimeBits = countPrimeBits;
     UpdatePG();
     this.server          = server;
     server.OnDisconnect += OnClientDisconnect;
 }
 public void InitServer(P2PClient thisServer)
 {
     Server?.Dispose();
     Server  = thisServer;
     crypter = new RSA(Server, 768);
     Server.OnDebugMessage += str => OnDebugMessage?.Invoke(this, str);
     Server.OnMessageSend  += p_OnMessageSend;
     Server.OnConnect      += p_OnConnection;
     Server.OnDisconnect   += (_, u) => OnUserDisconnect?.Invoke(this, u);
 }
 /// <summary>
 /// Создаёт новый экземпляр клиента получателя и отправителя сообщений.
 /// </summary>
 /// <param name="server">Сервер, с помощью которого отправляются и получаются пакеты.</param>
 /// <param name="timeout">Максимальный интервал ожидания для получения пакета.</param>
 public SimpleWriterReader(P2PClient server, TimeSpan timeout = default)
 {
     server.DebugInfo($"{this}.SimpleWriterReader = {server}, {timeout}");
     if (timeout != default)
     {
         Timeout = timeout;
     }
     this.server           = server;
     server.OnMessageSend += OnMessageSend;
     server.OnDisconnect  += OnUserDisconnect;
 }
 private void p_OnConnection(P2PClient server, ulong userId)
 {
     try
     {
         crypter.AddUser(userId);
         OnUserConnect?.Invoke(this, userId);
     }
     catch (System.OperationCanceledException)
     {
         server.Disconnect(userId);
     }
 }
 private void p_OnMessageSend(P2PClient sender, ulong userId, Memory <byte> msg)
 {
     if (crypter.IsConnectionSafe(userId, msg))
     {
         Memory <byte> deMsg = crypter.Decrypt(userId, msg);
         if (deMsg.Length > 0)
         {
             dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(deMsg.Span));
             messages.Push(new PackageInfo(json, userId));
             OnMessageSend?.Invoke(this, userId, messages.Peek());
         }
     }
 }
 public void Dispose()
 {
     Server?.Dispose();
     Server = null;
 }
Example #7
0
 public RSA(P2PClient server, ushort countBits = 2048)
 {
     this.CountBits = countBits;
     UpdatePQED();
     this.server = server ?? throw new ArgumentNullException();
 }
 /// <summary>
 /// Отвязывает этот экземпляр от сервера.
 /// </summary>
 public void Dispose()
 {
     server.OnMessageSend -= OnMessageSend;
     server.OnDisconnect  -= OnUserDisconnect;
     server = null;
 }
 /// <summary>
 /// Происходит при отключении игрока от сервера. Идёт очистка сообщений.
 /// </summary>
 /// <param name="server">Сервер, откуда отключился пользователь.</param>
 /// <param name="userId">Идентификатор пользователя.</param>
 private void OnUserDisconnect(P2PClient server, ulong userId)
 => messages.TryRemove(userId, out _);
 private void OnMessageSend(P2PClient server, ulong userId, Memory <byte> Package)
 {
     server.DebugInfo($"{this}.OnMessageSend = {server}, {userId}, {string.Join(", ", Package)}");
     GetUserMessages(userId).Add(Package);
 }
Example #11
0
 private void OnClientDisconnect(P2PClient server, ulong client)
 => users.TryRemove(client, out _);