RejectedSoftware Forums

Sign up

how to read some?

I am writing a forward server, read some data, and forward it.

vibe.d has only the read method, it will block until the buf is fully filled.

I want a "read some" method,if data comes , readSome method will return, and I can forward the received data.

what should I do?

Re: how to read some?

in boost::asio, It has a method like this :

basicstreamsocket::read_some (1 of 2 overloads)

This function is used to read data from the stream socket. The
function call will block until one or more bytes of data has been
read successfully, or until an error occurs.

how can I get this function in vibe.d?

Re: how to read some?

On Fri, 06 Mar 2015 10:24:57 GMT, zhmt wrote:

in boost::asio, It has a method like this :

basicstreamsocket::read_some (1 of 2 overloads)

This function is used to read data from the stream socket. The
function call will block until one or more bytes of data has been
read successfully, or until an error occurs.

how can I get this function in vibe.d?

You have two possibilities depending on how low level you need to go:

  1. If you simply want to stream from an InputStream to an OutputStream, you can call output.write(input) and it will pipe everything until the input connection is closed (or until the specified limit is reached).

  2. The underlying low-level functionality is achieved using the leastSize property. It returns the amount of data that has currently already been received:

    ubyte[256] buffer;
    while (!input.empty) {
    	auto chunk = min(input.leastSize, buffer.length);
    	input.read(buffer[0 .. chunk]); // guaranteed to not block now
    	output.write(buffer[0 .. chunk]);
    }
    

Re: how to read some?

On Fri, 06 Mar 2015 11:23:52 GMT, Sönke Ludwig wrote:

On Fri, 06 Mar 2015 10:24:57 GMT, zhmt wrote:

in boost::asio, It has a method like this :

basicstreamsocket::read_some (1 of 2 overloads)

This function is used to read data from the stream socket. The
function call will block until one or more bytes of data has been
read successfully, or until an error occurs.

how can I get this function in vibe.d?

You have two possibilities depending on how low level you need to go:

  1. If you simply want to stream from an InputStream to an OutputStream, you can call output.write(input) and it will pipe everything until the input connection is closed (or until the specified limit is reached).

  2. The underlying low-level functionality is achieved using the leastSize property. It returns the amount of data that has currently already been received:

    ubyte[256] buffer;
    while (!input.empty) {
    	auto chunk = min(input.leastSize, buffer.length);
    	input.read(buffer[0 .. chunk]); // guaranteed to not block now
    	output.write(buffer[0 .. chunk]);
    }
    

Thank u very much.

I will choose the first.
But the second is very useful for me in other scenes.

That is a big help, Thanks.