summaryrefslogtreecommitdiff
path: root/src/Selectable.cc
blob: 40e85bc2d096d34143a57a8c1a9a1c71f74873b4 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <winsock2.h>
#include <sys/types.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include "Selectable.h"
#include "Threads.h"
#include "Printer.h"
#include "Main.h"
#include "Async.h"
#include "Task.h"
#include "TaskMan.h"

#ifndef _WIN32
namespace {

class SigpipeBlocker : public Balau::AtStart {
  public:
      SigpipeBlocker() : AtStart(5) { }
    virtual void doStart() {
        struct sigaction new_actn, old_actn;
        new_actn.sa_handler = SIG_IGN;
        sigemptyset(&new_actn.sa_mask);
        new_actn.sa_flags = 0;
        sigaction(SIGPIPE, &new_actn, &old_actn);
    }
};

static SigpipeBlocker sigpipeBlocker;

};
#endif

void Balau::Selectable::SelectableEvent::gotOwner(Task * task) {
    Printer::elog(E_SELECT, "Arming SelectableEvent at %p", this);
    if (!m_task) {
        Printer::elog(E_SELECT, "...with a new task (%p)", task);
    } else if (task == m_task) {
        Printer::elog(E_SELECT, "...with the same task, doing nothing.");
        return;
    } else {
        Printer::elog(E_SELECT, "...with a new task (%p -> %p); stopping first", m_task, task);
        m_evt.stop();
    }
    m_task = task;
    m_evt.set(task->getLoop());
    m_evt.start();
}

void Balau::Selectable::setFD(int fd) throw (GeneralException) {
    if (m_fd >= 0)
        throw GeneralException("FD already set.");
    m_fd = fd;

    m_evtR = new SelectableEvent(m_fd, ev::READ);
    m_evtW = new SelectableEvent(m_fd, ev::WRITE);
#ifdef _WIN32
    u_long iMode = 1;
    ioctlsocket(m_fd, FIONBIO, &iMode);
#else
    fcntl(m_fd, F_SETFL, O_NONBLOCK);
#endif
}

Balau::Selectable::~Selectable() {
    m_fd = -1;
    delete m_evtR;
    delete m_evtW;
    m_evtR = m_evtW = NULL;
}

bool Balau::Selectable::isClosed() { return m_fd < 0; }
bool Balau::Selectable::isEOF() { return isClosed(); }

ssize_t Balau::Selectable::read(void * buf, size_t count) throw (GeneralException) {
    if (count == 0)
        return 0;

    AAssert(m_fd >= 0, "You can't call read() on a closed selectable");

    int spins = 0;

    do {
        ssize_t r = recv(m_fd, (char *) buf, count, 0);

        if (r >= 0) {
            if (r == 0)
                close();
            return r;
        }

        if ((errno == EAGAIN) || (errno == EINTR) || (errno == EWOULDBLOCK)) {
            Task::operationYield(m_evtR, Task::INTERRUPTIBLE);
        } else {
            m_evtR->stop();
            return r;
        }
    } while (spins++ < 2);

    return -1;
}

ssize_t Balau::Selectable::write(const void * buf, size_t count) throw (GeneralException) {
    if (count == 0)
        return 0;

    AAssert(m_fd >= 0, "You can't call write() on a closed selectable");

    int spins = 0;

    do {
        ssize_t r = send(m_fd, (const char *) buf, count, 0);

        EAssert(r != 0, "send() returned 0 (broken pipe ?)");

        if (r > 0)
            return r;

#ifndef _WIN32
        if (errno == EPIPE) {
            close();
            return 0;
        }
#endif

        if ((errno == EAGAIN) || (errno == EINTR) || (errno == EWOULDBLOCK)) {
            Task::operationYield(m_evtW, Task::INTERRUPTIBLE);
        } else {
            m_evtW->stop();
            return r;
        }
    } while (spins++ < 2);

    return -1;
}