diff options
author | Marc Laukien <marc@zeroc.com> | 2002-01-15 15:14:53 +0000 |
---|---|---|
committer | Marc Laukien <marc@zeroc.com> | 2002-01-15 15:14:53 +0000 |
commit | 037ed23c6d3fd3e817f663c11ee254e8a7a13026 (patch) | |
tree | e8e47285272bf60dae6ea9aef929c432147fd9c7 /cpp/src | |
parent | fixes (diff) | |
download | ice-037ed23c6d3fd3e817f663c11ee254e8a7a13026.tar.bz2 ice-037ed23c6d3fd3e817f663c11ee254e8a7a13026.tar.xz ice-037ed23c6d3fd3e817f663c11ee254e8a7a13026.zip |
connected udp endpoints
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/Ice/Endpoint.cpp | 366 | ||||
-rw-r--r-- | cpp/src/Ice/Endpoint.h | 2 | ||||
-rw-r--r-- | cpp/src/Ice/ReferenceFactory.cpp | 26 | ||||
-rw-r--r-- | cpp/src/Ice/SUdpTransceiver.cpp | 10 | ||||
-rw-r--r-- | cpp/src/Ice/SUdpTransceiver.h | 6 | ||||
-rw-r--r-- | cpp/src/Ice/SslAcceptor.h | 3 | ||||
-rw-r--r-- | cpp/src/Ice/TcpAcceptor.h | 3 | ||||
-rw-r--r-- | cpp/src/Ice/UdpTransceiver.cpp | 58 | ||||
-rw-r--r-- | cpp/src/Ice/UdpTransceiver.h | 9 |
9 files changed, 311 insertions, 172 deletions
diff --git a/cpp/src/Ice/Endpoint.cpp b/cpp/src/Ice/Endpoint.cpp index ae1876d9f6e..c180be232fb 100644 --- a/cpp/src/Ice/Endpoint.cpp +++ b/cpp/src/Ice/Endpoint.cpp @@ -318,7 +318,6 @@ IceInternal::TcpEndpoint::TcpEndpoint(const InstancePtr& instance, const string& { throw EndpointParseException(__FILE__, __LINE__); } - const_cast<string&>(_host) = argument; break; } @@ -329,7 +328,6 @@ IceInternal::TcpEndpoint::TcpEndpoint(const InstancePtr& instance, const string& { throw EndpointParseException(__FILE__, __LINE__); } - const_cast<Int&>(_port) = atoi(argument.c_str()); break; } @@ -340,7 +338,6 @@ IceInternal::TcpEndpoint::TcpEndpoint(const InstancePtr& instance, const string& { throw EndpointParseException(__FILE__, __LINE__); } - const_cast<Int&>(_timeout) = atoi(argument.c_str()); break; } @@ -385,7 +382,11 @@ string IceInternal::TcpEndpoint::toString() const { ostringstream s; - s << "tcp -h " << _host << " -p " << _port << " -t " << _timeout; + s << "tcp -h " << _host << " -p " << _port; + if (_timeout != -1) + { + s << " -t " << _timeout; + } return s.str(); } @@ -494,13 +495,19 @@ IceInternal::TcpEndpoint::operator==(const Endpoint& r) const return false; } - struct sockaddr_in laddr; - struct sockaddr_in raddr; - getAddress(_host.c_str(), _port, laddr); - getAddress(p->_host.c_str(), p->_port, raddr); - if (memcmp(&laddr, &raddr, sizeof(struct sockaddr_in)) != 0) + if (_host != p->_host) { - return false; + // + // We do the most time-consuming part of the comparison last. + // + struct sockaddr_in laddr; + struct sockaddr_in raddr; + getAddress(_host.c_str(), _port, laddr); + getAddress(p->_host.c_str(), p->_port, raddr); + if (memcmp(&laddr, &raddr, sizeof(struct sockaddr_in)) != 0) + { + return false; + } } return true; @@ -548,19 +555,6 @@ IceInternal::TcpEndpoint::operator<(const Endpoint& r) const return false; } - struct sockaddr_in laddr; - struct sockaddr_in raddr; - getAddress(_host.c_str(), _port, laddr); - getAddress(p->_host.c_str(), p->_port, raddr); - if (laddr.sin_addr.s_addr < raddr.sin_addr.s_addr) - { - return true; - } - else if (raddr.sin_addr.s_addr < laddr.sin_addr.s_addr) - { - return false; - } - if (_port < p->_port) { return true; @@ -579,6 +573,25 @@ IceInternal::TcpEndpoint::operator<(const Endpoint& r) const return false; } + if (_host != p->_host) + { + // + // We do the most time-consuming part of the comparison last. + // + struct sockaddr_in laddr; + struct sockaddr_in raddr; + getAddress(_host.c_str(), _port, laddr); + getAddress(p->_host.c_str(), p->_port, raddr); + if (laddr.sin_addr.s_addr < raddr.sin_addr.s_addr) + { + return true; + } + else if (raddr.sin_addr.s_addr < laddr.sin_addr.s_addr) + { + return false; + } + } + return false; } @@ -620,36 +633,47 @@ IceInternal::SslEndpoint::SslEndpoint(const InstancePtr& instance, const string& throw EndpointParseException(__FILE__, __LINE__); } - beg = str.find_first_not_of(delim, end); - if (beg == string::npos) - { - throw EndpointParseException(__FILE__, __LINE__); - } - - end = str.find_first_of(delim, beg); - if (end == string::npos) + string argument; + string::size_type argumentBeg = str.find_first_not_of(delim, end); + if (argumentBeg != string::npos && str[argumentBeg] != '-') { - end = str.length(); + beg = argumentBeg; + end = str.find_first_of(delim, beg); + if (end == string::npos) + { + end = str.length(); + } + argument = str.substr(beg, end - beg); } - - string argument = str.substr(beg, end - beg); switch (option[1]) { case 'h': { + if (argument.empty()) + { + throw EndpointParseException(__FILE__, __LINE__); + } const_cast<string&>(_host) = argument; break; } case 'p': { + if (argument.empty()) + { + throw EndpointParseException(__FILE__, __LINE__); + } const_cast<Int&>(_port) = atoi(argument.c_str()); break; } case 't': { + if (argument.empty()) + { + throw EndpointParseException(__FILE__, __LINE__); + } const_cast<Int&>(_timeout) = atoi(argument.c_str()); break; } @@ -694,7 +718,11 @@ string IceInternal::SslEndpoint::toString() const { ostringstream s; - s << "ssl -h " << _host << " -p " << _port << " -t " << _timeout; + s << "ssl -h " << _host << " -p " << _port; + if (_timeout != -1) + { + s << " -t " << _timeout; + } return s.str(); } @@ -803,13 +831,19 @@ IceInternal::SslEndpoint::operator==(const Endpoint& r) const return false; } - struct sockaddr_in laddr; - struct sockaddr_in raddr; - getAddress(_host.c_str(), _port, laddr); - getAddress(p->_host.c_str(), p->_port, raddr); - if (memcmp(&laddr, &raddr, sizeof(struct sockaddr_in)) != 0) + if (_host != p->_host) { - return false; + // + // We do the most time-consuming part of the comparison last. + // + struct sockaddr_in laddr; + struct sockaddr_in raddr; + getAddress(_host.c_str(), _port, laddr); + getAddress(p->_host.c_str(), p->_port, raddr); + if (memcmp(&laddr, &raddr, sizeof(struct sockaddr_in)) != 0) + { + return false; + } } return true; @@ -857,19 +891,6 @@ IceInternal::SslEndpoint::operator<(const Endpoint& r) const return false; } - struct sockaddr_in laddr; - struct sockaddr_in raddr; - getAddress(_host.c_str(), _port, laddr); - getAddress(p->_host.c_str(), p->_port, raddr); - if (laddr.sin_addr.s_addr < raddr.sin_addr.s_addr) - { - return true; - } - else if (raddr.sin_addr.s_addr < laddr.sin_addr.s_addr) - { - return false; - } - if (_port < p->_port) { return true; @@ -888,19 +909,40 @@ IceInternal::SslEndpoint::operator<(const Endpoint& r) const return false; } + if (_host != p->_host) + { + // + // We do the most time-consuming part of the comparison last. + // + struct sockaddr_in laddr; + struct sockaddr_in raddr; + getAddress(_host.c_str(), _port, laddr); + getAddress(p->_host.c_str(), p->_port, raddr); + if (laddr.sin_addr.s_addr < raddr.sin_addr.s_addr) + { + return true; + } + else if (raddr.sin_addr.s_addr < laddr.sin_addr.s_addr) + { + return false; + } + } + return false; } IceInternal::UdpEndpoint::UdpEndpoint(const InstancePtr& instance, const string& ho, Int po) : _instance(instance), _host(ho), - _port(po) + _port(po), + _connect(false) { } IceInternal::UdpEndpoint::UdpEndpoint(const InstancePtr& instance, const string& str) : _instance(instance), - _port(0) + _port(0), + _connect(false) { static const string delim = " \t\n\r"; @@ -927,34 +969,51 @@ IceInternal::UdpEndpoint::UdpEndpoint(const InstancePtr& instance, const string& throw EndpointParseException(__FILE__, __LINE__); } - beg = str.find_first_not_of(delim, end); - if (beg == string::npos) - { - throw EndpointParseException(__FILE__, __LINE__); - } - - end = str.find_first_of(delim, beg); - if (end == string::npos) + string argument; + string::size_type argumentBeg = str.find_first_not_of(delim, end); + if (argumentBeg != string::npos && str[argumentBeg] != '-') { - end = str.length(); + beg = argumentBeg; + end = str.find_first_of(delim + ":", beg); + if (end == string::npos) + { + end = str.length(); + } + argument = str.substr(beg, end - beg); } - - string argument = str.substr(beg, end - beg); switch (option[1]) { case 'h': { + if (argument.empty()) + { + throw EndpointParseException(__FILE__, __LINE__); + } const_cast<string&>(_host) = argument; break; } case 'p': { + if (argument.empty()) + { + throw EndpointParseException(__FILE__, __LINE__); + } const_cast<Int&>(_port) = atoi(argument.c_str()); break; } + case 'c': + { + if (!argument.empty()) + { + throw EndpointParseException(__FILE__, __LINE__); + } + const_cast<bool&>(_connect) = true; + break; + } + default: { throw EndpointParseException(__FILE__, __LINE__); @@ -970,11 +1029,13 @@ IceInternal::UdpEndpoint::UdpEndpoint(const InstancePtr& instance, const string& IceInternal::UdpEndpoint::UdpEndpoint(BasicStream* s) : _instance(s->instance()), - _port(0) + _port(0), + _connect(false) { s->startReadEncaps(); s->read(const_cast<string&>(_host)); s->read(const_cast<Int&>(_port)); + s->read(const_cast<bool&>(_connect)); s->endReadEncaps(); } @@ -985,6 +1046,7 @@ IceInternal::UdpEndpoint::streamWrite(BasicStream* s) const s->startWriteEncaps(); s->write(_host); s->write(_port); + s->write(_connect); s->endWriteEncaps(); } @@ -993,6 +1055,10 @@ IceInternal::UdpEndpoint::toString() const { ostringstream s; s << "udp -h " << _host << " -p " << _port; + if (_connect) + { + s << " -c"; + } return s.str(); } @@ -1035,7 +1101,7 @@ IceInternal::UdpEndpoint::clientTransceiver() const TransceiverPtr IceInternal::UdpEndpoint::serverTransceiver(EndpointPtr& endp) const { - UdpTransceiver* p = new UdpTransceiver(_instance, _port); + UdpTransceiver* p = new UdpTransceiver(_instance, _port, _connect); endp = new UdpEndpoint(_instance, _host, p->effectivePort()); return p; } @@ -1089,15 +1155,26 @@ IceInternal::UdpEndpoint::operator==(const Endpoint& r) const return false; } - struct sockaddr_in laddr; - struct sockaddr_in raddr; - getAddress(_host.c_str(), _port, laddr); - getAddress(p->_host.c_str(), p->_port, raddr); - if (memcmp(&laddr, &raddr, sizeof(struct sockaddr_in)) != 0) + if (_connect != p->_connect) { return false; } + if (_host != p->_host) + { + // + // We do the most time-consuming part of the comparison last. + // + struct sockaddr_in laddr; + struct sockaddr_in raddr; + getAddress(_host.c_str(), _port, laddr); + getAddress(p->_host.c_str(), p->_port, raddr); + if (memcmp(&laddr, &raddr, sizeof(struct sockaddr_in)) != 0) + { + return false; + } + } + return true; } @@ -1143,41 +1220,58 @@ IceInternal::UdpEndpoint::operator<(const Endpoint& r) const return false; } - struct sockaddr_in laddr; - struct sockaddr_in raddr; - getAddress(_host.c_str(), _port, laddr); - getAddress(p->_host.c_str(), p->_port, raddr); - if (laddr.sin_addr.s_addr < raddr.sin_addr.s_addr) + if (_port < p->_port) { return true; } - else if (raddr.sin_addr.s_addr < laddr.sin_addr.s_addr) + else if (p->_port < _port) { return false; } - if (_port < p->_port) + if (!_connect && p->_connect) { return true; } - else if (p->_port < _port) + else if (!p->_connect && _connect) { return false; } + if (_host != p->_host) + { + // + // We do the most time-consuming part of the comparison last. + // + struct sockaddr_in laddr; + struct sockaddr_in raddr; + getAddress(_host.c_str(), _port, laddr); + getAddress(p->_host.c_str(), p->_port, raddr); + if (laddr.sin_addr.s_addr < raddr.sin_addr.s_addr) + { + return true; + } + else if (raddr.sin_addr.s_addr < laddr.sin_addr.s_addr) + { + return false; + } + } + return false; } IceInternal::SUdpEndpoint::SUdpEndpoint(const InstancePtr& instance, const string& ho, Int po) : _instance(instance), _host(ho), - _port(po) + _port(po), + _connect(false) { } IceInternal::SUdpEndpoint::SUdpEndpoint(const InstancePtr& instance, const string& str) : _instance(instance), - _port(0) + _port(0), + _connect(false) { static const string delim = " \t\n\r"; @@ -1204,34 +1298,51 @@ IceInternal::SUdpEndpoint::SUdpEndpoint(const InstancePtr& instance, const strin throw EndpointParseException(__FILE__, __LINE__); } - beg = str.find_first_not_of(delim, end); - if (beg == string::npos) - { - throw EndpointParseException(__FILE__, __LINE__); - } - - end = str.find_first_of(delim, beg); - if (end == string::npos) + string argument; + string::size_type argumentBeg = str.find_first_not_of(delim, end); + if (argumentBeg != string::npos && str[argumentBeg] != '-') { - end = str.length(); + beg = argumentBeg; + end = str.find_first_of(delim + ":", beg); + if (end == string::npos) + { + end = str.length(); + } + argument = str.substr(beg, end - beg); } - - string argument = str.substr(beg, end - beg); switch (option[1]) { case 'h': { + if (argument.empty()) + { + throw EndpointParseException(__FILE__, __LINE__); + } const_cast<string&>(_host) = argument; break; } case 'p': { + if (argument.empty()) + { + throw EndpointParseException(__FILE__, __LINE__); + } const_cast<Int&>(_port) = atoi(argument.c_str()); break; } + case 'c': + { + if (!argument.empty()) + { + throw EndpointParseException(__FILE__, __LINE__); + } + const_cast<bool&>(_connect) = true; + break; + } + default: { throw EndpointParseException(__FILE__, __LINE__); @@ -1247,11 +1358,13 @@ IceInternal::SUdpEndpoint::SUdpEndpoint(const InstancePtr& instance, const strin IceInternal::SUdpEndpoint::SUdpEndpoint(BasicStream* s) : _instance(s->instance()), - _port(0) + _port(0), + _connect(false) { s->startReadEncaps(); s->read(const_cast<string&>(_host)); s->read(const_cast<Int&>(_port)); + s->read(const_cast<bool&>(_connect)); s->endReadEncaps(); } @@ -1262,6 +1375,7 @@ IceInternal::SUdpEndpoint::streamWrite(BasicStream* s) const s->startWriteEncaps(); s->write(_host); s->write(_port); + s->write(_connect); s->endWriteEncaps(); } @@ -1270,6 +1384,10 @@ IceInternal::SUdpEndpoint::toString() const { ostringstream s; s << "sudp -h " << _host << " -p " << _port; + if (_connect) + { + s << " -c"; + } return s.str(); } @@ -1313,8 +1431,8 @@ IceInternal::SUdpEndpoint::clientTransceiver() const TransceiverPtr IceInternal::SUdpEndpoint::serverTransceiver(EndpointPtr& endp) const { -// SUdpTransceiver* p = new SUdpTransceiver(_instance, _port); - UdpTransceiver* p = new UdpTransceiver(_instance, _port); +// SUdpTransceiver* p = new SUdpTransceiver(_instance, _port, _connect); + UdpTransceiver* p = new UdpTransceiver(_instance, _port, _connect); endp = new SUdpEndpoint(_instance, _host, p->effectivePort()); return p; } @@ -1370,15 +1488,26 @@ IceInternal::SUdpEndpoint::operator==(const Endpoint& r) const return false; } - struct sockaddr_in laddr; - struct sockaddr_in raddr; - getAddress(_host.c_str(), _port, laddr); - getAddress(p->_host.c_str(), p->_port, raddr); - if (memcmp(&laddr, &raddr, sizeof(struct sockaddr_in)) != 0) + if (_connect != p->_connect) { return false; } + if (_host != p->_host) + { + // + // We do the most time-consuming part of the comparison last. + // + struct sockaddr_in laddr; + struct sockaddr_in raddr; + getAddress(_host.c_str(), _port, laddr); + getAddress(p->_host.c_str(), p->_port, raddr); + if (memcmp(&laddr, &raddr, sizeof(struct sockaddr_in)) != 0) + { + return false; + } + } + return true; } @@ -1422,27 +1551,42 @@ IceInternal::SUdpEndpoint::operator<(const Endpoint& r) const return false; } - struct sockaddr_in laddr; - struct sockaddr_in raddr; - getAddress(_host.c_str(), _port, laddr); - getAddress(p->_host.c_str(), p->_port, raddr); - if (laddr.sin_addr.s_addr < raddr.sin_addr.s_addr) + if (_port < p->_port) { return true; } - else if (raddr.sin_addr.s_addr < laddr.sin_addr.s_addr) + else if (p->_port < _port) { return false; } - if (_port < p->_port) + if (!_connect && p->_connect) { return true; } - else if (p->_port < _port) + else if (!p->_connect && _connect) { return false; } + if (_host != p->_host) + { + // + // We do the most time-consuming part of the comparison last. + // + struct sockaddr_in laddr; + struct sockaddr_in raddr; + getAddress(_host.c_str(), _port, laddr); + getAddress(p->_host.c_str(), p->_port, raddr); + if (laddr.sin_addr.s_addr < raddr.sin_addr.s_addr) + { + return true; + } + else if (raddr.sin_addr.s_addr < laddr.sin_addr.s_addr) + { + return false; + } + } + return false; } diff --git a/cpp/src/Ice/Endpoint.h b/cpp/src/Ice/Endpoint.h index 1eebf4e3d75..8d61916da2f 100644 --- a/cpp/src/Ice/Endpoint.h +++ b/cpp/src/Ice/Endpoint.h @@ -267,6 +267,7 @@ private: const InstancePtr _instance; const std::string _host; const ::Ice::Int _port; + const bool _connect; }; class SUdpEndpoint : public Endpoint @@ -303,6 +304,7 @@ private: const InstancePtr _instance; const std::string _host; const ::Ice::Int _port; + const bool _connect; }; } diff --git a/cpp/src/Ice/ReferenceFactory.cpp b/cpp/src/Ice/ReferenceFactory.cpp index d1fa68f4116..386466d41ca 100644 --- a/cpp/src/Ice/ReferenceFactory.cpp +++ b/cpp/src/Ice/ReferenceFactory.cpp @@ -184,9 +184,8 @@ IceInternal::ReferenceFactory::create(const string& str) { if (argument.empty()) { - throw EndpointParseException(__FILE__, __LINE__); + throw ReferenceParseException(__FILE__, __LINE__); } - facet = argument; break; } @@ -195,9 +194,8 @@ IceInternal::ReferenceFactory::create(const string& str) { if (!argument.empty()) { - throw EndpointParseException(__FILE__, __LINE__); + throw ReferenceParseException(__FILE__, __LINE__); } - mode = Reference::ModeTwoway; break; } @@ -206,9 +204,8 @@ IceInternal::ReferenceFactory::create(const string& str) { if (!argument.empty()) { - throw EndpointParseException(__FILE__, __LINE__); + throw ReferenceParseException(__FILE__, __LINE__); } - mode = Reference::ModeOneway; break; } @@ -217,9 +214,8 @@ IceInternal::ReferenceFactory::create(const string& str) { if (!argument.empty()) { - throw EndpointParseException(__FILE__, __LINE__); + throw ReferenceParseException(__FILE__, __LINE__); } - mode = Reference::ModeBatchOneway; break; } @@ -228,9 +224,8 @@ IceInternal::ReferenceFactory::create(const string& str) { if (!argument.empty()) { - throw EndpointParseException(__FILE__, __LINE__); + throw ReferenceParseException(__FILE__, __LINE__); } - mode = Reference::ModeDatagram; break; } @@ -239,9 +234,8 @@ IceInternal::ReferenceFactory::create(const string& str) { if (!argument.empty()) { - throw EndpointParseException(__FILE__, __LINE__); + throw ReferenceParseException(__FILE__, __LINE__); } - mode = Reference::ModeBatchDatagram; break; } @@ -250,20 +244,14 @@ IceInternal::ReferenceFactory::create(const string& str) { if (!argument.empty()) { - throw EndpointParseException(__FILE__, __LINE__); + throw ReferenceParseException(__FILE__, __LINE__); } - secure = true; break; } default: { - if (!argument.empty()) - { - throw EndpointParseException(__FILE__, __LINE__); - } - throw ReferenceParseException(__FILE__, __LINE__); } } diff --git a/cpp/src/Ice/SUdpTransceiver.cpp b/cpp/src/Ice/SUdpTransceiver.cpp index 9ca714095b0..2cefdefee85 100644 --- a/cpp/src/Ice/SUdpTransceiver.cpp +++ b/cpp/src/Ice/SUdpTransceiver.cpp @@ -113,19 +113,17 @@ IceInternal::SUdpTransceiver::SUdpTransceiver(const InstancePtr& instance, const _udpTransceiver(instance, host, port, "sudp"), _instance(instance), _traceLevels(instance->traceLevels()), - _logger(instance->logger()), - _sender(true) + _logger(instance->logger()) { // Perform our handshake with the server connectControlChannel(host, port); } -IceInternal::SUdpTransceiver::SUdpTransceiver(const InstancePtr& instance, int port) : - _udpTransceiver(instance, port, "sudp"), +IceInternal::SUdpTransceiver::SUdpTransceiver(const InstancePtr& instance, int port, bool connect) : + _udpTransceiver(instance, port, connect, "sudp"), _instance(instance), _traceLevels(instance->traceLevels()), - _logger(instance->logger()), - _sender(false) + _logger(instance->logger()) { // Build our control channel createControlChannel(port); diff --git a/cpp/src/Ice/SUdpTransceiver.h b/cpp/src/Ice/SUdpTransceiver.h index 1193125f7cb..aac1d48c219 100644 --- a/cpp/src/Ice/SUdpTransceiver.h +++ b/cpp/src/Ice/SUdpTransceiver.h @@ -44,8 +44,7 @@ public: virtual void read(Buffer&, int); virtual std::string toString() const; - virtual bool equivalent(const std::string&, int) const; - + bool equivalent(const std::string&, int) const; int effectivePort(); // Server Channel Implementation methods @@ -62,7 +61,7 @@ public: private: SUdpTransceiver(const InstancePtr&, const std::string&, int); - SUdpTransceiver(const InstancePtr&, int); + SUdpTransceiver(const InstancePtr&, int, bool); virtual ~SUdpTransceiver(); friend class SUdpEndpoint; @@ -77,7 +76,6 @@ private: InstancePtr _instance; TraceLevelsPtr _traceLevels; ::Ice::LoggerPtr _logger; - bool _sender; }; } diff --git a/cpp/src/Ice/SslAcceptor.h b/cpp/src/Ice/SslAcceptor.h index 5d2aa855795..fa11f996c33 100644 --- a/cpp/src/Ice/SslAcceptor.h +++ b/cpp/src/Ice/SslAcceptor.h @@ -37,8 +37,7 @@ public: virtual TransceiverPtr accept(int); virtual std::string toString() const; - virtual bool equivalent(const std::string&, int) const; - + bool equivalent(const std::string&, int) const; int effectivePort(); private: diff --git a/cpp/src/Ice/TcpAcceptor.h b/cpp/src/Ice/TcpAcceptor.h index 4ff3c09750d..82757cce8e4 100644 --- a/cpp/src/Ice/TcpAcceptor.h +++ b/cpp/src/Ice/TcpAcceptor.h @@ -37,8 +37,7 @@ public: virtual TransceiverPtr accept(int); virtual std::string toString() const; - virtual bool equivalent(const std::string&, int) const; - + bool equivalent(const std::string&, int) const; int effectivePort(); private: diff --git a/cpp/src/Ice/UdpTransceiver.cpp b/cpp/src/Ice/UdpTransceiver.cpp index 5144419160e..a3c1523fcbf 100644 --- a/cpp/src/Ice/UdpTransceiver.cpp +++ b/cpp/src/Ice/UdpTransceiver.cpp @@ -32,14 +32,7 @@ IceInternal::UdpTransceiver::close() if (_traceLevels->network >= 1) { ostringstream s; - if (_sender) - { - s << "stopping to send " << _protocolName << " packets to " << toString(); - } - else - { - s << "stopping to receive " << _protocolName << " packets at " << toString(); - } + s << "closing udp connection\n" << toString(); _logger->trace(_traceLevels->networkCat, s.str()); } @@ -56,7 +49,6 @@ IceInternal::UdpTransceiver::shutdown() void IceInternal::UdpTransceiver::write(Buffer& buf, int) { - assert(_sender); assert(buf.i == buf.b.begin()); #ifndef NDEBUG const int packetSize = 64 * 1024; // TODO: configurable @@ -81,7 +73,7 @@ repeat: if (_traceLevels->network >= 3) { ostringstream s; - s << "sent " << ret << " bytes via " << _protocolName << " to " << toString(); + s << "sent " << ret << " bytes via " << _protocolName << "\n" << toString(); _logger->trace(_traceLevels->networkCat, s.str()); } @@ -92,7 +84,6 @@ repeat: void IceInternal::UdpTransceiver::read(Buffer& buf, int) { - assert(!_sender); assert(buf.i == buf.b.begin()); const int packetSize = 64 * 1024; // TODO: configurable assert(packetSize >= static_cast<int>(buf.b.size())); // TODO: exception @@ -100,7 +91,27 @@ IceInternal::UdpTransceiver::read(Buffer& buf, int) buf.i = buf.b.begin(); repeat: - int ret = ::recv(_fd, buf.b.begin(), packetSize, 0); + int ret; + if (_connect) + { + // + // If we must connect, then we connect to the first peer that + // sends us a packet. + // + struct sockaddr_in peerAddr; + memset(&peerAddr, 0, sizeof(struct sockaddr_in)); + socklen_t len = sizeof(peerAddr); + ret = recvfrom(_fd, buf.b.begin(), packetSize, 0, reinterpret_cast<struct sockaddr*>(&peerAddr), &len); + if (ret != SOCKET_ERROR) + { + doConnect(_fd, peerAddr, -1); + _connect = false; // We're connected now + } + } + else + { + ret = ::recv(_fd, buf.b.begin(), packetSize, 0); + } if (ret == SOCKET_ERROR) { @@ -117,7 +128,7 @@ repeat: if (_traceLevels->network >= 3) { ostringstream s; - s << "received " << ret << " bytes via " << _protocolName << " at " << toString(); + s << "received " << ret << " bytes via " << _protocolName << "\n" << toString(); _logger->trace(_traceLevels->networkCat, s.str()); } @@ -128,16 +139,13 @@ repeat: string IceInternal::UdpTransceiver::toString() const { - return addrToString(_addr); + return fdToString(_fd); } bool IceInternal::UdpTransceiver::equivalent(const string& host, int port) const { - if (_sender) - { - return false; - } + assert(_incoming); // This equivalence test is only valid for incoming connections. struct sockaddr_in addr; getAddress(host.c_str(), port, addr); @@ -170,7 +178,8 @@ IceInternal::UdpTransceiver::UdpTransceiver(const InstancePtr& instance, _instance(instance), _traceLevels(instance->traceLevels()), _logger(instance->logger()), - _sender(true), + _incoming(false), + _connect(true), _protocolName(protocolName) { try @@ -179,11 +188,12 @@ IceInternal::UdpTransceiver::UdpTransceiver(const InstancePtr& instance, _fd = createSocket(true); doConnect(_fd, _addr, -1); + _connect = false; // We're connected now if (_traceLevels->network >= 1) { ostringstream s; - s << "starting to send " << _protocolName << " packets to " << toString(); + s << "starting to send " << _protocolName << " packets\n" << toString(); _logger->trace(_traceLevels->networkCat, s.str()); } } @@ -194,11 +204,13 @@ IceInternal::UdpTransceiver::UdpTransceiver(const InstancePtr& instance, } } -IceInternal::UdpTransceiver::UdpTransceiver(const InstancePtr& instance, int port, const string& protocolName) : +IceInternal::UdpTransceiver::UdpTransceiver(const InstancePtr& instance, int port, bool connect, + const string& protocolName) : _instance(instance), _traceLevels(instance->traceLevels()), _logger(instance->logger()), - _sender(false), + _incoming(true), + _connect(connect), _protocolName(protocolName) { try @@ -214,7 +226,7 @@ IceInternal::UdpTransceiver::UdpTransceiver(const InstancePtr& instance, int por if (_traceLevels->network >= 1) { ostringstream s; - s << "starting to receive " << _protocolName << " packets at " << toString(); + s << "starting to receive " << _protocolName << " packets\n" << toString(); _logger->trace(_traceLevels->networkCat, s.str()); } } diff --git a/cpp/src/Ice/UdpTransceiver.h b/cpp/src/Ice/UdpTransceiver.h index e70f99a5d97..5b80265fbbb 100644 --- a/cpp/src/Ice/UdpTransceiver.h +++ b/cpp/src/Ice/UdpTransceiver.h @@ -38,16 +38,14 @@ public: virtual void read(Buffer&, int); virtual std::string toString() const; - virtual bool equivalent(const std::string&, int) const; - + bool equivalent(const std::string&, int) const; int effectivePort(); - void setProtocolName(const std::string&); private: UdpTransceiver(const InstancePtr&, const std::string&, int, const std::string& protocolName = "udp"); - UdpTransceiver(const InstancePtr&, int, const std::string& protocolName = "udp"); + UdpTransceiver(const InstancePtr&, int, bool, const std::string& protocolName = "udp"); virtual ~UdpTransceiver(); friend class UdpEndpoint; @@ -57,7 +55,8 @@ private: InstancePtr _instance; TraceLevelsPtr _traceLevels; ::Ice::LoggerPtr _logger; - bool _sender; + bool _incoming; + bool _connect; SOCKET _fd; struct sockaddr_in _addr; std::string _protocolName; |