iofetch.h 4.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

15 16
#ifndef __IOFETCH_H
#define __IOFETCH_H 1
17 18 19 20 21 22 23 24 25 26 27 28 29 30

#include <config.h>

#include <asio.hpp>
#include <boost/shared_array.hpp>
#include <boost/shared_ptr.hpp>

#include <dns/buffer.h>
#include <dns/message.h>
#include <dns/messagerenderer.h>

#include <asiolink/asiolink.h>
#include <asiolink/internal/coroutine.h>

Scott Mann's avatar
Scott Mann committed
31
// This file contains TCP/UDP-specific implementations of generic classes 
32 33 34 35 36
// defined in asiolink.h.  It is *not* intended to be part of the public
// API.

namespace asiolink {
//
Scott Mann's avatar
Scott Mann committed
37
// Asynchronous UDP/TCP coroutine for upstream fetches
38
//
Scott Mann's avatar
Scott Mann committed
39
//class IOFetch : public coroutine, public UdpFetch, public TcpFetch {
40
class IOFetch : public coroutine {
41
public:
42
    // TODO Maybe this should be more generic than just for IOFetch?
43 44 45 46 47 48 49 50 51 52 53
    ///
    /// \brief Result of the query
    ///
    /// This is related only to contacting the remote server. If the answer
    ///indicates error, it is still counted as SUCCESS here, if it comes back.
    ///
    enum Result {
        SUCCESS,
        TIME_OUT,
        STOPPED
    };
54
    /// Abstract callback for the IOFetch.
55 56 57 58
    class Callback {
    public:
        virtual ~Callback() {}

59
        /// This will be called when the IOFetch is completed
60 61 62 63 64 65 66 67 68 69
        virtual void operator()(Result result) = 0;
    };
    ///
    /// \brief Constructor.
    ///
    /// It creates the query.
    /// @param callback will be called when we terminate. It is your task to
    ///        delete it if allocated on heap.
    ///@param timeout in ms.
    ///
Scott Mann's avatar
Scott Mann committed
70
    IOFetch(asio::io_service& io_service,
71 72 73
                      const isc::dns::Question& q,
                      const IOAddress& addr, uint16_t port,
                      isc::dns::OutputBufferPtr buffer,
Scott Mann's avatar
Scott Mann committed
74 75
                      Callback* callback, int timeout = -1, 
                      int protocol = IPPROTO_UDP);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    void operator()(asio::error_code ec = asio::error_code(),
                    size_t length = 0);
    /// Terminate the query.
    void stop(Result reason = STOPPED);
private:
    enum { MAX_LENGTH = 4096 };

    ///
    /// \short Private data
    ///
    /// They are not private because of stability of the
    /// interface (this is private class anyway), but because this class
    /// will be copyed often (it is used as a coroutine and passed as callback
    /// to many async_*() functions) and we want keep the same data. Some of
    /// the data is not copyable too.
    ///
Scott Mann's avatar
Scott Mann committed
92 93 94 95 96 97
    //struct IOFetchProtocol;
    //boost::shared_ptr<IOFetchProtocol> data_;
    //struct UdpData;
    //struct TcpData;
    boost::shared_ptr<UdpFetch> data_;
    boost::shared_ptr<TcpFetch> tcp_data_;
98
};
Scott Mann's avatar
Scott Mann committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
class UdpFetch : public IOFetch {
    public:
        struct UdpData;
        explicit UdpFetch(asio::io_service& io_service,
                          const isc::dns::Question& q,
                          const IOAddress& addr,
                          uint16_t port,
                          isc::dns::OutputBufferPtr buffer,
                          IOFetch::Callback *callback,
                          int timeout);
};
class TcpFetch : public IOFetch {
    public:
        struct TcpData;
        explicit TcpFetch(io_service& io_service, const Question& q,
                 const IOAddress& addr, uint16_t port,
                 OutputBufferPtr buffer, Callback *callback, int timeout);
};

118 119 120
}


121
#endif // __IOFETCH_H
122 123 124 125

// Local Variables: 
// mode: c++
// End: