xorp

buffered_asyncio.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 #ifndef __LIBXORP_BUFFERED_ASYNCIO_HH__
00023 #define __LIBXORP_BUFFERED_ASYNCIO_HH__
00024 
00025 #include "libxorp/xorp.h"
00026 #ifdef HAVE_FCNTL_H
00027 #include <fcntl.h>
00028 #endif
00029 #include "libxorp/callback.hh"
00030 #include "libxorp/eventloop.hh"
00031 
00032 class BufferedAsyncReader :
00033     public NONCOPYABLE
00034 {
00035 public:
00036     enum Event {
00037     DATA          = 1,
00038     OS_ERROR      = 2,
00039     END_OF_FILE       = 3
00040     };
00041 
00042     /*
00043      * Callback has arguments:
00044      *      BufferedAsyncReader*    caller,
00045      *      ErrorCode       e,
00046      *      uint8_t*        buffer,
00047      *      size_t          buffer_bytes,
00048      *          size_t          offset
00049      *
00050      * The callback is invoked when data arrives, when an error
00051      * occurs, or the end of file is detected.  For data, the callback is
00052      * only invoked when the threshold is crossed.
00053      */
00054     typedef XorpCallback4<void, BufferedAsyncReader*, Event, uint8_t*, size_t>::RefPtr Callback;
00055 
00065     BufferedAsyncReader(EventLoop&  e,
00066             XorpFd      fd,
00067             size_t      reserve_bytes,
00068             const Callback& cb,
00069             int priority = XorpTask::PRIORITY_DEFAULT);
00070 
00071     virtual ~BufferedAsyncReader();
00072 
00088     bool set_trigger_bytes(size_t bytes);
00089 
00093     size_t trigger_bytes() const;
00094 
00106     bool dispose(size_t bytes);
00107 
00114     bool set_reserve_bytes(size_t bytes);
00115 
00119     size_t reserve_bytes() const;
00120 
00124     size_t available_bytes() const;
00125 
00126     int error() const { return _last_error; }
00127 
00133     void start();
00134 
00140     void stop();
00141 
00142     virtual string toString() const;
00143 
00144 private:
00145     BufferedAsyncReader();      // Not directly constructible
00146 
00147 private:
00148     void io_event(XorpFd fd, IoEventType type);
00149     void announce_event(Event e);
00150     void provision_trigger_bytes();
00151 
00152     struct Config {
00153     uint8_t* head;      // The beginning of data
00154     size_t   head_bytes;    // The number of available bytes with data
00155     size_t   trigger_bytes; // The number of bytes to trigger cb delivery
00156     size_t   reserve_bytes; // The number of bytes to reserve for data
00157     };
00158 
00159     Config      _config;
00160 
00161     EventLoop&      _eventloop;
00162     XorpFd      _fd;
00163     Callback        _cb;
00164 
00165     vector<uint8_t> _buffer;
00166     XorpTimer       _ready_timer;
00167     int         _last_error;
00168     int         _priority;
00169 };
00170 
00171 #endif // __LIBXORP_BUFFERED_ASYNCIO_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations