Recv() public method

public Recv ( ) : byte[]
return byte[]
Exemplo n.º 1
0
        public void Run(CancellationToken token)
        {
            using (Socket socket = CreateSocket(_socketType))
            {
                // Bind to address
                socket.Bind(_address);

                // Process while canellation not requested
                while (!token.IsCancellationRequested)
                {
                    // Waits for messages
                    var data = socket.Recv();
                    if (data == null)
                    {
                        continue;
                    }

                    Response response = null;

                    using (var buffer = new BinaryMemoryStream(data))
                    {
                        // by request type we can distinguish actual request
                        var requestType = (RequestType)buffer.Reader.ReadInt16();
                        var handler     = _handlerMapping(requestType, buffer);

                        response = handler(buffer);
                    }

                    if (response != null)
                    {
                        using (var buffer = new BinaryMemoryStream())
                        {
                            buffer.Writer.Write((short)response.ResponseType);
                            response.WriteToStream(buffer);
                            socket.Send(buffer.ToArray());
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private Response Send(Request request, bool responseAvailable)
        {
            using (var memoryStream = new BinaryMemoryStream())
            {
                memoryStream.Writer.Write((short)request.RequestType);
                request.WriteToStream(memoryStream);
                var data = memoryStream.ToArray();
                _socket.Send(data);
            }

            if (!responseAvailable)
            {
                return(null);
            }

            var result = _socket.Recv();

            using (var stream = new BinaryMemoryStream(result))
            {
                var responseType = (ResponseType)stream.Reader.ReadInt16();

                switch (responseType)
                {
                case ResponseType.BrokerInfoResponse:
                    return(BrokerInfoResponse.ReadFromStream(stream));

                case ResponseType.FetchResponse:
                    return(FetchResponse.ReadFromStream(stream));

                case ResponseType.MultiFetchResponse:
                    return(MultiFetchResponse.ReadFromStream(stream));
                }

                return(null);
            }
        }