示例#1
0
        public ServiceProtocolServer(ServiceProtocolDataContract dataContract, uint maxConcurrentRequestsPerClient, IServiceProtocolErrorLogger logger)
        {
            this.dataContract = dataContract ?? throw new ArgumentNullException(nameof(dataContract));

            if (maxConcurrentRequestsPerClient == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maxConcurrentRequestsPerClient));
            }

            this.maxConcurrentRequestsPerClient = maxConcurrentRequestsPerClient;

            this.logger = logger ?? throw new ArgumentNullException(nameof(logger));

            handlers = new Action <ServiceProtocolIncomingConnection, ServiceProtocolRequest> [dataContract.requestUnpackers.Length];

            SetHandler <ServiceProtocolInternalRequest>(pingRequest => { pingRequest.SendResponse(new ServiceProtocolInternalResponse()); });
        }
        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);
        }
示例#3
0
        internal ServiceProtocolClient(uint id, ServiceProtocolDataContract dataContract, uint maxConcurrentRequests, IServiceProtocolErrorLogger logger)
        {
            this.id = id;

            this.dataContract = dataContract;

            sendBufferSize = dataContract.requestBufferSize;

            this.maxConcurrentRequests = maxConcurrentRequests;
            requestStates        = new int[maxConcurrentRequests + ServiceProtocolDataContract.InternalProtocolRequestTypesCount];
            requestContinuations = new Action[maxConcurrentRequests + ServiceProtocolDataContract.InternalProtocolRequestTypesCount];

            this.logger = logger;

            sendArgs            = new SocketAsyncEventArgs();
            sendArgs.Completed += SendingComplete;

            sendArgs.SetBuffer(new byte[sendBufferSize + ServiceProtocolRequest.HeaderSize + ServiceProtocolRequest.MaxSize], 0, (int)sendBufferSize);

            var currentRequestIdsSize = sendArgs.Buffer.Length / (ServiceProtocolRequest.HeaderSize + ServiceProtocolRequest.MaxSize);

            if (currentRequestIdsSize < 128)
            {
                currentRequestIdsSize = 128;
            }
            currentRequestIds = new uint[currentRequestIdsSize];

            receiveArgs            = new SocketAsyncEventArgs();
            receiveArgs.Completed += Receive;
            receiveArgs.SetBuffer(new byte[dataContract.responseBufferSize], 0, (int)dataContract.responseBufferSize);

            packStream   = new MemoryStream(sendArgs.Buffer, 0, sendArgs.Buffer.Length);
            unpackStream = new MemoryStream(receiveArgs.Buffer, 0, receiveArgs.Buffer.Length);

            packWriter   = new BinaryWriter(packStream, dataContract.requestEncoding);
            unpackReader = new BinaryReader(unpackStream, dataContract.responseEncoding);
        }