File: reactor.cpp

package info (click to toggle)
yrmcds 1.0.4-6
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 924 kB
  • ctags: 1,346
  • sloc: cpp: 9,634; sh: 133; makefile: 97
file content (73 lines) | stat: -rw-r--r-- 1,833 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <cybozu/reactor.hpp>
#include <cybozu/util.hpp>

#include <cassert>
#include <cerrno>
#include <fcntl.h>
#include <iostream>
#include <string>
#include <unistd.h>

int g_counter = 0;
bool got_hangup = false;
bool data_read = false;

class in_resource: public cybozu::resource {
public:
    in_resource(int fd): cybozu::resource(fd) {}
    ~in_resource() {
        std::cout << "stdin_resource dtor." << std::endl;
    }

private:
    virtual bool on_readable() override final {
        char buf[512];
        buf[sizeof(buf) - 1] = '\0';
        ssize_t t = ::read(fileno(), buf, sizeof(buf) - 1);
        if( t == -1 )
            cybozu::throw_unix_error(errno, "read");
        std::cout << "aaa: " << t << std::endl;
        buf[t] = '\0';
        std::cout << "read: " << buf << std::endl;
        data_read = true;
        return false;
    }
    virtual bool on_writable() override final {
        return true;
    }
    virtual bool on_hangup() override final {
        std::cout << "got hangup!" << std::endl;
        got_hangup = true;
        return true;
    }
};

void f(cybozu::reactor& r) {
    g_counter++;
    if( g_counter > 2 ) {
        r.quit();
        return;
    }
    std::cout << g_counter << std::endl;
    if( r.fix_garbage() )
        r.gc();
}

int main(int argc, char** argv) {
    cybozu::logger::set_threshold(cybozu::severity::debug);

    int fds[2];
    if( ::pipe2(fds, O_NONBLOCK) == -1 )
        cybozu::throw_unix_error(errno, "pipe");
    if( ::write(fds[1], "Hello!", 6) == -1 )
        cybozu::throw_unix_error(errno, "write");
    ::close(fds[1]);
    cybozu::reactor r;
    std::unique_ptr<cybozu::resource> res(new in_resource(fds[0]));
    r.add_resource(std::move(res), cybozu::reactor::EVENT_IN);
    r.run(f);

    assert(got_hangup);
    assert(data_read);
    return 0;
}