File: aios.h

package info (click to toggle)
sfs 1%3A0.5k-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,388 kB
  • ctags: 8,556
  • sloc: cpp: 43,410; ansic: 17,574; sh: 8,412; makefile: 771; yacc: 277; lex: 96; sed: 47
file content (173 lines) | stat: -rw-r--r-- 4,631 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
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
// -*-c++-*-
/* $Id: aios.h,v 1.19 2001/10/29 23:05:50 ericp Exp $ */

/*
 *
 * Copyright (C) 1998 David Mazieres (dm@uun.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

#ifndef _ASYNC_AIOS_H_
#define _ASYNC_AIOS_H_ 1

#include "str.h"
#include "union.h"
#include "init.h"
#include "cbuf.h"

struct timecb_t;

INIT(aiosinit);

class aios : virtual public refcount {
  friend class aiosout;

  typedef callback<void, str, int>::ptr rcb_t;
  typedef callback<void, int>::ptr wcb_t;

  int fd;
  int err;
  bool eof;
  bool weof;

  bool rlock;
  cbuf inb;
  bool (aios::*infn) ();
  rcb_t rcb;

  strbuf outb;
  bool wblock;

  time_t timeoutval;
  time_t timeoutnext;
  timecb_t *timeoutcb;

  str debugname;
  ssize_t debugiov;

  vec<int> fdsendq;

  void fail (int e);
  void mkrcb (const str &s)
    { infn = &aios::rnone; rcb_t::ref cb = rcb; rcb = NULL; (*cb) (s, err); }
  void timeoutcatch ();
  void timeoutbump ();

  void input ();
  bool rnone () { return false; }
  bool rline ();
  bool rany ();
  void setreadcb (bool (aios::*fn) (), rcb_t cb);

  void mkwcb (wcb_t cb) { if (fd >= 0) (*cb) (err); }
  void (output) ();
  void setoutcb ();
  void schedwrite ();

  void dumpdebug ();
  void outstart () {
    assert (!weof);
    if (debugname) {
      outb.tosuio ()->breakiov ();
      debugiov = outb.tosuio ()->iovcnt ();
    }
  }
  void outstop () {
    if (debugname)
      dumpdebug ();
    debugiov = -1;
    schedwrite ();
  }

protected:
  aios (int, size_t);
  ~aios ();
  void finalize ();

public:
  enum { defrbufsize = 0x2000 };
  static ref<aios> alloc (int fd, size_t rbsz = defrbufsize)
    { return New refcounted<aios> (fd, rbsz); }
  int fdno () { return fd; }
  void setdebug (str name) { debugname = name; }
  void settimeout (time_t secs) { timeoutval = secs; timeoutbump (); }
  void abort ();

  void setrbufsize (size_t n) { inb.resize (n); }
  void readline (rcb_t cb) { setreadcb (&aios::rline, cb); }
  void readany (rcb_t cb) { setreadcb (&aios::rany, cb); }
  void unread (size_t n) { inb.unrembytes (n); }

  void writev (const iovec *iov, int iovcnt);
  void write (void *buf, size_t len)
    { iovec iov = { iovbase_t (buf), len }; writev (&iov, 1); }
  void sendeof ();
  void setwcb (wcb_t cb)
    { suio_callback (outb.tosuio (), wrap (this, &aios::mkwcb, cb)); }
  int flush ();
  void sendfd (int sfd) { fdsendq.push_back (sfd); }
};
typedef ref<aios> aios_t;

class aiosout : public strbuf {
  aios *s;

  // aiosout (aiosout &o) : strbuf (o), s (o.s) { o.s = NULL; }
  aiosout &operator= (const aiosout &);
public:
  /* XXX - We intentionally make the copy constructor public and
   * undefined, because aiosout objects cannot be copied.  No
   * reasonable compiler should copy an aiosout during reference copy
   * initialization (e.g., ``aout << "hello world\n";'').  However,
   * section 8.5.3/5 of the C++ standard implies the compiler is
   * allowed to make copies of an aios object during reference copy
   * initialization, because it can make and initialize a temporary
   * aiosout using copy (not direct) initialization from the const
   * aios_t &.  Section 12.1/1 therefore requires that the copy
   * constructor be accessible, so we can't make it private either. */
  aiosout (const aiosout &o);

  aiosout (const aios_t &a) : strbuf (a->outb), s(a) { s->outstart (); }
  aiosout (const aios_t::ptr &a) : strbuf (a->outb), s(a) { s->outstart (); }
  ~aiosout () { s->outstop (); }
};

template<class T> inline const strbuf &
operator<< (const aiosout &o, const T &a)
{
  return strbuf_cat (o, a);
}
inline const strbuf &
operator<< (const aiosout &o, const str &s)
{
  suio_print (o.tosuio (), s);
  return o;
}
// XXX - gcc bug requires this:
inline const strbuf &
operator<< (const aiosout &o, const char *a)
{
  return strbuf_cat (o, a);
}

// extern const str endl;
#ifndef __AIOS_IMPLEMENTATION
extern aios_t ain, aout;
#endif /* !__AIOS_IMPLEMENTATION */

#endif /* !_ASYNC_AIOS_H_ */