RejectedSoftware Forums

Sign up

How does readUntil work?

Hello,

I want to readUntil I get an STX byte (which in hex is 03).

Sample code:

module app;

import std.exception;
import std.stdio;
import vibe.core.core;
import vibe.core.drivers.utils;
import vibe.core.net;
import vibe.stream.operations;

shared static this()
{
    auto listener = listenTCP
    (
        11005,
        newConnection => onConnect(newConnection)
    );
}

void onConnect(TCPConnection newConnection)
{
    writeln("Connected");
    while (newConnection.connected)
    {
        try
        {
            auto msg = newConnection.readUntil(['\x03']);
            writeln(msg);
        }
        catch (SystemSocketException ex)
        {
            writeln(ex);
        }
    }
    writeln("Disconnected");
}

While this program is running, I do:
echo -ne "Hello\0003There\0003" | nc localhost 11005
which gives me a confusing result:

Connected
[72, 101, 108, 108, 111]
[84, 104, 101, 114, 101]
Handling of connection failed: Reached EOF before reaching end marker.

Any ideas or suggestions on how to fix this?

Re: How does readUntil work?

On Thu, 16 Apr 2015 20:11:23 GMT, Kelet wrote:

Hello,

I want to readUntil I get an STX byte (which in hex is 03).

Sample code:

module app;

import std.exception;
import std.stdio;
import vibe.core.core;
import vibe.core.drivers.utils;
import vibe.core.net;
import vibe.stream.operations;

shared static this()
{
    auto listener = listenTCP
    (
        11005,
        newConnection => onConnect(newConnection)
    );
}

void onConnect(TCPConnection newConnection)
{
    writeln("Connected");
    while (newConnection.connected)
    {
        try
        {
            auto msg = newConnection.readUntil(['\x03']);
            writeln(msg);
        }
        catch (SystemSocketException ex)
        {
            writeln(ex);
        }
    }
    writeln("Disconnected");
}

While this program is running, I do:
echo -ne "Hello\0003There\0003" | nc localhost 11005
which gives me a confusing result:

Connected
[72, 101, 108, 108, 111]
[84, 104, 101, 114, 101]
Handling of connection failed: Reached EOF before reaching end marker.

Any ideas or suggestions on how to fix this?

This is just a guess, but the loop condition should be while (!newConnection.empty). During connection shutdown, both properties can behave differently. !.empty signals read-readiness, while .connected signals write readiness.

Re: How does readUntil work?

Hi Sönke,

That seems to have been the problem.

Thanks!