www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to read some in vibe.d tcpconnection?

reply "zhmt" <zhmtzhmt qq.com> writes:
TCPConnecion has a read method, which will block untill the buf 
is fully filled.

This is not what I want.

I want a readSome method, which will return if some data comes 
from remote end, and notify the length of data to me. so I can 
process data in time.

what should I do ?
Mar 06 2015
next sibling parent reply "zhmt" <zhmtzhmt qq.com> writes:
I am forwarding data from client to another server, if the client 
send data to server,I want to forward that to another server in 
time.

So, if the server recieves nothing it will wait, if it receive 
some data , forward them to another server immediately.

I dont think the TCPConnecion.read method works in this scene.

I have read the document of TCPConnection, found nothing helpful.

Will somebody help me?
Mar 06 2015
parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 6/03/2015 10:30 p.m., zhmt wrote:
 I am forwarding data from client to another server, if the client send
 data to server,I want to forward that to another server in time.

 So, if the server recieves nothing it will wait, if it receive some data
 , forward them to another server immediately.

 I dont think the TCPConnecion.read method works in this scene.

 I have read the document of TCPConnection, found nothing helpful.

 Will somebody help me?
Take a look at empty and leastSize. https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/core/stream.d#L33
Mar 06 2015
parent reply "zhmt" <zhmtzhmt qq.com> writes:
 Take a look at empty and leastSize.

 https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/core/stream.d#L33
Rikki Cattermole Thanks for your reply, but it is not what I want, and I cant implement my goal with them. in boost::asio, It has a method like this : basic_stream_socket::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.
Mar 06 2015
parent reply "Stefan Koch" <uplink.coder googlemail.com> writes:
On Friday, 6 March 2015 at 10:10:35 UTC, zhmt wrote:
 Take a look at empty and leastSize.

 https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/core/stream.d#L33
Rikki Cattermole Thanks for your reply, but it is not what I want, and I cant implement my goal with them. in boost::asio, It has a method like this : basic_stream_socket::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.
There should be a function called DataAvilable. Simply read the data if this function returns true
Mar 06 2015
parent reply "zhmt" <zhmtzhmt qq.com> writes:
 There should be a function called DataAvilable.
 Simply read the data if this function returns true
Thanks you! But the DataAvilable just return a bool value, so I dont know the available data size, I cant call the read method simply. Is there more info to solve this problem?
Mar 06 2015
parent "Stefan Koch" <uplink.coder googlemail.com> writes:
On Friday, 6 March 2015 at 10:16:32 UTC, zhmt wrote:
 There should be a function called DataAvilable.
 Simply read the data if this function returns true
