diff options
author | Adam <Adam@anope.org> | 2011-08-21 13:38:42 -0400 |
---|---|---|
committer | Adam <Adam@anope.org> | 2011-09-10 01:55:09 -0400 |
commit | 2eb708e5ad8b259876d24d828f7472b77864c256 (patch) | |
tree | bed6b70d4bc67eb413453a116e77f8f724cdf3fd /src/socket_transport.cpp | |
parent | 4fcb371bc8813cd647b7769a64d586e3a57d684d (diff) |
Cleaned up some of the socket code, cleaned up the pipe engines, added support for binary sockets, and cleaned up the asynch connect/accept code
Diffstat (limited to 'src/socket_transport.cpp')
-rw-r--r-- | src/socket_transport.cpp | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/src/socket_transport.cpp b/src/socket_transport.cpp new file mode 100644 index 000000000..a7b92ff20 --- /dev/null +++ b/src/socket_transport.cpp @@ -0,0 +1,185 @@ +/* + * + * (C) 2003-2011 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + +#include "services.h" + +BufferedSocket::BufferedSocket() +{ +} + +BufferedSocket::~BufferedSocket() +{ +} + +bool BufferedSocket::ProcessRead() +{ + char tbuffer[NET_BUFSIZE]; + + this->RecvLen = 0; + + int len = this->IO->Recv(this, tbuffer, sizeof(tbuffer) - 1); + if (len <= 0) + return false; + + tbuffer[len] = 0; + this->RecvLen = len; + + Anope::string sbuffer = this->extrabuf; + sbuffer += tbuffer; + this->extrabuf.clear(); + size_t lastnewline = sbuffer.rfind('\n'); + if (lastnewline == Anope::string::npos) + { + this->extrabuf = sbuffer; + return true; + } + if (lastnewline < sbuffer.length() - 1) + { + this->extrabuf = sbuffer.substr(lastnewline); + this->extrabuf.trim(); + sbuffer = sbuffer.substr(0, lastnewline); + } + + sepstream stream(sbuffer, '\n'); + + Anope::string tbuf; + while (stream.GetToken(tbuf)) + { + tbuf.trim(); + if (!tbuf.empty() && !Read(tbuf)) + return false; + } + + return true; +} + +bool BufferedSocket::ProcessWrite() +{ + int count = this->IO->Send(this, this->WriteBuffer); + if (count <= -1) + return false; + this->WriteBuffer = this->WriteBuffer.substr(count); + if (this->WriteBuffer.empty()) + SocketEngine::ClearWritable(this); + + return true; +} + +bool BufferedSocket::Read(const Anope::string &buf) +{ + return false; +} + +void BufferedSocket::Write(const char *message, ...) +{ + va_list vi; + char tbuffer[BUFSIZE]; + + if (!message) + return; + + va_start(vi, message); + vsnprintf(tbuffer, sizeof(tbuffer), message, vi); + va_end(vi); + + Anope::string sbuf = tbuffer; + Write(sbuf); +} + +void BufferedSocket::Write(const Anope::string &message) +{ + this->WriteBuffer += message + "\r\n"; + SocketEngine::MarkWritable(this); +} + +int BufferedSocket::ReadBufferLen() const +{ + return RecvLen; +} + +int BufferedSocket::WriteBufferLen() const +{ + return this->WriteBuffer.length(); +} + + +BinarySocket::DataBlock::DataBlock(const char *b, size_t l) +{ + this->buf = new char[l]; + memcpy(this->buf, b, l); + this->len = l; +} + +BinarySocket::DataBlock::~DataBlock() +{ + delete [] this->buf; +} + +BinarySocket::BinarySocket() +{ +} + +BinarySocket::~BinarySocket() +{ +} + +bool BinarySocket::ProcessRead() +{ + char tbuffer[NET_BUFSIZE]; + + int len = this->IO->Recv(this, tbuffer, sizeof(tbuffer)); + if (len <= 0) + return false; + + return this->Read(tbuffer, len); +} + +bool BinarySocket::ProcessWrite() +{ + if (this->WriteBuffer.empty()) + { + SocketEngine::ClearWritable(this); + return true; + } + + DataBlock *d = this->WriteBuffer.front(); + + int len = this->IO->Send(this, d->buf, d->len); + if (len <= -1) + return false; + else if (static_cast<size_t>(len) == d->len) + { + delete d; + this->WriteBuffer.pop_front(); + } + else + { + d->buf += len; + d->len -= len; + } + + if (this->WriteBuffer.empty()) + SocketEngine::ClearWritable(this); + + return true; +} + +void BinarySocket::Write(const char *buffer, size_t l) +{ + this->WriteBuffer.push_back(new DataBlock(buffer, l)); + SocketEngine::MarkWritable(this); +} + +bool BinarySocket::Read(const char *buffer, size_t l) +{ + return true; +} + |