diff options
author | w00t <w00t@e03df62e-2008-0410-955e-edbf42e46eb7> | 2008-07-02 14:20:38 +0000 |
---|---|---|
committer | w00t <w00t@e03df62e-2008-0410-955e-edbf42e46eb7> | 2008-07-02 14:20:38 +0000 |
commit | f4ea51671f367d6e473049d52919a3c879329c6f (patch) | |
tree | 1cfd0b16185ae251f3db8fad2a66e07190e7fa8b | |
parent | b928a79eebfc9d87d84886185aba4a75705e221a (diff) |
Forward port r9946: make SVSHOLD silent to avoid useless irritation to opers
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@9947 e03df62e-2008-0410-955e-edbf42e46eb7
-rw-r--r-- | include/linebuffer.h | 110 | ||||
-rw-r--r-- | include/users.h | 39 | ||||
-rw-r--r-- | src/commands/cmd_stats.cpp | 4 | ||||
-rw-r--r-- | src/modules/m_svshold.cpp | 3 | ||||
-rw-r--r-- | src/modules/m_xmlsocket.cpp | 5 | ||||
-rw-r--r-- | src/users.cpp | 206 |
6 files changed, 281 insertions, 86 deletions
diff --git a/include/linebuffer.h b/include/linebuffer.h new file mode 100644 index 000000000..8b579c452 --- /dev/null +++ b/include/linebuffer.h @@ -0,0 +1,110 @@ +/* +------------------------------------+
+ * | Inspire Internet Relay Chat Daemon |
+ * +------------------------------------+
+ *
+ * InspIRCd: (C) 2002-2008 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
+ * This program is free but copyrighted software; see
+ * the file COPYING for details.
+ *
+ * ---------------------------------------------------
+ */
+
+/** Right, Line Buffers. Time for an explanation as to how sendqs work. By the way, ircd people,
+ * before you jump up and down screaming, this is not anything like Adrian Chadd's linebuffer
+ * stuff done for hybrid(and never really completed.) Rather, this is (I think) where linebuffer
+ * was heading, and should have been.
+ *
+ * Enough introduction, actual explanation starts here.
+ * In IRCd, we have a sendq. Traditionally, a sendq has been a big string. Stuff is tacked onto
+ * the end, and when we can, we send the user some data off the start of it. A circular buffer.
+ *
+ * This model works okay, and is quite simplistic to code, but has a drawback in that most IRC
+ * messages are multicast: topic changes, joins, parts, channel messages, and so on.
+ * This means that for each of these messages, we must do O(n^n) amount of work: bytes ^ recipients
+ * of writes will be made to sendqs, and this is a slow and expensive operation.
+ *
+ * The solution comes with the use of this linebuffer class below, which is managed entirely by
+ * the user class (though it *may* be possible to add a server to server implementation of this later,
+ * but that's nowhere near so needed, and nowhere near so trivial, thanks to the inherited nature
+ * of buffered socket, but I digress).
+ *
+ * What this class does, in a nutshell:
+ * When we need to send a message to a user, we create a LineBuffer object. It has a reference count, and
+ * we copy the string we need to send into the LineBuffer object also.
+ * We then tack a pointer to this LineBuffer into an std::list stored in the User class.
+ * When the user writes data, a ptr is advanced depending how much of that line they wrote. If they wrote all
+ * of the line, the pointer is popped off the std::list, the ptr is reset, and the buffer's refcount is
+ * decremented - and if it reaches 0, the linebuffer is destroyed as it has fulfilled it's purpose.
+ *
+ * Effectively, this means that multicast writes become O(n) + time taken to copy message once, or just about.
+ *
+ * We gain efficiency, and much, much better RAM usage.
+ */
+static unsigned int totalbuffers = 0;
+
+class LineBuffer
+{
+ private:
+ std::string msg;
+ unsigned long refcount;
+
+ // Don't let it be copied.
+ LineBuffer(const LineBuffer &) { }
+
+ public:
+ ~LineBuffer()
+ {
+ totalbuffers--;
+ printf("Destroying LineBuffer with %u bytes, total buffers is %u\n", msg.length(), totalbuffers);
+ msg.resize(0);
+ }
+
+ LineBuffer(std::string &m)
+ {
+ if (m.length() > MAXBUF - 2) /* MAXBUF has a value of 514, to account for line terminators */
+ {
+ // Trim the message to fit, 510 characters max.
+ m = m.substr(0, MAXBUF - 4); // MAXBUF is 514, we need 510.
+ }
+
+ // Add line terminator
+ m.append("\r\n");
+
+ // And copy
+ msg = m;
+ refcount = 0;
+ totalbuffers++;
+ printf("Creating LineBuffer with %u bytes, total buffers is %u\n", msg.length(), totalbuffers);
+ }
+
+ std::string &GetMessage()
+ {
+ return msg;
+ }
+
+ unsigned long GetMessageLength()
+ {
+ return msg.length();
+ }
+
+ // To be used after creation, when we know how many recipients we actually have.
+ void SetRefcount(unsigned long r)
+ {
+ refcount = r;
+ }
+
+ unsigned long DecrementCount()
+ {
+ if (refcount == 0)
+ {
+ throw "decrementing a refcount when nobody is using it is weird and wrong";
+ }
+
+ refcount--;
+ return refcount;
+ }
+
+ // There is no increment method as it isn't really needed.
+};
diff --git a/include/users.h b/include/users.h index f098500e3..883fbadf8 100644 --- a/include/users.h +++ b/include/users.h @@ -17,12 +17,17 @@ #include "socket.h" #include "connection.h" #include "dns.h" - #include "mode.h" +#include "linebuffer.h" + +#include <list> // XXX XXX XXX this should probably be moved to globals.h, and globals.h should probably be merged in with inspircd.h sometime. -- w00t +#define _GLIBCXX_FORCE_NEW 1 + /** Channel status for a user */ -enum ChanStatus { +enum ChanStatus +{ /** Op */ STATUS_OP = 4, /** Halfop */ @@ -35,7 +40,8 @@ enum ChanStatus { /** connect class types */ -enum ClassTypes { +enum ClassTypes +{ /** connect:allow */ CC_ALLOW = 0, /** connect:deny */ @@ -44,7 +50,8 @@ enum ClassTypes { /** RFC1459 channel modes */ -enum UserModes { +enum UserModes +{ /** +s: Server notices */ UM_SERVERNOTICE = 's' - 65, /** +w: WALLOPS */ @@ -60,16 +67,17 @@ enum UserModes { /** Registration state of a user, e.g. * have they sent USER, NICK, PASS yet? */ -enum RegistrationState { - -#ifndef WIN32 // Burlex: This is already defined in win32, luckily it is still 0. +enum RegistrationState +{ +#ifndef REG_NONE /* This is already defined in win32, luckily it is still 0. -- Burlex + XXX perhaps we should undef it just in case.. Relying on magic numbers... -- w00t */ REG_NONE = 0, /* Has sent nothing */ #endif REG_USER = 1, /* Has sent USER */ REG_NICK = 2, /* Has sent NICK */ REG_NICKUSER = 3, /* Bitwise combination of REG_NICK and REG_USER */ - REG_ALL = 7 /* REG_NICKUSER plus next bit along */ + REG_ALL = 7 /* REG_NICKUSER plus next bit along */ }; /* Required forward declaration */ @@ -598,10 +606,15 @@ class CoreExport User : public connection */ std::string recvq; - /** User's send queue. - * Lines waiting to be sent are stored here until their buffer is flushed. + /** How many bytes are currently in the user's sendq. + */ + unsigned long sendqlength; + /** List of pointers to buffer objects, this is the actual user's sendq. + */ + std::list<LineBuffer*, __gnu_cxx::new_allocator<LineBuffer*> > sendq; + /** How far into the current sendq line is the user? */ - std::string sendq; + unsigned long sendqpos; /** Message user will quit with. Not to be set externally. */ @@ -843,13 +856,13 @@ class CoreExport User : public connection */ const char* GetWriteError(); - /** Adds to the user's write buffer. + /** Adds a line buffer to the user's sendq. * You may add any amount of text up to this users sendq value, if you exceed the * sendq value, SetWriteError() will be called to set the users error string to * "SendQ exceeded", and further buffer adds will be dropped. * @param data The data to add to the write buffer */ - void AddWriteBuf(const std::string &data); + void AddWriteBuf(LineBuffer *l); /** Flushes as much of the user's buffer to the file descriptor as possible. * This function may not always flush the entire buffer, rather instead as much of it diff --git a/src/commands/cmd_stats.cpp b/src/commands/cmd_stats.cpp index f9bb0d634..9e7db92b2 100644 --- a/src/commands/cmd_stats.cpp +++ b/src/commands/cmd_stats.cpp @@ -287,7 +287,7 @@ DllExport void DoStats(InspIRCd* ServerInstance, char statschar, User* user, str for (std::vector<User*>::iterator n = ServerInstance->Users->local_users.begin(); n != ServerInstance->Users->local_users.end(); n++) { User* i = *n; - results.push_back(sn+" 211 "+user->nick+" "+i->nick+"["+i->ident+"@"+i->dhost+"] "+ConvToStr(i->sendq.length())+" "+ConvToStr(i->cmds_out)+" "+ConvToStr(i->bytes_out)+" "+ConvToStr(i->cmds_in)+" "+ConvToStr(i->bytes_in)+" "+ConvToStr(ServerInstance->Time() - i->age)); + results.push_back(sn+" 211 "+user->nick+" "+i->nick+"["+i->ident+"@"+i->dhost+"] "+ConvToStr(i->sendqlength)+" "+ConvToStr(i->cmds_out)+" "+ConvToStr(i->bytes_out)+" "+ConvToStr(i->cmds_in)+" "+ConvToStr(i->bytes_in)+" "+ConvToStr(ServerInstance->Time() - i->age)); } break; @@ -297,7 +297,7 @@ DllExport void DoStats(InspIRCd* ServerInstance, char statschar, User* user, str for (std::vector<User*>::iterator n = ServerInstance->Users->local_users.begin(); n != ServerInstance->Users->local_users.end(); n++) { User* i = *n; - results.push_back(sn+" 211 "+user->nick+" "+i->nick+"["+i->ident+"@"+i->GetIPString()+"] "+ConvToStr(i->sendq.length())+" "+ConvToStr(i->cmds_out)+" "+ConvToStr(i->bytes_out)+" "+ConvToStr(i->cmds_in)+" "+ConvToStr(i->bytes_in)+" "+ConvToStr(ServerInstance->Time() - i->age)); + results.push_back(sn+" 211 "+user->nick+" "+i->nick+"["+i->ident+"@"+i->GetIPString()+"] "+ConvToStr(i->sendqlength)+" "+ConvToStr(i->cmds_out)+" "+ConvToStr(i->bytes_out)+" "+ConvToStr(i->cmds_in)+" "+ConvToStr(i->bytes_in)+" "+ConvToStr(ServerInstance->Time() - i->age)); } break; diff --git a/src/modules/m_svshold.cpp b/src/modules/m_svshold.cpp index b90e452bf..4a3858085 100644 --- a/src/modules/m_svshold.cpp +++ b/src/modules/m_svshold.cpp @@ -129,12 +129,10 @@ class CommandSvshold : public Command if(length > 0) { user->WriteServ( "385 %s %s :Added %lu second SVSHOLD (%s)", user->nick.c_str(), parameters[0].c_str(), length, reason.c_str()); - ServerInstance->SNO->WriteToSnoMask('A', "%s added %lu second SVSHOLD on %s (%s)", user->nick.c_str(), length, parameters[0].c_str(), reason.c_str()); } else { user->WriteServ( "385 %s %s :Added permanent SVSHOLD on %s (%s)", user->nick.c_str(), parameters[0].c_str(), parameters[0].c_str(), reason.c_str()); - ServerInstance->SNO->WriteToSnoMask('A', "%s added permanent SVSHOLD on %s (%s)", user->nick.c_str(), parameters[0].c_str(), reason.c_str()); } } else @@ -265,7 +263,6 @@ class ModuleSVSHold : public Module if ((*iter)->set_on + (*iter)->length <= ServerInstance->Time()) { ServerInstance->Logs->Log("m_svshold",DEBUG, "m_svshold.so: hold on %s expired, removing...", (*iter)->nickname.c_str()); - ServerInstance->SNO->WriteToSnoMask('A',"%lu second SVSHOLD on %s (%s) set %lu seconds ago expired", (unsigned long) (*iter)->length, (*iter)->nickname.c_str(), (*iter)->reason.c_str(), (unsigned long) ServerInstance->Time() - (*iter)->set_on); HoldMap.erase(assign((*iter)->nickname)); delete *iter; safeiter = iter; diff --git a/src/modules/m_xmlsocket.cpp b/src/modules/m_xmlsocket.cpp index 0e29cb993..2973ed187 100644 --- a/src/modules/m_xmlsocket.cpp +++ b/src/modules/m_xmlsocket.cpp @@ -170,7 +170,10 @@ class ModuleXMLSocket : public Module if ((tmpbuffer[n] == '\r') || (tmpbuffer[n] == '\n')) tmpbuffer[n] = 0; - user->AddWriteBuf(std::string(tmpbuffer,count)); + std::string buf(tmpbuffer, count); + LineBuffer *l = new LineBuffer(buf); + l->SetRefcount(1); + user->AddWriteBuf(l); delete [] tmpbuffer; return 1; diff --git a/src/users.cpp b/src/users.cpp index f532b60d1..d6d7b72ec 100644 --- a/src/users.cpp +++ b/src/users.cpp @@ -189,12 +189,11 @@ User::User(InspIRCd* Instance, const std::string &uid) : ServerInstance(Instance reset_due = ServerInstance->Time(); age = ServerInstance->Time(); Penalty = 0; - lines_in = lastping = signon = idle_lastmsg = nping = registered = 0; + sendqpos = sendqlength = lines_in = lastping = signon = idle_lastmsg = nping = registered = 0; ChannelCount = timeout = bytes_in = bytes_out = cmds_in = cmds_out = 0; quietquit = OverPenalty = ExemptFromPenalty = quitting = exempt = haspassed = dns_done = false; fd = -1; recvq.clear(); - sendq.clear(); WriteError.clear(); res_forward = res_reverse = NULL; Visibility = NULL; @@ -618,12 +617,12 @@ std::string User::GetBuffer() } } -void User::AddWriteBuf(const std::string &data) +void User::AddWriteBuf(LineBuffer *l) { if (*this->GetWriteError()) return; - if (this->MyClass && (sendq.length() + data.length() > this->MyClass->GetSendqMax())) + if (this->MyClass && (sendqlength + l->GetMessageLength() > this->MyClass->GetSendqMax())) { /* * Fix by brain - Set the error text BEFORE calling, because @@ -631,69 +630,79 @@ void User::AddWriteBuf(const std::string &data) * to repeatedly add the text to the sendq! */ this->SetWriteError("SendQ exceeded"); - ServerInstance->SNO->WriteToSnoMask('A', "User %s SendQ of %lu exceeds connect class maximum of %lu",this->nick.c_str(),(unsigned long int)sendq.length() + data.length(),this->MyClass->GetSendqMax()); + ServerInstance->SNO->WriteToSnoMask('A', "User %s SendQ of %lu exceeds connect class maximum of %lu",this->nick.c_str(), sendqlength + l->GetMessageLength(), this->MyClass->GetSendqMax()); return; } - if (data.length() > MAXBUF - 2) /* MAXBUF has a value of 514, to account for line terminators */ - sendq.append(data.substr(0,MAXBUF - 4)).append("\r\n"); /* MAXBUF-4 = 510 */ - else - sendq.append(data); + sendq.push_back(l); + ServerInstance->stats->statsSent += l->GetMessageLength(); + this->ServerInstance->SE->WantWrite(this); } // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it) void User::FlushWriteBuf() { - try + if ((this->fd == FD_MAGIC_NUMBER) || (*this->GetWriteError())) { - if ((this->fd == FD_MAGIC_NUMBER) || (*this->GetWriteError())) + return; // Don't do this for module created users, nor for users with a write error. + } + + // While the sendq has lines to send.. + while (!sendq.empty()) + { + LineBuffer *l = sendq.front(); + + int s = 0; + + // We want to send from where we're up to to the end of the line if possible. I know this looks confusing. Where we're up to is sendqpos, + // which makes message length total length - sendqpos. + s = ServerInstance->SE->Send(this, l->GetMessage().substr(sendqpos, (l->GetMessageLength() - sendqpos)).data(), l->GetMessageLength() - sendqpos, 0); + + if (s == -1) { - sendq.clear(); + // Write error. + if (errno == EAGAIN) + { + + // Non-fatal; writing would just block. We don't want to block. + // So try write again later. + this->ServerInstance->SE->WantWrite(this); + } + else + { + this->SetWriteError(errno ? strerror(errno) : "Write error"); + return; + } } - if ((sendq.length()) && (this->fd != FD_MAGIC_NUMBER)) + else { - int old_sendq_length = sendq.length(); - int n_sent = ServerInstance->SE->Send(this, this->sendq.data(), this->sendq.length(), 0); + // Update bytes sent. + this->bytes_out += s; - if (n_sent == -1) + // If what was just written + already written is not the whole message + if ((s + sendqpos) != l->GetMessageLength()) { - if (errno == EAGAIN) - { - /* The socket buffer is full. This isnt fatal, - * try again later. - */ - this->ServerInstance->SE->WantWrite(this); - } - else - { - /* Fatal error, set write error and bail - */ - this->SetWriteError(errno ? strerror(errno) : "EOF from client"); - return; - } + sendqpos = s; // save our current position } else { - /* advance the queue */ - if (n_sent) - this->sendq = this->sendq.substr(n_sent); - /* update the user's stats counters */ - this->bytes_out += n_sent; + sendqpos = 0; // it was the full message. this->cmds_out++; - if (n_sent != old_sendq_length) - this->ServerInstance->SE->WantWrite(this); + sendq.pop_front(); + + // If we're the last one to use this line buffer, delete it + if (l->DecrementCount() == 0) + { + delete l; + } } } } - catch (...) - { - ServerInstance->Logs->Log("USERS", DEBUG,"Exception in User::FlushWriteBuf()"); - } - if (this->sendq.empty()) { - FOREACH_MOD(I_OnBufferFlushed,OnBufferFlushed(this)); + sendq.resize(0); + FOREACH_MOD(I_OnBufferFlushed, OnBufferFlushed(this)); } } @@ -1195,10 +1204,10 @@ void User::Write(std::string text) } else { - this->AddWriteBuf(text); + LineBuffer *l = new LineBuffer(text); + l->SetRefcount(1); + this->AddWriteBuf(l); } - ServerInstance->stats->statsSent += text.length(); - this->ServerInstance->SE->WantWrite(this); } /** Write() @@ -1339,8 +1348,13 @@ void User::WriteCommon(const std::string &text) InitializeAlreadySent(ServerInstance->SE); /* We dont want to be doing this n times, just once */ - snprintf(tb,MAXBUF,":%s %s",this->GetFullHost().c_str(),text.c_str()); - std::string out = tb; + std::string buf = ":"; + buf.append(this->GetFullHost()); + buf.append(" "); + buf.append(text); + + LineBuffer *l = NULL; + unsigned long total = 0; for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++) { @@ -1349,8 +1363,14 @@ void User::WriteCommon(const std::string &text) { if ((IS_LOCAL(i->first)) && (already_sent[i->first->fd] != uniq_id)) { + if (!l) + { + // sending to the first user + l = new LineBuffer(buf); + } already_sent[i->first->fd] = uniq_id; - i->first->Write(out); + i->first->AddWriteBuf(l); + total++; sent_to_at_least_one = true; } } @@ -1364,6 +1384,10 @@ void User::WriteCommon(const std::string &text) { this->Write(std::string(tb)); } + else + { + l->SetRefcount(total); + } } @@ -1385,9 +1409,6 @@ void User::WriteCommonExcept(const char* text, ...) void User::WriteCommonQuit(const std::string &normal_text, const std::string &oper_text) { - char tb1[MAXBUF]; - char tb2[MAXBUF]; - if (this->registered != REG_ALL) return; @@ -1396,10 +1417,21 @@ void User::WriteCommonQuit(const std::string &normal_text, const std::string &op if (!already_sent) InitializeAlreadySent(ServerInstance->SE); - snprintf(tb1,MAXBUF,":%s QUIT :%s",this->GetFullHost().c_str(),normal_text.c_str()); - snprintf(tb2,MAXBUF,":%s QUIT :%s",this->GetFullHost().c_str(),oper_text.c_str()); - std::string out1 = tb1; - std::string out2 = tb2; + unsigned int opercount = 0; + unsigned int usercount = 0; + + std::string operquit = ":"; + operquit.append(this->GetFullHost()); + operquit.append(" QUIT :"); + operquit.append(oper_text); + + std::string userquit = ":"; + userquit.append(this->GetFullHost()); + userquit.append(" QUIT :"); + userquit.append(normal_text); + + LineBuffer *ol = new LineBuffer(operquit); + LineBuffer *ul = new LineBuffer(userquit); for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++) { @@ -1411,18 +1443,35 @@ void User::WriteCommonQuit(const std::string &normal_text, const std::string &op if ((IS_LOCAL(i->first)) && (already_sent[i->first->fd] != uniq_id)) { already_sent[i->first->fd] = uniq_id; - i->first->Write(IS_OPER(i->first) ? out2 : out1); + + if (IS_OPER(i->first)) + { + i->first->AddWriteBuf(ol); + opercount++; + } + else + { + i->first->AddWriteBuf(ul); + usercount++; + } } } } } + + if (opercount == 0) + free(ol); + else + ol->SetRefcount(opercount); + + if (usercount == 0) + free(ul); + else + ul->SetRefcount(usercount); } void User::WriteCommonExcept(const std::string &text) { - char tb1[MAXBUF]; - std::string out1; - if (this->registered != REG_ALL) return; @@ -1431,8 +1480,13 @@ void User::WriteCommonExcept(const std::string &text) if (!already_sent) InitializeAlreadySent(ServerInstance->SE); - snprintf(tb1,MAXBUF,":%s %s",this->GetFullHost().c_str(),text.c_str()); - out1 = tb1; + unsigned long total = 0; + LineBuffer *l = NULL; + + std::string buf = ":"; + buf.append(this->GetFullHost()); + buf.append(" "); + buf.append(text); for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++) { @@ -1443,17 +1497,26 @@ void User::WriteCommonExcept(const std::string &text) { if ((IS_LOCAL(i->first)) && (already_sent[i->first->fd] != uniq_id)) { + if (!l) + l = new LineBuffer(buf); + already_sent[i->first->fd] = uniq_id; - i->first->Write(out1); + i->first->AddWriteBuf(l); + total++; } } } } + if (l) + { + l->SetRefcount(total); + } } void User::WriteWallOps(const std::string &text) { + // XXX: this does not yet abuse refcounted linebuffers for sending -- w00t if (!IS_LOCAL(this)) return; @@ -1598,20 +1661,29 @@ bool User::ChangeIdent(const char* newident) void User::SendAll(const char* command, const char* text, ...) { char textbuffer[MAXBUF]; - char formatbuffer[MAXBUF]; va_list argsPtr; va_start(argsPtr, text); vsnprintf(textbuffer, MAXBUF, text, argsPtr); va_end(argsPtr); - snprintf(formatbuffer,MAXBUF,":%s %s $* :%s", this->GetFullHost().c_str(), command, textbuffer); - std::string fmt = formatbuffer; + std::string buf = ":"; + buf.append(this->GetFullHost()); + buf.append(" "); + buf.append(command); + buf.append(" $* :"); + buf.append(textbuffer); + LineBuffer *l = NULL; for (std::vector<User*>::const_iterator i = ServerInstance->Users->local_users.begin(); i != ServerInstance->Users->local_users.end(); i++) { - (*i)->Write(fmt); + if (!l) + l = new LineBuffer(buf); + (*i)->AddWriteBuf(l); } + + if (l) + l->SetRefcount(ServerInstance->Users->local_users.size()); } |