Thanks you! But the DataAvilable just return a bool value, so I dont know the available data size, I cant call the read method simply. Is there more info to solve this problem?
you first check dataAvilableForRead and then leastSize
Mar 06 2015
prev sibling parent reply "Kagamin" <spam here.lot> writes:
I'd say, peek is the right method, it returns what's already in 
the buffer (but doesn't read), while leastSize returns full 
logical size of the stream.
Mar 06 2015
parent reply "zhmt" <zhmtzhmt qq.com> writes:
On Friday, 6 March 2015 at 18:40:28 UTC, Kagamin wrote:
 I'd say, peek is the right method, it returns what's already in 
 the buffer (but doesn't read), while leastSize returns full 
 logical size of the stream.
The api of vibe.d is too simple to use in real work.
Mar 06 2015
next sibling parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Friday, 6 March 2015 at 19:33:56 UTC, zhmt wrote:
 On Friday, 6 March 2015 at 18:40:28 UTC, Kagamin wrote:
 I'd say, peek is the right method, it returns what's already 
 in the buffer (but doesn't read), while leastSize returns full 
 logical size of the stream.
The api of vibe.d is too simple to use in real work.
You might want to go lower level: http://stackoverflow.com/questions/17590816/kernel-based-linux-data-relay-between-two-tcp-sockets
Mar 06 2015
parent reply "zhmt" <zhmtzhmt qq.com> writes:
On Friday, 6 March 2015 at 20:03:52 UTC, Ola Fosheim Grøstad 
wrote:
 On Friday, 6 March 2015 at 19:33:56 UTC, zhmt wrote:
 On Friday, 6 March 2015 at 18:40:28 UTC, Kagamin wrote:
 I'd say, peek is the right method, it returns what's already 
 in the buffer (but doesn't read), while leastSize returns 
 full logical size of the stream.
The api of vibe.d is too simple to use in real work.
You might want to go lower level: http://stackoverflow.com/questions/17590816/kernel-based-linux-data-relay-between-two-tcp-sockets
no, I just want receive data and forward them in time, but not to wait unitil the buffer is full. That means if there is data in buffer, the readSome method should return , and let me forward the data.
Mar 06 2015
parent reply "zhmt" <zhmtzhmt qq.com> writes:
Hi,I got the right answer in vibe.d forum,here is the link:

http://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/24403/#post-24416
Mar 06 2015
parent reply "Kagamin" <spam here.lot> writes:
On Saturday, 7 March 2015 at 02:23:10 UTC, zhmt wrote:
 Hi,I got the right answer in vibe.d forum,here is the link:

 http://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/24403/#post-24416
If that's correct, you can write an extension method for InputStream in terms of asio: ubyte[] readSome(InputStream input, ubyte[] buffer) { if(input.empty)return null; const len = min(input.leastSize, buffer.length); ubyte[] chunk = buffer[0 .. len]; input.read(chunk); // guaranteed to not block now return chunk; } And use: InputStream input=...; ubyte[256] buffer; auto chunk = input.readSome(buffer); if(chunk==null)return done();
Mar 07 2015
parent reply "zhmt" <zhmtzhmt qq.com> writes:
On Saturday, 7 March 2015 at 09:39:42 UTC, Kagamin wrote:
 On Saturday, 7 March 2015 at 02:23:10 UTC, zhmt wrote:
 Hi,I got the right answer in vibe.d forum,here is the link:

 http://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/24403/#post-24416
If that's correct, you can write an extension method for InputStream in terms of asio: ubyte[] readSome(InputStream input, ubyte[] buffer) { if(input.empty)return null; const len = min(input.leastSize, buffer.length); ubyte[] chunk = buffer[0 .. len]; input.read(chunk); // guaranteed to not block now return chunk; } And use: InputStream input=...; ubyte[256] buffer; auto chunk = input.readSome(buffer); if(chunk==null)return done();
Yes, this a good idea, if the author of vibe.d do this, will be better, it is used frequently in many scenes.
Mar 07 2015
parent reply "Kagamin" <spam here.lot> writes:
On Sunday, 8 March 2015 at 03:09:00 UTC, zhmt wrote:
 Yes, this a good idea, if the author of vibe.d do this, will be 
 better, it is used frequently in many scenes.
You can do it too, unlike in C++, in D you can write extension methods to any type, as long as they are visible, you can call them on the first argument, it's called UFCS: http://dlang.org/function.html#pseudo-member
Mar 08 2015
parent "zhmt" <zhmtzhmt qq.com> writes:
On Sunday, 8 March 2015 at 13:56:37 UTC, Kagamin wrote:
 On Sunday, 8 March 2015 at 03:09:00 UTC, zhmt wrote:
 Yes, this a good idea, if the author of vibe.d do this, will 
 be better, it is used frequently in many scenes.
You can do it too, unlike in C++, in D you can write extension methods to any type, as long as they are visible, you can call them on the first argument, it's called UFCS: http://dlang.org/function.html#pseudo-member
This is a good idea. I take it. Thank u very much, you are so kind.
Mar 09 2015
prev sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
On Fri, 06 Mar 2015 19:33:54 +0000, zhmt wrote:

 On Friday, 6 March 2015 at 18:40:28 UTC, Kagamin wrote:
 I'd say, peek is the right method, it returns what's already in the
 buffer (but doesn't read), while leastSize returns full logical size of
 the stream.
=20 The api of vibe.d is too simple to use in real work.
lol.=
Mar 07 2015