<commit_before>// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable
using System.Buffers.Binary;
using System.Net.Security;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;

namespace System.Net.Quic.Implementations.Mock
{
    internal sealed class MockConnection : QuicConnectionProvider
    {
        private readonly bool _isClient;
        private bool _disposed;
        private IPEndPoint? _remoteEndPoint;
        private IPEndPoint? _localEndPoint;
        private object _syncObject = new object();
        private Socket? _socket;
        private IPEndPoint? _peerListenEndPoint;
        private TcpListener? _inboundListener;
        private long _nextOutboundBidirectionalStream;
        private long _nextOutboundUnidirectionalStream;

        // Constructor for outbound connections
        internal MockConnection(IPEndPoint? remoteEndPoint, SslClientAuthenticationOptions? sslClientAuthenticationOptions, IPEndPoint? localEndPoint = null)
        {
            _remoteEndPoint = remoteEndPoint;
            _localEndPoint = localEndPoint;

            _isClient = true;
            _nextOutboundBidirectionalStream = 0;
            _nextOutboundUnidirectionalStream = 2;
        }

        // Constructor for accepted inbound connections
        internal MockConnection(Socket socket, IPEndPoint peerListenEndPoint, TcpListener inboundListener)
        {
            _isClient = false;
            _nextOutboundBidirectionalStream = 1;
            _nextOutboundUnidirectionalStream = 3;
            _socket = socket;
            _peerListenEndPoint = peerListenEndPoint;
            _inboundListener = inboundListener;
            _localEndPoint = (IPEndPoint?)socket.LocalEndPoint;
            _remoteEndPoint = (IPEndPoint?)socket.RemoteEndPoint;
        }

        internal override bool Connected
        {
            get
            {
                CheckDisposed();

                return _socket != null;
            }
        }

        internal override IPEndPoint LocalEndPoint => new IPEndPoint(_localEndPoint!.Address, _localEndPoint.Port);

        internal override IPEndPoint RemoteEndPoint => new IPEndPoint(_remoteEndPoint!.Address, _remoteEndPoint.Port);

        internal override SslApplicationProtocol NegotiatedApplicationProtocol => throw new NotImplementedException();

        internal override async ValueTask ConnectAsync(CancellationToken cancellationToken = default)
        {
            CheckDisposed();

            if (Connected)
            {
                // TODO: Exception text
                throw new InvalidOperationException("Already connected");
            }

            Socket socket = new Socket(_remoteEndPoint!.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            await socket.ConnectAsync(_remoteEndPoint).ConfigureAwait(false);
            socket.NoDelay = true;

            _localEndPoint = (IPEndPoint?)socket.LocalEndPoint;
<commit_msg>Sync shared code from runtime (#24996)<commit_after>// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable
using System.Buffers.Binary;
using System.Net.Security;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;

namespace System.Net.Quic.Implementations.Mock
{
    internal sealed class MockConnection : QuicConnectionProvider
    {
        private readonly bool _isClient;
        private bool _disposed;
        private IPEndPoint? _remoteEndPoint;
        private IPEndPoint? _localEndPoint;
        private object _syncObject = new object();
        private Socket? _socket;
        private IPEndPoint? _peerListenEndPoint;
        private TcpListener? _inboundListener;
        private long _nextOutboundBidirectionalStream;
        private long _nextOutboundUnidirectionalStream;

        // Constructor for outbound connections
        internal MockConnection(IPEndPoint? remoteEndPoint, SslClientAuthenticationOptions? sslClientAuthenticationOptions, IPEndPoint? localEndPoint = null)
        {
            _remoteEndPoint = remoteEndPoint;
            _localEndPoint = localEndPoint;

            _isClient = true;
            _nextOutboundBidirectionalStream = 0;
            _nextOutboundUnidirectionalStream = 2;
        }

        // Constructor for accepted inbound connections
        internal MockConnection(Socket socket, IPEndPoint peerListenEndPoint, TcpListener inboundListener)
        {
            _isClient = false;
            _nextOutboundBidirectionalStream = 1;
            _nextOutboundUnidirectionalStream = 3;
            _socket = socket;
            _peerListenEndPoint = peerListenEndPoint;
            _inboundListener = inboundListener;
            _localEndPoint = (IPEndPoint?)socket.LocalEndPoint;
            _remoteEndPoint = (IPEndPoint?)socket.RemoteEndPoint;
        }

        internal override bool Connected
        {
            get
            {
                CheckDisposed();

                return _socket != null;
            }
        }

        internal override IPEndPoint LocalEndPoint => new IPEndPoint(_localEndPoint!.Address, _localEndPoint.Port);

        internal override IPEndPoint RemoteEndPoint => new IPEndPoint(_remoteEndPoint!.Address, _remoteEndPoint.Port);

        internal override SslApplicationProtocol NegotiatedApplicationProtocol => throw new NotImplementedException();

        internal override async ValueTask ConnectAsync(CancellationToken cancellationToken = default)
        {
            CheckDisposed();

            if (Connected)
            {
                // TODO: Exception text
                throw new InvalidOperationException("Already connected");
            }

            Socket socket = new Socket(_remoteEndPoint!.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            await socket.ConnectAsync(_remoteEndPoint, cancellationToken).ConfigureAwait(false);
            socket.NoDelay = true;

            _localEndPoint = (IPEndPoint?)socket.LocalEndPoint;

{'fraction_non_alphanumeric': 0.06008444300097434, 'fraction_numerical': 0.002111075024358558, 'mean_word_length': 2.620811287477954, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6646358', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1482, 'n_words': 374}