Exemple #1
0
        public ServiceProtocolAwaiter <T> SendRequest <T>(ServiceProtocolRequest request) where T : ServiceProtocolResponse, new()
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            localThreadClient = this;

            requestIndex = (uint)Interlocked.Increment(ref lastRequestQueueIndex) % maxConcurrentRequests;

            if (Interlocked.CompareExchange(ref requestStates[requestIndex], RequestStateBeforeSend, RequestStateInactive) != RequestStateInactive)
            {
                ServiceProtocolResponse.localThreadCodeForErrorResponses = ServiceProtocolResponseCode.RequestQueueOverflow;

                return(ServiceProtocolAwaiter <T> .Instance);
            }

            ServiceProtocolResponse.localThreadCodeForErrorResponses = ServiceProtocolResponseCode.Success;

            request.id = requestIndex;

            Thread.MemoryBarrier();

            AddToSendQueue(request);

            return(ServiceProtocolAwaiter <T> .Instance);
        }
        public ServiceProtocolClient CreateClient(ServiceProtocolDataContract dataContract, uint maxConcurrentRequests)
        {
            if (dataContract == null)
            {
                throw new ArgumentNullException(nameof(dataContract));
            }

            if (maxConcurrentRequests == 0 || maxConcurrentRequests == uint.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(maxConcurrentRequests));
            }

            var client = new ServiceProtocolClient(++lastId, dataContract, maxConcurrentRequests, logger);

            lock (clients)
            {
                clients.Add(client);
            }

            return(client);
        }