Class: AIO::CB

Inherits:
Object
  • Object
show all
Defined in:
ext/aio/aio.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'ext/aio/aio.c', line 166

static VALUE
control_block_initialize(int argc, VALUE *argv, VALUE cb)
{
    VALUE file, mode;
    VALUE args[2];
    rb_scan_args(argc, argv, "02", &file, &mode);
    if (RTEST(file)){ 
      args[0] = file;
      args[1] = mode;
      control_block_open(1, (VALUE *)args, cb);
    }
    if (rb_block_given_p()) rb_obj_instance_eval( 0, 0, cb );
    return cb;
}

Instance Method Details

#bufObject



233
234
235
236
237
238
# File 'ext/aio/aio.c', line 233

static VALUE
control_block_buf_get(VALUE cb)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    return cbs->cb.aio_buf == NULL ? rb_str_new2("") : rb_str_new((char *)cbs->cb.aio_buf, cbs->cb.aio_nbytes);
}

#buf=(buf) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'ext/aio/aio.c', line 240

static VALUE
control_block_buf_set(VALUE cb, VALUE buf)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    Check_Type(buf, T_STRING);
    if (cbs->cb.aio_buf != NULL){ 
      free((char *)cbs->cb.aio_buf);
      cbs->cb.aio_buf = NULL;
    }
    cbs->cb.aio_buf = RSTRING_PTR(buf);
    cbs->cb.aio_nbytes = RSTRING_LEN(buf);
    return buf;
}

#callbackObject



201
202
203
204
205
206
# File 'ext/aio/aio.c', line 201

static VALUE
control_block_callback_get(VALUE cb)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    return cbs->rcb;
}

#callback=(rcb) ⇒ Object



208
209
210
211
212
213
214
215
# File 'ext/aio/aio.c', line 208

static VALUE
control_block_callback_set(VALUE cb, VALUE rcb)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    if (RBASIC(rcb)->klass != rb_cProc) rb_aio_error("Block required for callback!");
    cbs->rcb = rcb;
    return cbs->rcb;
}

#closeObject



337
338
339
340
341
342
343
344
345
346
# File 'ext/aio/aio.c', line 337

static VALUE
control_block_close(VALUE cb)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    if NIL_P(cbs->io) return Qfalse;
    rb_io_close(cbs->io);
    cbs->io = Qnil; 
    cbs->rcb = Qnil;
    return Qtrue;
}

#closed?Boolean

Returns:

  • (Boolean)


330
331
332
333
334
335
# File 'ext/aio/aio.c', line 330

static VALUE
control_block_closed_p(VALUE cb)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    return NIL_P(cbs->io) ? Qtrue : Qfalse;
}

#fildesObject



217
218
219
220
221
222
# File 'ext/aio/aio.c', line 217

static VALUE
control_block_fildes_get(VALUE cb)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    return INT2FIX(cbs->cb.aio_fildes);
}

#fildes=(fd) ⇒ Object



224
225
226
227
228
229
230
231
# File 'ext/aio/aio.c', line 224

static VALUE
control_block_fildes_set(VALUE cb, VALUE fd)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    Check_Type(fd, T_FIXNUM);
    cbs->cb.aio_fildes = FIX2INT(fd);
    return fd;
}

#lio_opcodeObject



293
294
295
296
297
298
# File 'ext/aio/aio.c', line 293

static VALUE
control_block_lio_opcode_get(VALUE cb)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    return INT2FIX(cbs->cb.aio_lio_opcode);
}

#lio_opcode=(opcode) ⇒ Object



300
301
302
303
304
305
306
307
308
# File 'ext/aio/aio.c', line 300

static VALUE
control_block_lio_opcode_set(VALUE cb, VALUE opcode)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    Check_Type(opcode, T_FIXNUM);
    if (opcode != c_aio_read && opcode != c_aio_write) rb_aio_error( "Only AIO::READ and AIO::WRITE modes supported" );
    cbs->cb.aio_lio_opcode = NUM2INT(opcode);
    return opcode;
}

#nbytesObject



254
255
256
257
258
259
# File 'ext/aio/aio.c', line 254

static VALUE
control_block_nbytes_get(VALUE cb)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    return INT2FIX(cbs->cb.aio_nbytes);
}

#nbytes=(bytes) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'ext/aio/aio.c', line 82

static VALUE
control_block_nbytes_set(VALUE cb, VALUE bytes)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    Check_Type(bytes, T_FIXNUM);
    cbs->cb.aio_nbytes = FIX2INT(bytes);
    if (cbs->cb.aio_buf != NULL) free((char *)cbs->cb.aio_buf);
    cbs->cb.aio_buf = malloc(cbs->cb.aio_nbytes + 1);
    if (!cbs->cb.aio_buf) rb_aio_error( "not able to allocate a read buffer" ); 
    return bytes;
}

