Ejemplo n.º 1
0
        //  public CdnManager Cdn()
        //{
        //    WaitAuthLock();
        //   GuardAgainst.ArgumentBeingNull(_cdn, exceptionMessage: "Session isn't authenticated");
        //   return _cdn;
        //  }

        // public CacheManager CacheManager()
        // {
        //    WaitAuthLock();
        //   GuardAgainst.ArgumentBeingNull(_cache, exceptionMessage: "Session isn't authenticated");
        //   return _cache;
        // }
        internal void Send(MercuryPacket.Type cmd, byte[] payload)
        {
            if (closedToken.IsCancellationRequested)
            {
                Debug.WriteLine("Connection was broken while Session.close() has been called");
                return;
            }

            using (authLock.Lock())
            {
                if (sendCipher == null)
                {
                    try
                    {
                        authLockEventWaitHandle.Wait(closedToken);
                    }
                    catch (Exception)
                    {
                        return;
                    }
                }

                SendUnchecked(cmd,
                              payload,
                              CancellationToken.None);
            }
        }
Ejemplo n.º 2
0
        private void SendUnchecked(MercuryPacket.Type cmd, byte[] payload, CancellationToken cts)
        {
            using (sendLock.Lock(cts))
            {
                var a = conn.Result.NetworkStream;
                var payloadLengthAsByte = BitConverter.GetBytes((short)payload.Length).Reverse().ToArray();
                using var yetAnotherBuffer = new MemoryStream(3 + payload.Length);
                yetAnotherBuffer.WriteByte((byte)cmd);
                yetAnotherBuffer.Write(payloadLengthAsByte, 0, payloadLengthAsByte.Length);
                yetAnotherBuffer.Write(payload, 0, payload.Length);

                sendCipher.nonce(Utils.toByteArray(sendNonce));
                Interlocked.Increment(ref sendNonce);

                var bufferBytes = yetAnotherBuffer.ToArray();
                sendCipher.encrypt(bufferBytes);

                var fourBytesBuffer = new byte[4];
                sendCipher.finish(fourBytesBuffer);
                a.Write(bufferBytes, 0, bufferBytes.Length);
                a.Write(fourBytesBuffer, 0, fourBytesBuffer.Length);
                a.Flush();
            }
        }