mirror of
https://github.com/anope/anope.git
synced 2026-07-08 10:23:14 +02:00
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
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user