CastorChannel.h
Go to the documentation of this file.
00001 #ifndef CASTOR_NET_CASTORCHANNEL_H
00002 #define CASTOR_NET_CASTORCHANNEL_H 1
00003 
00004 #include <ifaddrs.h>
00005 
00006 #include <string>
00007 #include <vector>
00008 #include <set>
00009 
00010 #include <boost/function.hpp>
00011 #include <boost/algorithm/string.hpp>
00012 #include <boost/enable_shared_from_this.hpp>
00013 
00014 #include "asio.hpp"
00015 
00016 #include "Exception.h"
00017 #include "Network.h"
00018 #include "NetAddress.h"
00019 #include "NetInterface.h"
00020 #include "NetException.h"
00021 
00022 namespace castor { namespace net {
00023 
00024         class CastorChannel;
00025 
00026         typedef boost::shared_ptr<CastorChannel> CastorChannelPtr;
00027 
00028         typedef boost::function<void (const char *, size_t, NetAddressPtr)> CastorChannelCallback;
00029         typedef boost::function<void (const char *, size_t, NetAddressPtr, CastorChannelPtr)> CastorChannelExtCallback;
00030 
00031         typedef boost::function<void (const NetAddress &)> Event;
00032 
00033         class CCallback {
00034                 public:
00035                         virtual ~CCallback() { }
00036                         virtual void call(const char *data, size_t count, NetAddressPtr address) = 0;
00037         };
00038 
00039         typedef boost::shared_ptr<CCallback> CCallbackPtr;
00040 
00041         typedef asio::socket_base::reuse_address                ReuseAddress;
00042         typedef asio::ip::multicast::leave_group                LeaveGroup;
00043         typedef asio::ip::multicast::join_group                 JoinGroup;
00044         typedef asio::ip::multicast::enable_loopback    EnableLoopback;
00045         typedef asio::ip::multicast::hops                               MulticastHops;
00046 
00047         class CastorChannel : public boost::enable_shared_from_this<CastorChannel> {
00048 
00049                 protected:
00050 
00051                         NetAddress address;
00052 
00053                         char buffer[8192];
00054 
00055                         typedef boost::shared_ptr<asio::thread> ThreadType;
00056 
00057                         asio::io_service *service;
00058                         asio::io_service::strand strand;
00059 
00060                         ThreadType thread;
00061 
00062                         CastorChannel(asio::io_service &service);
00063 
00064                         inline void signalReceive(const char *buffer, size_t count, NetAddressPtr address) {
00065                                 if (this->callback) {
00066                                         this->callback(buffer, count, address);
00067                                 }
00068                                 if (this->callbackExt) {
00069                                         this->callbackExt(buffer, count, address, shared_from_this());
00070                                 }
00071                         }
00072 
00073 
00074                         enum { mode_undefined, mode_accept, mode_connect } mode;
00075 
00076                         CCallbackPtr cb;
00077 
00078                         static CastorChannelPtr create(asio::io_service &service, const std::string spec) throw(NetException);
00079                         static CastorChannelPtr create(asio::io_service &service, const NetAddress &address) throw(NetException);
00080 
00081                         friend class Communication;
00082 
00083                         typedef std::map<std::string, NetInterfacePtr> InterfaceMap;
00084                         typedef std::set<NetAddress> AddressSet;
00085 
00086                         InterfaceMap interfaces;
00087                         AddressSet localAddresses;
00088 
00089                         bool closed;
00090                         bool async;
00091 
00092                         void getIfaddrs();
00093 
00094                 public:
00095 
00096                         CastorChannelCallback callback;
00097                         CastorChannelExtCallback callbackExt;
00098 
00099                         Event peerAdded;
00100                         Event peerRemoved;
00101 
00102                         Event connectionLost;
00103                         Event connectionEstablished;
00104 
00105                         CastorChannel(const CastorChannel &other);
00106 
00107                         virtual ~CastorChannel();
00108 
00109                         void registerCallback(CCallbackPtr cb);
00110 
00111                         asio::mutable_buffers_1 getBuffer();
00112 
00113                         NetAddress &getAddress();
00114 
00115                         asio::io_service &getIoService();
00116                         void start();
00117                         void join();
00118                         void run();
00119 
00120                         virtual NetAddress getLocalAddress() = 0;
00121 
00122                         bool isAsync() { return this->async; }
00123 
00124                         virtual void setAsync(bool value) = 0;
00125 
00126                         virtual void open() = 0;
00127                         virtual void open(const NetAddress &a) = 0;
00128                         virtual void bind() = 0;
00129 
00130                         virtual void reuseAddress(bool on) = 0;
00131                         virtual bool reuseAddress() = 0;
00132 
00133                         virtual void enableLoopback(bool on) = 0;
00134                         virtual bool enableLoopback() = 0;
00135 
00136                         virtual void enableBroadcast(bool on) = 0;
00137                         virtual bool enableBroadcast() = 0;
00138 
00139                         virtual void multicastHops(int hops) = 0;
00140                         virtual int multicastHops() = 0;
00141 
00142                         virtual void joinGroup(const asio::ip::address &address) = 0;
00143                         virtual void leaveGroup(const asio::ip::address &address) = 0;
00144 
00145                         virtual void accept() throw(NetException) = 0;
00146                         virtual void connect() throw(NetException) = 0;
00147 
00148                         virtual void close() throw(NetException) = 0;
00149                         virtual void close(const NetAddress &address) throw(NetException) = 0;
00150                         virtual bool isOpen() = 0;
00151 
00152                         virtual size_t receive(char *data, size_t length) = 0;
00153                         virtual size_t receive(char *data, size_t length, NetAddress &remote) = 0;
00154 
00155                         virtual void send(const char *data, size_t length) = 0;
00156                         virtual void send(const char *data, size_t length, const NetAddress &remote) = 0;
00157         };
00158 
00159 } }
00160 
00161 #endif /* CASTOR_NET_CHANNEL_H */
00162 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


Castor
Author(s): Carpe Noctem
autogenerated on Fri Nov 8 2013 11:05:39