aboutsummaryrefslogtreecommitdiffstats
path: root/toj/center/src/netio.h
blob: db54b23a19767a708f49cc211654443786b746e2 (plain) (blame)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
class netio_protoiofn{
public:
    virtual void operator()(void *buf,size_t len,void *data) = 0;
};

template<typename C>
class netio_iofn : public netio_protoiofn{
private:
    typedef void (C::*netio_iofn_type)(void *buf,size_t len,void *data);
    C *obj;
    netio_iofn_type fn;

public:
    netio_iofn(C *obj,netio_iofn_type fn){
        this->obj = obj;
        this->fn = fn;
    }
    void operator()(void *buf,size_t len,void *data){
        (obj->*fn)(buf,len,data);
    }
};

#define NETIO_IOTYPE_PLAIN 0
#define NETIO_IOTYPE_FILE 1
class netio_iocb{
public:
    int type;

    void *buf;
    int fd;
    off_t off;
    size_t len;
    netio_protoiofn *cb_fn; 
    void *cb_data;

    netio_iocb(void *buf,size_t len,netio_protoiofn *cb_fn,void *cb_data){
        this->type = NETIO_IOTYPE_PLAIN;
        this->buf = buf;
        this->off = 0;
        this->len = len; 
        this->cb_fn = cb_fn;
        this->cb_data = cb_data;
    }
    netio_iocb(int fd,size_t len,netio_protoiofn *cb_fn,void *cb_data){
        this->type = NETIO_IOTYPE_FILE;
        this->buf = &this->fd;
        this->fd = fd;
        this->off = 0;
        this->len = len; 
        this->cb_fn = cb_fn;
        this->cb_data = cb_data;
    }
};

#define NETIO_IOSIZE 65536
class netio{
private:
    netio_iocb *read_iocb;
    std::queue<netio_iocb*> write_queue;
    bool readio_reen;
    char readio_buf[NETIO_IOSIZE];

public:
    int fd;
    virtual int readidle() = 0;

    netio(int fd){
        this->fd = fd;
        this->read_iocb = NULL;
        this->readio_reen = false;
    }
    ~netio(){
        close(this->fd);
    }
    int readio(){
        int ret;
        size_t len;
        netio_iocb *iocb;

        if(readio_reen == true){
            return -1;
        }
        readio_reen = true;

        while(true){
            if(read_iocb == NULL){
                readidle();
            }

            iocb = read_iocb;
            if(iocb->type == NETIO_IOTYPE_PLAIN){
                while((ret = read(fd,(char*)iocb->buf + iocb->off,iocb->len - iocb->off)) > 0){
                    iocb->off += ret;
                }
            }else if(iocb->type == NETIO_IOTYPE_FILE){
                while(true){
                    len = iocb->len - iocb->off;
                    if(len >= NETIO_IOSIZE){
                        len = NETIO_IOSIZE;
                    }
                    if((ret = read(fd,readio_buf,len)) <= 0){
                        break;
                    }

                    write(iocb->fd,readio_buf,ret);
                    iocb->off += ret;
                }
            }
            if(iocb->off == iocb->len){
                read_iocb = NULL;

                if(iocb->cb_fn != NULL){
                    (*iocb->cb_fn)(iocb->buf,iocb->len,iocb->cb_data);
                }else{
                    if(iocb->type == NETIO_IOTYPE_PLAIN){
                        delete (char*)iocb->buf;
                    }else if(iocb->type == NETIO_IOTYPE_FILE){
                        close(iocb->fd);
                    }
                }

                delete iocb;
            }else{
                break;
            }
        }

        readio_reen = false;
        return 0;
    }
    int readbytes(void *buf,size_t len,netio_protoiofn *cb_fn,void *cb_data){
        read_iocb = new netio_iocb(buf,len,cb_fn,cb_data);
        readio();
        return 0;
    }
    int readfile(int fd,size_t len,netio_protoiofn *cb_fn,void *cb_data){
        read_iocb = new netio_iocb(fd,len,cb_fn,cb_data);
        readio();
        return 0;
    }
    int writeio(){
        int ret;
        size_t len;
        netio_iocb *iocb;

        while(!write_queue.empty()){
            iocb = write_queue.front();
            if(iocb->type == NETIO_IOTYPE_PLAIN){
                while((ret = write(fd,(char*)iocb->buf + iocb->off,iocb->len - iocb->off)) > 0){
                    iocb->off += ret;
                }
            }else if(iocb->type == NETIO_IOTYPE_FILE){
                len = iocb->len - iocb->off;
                if(len >= NETIO_IOSIZE){
                    len = NETIO_IOSIZE;
                }
                while((ret = sendfile(fd,iocb->fd,NULL,len)) > 0){
                    iocb->off += ret;
                }
            }
            if(iocb->off == iocb->len){
                write_queue.pop();

                if(iocb->cb_fn != NULL){
                    (*iocb->cb_fn)(iocb->buf,iocb->len,iocb->cb_data);
                }else{
                    if(iocb->type == NETIO_IOTYPE_PLAIN){
                        delete (char*)iocb->buf;
                    }else if(iocb->type == NETIO_IOTYPE_FILE){
                        close(iocb->fd);
                    }
                }

                delete iocb;
            }else{
                break;
            }
        }

        return 0;
    }
    int writebytes(void *buf,size_t len,netio_protoiofn *cb_fn,void *cb_data){
        write_queue.push(new netio_iocb(buf,len,cb_fn,cb_data));
        writeio();
        return 0;
    }
    int writefile(int fd,size_t len,netio_protoiofn *cb_fn,void *cb_data){
        write_queue.push(new netio_iocb(fd,len,cb_fn,cb_data));
        writeio();
        return 0;
    }
};