Пример #1
0
        async ValueTask <bool> AwaitRequest <H>(
            ushort requestId, H handler, byte[] buffer, IPEndPoint endPoint,
            ManualResetEvent manualResetEvent, int length) where H : class
        {
            var userId  = UserId;
            var keyInfo = _keysClient.GetMyKeyInfo();

            if (userId == null)
            {
                Console.Error.WriteLine($"Could not send packet because userId is null");
                return(false);
            }
            if (keyInfo == null)
            {
                Console.Error.WriteLine($"Could not send packet because keyInfo is null");
                return(false);
            }

            _requests[requestId] = handler;

            var packet = _sendBufferPool.Get();

            try
            {
                int packetLength = _packetEncryption.CreateEncryptedPacket(buffer.AsSpan(0, length), packet, false, userId.Value, keyInfo);

                _socket.SendTo(packet, 0, packetLength, SocketFlags.None, endPoint);
                bool acknowledged = await AwaitResetEvent(manualResetEvent);

                _sendBufferPool.Add(packet);

                _requests[requestId] = null;
                return(acknowledged);
            }
            catch (Exception exception)
            {
                Console.Error.WriteLine(exception);
                throw exception;
            }
        }
Пример #2
0
        void SendToUserEncrypted(Span <byte> payload, IPEndPoint endPoint)
        {
            var keyInfo = _keysClient.GetMyKeyInfo();

            if (keyInfo == null)
            {
                Console.Error.WriteLine("Failed to send encrypted packet because we could not get our key info");
                return;
            }
            int packetLength = _packetEncryption.CreateEncryptedPacket(payload, _encryptedBuffer, false, UserId, keyInfo);

            _socket.SendTo(_encryptedBuffer, 0, packetLength, SocketFlags.None, endPoint);
        }