Beispiel #1
0
        /// <summary>
        /// Initialises a new instance of the <see cref="RawConnectionBase" /> class.
        /// </summary>
        /// <param name="connectionSocketType">
        /// The socket type for the underlying network connection.
        /// </param>
        /// <param name="connectionProtocolType">
        /// The protocol type for the underlying network connection.
        /// </param>
        /// <param name="defaultRemoteEndPoint">
        /// The default remote endpoint to which network writes will be made.
        /// </param>
        protected RawConnectionBase(
            SocketType connectionSocketType,
            ProtocolType connectionProtocolType,
            EndPoint defaultRemoteEndPoint)
        {
            bufferPool = ArrayPool <byte> .Shared;

            connection = new Socket(connectionSocketType, connectionProtocolType);

            socketArgsPool =
                new SlimObjectPool <SocketAsyncEventArgs>(CreateSocketArgs, ResetSocketArgs, DestroySocketArgs);

            DefaultRemoteEndPoint = defaultRemoteEndPoint;
        }
Beispiel #2
0
        protected const int MaxDatagramSize = ushort.MaxValue - 28;  // 65535 - 28 = 65507

        /// <summary>
        /// Constructs a new instance of the <see cref="RawNetworkConnectionBase" /> class.
        /// </summary>
        /// <param name="rawConnection">
        /// The underlying <see cref="Socket" /> to use for the connection.
        /// </param>
        /// <param name="defaultEndPoint">
        /// The default endpoint to use to represent remote clients.
        /// </param>
        /// <param name="maxPooledBufferSize">
        /// The maximum size of a pooled buffer.
        /// </param>
        /// <param name="pooledBuffersPerBucket">
        /// The number of pooled buffers to hold in a single pool bucket.
        /// </param>
        /// <param name="preallocatedStateObjects">
        /// The number of state objects to preallocate.
        /// </param>
        protected RawNetworkConnectionBase(ref Socket rawConnection, EndPoint defaultEndPoint, int maxPooledBufferSize,
                                           int pooledBuffersPerBucket = 50, uint preallocatedStateObjects = 0)
        {
            connection = rawConnection;

            bufferPool = maxPooledBufferSize <= DefaultMaxPooledBufferSize && pooledBuffersPerBucket <= DefaultMaxPooledBuffersPerBucket
                ? ArrayPool <byte> .Shared
                : ArrayPool <byte> .Create(maxPooledBufferSize, pooledBuffersPerBucket);

            this.defaultEndPoint = defaultEndPoint;

            argsPool = new SlimObjectPool <SocketAsyncEventArgs>(CreateStateObject, ResetStateObject, DestroyStateObject, CanReuseStateObject);

            // TODO implement pooling in better way
            for (uint i = 0; i < preallocatedStateObjects; i++)
            {
                argsPool.Return(CreateStateObject());
            }
        }