#offsetObject



261
262
263
264
265
266
# File 'ext/aio/aio.c', line 261

static VALUE
control_block_offset_get(VALUE cb)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    return INT2FIX(cbs->cb.aio_offset);
}

#offset=(offset) ⇒ Object



268
269
270
271
272
273
274
275
# File 'ext/aio/aio.c', line 268

static VALUE
control_block_offset_set(VALUE cb, VALUE offset)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    Check_Type(offset, T_FIXNUM);
    cbs->cb.aio_offset = FIX2INT(offset);
    return offset;
}

#open(*args) ⇒ Object



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
# File 'ext/aio/aio.c', line 94

static VALUE
control_block_open(int argc, VALUE *argv, VALUE cb)
{
   const char *fmode;
#ifdef RUBY19
    rb_io_t *fptr;
#else 
    OpenFile *fptr;
#endif
    VALUE file, mode;
    rb_aiocb_t *cbs = GetCBStruct(cb);
    rb_scan_args(argc, argv, "02", &file, &mode);
    fmode = NIL_P(mode) ? "r" : RSTRING_PTR(mode);
    struct stat stats;
   
    Check_Type(file, T_STRING); 

    cbs->io = rb_file_open(RSTRING_PTR(file), fmode);
    GetOpenFile(cbs->io, fptr);
    rb_io_check_readable(fptr);   

    if ( cbs->cb.aio_fildes == 0 && cbs->cb.aio_nbytes == 0){
#ifdef RUBY19
      cbs->cb.aio_fildes = fptr->fd;
#else 
      cbs->cb.aio_fildes = fileno(fptr->f);
#endif
      fstat(cbs->cb.aio_fildes, &stats);
      control_block_nbytes_set(cb, INT2FIX(stats.st_size));
    }
    return cb;    
}

#open?Boolean

Returns:

  • (Boolean)


323
324
325
326
327
328
# File 'ext/aio/aio.c', line 323

static VALUE
control_block_open_p(VALUE cb)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    return NIL_P(cbs->io) ? Qfalse : Qtrue;
}

#pathObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'ext/aio/aio.c', line 181

static VALUE 
control_block_path(VALUE cb)
{ 
    rb_aiocb_t *cbs = GetCBStruct(cb);
#ifdef RUBY19
    rb_io_t *fptr;
#else 
    OpenFile *fptr;
#endif
    if NIL_P(cbs->io) return rb_str_new2("");
    GetOpenFile(cbs->io, fptr);
    rb_io_check_readable(fptr);
#ifdef RUBY19
    return rb_file_s_expand_path( 1, &fptr->pathv );
#else
    VALUE path = rb_str_new2(fptr->path);
    return rb_file_s_expand_path( 1, &path );
#endif
}

#reqprioObject



277
278
279
280
281
282
# File 'ext/aio/aio.c', line 277

static VALUE
control_block_reqprio_get(VALUE cb)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    return INT2FIX(cbs->cb.aio_reqprio);
}

#reqprio=(reqprio) ⇒ Object



284
285
286
287
288
289
290
291
# File 'ext/aio/aio.c', line 284

static VALUE
control_block_reqprio_set(VALUE cb, VALUE reqprio)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    Check_Type(reqprio, T_FIXNUM);
    cbs->cb.aio_reqprio = FIX2INT(reqprio);
    return reqprio;
}

#resetObject



147
148
149
150
151
152
153
# File 'ext/aio/aio.c', line 147

static VALUE
control_block_reset(VALUE cb)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    control_block_reset0(cbs);
    return cb;
}

#validateObject



310
311
312
313
314
315
316
317
318
319
320
321
# File 'ext/aio/aio.c', line 310

static VALUE
control_block_validate(VALUE cb)
{
    rb_aiocb_t *cbs = GetCBStruct(cb);
    if (cbs->cb.aio_fildes <= 0) rb_aio_error( "Invalid file descriptor" );    
    if (cbs->cb.aio_nbytes <= 0) rb_aio_error( "Invalid buffer length" );    
    if (cbs->cb.aio_offset < 0) rb_aio_error( "Invalid file offset" );    
    if (cbs->cb.aio_reqprio < 0) rb_aio_error( "Invalid request priority" );
    if (cbs->cb.aio_lio_opcode != LIO_READ && cbs->cb.aio_lio_opcode != LIO_WRITE) rb_aio_error( "Only AIO::READ and AIO::WRITE modes supported" );
    if (!cbs->cb.aio_buf) rb_aio_error( "No buffer allocated" );  
    return cb;    
}