private async ValueTask <IConnection?> InternalConnectAsync(ICap cap, string serviceType, CancellationToken cancellationToken)
        {
            using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            linkedTokenSource.CancelAfter(TimeSpan.FromSeconds(20));

            var baseConnectionOptions = new BaseConnectionOptions()
            {
                MaxSendByteCount    = 4 * 1024 * 1024,
                MaxReceiveByteCount = 4 * 1024 * 1024,
                BytesPool           = _bytesPool,
            };
            var baseConnection = new BaseConnection(cap, _baseConnectionDispatcher, baseConnectionOptions);

            var omniSecureConnectionOptions = new OmniSecureConnectionOptions()
            {
                Type       = OmniSecureConnectionType.Connected,
                BufferPool = _bytesPool,
            };
            var omniSecureConnection = new OmniSecureConnection(baseConnection, omniSecureConnectionOptions);

            await omniSecureConnection.HandshakeAsync(linkedTokenSource.Token);

            var helloMessage = new ConnectionHelloMessage(serviceType);
            await omniSecureConnection.EnqueueAsync((bufferWriter) => helloMessage.Export(bufferWriter, _bytesPool), linkedTokenSource.Token);

            return(omniSecureConnection);
        }
        private async ValueTask <(IConnection?, string?)> InternalAcceptAsync(ICap cap, CancellationToken cancellationToken)
        {
            using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            linkedTokenSource.CancelAfter(TimeSpan.FromSeconds(20));

            var baseConnectionOptions = new BaseConnectionOptions()
            {
                MaxSendByteCount    = 4 * 1024 * 1024,
                MaxReceiveByteCount = 4 * 1024 * 1024,
                BytesPool           = _bytesPool,
            };
            var baseConnection = new BaseConnection(cap, _baseConnectionDispatcher, baseConnectionOptions);

            var omniSecureConnectionOptions = new OmniSecureConnectionOptions()
            {
                Type       = OmniSecureConnectionType.Accepted,
                BufferPool = _bytesPool,
            };
            var omniSecureConnection = new OmniSecureConnection(baseConnection, omniSecureConnectionOptions);

            await omniSecureConnection.HandshakeAsync(linkedTokenSource.Token);

            ConnectionHelloMessage?helloMessage = null;
            await omniSecureConnection.DequeueAsync((sequence) => helloMessage = ConnectionHelloMessage.Import(sequence, _bytesPool), linkedTokenSource.Token);

            return(omniSecureConnection, helloMessage?.ServiceType);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ClientBase"/> class.
 /// </summary>
 /// <param name="port">The port.</param>
 public ClientBase(int port)
 {
     options = new BaseConnectionOptions
     {
         GameServerPort = port
     };
     Initialize();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ClientBase"/> class.
 /// </summary>
 /// <param name="host">The host.</param>
 /// <param name="port">The port.</param>
 public ClientBase(string host, int port)
 {
     options = new BaseConnectionOptions
     {
         GameServerPort = port,
         GameServerHost = host
     };
     Initialize();
 }
        public async Task RandomSendAndReceiveTest()
        {
            var random = new Random();

            var caseList = new List <int>();

            caseList.AddRange(Enumerable.Range(1, 32));
            caseList.AddRange(new int[] { 100, 1000, 10000, 1024 * 1024 });

            var(socket1, socket2) = SocketHelper.GetSocketPair();

            var dispatcherOptions = new BaseConnectionDispatcherOptions()
            {
                MaxSendBytesPerSeconds    = 1024 * 1024 * 1,
                MaxReceiveBytesPerSeconds = 1024 * 1024 * 1,
            };

            var options = new BaseConnectionOptions()
            {
                MaxReceiveByteCount = 1024 * 1024 * 256,
                BytesPool           = BytesPool.Shared,
            };

            await using var dispatcher = new BaseConnectionDispatcher(dispatcherOptions);
            using var baseConnection1  = new BaseConnection(new SocketCap(socket1), dispatcher, options);
            using var baseConnection2  = new BaseConnection(new SocketCap(socket2), dispatcher, options);
            using var connection1      = new OmniSecureConnection(baseConnection1, new OmniSecureConnectionOptions()
            {
                Type = OmniSecureConnectionType.Connected
            });
            using var connection2 = new OmniSecureConnection(baseConnection2, new OmniSecureConnectionOptions()
            {
                Type = OmniSecureConnectionType.Accepted
            });

            // ハンドシェイクを行う
            {
                var valueTask1 = connection1.HandshakeAsync();
                var valueTask2 = connection2.HandshakeAsync();

                var stopwatch = Stopwatch.StartNew();

                while (!valueTask1.IsCompleted || !valueTask2.IsCompleted)
                {
                    if (stopwatch.Elapsed.TotalSeconds > 60)
                    {
                        throw new TimeoutException("Handshake");
                    }

                    await Task.Delay(100).ConfigureAwait(false);
                }
            }

            foreach (var bufferSize in caseList)
            {
                var buffer1 = new byte[bufferSize];
                var buffer2 = new byte[bufferSize];

                random.NextBytes(buffer1);

                var valueTask1 = connection1.EnqueueAsync((bufferWriter) =>
                {
                    bufferWriter.Write(buffer1);
                });

                var valueTask2 = connection2.DequeueAsync((sequence) =>
                {
                    sequence.CopyTo(buffer2);
                });

                var stopwatch = Stopwatch.StartNew();

                while (!valueTask1.IsCompleted || !valueTask2.IsCompleted)
                {
                    if (stopwatch.Elapsed.TotalSeconds > 60)
                    {
                        throw new TimeoutException("SendAndReceive");
                    }

                    await Task.Delay(100).ConfigureAwait(false);
                }

                Assert.Equal(buffer1, buffer2);
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ClientBase"/> class.
 /// </summary>
 /// <param name="Options">The options.</param>
 public ClientBase(BaseConnectionOptions Options)
 {
     options = Options;
     Initialize();
 }