void InitializeAlreadySent(SocketEngine* SE)
{
already_sent = new unsigned long[SE->GetMaxFds()];
- memset(already_sent, 0, SE->GetMaxFds() * sizeof(long));
- printf("\n\nInit Already Sent, uniq id = %lu\n\n", uniq_id);
+ memset(already_sent, 0, SE->GetMaxFds() * sizeof(unsigned long));
}
return false;
}
-/** NOTE: We cannot pass a const reference to this method.
- * The string is changed by the workings of the method,
- * so that if we pass const ref, we end up copying it to
- * something we can change anyway. Makes sense to just let
- * the compiler do that copy for us.
- */
-bool User::AddBuffer(std::string a)
+bool User::AddBuffer(const std::string &a)
{
- try
+ std::string::size_type start = 0;
+ std::string::size_type i = a.find('\r');
+
+ /*
+ * The old implementation here took a copy, and rfind() on \r, removing as it found them, before
+ * copying a second time onto the recvq. That's ok, but involves three copies minimum (recv() to buffer,
+ * buffer to here, here to recvq) - The new method now copies twice (recv() to buffer, buffer to recvq).
+ *
+ * We use find() instead of rfind() for clarity, however unlike the old code, our scanning of the string is
+ * contiguous: as we specify a startpoint, we never see characters we have scanned previously, making this
+ * marginally faster in cases with a number of \r hidden early on in the buffer.
+ *
+ * How it works:
+ * Start at first pos of string, find first \r, append everything in the chunk (excluding \r) to recvq. Set
+ * i ahead of the \r, search for next \r, add next chunk to buffer... repeat.
+ * -- w00t (7 may, 2008)
+ */
+ if (i == std::string::npos)
+ {
+ // no \r that we need to dance around, just add to buffer
+ recvq.append(a);
+ }
+ else
{
- std::string::size_type i = a.rfind('\r');
+ ServerInstance->Logs->Log("recvqdebug", DEBUG, "Current recvq size is %d and I got called with a string of %d\n(%s)", recvq.length(), a.length(), a.c_str());
+ // While we can find the end of a chunk to add
while (i != std::string::npos)
{
- a.erase(i, 1);
- i = a.rfind('\r');
- }
+ // Append the chunk that we have
+ recvq.append(a, start, (i - start));
+ ServerInstance->Logs->Log("recvqdebug", DEBUG, "Appended a chunk, length is now %d", recvq.length());
- if (a.length())
- recvq.append(a);
+ // Start looking for the next one
+ start = i + 1;
+ i = a.find('\r', start);
+ }
- if (this->MyClass && (recvq.length() > this->MyClass->GetRecvqMax()))
+ if (start != a.length())
{
- this->SetWriteError("RecvQ exceeded");
- ServerInstance->SNO->WriteToSnoMask('A', "User %s RecvQ of %lu exceeds connect class maximum of %lu",this->nick,(unsigned long int)recvq.length(),this->MyClass->GetRecvqMax());
- return false;
+ /*
+ * This is here to catch a corner case when we get something like:
+ * NICK w0
+ * 0t\r\nU
+ * SER ...
+ * in successive calls to us.
+ *
+ * Without this conditional, the 'U' on the second case will be dropped,
+ * which is most *certainly* not the behaviour we want!
+ * -- w00t
+ */
+ ServerInstance->Logs->Log("recvqdebug", DEBUG, "*** ALERT *** start != a.length, we should probably add more");
+ recvq.append(a, start, (a.length() - start));
}
- return true;
+ ServerInstance->Logs->Log("recvqdebug", DEBUG, "Final recvq length is %d\n(%s)", recvq.length(), recvq.c_str());
}
- catch (...)
+ if (this->MyClass && (recvq.length() > this->MyClass->GetRecvqMax()))
{
- ServerInstance->Logs->Log("USERS", DEBUG,"Exception in User::AddBuffer()");
+ this->SetWriteError("RecvQ exceeded");
+ ServerInstance->SNO->WriteToSnoMask('A', "User %s RecvQ of %lu exceeds connect class maximum of %lu",this->nick,(unsigned long int)recvq.length(),this->MyClass->GetRecvqMax());
return false;
}
+
+ return true;
}
bool User::BufferIsReady()
// send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
void User::FlushWriteBuf()
{
- printf("\nFlush buffer on %d\n", this->GetFd());
try
{
- printf("1\n");
if ((this->fd == FD_MAGIC_NUMBER) || (*this->GetWriteError()))
{
- printf("Write error set, clear sendq: %s\n", this->GetWriteError());
sendq.clear();
}
- printf("2 sendq len = %u fd = %d\n", sendq.length(), this->fd);
if ((sendq.length()) && (this->fd != FD_MAGIC_NUMBER))
{
- printf("3\n");
int old_sendq_length = sendq.length();
int n_sent = ServerInstance->SE->Send(this, this->sendq.data(), this->sendq.length(), 0);
- printf("\nWrote %d\n", n_sent);
-
if (n_sent == -1)
{
if (errno == EAGAIN)
/* Fatal error, set write error and bail
*/
this->SetWriteError(errno ? strerror(errno) : "EOF from client");
- printf("\nError on %d\n", this->GetFd());
return;
}
}
ServerInstance->Logs->Log("USERS", DEBUG,"Exception in User::FlushWriteBuf()");
}
- printf("end flush\n");
-
if (this->sendq.empty())
{
FOREACH_MOD(I_OnBufferFlushed,OnBufferFlushed(this));
void User::SetWriteError(const std::string &error)
{
- printf("SET WRITE ERROR: %s\n", error.c_str());
// don't try to set the error twice, its already set take the first string.
if (this->WriteError.empty())
this->WriteError = error;
}
}
- const char* parameters[] = { this->nick, moderemove.c_str() };
- ServerInstance->Parser->CallHandler("MODE", parameters, 2, this);
+ std::vector<std::string> parameters;
+ parameters.push_back(this->nick);
+ parameters.push_back(moderemove);
+
+ ServerInstance->Parser->CallHandler("MODE", parameters, this);
/* unset their oper type (what IS_OPER checks), and remove +o */
*this->oper = 0;
/* Trigger LUSERS output, give modules a chance too */
int MOD_RESULT = 0;
- FOREACH_RESULT(I_OnPreCommand, OnPreCommand("LUSERS", NULL, 0, this, true, "LUSERS"));
+ FOREACH_RESULT(I_OnPreCommand, OnPreCommand("LUSERS", std::vector<std::string>(), this, true, "LUSERS"));
if (!MOD_RESULT)
- ServerInstance->CallCommandHandler("LUSERS", NULL, 0, this);
+ ServerInstance->CallCommandHandler("LUSERS", std::vector<std::string>(), this);
/*
* We don't set REG_ALL until triggering OnUserConnect, so some module events don't spew out stuff
Command* nickhandler = ServerInstance->Parser->GetHandler("NICK");
if (nickhandler) // wtfbbq, when would this not be here
{
+ std::vector<std::string> parameters;
nickhandler->HandleInternal(1, dummy);
- bool result = (ServerInstance->Parser->CallHandler("NICK", &newnick, 1, this) == CMD_SUCCESS);
+ parameters.push_back(newnick);
+ bool result = (ServerInstance->Parser->CallHandler("NICK", parameters, this) == CMD_SUCCESS);
nickhandler->HandleInternal(0, dummy);
return result;
}
*/
void User::Write(std::string text)
{
- printf("Write Error is %s\n", *(GetWriteError()) ? "set" : "unset");
if (!ServerInstance->SE->BoundsCheckFd(this))
return;