$search
00001 #include "CastorChannel.h" 00002 #include "Tcp.h" 00003 #include "Udp.h" 00004 #include "Exception.h" 00005 00006 namespace castor { namespace net { 00007 00008 CastorChannel::CastorChannel(asio::io_service &service) : 00009 address(), buffer(), service(&service), strand(service), 00010 thread(), mode(mode_undefined), cb(), interfaces(), 00011 localAddresses(), closed(false), async(true), callback(), callbackExt(), 00012 peerAdded(), peerRemoved(), connectionLost(), connectionEstablished() 00013 { 00014 getIfaddrs(); 00015 } 00016 00017 CastorChannel::CastorChannel(const CastorChannel &other) : 00018 boost::enable_shared_from_this<CastorChannel>(), 00019 address(other.address), buffer(), service(other.service), 00020 strand(*other.service), thread(), mode(other.mode), cb(other.cb), 00021 interfaces(other.interfaces), localAddresses(other.localAddresses), 00022 closed(other.closed), async(true), callback(other.callback), 00023 callbackExt(other.callbackExt), peerAdded(other.peerAdded), 00024 peerRemoved(other.peerRemoved), connectionLost(other.connectionLost), 00025 connectionEstablished(other.connectionEstablished) 00026 { 00027 } 00028 00029 CastorChannel::~CastorChannel() { 00030 } 00031 00032 void CastorChannel::getIfaddrs() { 00033 00034 NetInterfacePtrList list = NetInterface::getInterfaces(); 00035 00036 this->interfaces.clear(); 00037 this->localAddresses.clear(); 00038 00039 for (size_t i = 0; i < list.size(); i++) { 00040 NetInterfacePtr iface = list[i]; 00041 this->interfaces[iface->getName()] = iface; 00042 00043 NetAddressPtrSet addresses = iface->getAddresses(); 00044 00045 for (NetAddressPtrSet::iterator itr = addresses.begin(); 00046 itr != addresses.end(); itr++) 00047 { 00048 this->localAddresses.insert((*itr)->getAddress()); 00049 } 00050 } 00051 00052 /* for (InterfaceMap::iterator itr = this->interfaces.begin(); 00053 itr != this->interfaces.end(); itr++) 00054 { 00055 std::cout << itr->second->str() << std::endl; 00056 }*/ 00057 } 00058 00059 void CastorChannel::registerCallback(CCallbackPtr cb) { 00060 this->cb = cb; 00061 } 00062 00063 /* void CastorChannel::signalReceive(const char *buffer, size_t count, NetAddressPtr address) { 00064 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 // if (this->cb) { 00072 // this->cb->call(buffer, count, address); 00073 // } 00074 } 00075 */ 00076 CastorChannelPtr CastorChannel::create(asio::io_service &service, const std::string spec) throw(NetException) { 00077 00078 NetAddressPtr address = NetAddress::create(spec); 00079 00080 return CastorChannel::create(service, *address.get()); 00081 } 00082 00083 CastorChannelPtr CastorChannel::create(asio::io_service &service, const NetAddress &address) throw(NetException) { 00084 00085 switch (address.getProtocol()) { 00086 00087 case IPPROTO_IP: 00088 case IPPROTO_TCP: 00089 return CastorChannelPtr(new castor::net::channels::Tcp(service, address)); 00090 00091 case IPPROTO_UDP: 00092 return CastorChannelPtr(new castor::net::channels::Udp(service, address)); 00093 00094 } 00095 00096 throw NetException("Channel: unable to create new sender for %s!", address.str().c_str()); 00097 } 00098 00099 asio::mutable_buffers_1 CastorChannel::getBuffer() { 00100 // std::cout << "Cleaning buffer " << sizeof(this->buffer) << std::endl; 00101 // memset(this->buffer, 0, sizeof(this->buffer)); 00102 return asio::buffer(this->buffer, sizeof(this->buffer)); 00103 } 00104 00105 asio::io_service &CastorChannel::getIoService() { 00106 return *service; 00107 } 00108 00109 void CastorChannel::start() { 00110 this->thread = ThreadType(new asio::thread(boost::bind(&CastorChannel::run, this))); 00111 00112 // this->thread->join(); 00113 } 00114 00115 void CastorChannel::run() { 00116 try { 00117 this->service->run(); 00118 00119 } catch (std::exception &e) { 00120 std::cerr << e.what() << std::endl; 00121 } catch (...) { 00122 std::cerr << "Channel: uncaught exception" << std::endl; 00123 } 00124 } 00125 00126 void CastorChannel::join() { 00127 00128 this->service->stop(); 00129 00130 if (this->thread) { 00131 this->thread->join(); 00132 this->service->reset(); 00133 } 00134 } 00135 00136 NetAddress &CastorChannel::getAddress() { 00137 return this->address; 00138 } 00139 00140 } }