Go to the documentation of this file.00001 #ifndef CASTOR_NET_CHANNELS_TCP_H
00002 #define CASTOR_NET_CHANNELS_TCP_H 1
00003
00004 #include <set>
00005
00006 #include <boost/enable_shared_from_this.hpp>
00007 #include <boost/thread/mutex.hpp>
00008 #include <boost/bind.hpp>
00009
00010 #include <asio.hpp>
00011
00012 #include <CastorChannel.h>
00013
00014 namespace castor { namespace net { namespace channels {
00015
00016 class Tcp : public CastorChannel {
00017
00018 private:
00019
00020 class Connection {
00021
00022 protected:
00023
00024 friend class Tcp;
00025
00026 NetAddress address;
00027
00028 asio::ip::tcp::socket socket;
00029
00030 Tcp *channel;
00031
00032 bool closed;
00033
00034 char buffer[8192];
00035
00036 void handleRead(const asio::error_code& error, size_t count);
00037
00038 Connection(const Connection &other);
00039 Connection &operator=(const Connection &other);
00040
00041 public:
00042
00043 Connection(asio::io_service &service, Tcp *tcp);
00044
00045 virtual ~Connection();
00046
00047 inline asio::ip::tcp::socket &getSocket();
00048
00049 void start(const NetAddress &address);
00050 void stop();
00051 };
00052
00053 typedef boost::shared_ptr<Connection> ConnectionPtr;
00054 typedef std::map<NetAddress, ConnectionPtr> ConnectionMap;
00055
00056 ConnectionMap connections;
00057
00058 asio::io_service *service;
00059
00060 asio::ip::tcp::acceptor acceptor;
00061 asio::ip::tcp::socket socket;
00062
00063 asio::ip::tcp::endpoint endpoint;
00064 asio::ip::tcp::endpoint remoteEndpoint;
00065
00066 boost::mutex mutex;
00067
00068 bool opened;
00069
00070 protected:
00071
00072 Tcp(asio::io_service &service);
00073
00074 void add(ConnectionPtr connection);
00075 void remove(const NetAddress &address);
00076
00077 void handleAccept(ConnectionPtr connection, const asio::error_code& error);
00078 void handleConnect(const asio::error_code& error);
00079 void handleSend(const asio::error_code& error, size_t count);
00080 void handleRead(const asio::error_code& error, size_t count);
00081
00082 public:
00083
00084 Tcp(asio::io_service &service, const NetAddress &a);
00085
00086 virtual ~Tcp();
00087
00088 virtual NetAddress getLocalAddress();
00089
00090 virtual void setAsync(bool async);
00091
00092 virtual void open();
00093 virtual void open(const NetAddress &a);
00094 virtual void bind();
00095
00096 virtual void reuseAddress(bool on);
00097 virtual bool reuseAddress();
00098
00099 virtual void enableLoopback(bool on);
00100 virtual bool enableLoopback();
00101
00102 virtual void enableBroadcast(bool on);
00103 virtual bool enableBroadcast();
00104
00105 virtual void multicastHops(int hops);
00106 virtual int multicastHops();
00107
00108 virtual void joinGroup(const asio::ip::address &address);
00109 virtual void leaveGroup(const asio::ip::address &address);
00110
00111 virtual void accept() throw(NetException);
00112 virtual void connect() throw(NetException);
00113
00114 virtual void close() throw(NetException);
00115 virtual void close(const NetAddress &address) throw(NetException);
00116 virtual bool isOpen();
00117
00118 virtual size_t receive(char *data, size_t length);
00119 virtual size_t receive(char *data, size_t length, NetAddress &remote);
00120
00121 virtual void send(const char *data, size_t length);
00122 virtual void send(const char *data, size_t length, const NetAddress &remote);
00123
00124 };
00125
00126 } } }
00127
00128 #endif
00129