The remove paths on send fail thing in Peer.cpp was not well thought out, and there is no point in mallocing the TCP write buffer.

This commit is contained in:
Adam Ierymenko 2014-04-01 15:55:05 -07:00
parent c3b41c289d
commit 0e1fc06a6f
3 changed files with 20 additions and 49 deletions

View File

@ -121,30 +121,23 @@ bool Peer::send(const RuntimeEnvironment *_r,const void *data,unsigned int len,u
{ {
Mutex::Lock _l(_lock); Mutex::Lock _l(_lock);
for(;;) { std::vector<Path>::iterator p(_paths.begin());
std::vector<Path>::iterator p(_paths.begin()); if (p == _paths.end())
if (p == _paths.end()) return false;
return false;
uint64_t bestPathLastReceived = p->lastReceived(); uint64_t bestPathLastReceived = p->lastReceived();
std::vector<Path>::iterator bestPath = p; std::vector<Path>::iterator bestPath = p;
while (++p != _paths.end()) { while (++p != _paths.end()) {
uint64_t lr = p->lastReceived(); uint64_t lr = p->lastReceived();
if (lr > bestPathLastReceived) { if (lr > bestPathLastReceived) {
bestPathLastReceived = lr; bestPathLastReceived = lr;
bestPath = p; bestPath = p;
}
} }
}
if (_r->sm->send(bestPath->address(),bestPath->tcp(),bestPath->type() == Path::PATH_TYPE_TCP_OUT,data,len)) { if (_r->sm->send(bestPath->address(),bestPath->tcp(),bestPath->type() == Path::PATH_TYPE_TCP_OUT,data,len)) {
bestPath->sent(now); bestPath->sent(now);
return true; return true;
} else {
if (bestPath->fixed())
return false;
_paths.erase(bestPath);
// ... try again and pick a different path
}
} }
return false; return false;

View File

@ -48,8 +48,6 @@
#include <signal.h> #include <signal.h>
#endif #endif
#define ZT_TCP_MAX_SENDQ_LENGTH (ZT_SOCKET_MAX_MESSAGE_LEN * 8)
namespace ZeroTier { namespace ZeroTier {
TcpSocket::~TcpSocket() TcpSocket::~TcpSocket()
@ -59,8 +57,6 @@ TcpSocket::~TcpSocket()
#else #else
::close(_sock); ::close(_sock);
#endif #endif
if (_outbuf)
::free(_outbuf);
//printf("!!! TCP SOCKET DESTROYED @%.16llx to %s\r\n",(unsigned long long)this,_remote.toString().c_str()); //printf("!!! TCP SOCKET DESTROYED @%.16llx to %s\r\n",(unsigned long long)this,_remote.toString().c_str());
} }
@ -72,26 +68,11 @@ bool TcpSocket::send(const InetAddress &to,const void *msg,unsigned int msglen)
return true; // sanity check return true; // sanity check
Mutex::Lock _l(_writeLock); Mutex::Lock _l(_writeLock);
bool writeInProgress = ((_outptr != 0)||(_connecting)); bool writeInProgress = ((_outptr != 0)||(_connecting));
// Ensure that _outbuf is large enough if ((_outptr + 5 + msglen) > (unsigned int)sizeof(_outbuf))
unsigned int newptr = _outptr + 5 + msglen; return false;
if (newptr > _outbufsize) {
unsigned int newbufsize = _outbufsize;
while (newbufsize < newptr)
newbufsize += ZT_SOCKET_MAX_MESSAGE_LEN;
if (newbufsize > ZT_TCP_MAX_SENDQ_LENGTH)
return false; // cannot send, outbuf full
unsigned char *newbuf = (unsigned char *)::malloc(newbufsize);
if (!newbuf)
return false; // cannot send, no memory
_outbufsize = newbufsize;
if (_outbuf) {
memcpy(newbuf,_outbuf,_outptr);
::free(_outbuf);
}
_outbuf = newbuf;
}
_outbuf[_outptr++] = 0x17; // look like TLS data _outbuf[_outptr++] = 0x17; // look like TLS data
_outbuf[_outptr++] = 0x03; _outbuf[_outptr++] = 0x03;

View File

@ -78,10 +78,8 @@ protected:
Socket(t,s), Socket(t,s),
_lastActivity(Utils::now()), _lastActivity(Utils::now()),
_sm(sm), _sm(sm),
_outbuf((unsigned char *)0),
_outptr(0),
_outbufsize(0),
_inptr(0), _inptr(0),
_outptr(0),
_connecting(c), _connecting(c),
_remote(r) _remote(r)
{ {
@ -93,12 +91,11 @@ protected:
private: private:
unsigned char _inbuf[ZT_SOCKET_MAX_MESSAGE_LEN]; unsigned char _inbuf[ZT_SOCKET_MAX_MESSAGE_LEN];
unsigned char _outbuf[ZT_SOCKET_MAX_MESSAGE_LEN * 4];
uint64_t _lastActivity; // updated whenever data is received, checked directly by SocketManager for stale TCP cleanup uint64_t _lastActivity; // updated whenever data is received, checked directly by SocketManager for stale TCP cleanup
SocketManager *_sm; SocketManager *_sm;
unsigned char *_outbuf;
unsigned int _outptr;
unsigned int _outbufsize;
unsigned int _inptr; unsigned int _inptr;
unsigned int _outptr;
bool _connecting; // manipulated directly by SocketManager, true if connect() is in progress bool _connecting; // manipulated directly by SocketManager, true if connect() is in progress
InetAddress _remote; InetAddress _remote;
Mutex _writeLock; Mutex _writeLock;