www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using Vibe.d for not HTTP

reply Russel Winder <russel winder.org.uk> writes:
Hi,

Clearly Vibe.d is mostly for people doing HTTP and HTTPS stuff. Yet it clai=
ms
to be able to support TCP and UDP working with other protocols. However, al=
l
the serious examples are HTTP/HTTPS related. All the TCP and UDP examples a=
re
basically trivial and thus useless to me for learning.

I am hoping I have just missed the page/example that does something more th=
an
just echo for a TCP server. If I haven't, is ther an example somewhere peop=
le
know of that I can look at?

My problem is that I am not sure how to do a read/write system that is not
just echo using the "connection.write(connection)" trick.
=20
--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Dr Russel Winder      t: +44 20 7585 2200
41 Buckmaster Road    m: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk
May 24 2020
parent reply bauss <jj_1337 live.dk> writes:
On Sunday, 24 May 2020 at 08:10:33 UTC, Russel Winder wrote:
 Hi,

 Clearly Vibe.d is mostly for people doing HTTP and HTTPS stuff. 
 Yet it claims to be able to support TCP and UDP working with 
 other protocols. However, all the serious examples are 
 HTTP/HTTPS related. All the TCP and UDP examples are basically 
 trivial and thus useless to me for learning.

 I am hoping I have just missed the page/example that does 
 something more than just echo for a TCP server. If I haven't, 
 is ther an example somewhere people know of that I can look at?

 My problem is that I am not sure how to do a read/write system 
 that is not
 just echo using the "connection.write(connection)" trick.
Here is a quick example that I took from some old code I had: I cannot guarantee that it works as it was written for an older version of vibe about 3 years ago. I have not structured it either and just put all snippets together here. listeners = [listenTCP(port, &handleConnections, ip)]; or listeners = listenTCP(port, &handleConnections); void handleConnections(TCPConnection connection) { ... } auto buf = new ubyte[amount]; connection.read(temp); connection.write(buf); Then of course you need to handle all that in the way your server/client operates. How you implement it is of course just in similar fashion to general socket servers. You read N bytes until you have received all bytes for the specific packet. Then you can write your packets back afterwards. Someone can correct me if I am wrong but I believe you can yield from the handlers in vibe.d So if you haven't received all data yet you can yield between each call to read until all bytes have been received. I will admit I have not used sockets in vibe.d for a very long time though.
May 24 2020
next sibling parent Russel Winder <russel winder.org.uk> writes:
On Sun, 2020-05-24 at 12:26 +0000, bauss via Digitalmars-d-learn wrote:
[=E2=80=A6]

Thanks for responding, much appreciated.

 void handleConnections(TCPConnection connection) {
     ...
 }
I guess I was looking for an example of what to put in this function!
 auto buf =3D new ubyte[amount];
=20
 connection.read(temp);
The documentation on read is sadly lacking :-( https://vibed.org/api/vibe.core.net/TCPConnection.read It seems to block pending filling all the spaces in the buffer. Not really very useful. I tried: connection.read(buffer, IOMode.once); but there seems no report on how many bytes were read, you have to guess by parsing the buffer and making assumptions. Unless I am missing something, which I really hope I am.
 connection.write(buf);
Not quite at that stage yet! [=E2=80=A6] --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk
May 24 2020
prev sibling parent reply Russel Winder <russel winder.org.uk> writes:
On Sun, 2020-05-24 at 17:01 +0100, Russel Winder wrote:
=20
[=E2=80=A6]
 connection.read(buffer, IOMode.once);
=20
What an idiot I am, this call returns the read count, which makes it fine. Progress now being made. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk
May 24 2020
parent Panke <tobias pankrath.net> writes:
On Sunday, 24 May 2020 at 16:14:58 UTC, Russel Winder wrote:
 On Sun, 2020-05-24 at 17:01 +0100, Russel Winder wrote:
 
[…]
 connection.read(buffer, IOMode.once);
 
What an idiot I am, this call returns the read count, which makes it fine. Progress now being made.
I had a look. Documentation could be better. I think the other call does not return it, because it always reads until the buffer is filled.
May 24 2020