diff options
author | Adam <Adam@anope.org> | 2012-12-13 06:12:56 -0500 |
---|---|---|
committer | Adam <Adam@anope.org> | 2012-12-13 06:12:56 -0500 |
commit | c1077faa281c5635f85b892e605e23bd6c8fcc3b (patch) | |
tree | 213b5f87a19f182e1efd6110f03ff10d5b10ebf6 /src | |
parent | 76ba147c22944b67e8522cd2bb7b6e1bae498ced (diff) |
Optimize much of the database code and serialize code.
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 8 | ||||
-rw-r--r-- | src/access.cpp | 24 | ||||
-rw-r--r-- | src/base.cpp | 22 | ||||
-rw-r--r-- | src/bots.cpp | 22 | ||||
-rw-r--r-- | src/init.cpp | 181 | ||||
-rw-r--r-- | src/main.cpp | 39 | ||||
-rw-r--r-- | src/memos.cpp | 17 | ||||
-rw-r--r-- | src/modulemanager.cpp | 5 | ||||
-rw-r--r-- | src/nickalias.cpp | 34 | ||||
-rw-r--r-- | src/nickcore.cpp | 17 | ||||
-rw-r--r-- | src/pipeengine.cpp (renamed from src/socketengines/pipeengine_pipe.cpp) | 25 | ||||
-rw-r--r-- | src/regchannel.cpp | 180 | ||||
-rw-r--r-- | src/serialize.cpp | 35 | ||||
-rw-r--r-- | src/socketengines/pipeengine_eventfd.cpp | 39 | ||||
-rw-r--r-- | src/sockets.cpp | 17 | ||||
-rw-r--r-- | src/xline.cpp | 21 |
16 files changed, 334 insertions, 352 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2c7566e78..8012e03e1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,14 +11,6 @@ if(WIN32) append_to_list(SRC_SRCS win32/sigaction/sigaction.cpp) endif(WIN32) -# If we have eventfd, use it -if(HAVE_EVENTFD) - append_to_list(SRC_SRCS socketengines/pipeengine_pipe.cpp) -# Else fall back to pipe -else(HAVE_EVENTFD) - append_to_list(SRC_SRCS socketengines/pipeengine_pipe.cpp) -endif(HAVE_EVENTFD) - if(HAVE_EPOLL) append_to_list(SRC_SRCS socketengines/socketengine_epoll.cpp) else(HAVE_EPOLL) diff --git a/src/access.cpp b/src/access.cpp index 863e0d41d..c8cd3cf98 100644 --- a/src/access.cpp +++ b/src/access.cpp @@ -99,25 +99,26 @@ ChanAccess::~ChanAccess() { } -Serialize::Data ChanAccess::Serialize() const +void ChanAccess::Serialize(Serialize::Data &data) const { - Serialize::Data data; - data["provider"] << this->provider->name; data["ci"] << this->ci->name; data["mask"] << this->mask; data["creator"] << this->creator; - data["last_seen"].SetType(Serialize::DT_INT) << this->last_seen; - data["created"].SetType(Serialize::DT_INT) << this->created; + data.SetType("last_seen", Serialize::Data::DT_INT); data["last_seen"] << this->last_seen; + data.SetType("created", Serialize::Data::DT_INT); data["created"] << this->created; data["data"] << this->AccessSerialize(); - - return data; } Serializable* ChanAccess::Unserialize(Serializable *obj, Serialize::Data &data) { - ServiceReference<AccessProvider> aprovider("AccessProvider", data["provider"].astr()); - ChannelInfo *ci = ChannelInfo::Find(data["ci"].astr()); + Anope::string provider, chan; + + data["provider"] >> provider; + data["ci"] >>chan; + + ServiceReference<AccessProvider> aprovider("AccessProvider", provider); + ChannelInfo *ci = ChannelInfo::Find(chan); if (!aprovider || !ci) return NULL; @@ -131,7 +132,10 @@ Serializable* ChanAccess::Unserialize(Serializable *obj, Serialize::Data &data) data["creator"] >> access->creator; data["last_seen"] >> access->last_seen; data["created"] >> access->created; - access->AccessUnserialize(data["data"].astr()); + + Anope::string adata; + data["data"] >> adata; + access->AccessUnserialize(adata); if (!obj) ci->AddAccess(access); diff --git a/src/base.cpp b/src/base.cpp index 5b8d828b6..b98b68893 100644 --- a/src/base.cpp +++ b/src/base.cpp @@ -14,25 +14,37 @@ std::map<Anope::string, std::map<Anope::string, Service *> > Service::Services; std::map<Anope::string, std::map<Anope::string, Anope::string> > Service::Aliases; -Base::Base() +Base::Base() : references(NULL) { } Base::~Base() { - for (std::set<ReferenceBase *>::iterator it = this->references.begin(), it_end = this->references.end(); it != it_end; ++it) + if (this->references != NULL) { - (*it)->Invalidate(); + for (std::set<ReferenceBase *>::iterator it = this->references->begin(), it_end = this->references->end(); it != it_end; ++it) + (*it)->Invalidate(); + delete this->references; } } void Base::AddReference(ReferenceBase *r) { - this->references.insert(r); + if (this->references == NULL) + this->references = new std::set<ReferenceBase *>(); + this->references->insert(r); } void Base::DelReference(ReferenceBase *r) { - this->references.erase(r); + if (this->references != NULL) + { + this->references->erase(r); + if (this->references->empty()) + { + delete this->references; + this->references = NULL; + } + } } diff --git a/src/bots.cpp b/src/bots.cpp index 1e48d034e..da5fe7574 100644 --- a/src/bots.cpp +++ b/src/bots.cpp @@ -82,29 +82,33 @@ BotInfo::~BotInfo() BotListByUID->erase(this->uid); } -Serialize::Data BotInfo::Serialize() const +void BotInfo::Serialize(Serialize::Data &data) const { - Serialize::Data data; - - data["nick"].SetMax(64)/*XXX*/ << this->nick; + data["nick"] << this->nick; data["user"] << this->ident; data["host"] << this->host; data["realname"] << this->realname; data["created"] << this->created; data["flags"] << this->ToString(); - - return data; } Serializable* BotInfo::Unserialize(Serializable *obj, Serialize::Data &data) { + Anope::string nick, user, host, realname, flags; + + data["nick"] >> nick; + data["user"] >> user; + data["host"] >> host; + data["realname"] >> realname; + data["flags"] >> flags; + BotInfo *bi; if (obj) bi = anope_dynamic_static_cast<BotInfo *>(obj); - else if (!(bi = BotInfo::Find(data["nick"].astr()))) - bi = new BotInfo(data["nick"].astr(), data["user"].astr(), data["host"].astr(), data["realname"].astr()); + else if (!(bi = BotInfo::Find(nick))) + bi = new BotInfo(nick, user, host, realname); data["created"] >> bi->created; - bi->FromString(data["flags"].astr()); + bi->FromString(flags); return bi; } diff --git a/src/init.cpp b/src/init.cpp index e45d2a6f9..14e7d2025 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -16,7 +16,6 @@ #include "protocol.h" #include "bots.h" #include "xline.h" -#include "signals.h" #include "socketengine.h" #include "servers.h" #include "language.h" @@ -91,91 +90,6 @@ static bool GetCommandLineArgument(const Anope::string &name, char shortname = 0 return GetCommandLineArgument(name, shortname, Unused); } -/*************************************************************************/ - -/* Remove our PID file. Done at exit. */ - -static void remove_pidfile() -{ - remove(Config->PIDFilename.c_str()); -} - -/*************************************************************************/ - -/* Create our PID file and write the PID to it. */ - -static void write_pidfile() -{ - FILE *pidfile = fopen(Config->PIDFilename.c_str(), "w"); - if (pidfile) - { -#ifdef _WIN32 - fprintf(pidfile, "%d\n", static_cast<int>(GetCurrentProcessId())); -#else - fprintf(pidfile, "%d\n", static_cast<int>(getpid())); -#endif - fclose(pidfile); - atexit(remove_pidfile); - } - else - throw CoreException("Can not write to PID file " + Config->PIDFilename); -} - -/*************************************************************************/ - -class SignalReload : public Signal -{ - public: - SignalReload(int sig) : Signal(sig) { } - - void OnNotify() - { - Log() << "Received SIGHUP: Saving databases & rehashing configuration"; - - Anope::SaveDatabases(); - - ServerConfig *old_config = Config; - try - { - Config = new ServerConfig(); - FOREACH_MOD(I_OnReload, OnReload()); - delete old_config; - } - catch (const ConfigException &ex) - { - Config = old_config; - Log() << "Error reloading configuration file: " << ex.GetReason(); - } - } -}; - -class SignalExit : public Signal -{ - public: - SignalExit(int sig) : Signal(sig) { } - - void OnNotify() - { -#ifndef _WIN32 - Log() << "Received " << strsignal(this->signal) << " signal (" << this->signal << "), exiting."; - Anope::QuitReason = Anope::string("Services terminating via signal ") + strsignal(this->signal) + " (" + stringify(this->signal) + ")"; -#else - Log() << "Received signal " << this->signal << ", exiting."; - Anope::QuitReason = Anope::string("Services terminating via signal ") + stringify(this->signal); -#endif - Anope::SaveDatabases(); - Anope::Quitting = true; - } -}; - -class SignalNothing : public Signal -{ - public: - SignalNothing(int sig) : Signal(sig) { } - - void OnNotify() { } -}; - bool Anope::AtTerm() { return isatty(fileno(stdout)) && isatty(fileno(stdin)) && isatty(fileno(stderr)); @@ -196,6 +110,45 @@ void Anope::Fork() #endif } +void Anope::HandleSignal() +{ + switch (Signal) + { + case SIGHUP: + { + Anope::SaveDatabases(); + + ServerConfig *old_config = Config; + try + { + Config = new ServerConfig(); + FOREACH_MOD(I_OnReload, OnReload()); + delete old_config; + } + catch (const ConfigException &ex) + { + Config = old_config; + Log() << "Error reloading configuration file: " << ex.GetReason(); + } + break; + } + case SIGTERM: + case SIGINT: +#ifndef _WIN32 + Log() << "Received " << strsignal(Signal) << " signal (" << Signal << "), exiting."; + Anope::QuitReason = Anope::string("Services terminating via signal ") + strsignal(Signal) + " (" + stringify(Signal) + ")"; +#else + Log() << "Received signal " << Signal << ", exiting."; + Anope::QuitReason = Anope::string("Services terminating via signal ") + stringify(Signal); +#endif + Anope::SaveDatabases(); + Anope::Quitting = true; + break; + } + + Signal = 0; +} + #ifndef _WIN32 static void parent_signal_handler(int signal) { @@ -215,6 +168,57 @@ static void parent_signal_handler(int signal) } #endif +static void SignalHandler(int sig) +{ + Anope::Signal = sig; +} + +static void InitSignals() +{ + struct sigaction sa; + + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + + sa.sa_handler = SignalHandler; + + sigaction(SIGHUP, &sa, NULL); + + sigaction(SIGTERM, &sa, NULL); + sigaction(SIGINT, &sa, NULL); + + sa.sa_handler = SIG_IGN; + + sigaction(SIGCHLD, &sa, NULL); + sigaction(SIGPIPE, &sa, NULL); +} + +/* Remove our PID file. Done at exit. */ + +static void remove_pidfile() +{ + remove(Config->PIDFilename.c_str()); +} + +/* Create our PID file and write the PID to it. */ + +static void write_pidfile() +{ + FILE *pidfile = fopen(Config->PIDFilename.c_str(), "w"); + if (pidfile) + { +#ifdef _WIN32 + fprintf(pidfile, "%d\n", static_cast<int>(GetCurrentProcessId())); +#else + fprintf(pidfile, "%d\n", static_cast<int>(getpid())); +#endif + fclose(pidfile); + atexit(remove_pidfile); + } + else + throw CoreException("Can not write to PID file " + Config->PIDFilename); +} + void Anope::Init(int ac, char **av) { /* Set file creation mask and group ID. */ @@ -432,10 +436,7 @@ void Anope::Init(int ac, char **av) /* Announce ourselves to the logfile. */ Log() << "Anope " << Anope::Version() << " starting up" << (Anope::Debug || Anope::ReadOnly ? " (options:" : "") << (Anope::Debug ? " debug" : "") << (Anope::ReadOnly ? " readonly" : "") << (Anope::Debug || Anope::ReadOnly ? ")" : ""); - new SignalReload(SIGHUP); - new SignalExit(SIGTERM); - new SignalExit(SIGINT); - new SignalNothing(SIGPIPE); + InitSignals(); /* Initialize multi-language support */ Language::InitLanguages(); diff --git a/src/main.cpp b/src/main.cpp index 82cd1af90..c35ee40d9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,7 +14,6 @@ #include "timers.h" #include "config.h" #include "bots.h" -#include "signals.h" #include "socketengine.h" #include "uplink.h" @@ -31,6 +30,7 @@ Anope::string Anope::ServicesDir; Anope::string Anope::ServicesBin; int Anope::ReturnValue = 0; +sig_atomic_t Anope::Signal = 0; bool Anope::Quitting = false; bool Anope::Restarting = false; Anope::string Anope::QuitReason; @@ -63,40 +63,6 @@ void Anope::SaveDatabases() Log(LOG_DEBUG) << "Saving databases"; } -std::vector<Signal *> Signal::SignalHandlers; - -void Signal::SignalHandler(int signal) -{ - for (unsigned i = 0, j = SignalHandlers.size(); i < j; ++i) - if (SignalHandlers[i]->signal == signal) - SignalHandlers[i]->Notify(); -} - -Signal::Signal(int s) : Pipe(), signal(s) -{ - memset(&this->old, 0, sizeof(this->old)); - - this->action.sa_flags = 0; - sigemptyset(&this->action.sa_mask); - this->action.sa_handler = SignalHandler; - - if (sigaction(s, &this->action, &this->old) == -1) - throw CoreException("Unable to install signal " + stringify(s) + ": " + Anope::LastError()); - - SignalHandlers.push_back(this); -} - -Signal::~Signal() -{ - std::vector<Signal *>::iterator it = std::find(SignalHandlers.begin(), SignalHandlers.end(), this); - if (it != SignalHandlers.end()) - SignalHandlers.erase(it); - - sigaction(this->signal, &this->old, NULL); -} - -/*************************************************************************/ - /** The following comes from InspIRCd to get the full path of the Anope executable */ static Anope::string GetFullProgDir(const Anope::string &argv0) @@ -196,6 +162,9 @@ int main(int ac, char **av, char **envp) /* Process the socket engine */ SocketEngine::Process(); + + if (Anope::Signal) + Anope::HandleSignal(); } if (Anope::Restarting) diff --git a/src/memos.cpp b/src/memos.cpp index 2b221b03a..2f2672c51 100644 --- a/src/memos.cpp +++ b/src/memos.cpp @@ -24,17 +24,13 @@ template<> const Anope::string* Flags<MemoFlag>::flags_strings = MemoFlagString; Memo::Memo() : Serializable("Memo") { } -Serialize::Data Memo::Serialize() const +void Memo::Serialize(Serialize::Data &data) const { - Serialize::Data data; - data["owner"] << this->owner; - data["time"].SetType(Serialize::DT_INT) << this->time; + data.SetType("time", Serialize::Data::DT_INT); data["time"] << this->time; data["sender"] << this->sender; data["text"] << this->text; data["flags"] << this->ToString(); - - return data; } Serializable* Memo::Unserialize(Serializable *obj, Serialize::Data &data) @@ -42,8 +38,13 @@ Serializable* Memo::Unserialize(Serializable *obj, Serialize::Data &data) if (!MemoServService) return NULL; + Anope::string owner, flags; + + data["owner"] >> owner; + data["flags"] >> flags; + bool ischan; - MemoInfo *mi = MemoServService->GetMemoInfo(data["owner"].astr(), ischan); + MemoInfo *mi = MemoServService->GetMemoInfo(owner, ischan); if (!mi) return NULL; @@ -56,7 +57,7 @@ Serializable* Memo::Unserialize(Serializable *obj, Serialize::Data &data) data["time"] >> m->time; data["sender"] >> m->sender; data["text"] >> m->text; - m->FromString(data["flags"].astr()); + m->FromString(flags); if (obj == NULL) mi->memos->push_back(m); diff --git a/src/modulemanager.cpp b/src/modulemanager.cpp index 5e2165e30..50eff0cbd 100644 --- a/src/modulemanager.cpp +++ b/src/modulemanager.cpp @@ -93,16 +93,15 @@ static ModuleReturn moduleCopyFile(const Anope::string &name, Anope::string &out } int want = s.st_size; - char *buffer = new char[s.st_size]; + char buffer[1024]; while (want > 0 && !source.fail() && !target.fail()) { - source.read(buffer, want); + source.read(buffer, std::min(want, static_cast<int>(sizeof(buffer)))); int read_len = source.gcount(); target.write(buffer, read_len); want -= read_len; } - delete [] buffer; source.close(); target.close(); diff --git a/src/nickalias.cpp b/src/nickalias.cpp index f163c145f..4ce861943 100644 --- a/src/nickalias.cpp +++ b/src/nickalias.cpp @@ -238,17 +238,15 @@ NickAlias *NickAlias::Find(const Anope::string &nick) return NULL; } -Serialize::Data NickAlias::Serialize() const +void NickAlias::Serialize(Serialize::Data &data) const { - Serialize::Data data; - - data["nick"].SetMax(Config->NickLen) << this->nick; + data["nick"] << this->nick; data["last_quit"] << this->last_quit; data["last_realname"] << this->last_realname; data["last_usermask"] << this->last_usermask; data["last_realhost"] << this->last_realhost; - data["time_registered"].SetType(Serialize::DT_INT) << this->time_registered; - data["last_seen"].SetType(Serialize::DT_INT) << this->last_seen; + data.SetType("time_registered", Serialize::Data::DT_INT); data["time_registered"] << this->time_registered; + data.SetType("time_registered", Serialize::Data::DT_INT); data["last_seen"] << this->last_seen; data["nc"] << this->nc->display; data["flags"] << this->ToString(); @@ -259,13 +257,16 @@ Serialize::Data NickAlias::Serialize() const data["vhost_creator"] << this->GetVhostCreator(); data["vhost_time"] << this->GetVhostCreated(); } - - return data; } Serializable* NickAlias::Unserialize(Serializable *obj, Serialize::Data &data) { - NickCore *core = NickCore::Find(data["nc"].astr()); + Anope::string snc, snick; + + data["nc"] >> snc; + data["nick"] >> snick; + + NickCore *core = NickCore::Find(snc); if (core == NULL) return NULL; @@ -273,7 +274,7 @@ Serializable* NickAlias::Unserialize(Serializable *obj, Serialize::Data &data) if (obj) na = anope_dynamic_static_cast<NickAlias *>(obj); else - na = new NickAlias(data["nick"].astr(), core); + na = new NickAlias(snick, core); if (na->nc != core) { @@ -296,11 +297,20 @@ Serializable* NickAlias::Unserialize(Serializable *obj, Serialize::Data &data) data["last_realhost"] >> na->last_realhost; data["time_registered"] >> na->time_registered; data["last_seen"] >> na->last_seen; - na->FromString(data["flags"].astr()); + Anope::string flags; + data["flags"] >> flags; + na->FromString(flags); + + Anope::string vhost_ident, vhost_host, vhost_creator; time_t vhost_time; + + data["vhost_ident"] >> vhost_ident; + data["vhost_host"] >> vhost_host; + data["vhost_creator"] >> vhost_creator; data["vhost_time"] >> vhost_time; - na->SetVhost(data["vhost_ident"].astr(), data["vhost_host"].astr(), data["vhost_creator"].astr(), vhost_time); + + na->SetVhost(vhost_ident, vhost_host, vhost_creator, vhost_time); return na; } diff --git a/src/nickcore.cpp b/src/nickcore.cpp index f9ac3ef6c..304a40569 100644 --- a/src/nickcore.cpp +++ b/src/nickcore.cpp @@ -78,11 +78,9 @@ NickCore::~NickCore() } } -Serialize::Data NickCore::Serialize() const +void NickCore::Serialize(Serialize::Data &data) const { - Serialize::Data data; - - data["display"].SetMax(Config->NickLen) << this->display; + data["display"] << this->display; data["pass"] << this->pass; data["email"] << this->email; data["greet"] << this->greet; @@ -95,24 +93,27 @@ Serialize::Data NickCore::Serialize() const data["memomax"] << this->memos.memomax; for (unsigned i = 0; i < this->memos.ignores.size(); ++i) data["memoignores"] << this->memos.ignores[i] << " "; - - return data; } Serializable* NickCore::Unserialize(Serializable *obj, Serialize::Data &data) { NickCore *nc; + Anope::string sdisplay, sflags; + + data["display"] >> sdisplay; + data["flags"] >> sflags; + if (obj) nc = anope_dynamic_static_cast<NickCore *>(obj); else - nc = new NickCore(data["display"].astr()); + nc = new NickCore(sdisplay); data["pass"] >> nc->pass; data["email"] >> nc->email; data["greet"] >> nc->greet; data["language"] >> nc->language; - nc->FromString(data["flags"].astr()); + nc->FromString(sflags); { Anope::string buf; data["access"] >> buf; diff --git a/src/socketengines/pipeengine_pipe.cpp b/src/pipeengine.cpp index d56ffef55..6a5b8c3af 100644 --- a/src/socketengines/pipeengine_pipe.cpp +++ b/src/pipeengine.cpp @@ -48,15 +48,34 @@ Pipe::~Pipe() bool Pipe::ProcessRead() { + this->OnNotify(); + char dummy[512]; while (read(this->GetFD(), dummy, 512) == 512); - this->OnNotify(); return true; } +void Pipe::Write(const char *data, size_t sz) +{ + write(this->write_pipe, data, sz); +} + +int Pipe::Read(char *data, size_t sz) +{ + return read(this->GetFD(), data, sz); +} + +bool Pipe::SetWriteBlocking(bool state) +{ + int flags = fcntl(this->write_pipe, F_GETFL, 0); + if (state) + return !fcntl(this->write_pipe, F_SETFL, flags & ~O_NONBLOCK); + else + return !fcntl(this->write_pipe, F_SETFL, flags | O_NONBLOCK); +} + void Pipe::Notify() { - const char dummy = '*'; - write(this->write_pipe, &dummy, 1); + this->Write("\0", 1); } diff --git a/src/regchannel.cpp b/src/regchannel.cpp index 3075ea301..43c51d783 100644 --- a/src/regchannel.cpp +++ b/src/regchannel.cpp @@ -34,20 +34,21 @@ template<> const Anope::string* Flags<ChannelInfoFlag>::flags_strings = ChannelI static const Anope::string AutoKickFlagString[] = { "AK_ISNICK", "" }; template<> const Anope::string* Flags<AutoKickFlag>::flags_strings = AutoKickFlagString; -Serialize::Data BadWord::Serialize() const +void BadWord::Serialize(Serialize::Data &data) const { - Serialize::Data data; - - data["ci"].SetMax(64)/*XXX*/ << this->ci->name; - data["word"].SetMax(512) << this->word; - data["type"].SetType(Serialize::DT_INT) << this->type; - - return data; + data["ci"] << this->ci->name; + data["word"] << this->word; + data.SetType("type", Serialize::Data::DT_INT); data["type"] << this->type; } Serializable* BadWord::Unserialize(Serializable *obj, Serialize::Data &data) { - ChannelInfo *ci = ChannelInfo::Find(data["ci"].astr()); + Anope::string sci, sword; + + data["ci"] >> sci; + data["word"] >> sword; + + ChannelInfo *ci = ChannelInfo::Find(sci); if (!ci) return NULL; @@ -62,7 +63,7 @@ Serializable* BadWord::Unserialize(Serializable *obj, Serialize::Data &data) bw->type = static_cast<BadWordType>(n); } else - bw = ci->AddBadWord(data["word"].astr(), static_cast<BadWordType>(n)); + bw = ci->AddBadWord(sword, static_cast<BadWordType>(n)); return bw; } @@ -71,38 +72,39 @@ AutoKick::AutoKick() : Serializable("AutoKick") { } -Serialize::Data AutoKick::Serialize() const +void AutoKick::Serialize(Serialize::Data &data) const { - Serialize::Data data; - - data["ci"].SetMax(64)/*XXX*/ << this->ci->name; + data["ci"] << this->ci->name; if (this->HasFlag(AK_ISNICK) && this->nc) - data["nc"].SetMax(Config->NickLen) << this->nc->display; + data["nc"] << this->nc->display; else - data["mask"].SetMax(Config->NickLen) << this->mask; + data["mask"] << this->mask; data["reason"] << this->reason; data["creator"] << this->creator; - data["addtime"].SetType(Serialize::DT_INT) << this->addtime; - data["last_used"].SetType(Serialize::DT_INT) << this->last_used; + data.SetType("addtime", Serialize::Data::DT_INT); data["addtime"] << this->addtime; + data.SetType("last_used", Serialize::Data::DT_INT); data["last_used"] << this->last_used; data["flags"] << this->ToString(); - - return data; } Serializable* AutoKick::Unserialize(Serializable *obj, Serialize::Data &data) { - ChannelInfo *ci = ChannelInfo::Find(data["ci"].astr()); + Anope::string sci, snc; + + data["ci"] >> sci; + data["nc"] >> snc; + + ChannelInfo *ci = ChannelInfo::Find(sci); if (!ci) return NULL; AutoKick *ak; - NickCore *nc = NickCore::Find(data["nc"].astr()); + NickCore *nc = NickCore::Find(snc); if (obj) { ak = anope_dynamic_static_cast<AutoKick *>(obj); data["creator"] >> ak->creator; data["reason"] >> ak->reason; - ak->nc = NickCore::Find(data["nc"].astr()); + ak->nc = NickCore::Find(snc); data["mask"] >> ak->mask; data["addtime"] >> ak->addtime; data["last_used"] >> ak->last_used; @@ -113,12 +115,22 @@ Serializable* AutoKick::Unserialize(Serializable *obj, Serialize::Data &data) data["addtime"] >> addtime; data["last_used"] >> lastused; + Anope::string screator, sreason, smask; + + data["creator"] >> screator; + data["reason"] >> sreason; + data["mask"] >> smask; + if (nc) - ak = ci->AddAkick(data["creator"].astr(), nc, data["reason"].astr(), addtime, lastused); + ak = ci->AddAkick(screator, nc, sreason, addtime, lastused); else - ak = ci->AddAkick(data["creator"].astr(), data["mask"].astr(), data["reason"].astr(), addtime, lastused); + ak = ci->AddAkick(screator, smask, sreason, addtime, lastused); } + Anope::string sflags; + data["flags"] >> sflags; + ak->FromString(sflags); + return ak; } @@ -126,27 +138,28 @@ ModeLock::ModeLock(ChannelInfo *ch, bool s, ChannelModeName n, const Anope::stri { } -Serialize::Data ModeLock::Serialize() const +void ModeLock::Serialize(Serialize::Data &data) const { - Serialize::Data data; - if (!this->ci) - return data; + return; const Anope::string* ChannelModeNameStrings = Flags<ChannelModeName>::GetFlagStrings(); - data["ci"].SetMax(64)/*XXX*/ << this->ci->name; - data["set"].SetMax(5) << this->set; - data["name"].SetMax(64) << ChannelModeNameStrings[this->name]; - data["param"].SetMax(512) << this->param; + data["ci"] << this->ci->name; + data["set"] << this->set; + data["name"] << ChannelModeNameStrings[this->name]; + data["param"] << this->param; data["setter"] << this->setter; - data["created"].SetType(Serialize::DT_INT) << this->created; - - return data; + data.SetType("created", Serialize::Data::DT_INT); data["created"] << this->created; } Serializable* ModeLock::Unserialize(Serializable *obj, Serialize::Data &data) { - ChannelInfo *ci = ChannelInfo::Find(data["ci"].astr()); + Anope::string sci, sname; + + data["ci"] >> sci; + data["name"] >> sname; + + ChannelInfo *ci = ChannelInfo::Find(sci); if (!ci) return NULL; @@ -154,7 +167,7 @@ Serializable* ModeLock::Unserialize(Serializable *obj, Serialize::Data &data) const Anope::string* ChannelModeNameStrings = Flags<ChannelModeName>::GetFlagStrings(); for (unsigned i = 0; !ChannelModeNameStrings[i].empty(); ++i) - if (ChannelModeNameStrings[i] == data["name"].astr()) + if (ChannelModeNameStrings[i] == sname) { name = static_cast<ChannelModeName>(i); break; @@ -182,7 +195,10 @@ Serializable* ModeLock::Unserialize(Serializable *obj, Serialize::Data &data) time_t created; data["created"] >> created; - ml = new ModeLock(ci, set, name, "", data["setter"].astr(), created); + Anope::string setter; + data["setter"] >> setter; + + ml = new ModeLock(ci, set, name, "", setter, created); data["param"] >> ml->param; ci->mode_locks->insert(std::make_pair(ml->name, ml)); @@ -190,12 +206,10 @@ Serializable* ModeLock::Unserialize(Serializable *obj, Serialize::Data &data) } } -Serialize::Data LogSetting::Serialize() const +void LogSetting::Serialize(Serialize::Data &data) const { - Serialize::Data data; - if (!ci) - return data; + return; data["ci"] << ci->name; data["service_name"] << service_name; @@ -204,14 +218,16 @@ Serialize::Data LogSetting::Serialize() const data["method"] << method; data["extra"] << extra; data["creator"] << creator; - data["created"].SetType(Serialize::DT_INT) << created; - - return data; + data.SetType("created", Serialize::Data::DT_INT); data["created"] << created; } Serializable* LogSetting::Unserialize(Serializable *obj, Serialize::Data &data) { - ChannelInfo *ci = ChannelInfo::Find(data["ci"].astr()); + Anope::string sci; + + data["ci"] >> sci; + + ChannelInfo *ci = ChannelInfo::Find(sci); if (ci == NULL) return NULL; @@ -376,22 +392,20 @@ ChannelInfo::~ChannelInfo() --this->founder->channelcount; } -Serialize::Data ChannelInfo::Serialize() const +void ChannelInfo::Serialize(Serialize::Data &data) const { - Serialize::Data data; - - data["name"].SetMax(255) << this->name; + data["name"] << this->name; if (this->founder) data["founder"] << this->founder->display; if (this->successor) data["successor"] << this->successor->display; data["description"] << this->desc; - data["time_registered"].SetType(Serialize::DT_INT) << this->time_registered; - data["last_used"].SetType(Serialize::DT_INT) << this->last_used; + data.SetType("time_registered", Serialize::Data::DT_INT); data["time_registered"] << this->time_registered; + data.SetType("last_used", Serialize::Data::DT_INT); data["last_used"] << this->last_used; data["last_topic"] << this->last_topic; data["last_topic_setter"] << this->last_topic_setter; - data["last_topic_time"].SetType(Serialize::DT_INT) << this->last_topic_time; - data["bantype"].SetType(Serialize::DT_INT) << this->bantype; + data.SetType("last_topic_time", Serialize::Data::DT_INT); data["last_topic_time"] << this->last_topic_time; + data.SetType("bantype", Serialize::Data::DT_INT); data["bantype"] << this->bantype; data["flags"] << this->ToString(); data["botflags"] << this->botflags.ToString(); { @@ -404,40 +418,44 @@ Serialize::Data ChannelInfo::Serialize() const data["bi"] << this->bi->nick; for (int i = 0; i < TTB_SIZE; ++i) data["ttb"] << this->ttb[i] << " "; - data["capsmin"].SetType(Serialize::DT_INT) << this->capsmin; - data["capspercent"].SetType(Serialize::DT_INT) << this->capspercent; - data["floodlines"].SetType(Serialize::DT_INT) << this->floodlines; - data["floodsecs"].SetType(Serialize::DT_INT) << this->floodsecs; - data["repeattimes"].SetType(Serialize::DT_INT) << this->repeattimes; + data.SetType("capsmin", Serialize::Data::DT_INT); data["capsmin"] << this->capsmin; + data.SetType("capspercent", Serialize::Data::DT_INT); data["capspercent"] << this->capspercent; + data.SetType("floodlines", Serialize::Data::DT_INT); data["floodlines"] << this->floodlines; + data.SetType("floodsecs", Serialize::Data::DT_INT); data["floodsecs"] << this->floodsecs; + data.SetType("repeattimes", Serialize::Data::DT_INT); data["repeattimes"] << this->repeattimes; data["memomax"] << this->memos.memomax; for (unsigned i = 0; i < this->memos.ignores.size(); ++i) data["memoignores"] << this->memos.ignores[i] << " "; - - return data; } Serializable* ChannelInfo::Unserialize(Serializable *obj, Serialize::Data &data) { + Anope::string sname, sfounder, ssuccessor, sflags, sbotflags, slevels, sbi; + + data["name"] >> sname; + data["founder"] >> sfounder; + data["successor"] >> ssuccessor; + data["flags"] >> sflags; + data["botflags"] >> sbotflags; + data["levels"] >> slevels; + data["bi"] >> sbi; + ChannelInfo *ci; if (obj) ci = anope_dynamic_static_cast<ChannelInfo *>(obj); else - ci = new ChannelInfo(data["name"].astr()); + ci = new ChannelInfo(sname); - if (data.count("founder") > 0) - { - if (ci->founder) - --ci->founder->channelcount; - ci->founder = NickCore::Find(data["founder"].astr()); - if (ci->founder) - ++ci->founder->channelcount; - } - if (data.count("successor") > 0) - { - ci->successor = NickCore::Find(data["successor"].astr()); - if (ci->founder && *ci->founder == *ci->successor) - ci->successor = NULL; - } + if (ci->founder) + --ci->founder->channelcount; + ci->founder = NickCore::Find(sfounder); + if (ci->founder) + ++ci->founder->channelcount; + + ci->successor = NickCore::Find(ssuccessor); + if (ci->founder && *ci->founder == *ci->successor) + ci->successor = NULL; + data["description"] >> ci->desc; data["time_registered"] >> ci->time_registered; data["last_used"] >> ci->last_used; @@ -445,15 +463,15 @@ Serializable* ChannelInfo::Unserialize(Serializable *obj, Serialize::Data &data) data["last_topic_setter"] >> ci->last_topic_setter; data["last_topic_time"] >> ci->last_topic_time; data["bantype"] >> ci->bantype; - ci->FromString(data["flags"].astr()); - ci->botflags.FromString(data["botflags"].astr()); + ci->FromString(sflags); + ci->botflags.FromString(sbotflags); { std::vector<Anope::string> v; - spacesepstream(data["levels"].astr()).GetTokens(v); + spacesepstream(slevels).GetTokens(v); for (unsigned i = 0; i + 1 < v.size(); i += 2) ci->levels[v[i]] = convertTo<int16_t>(v[i + 1]); } - BotInfo *bi = BotInfo::Find(data["bi"].astr()); + BotInfo *bi = BotInfo::Find(sbi); if (*ci->bi != bi) { if (ci->bi) diff --git a/src/serialize.cpp b/src/serialize.cpp index b6da2da67..0f3e6c7a6 100644 --- a/src/serialize.cpp +++ b/src/serialize.cpp @@ -35,7 +35,7 @@ void Serialize::RegisterTypes() memo("Memo", Memo::Unserialize), xline("XLine", XLine::Unserialize); } -stringstream::stringstream() : std::stringstream(), type(Serialize::DT_TEXT), _max(0) +/*stringstream::stringstream() : std::stringstream(), type(Serialize::DT_TEXT), _max(0) { } @@ -84,14 +84,14 @@ stringstream &stringstream::SetMax(unsigned m) unsigned stringstream::GetMax() const { return this->_max; -} +}*/ -Serializable::Serializable() : last_commit_time(0), id(0) +Serializable::Serializable() : last_commit(NULL), last_commit_time(0), id(0) { throw CoreException("Default Serializable constructor?"); } -Serializable::Serializable(const Anope::string &serialize_type) : last_commit_time(0), id(0) +Serializable::Serializable(const Anope::string &serialize_type) : last_commit(NULL), last_commit_time(0), id(0) { if (SerializableItems == NULL) SerializableItems = new std::list<Serializable *>(); @@ -105,7 +105,7 @@ Serializable::Serializable(const Anope::string &serialize_type) : last_commit_ti FOREACH_MOD(I_OnSerializableConstruct, OnSerializableConstruct(this)); } -Serializable::Serializable(const Serializable &other) : last_commit_time(0), id(0) +Serializable::Serializable(const Serializable &other) : last_commit(NULL), last_commit_time(0), id(0) { SerializableItems->push_back(this); this->s_iter = SerializableItems->end(); @@ -119,6 +119,7 @@ Serializable::Serializable(const Serializable &other) : last_commit_time(0), id( Serializable::~Serializable() { SerializableItems->erase(this->s_iter); + delete last_commit; } Serializable &Serializable::operator=(const Serializable &) @@ -144,14 +145,15 @@ void Serializable::QueueUpdate() FOREACH_MOD(I_OnSerializableUpdate, OnSerializableUpdate(this)); } -bool Serializable::IsCached() +bool Serializable::IsCached(Serialize::Data *data) { - return this->last_commit == this->Serialize(); + return this->last_commit && this->last_commit->IsEqual(data); } -void Serializable::UpdateCache() +void Serializable::UpdateCache(Serialize::Data *data) { - this->last_commit = this->Serialize(); + delete this->last_commit; + this->last_commit = data; } bool Serializable::IsTSCached() @@ -164,11 +166,6 @@ void Serializable::UpdateTS() this->last_commit_time = Anope::CurTime; } -Type* Serializable::GetSerializableType() const -{ - return this->s_type; -} - const std::list<Serializable *> &Serializable::GetItems() { return *SerializableItems; @@ -188,11 +185,6 @@ Type::~Type() Types.erase(this->name); } -const Anope::string &Type::GetName() -{ - return this->name; -} - Serializable *Type::Unserialize(Serializable *obj, Serialize::Data &data) { return this->unserialize(obj, data); @@ -213,11 +205,6 @@ void Type::UpdateTimestamp() this->timestamp = Anope::CurTime; } -Module* Type::GetOwner() const -{ - return this->owner; -} - Type *Serialize::Type::Find(const Anope::string &name) { std::map<Anope::string, Type *>::iterator it = Types.find(name); diff --git a/src/socketengines/pipeengine_eventfd.cpp b/src/socketengines/pipeengine_eventfd.cpp deleted file mode 100644 index 61fddd0af..000000000 --- a/src/socketengines/pipeengine_eventfd.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - * - * (C) 2003-2012 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" -#include "sockets.h" - -#include <sys/eventfd.h> - -Pipe::Pipe() : Socket(eventfd(0, EFD_NONBLOCK)) -{ - if (this->sock < 0) - throw CoreException("Could not create pipe: " + Anope::LastError()); -} - -Pipe::~Pipe() -{ -} - -bool Pipe::ProcessRead() -{ - eventfd_t dummy; - eventfd_read(this->GetFD(), &dummy); - this->OnNotify(); - return true; -} - -void Pipe::Notify() -{ - eventfd_write(this->GetFD(), 1); -} - diff --git a/src/sockets.cpp b/src/sockets.cpp index 6aadb7802..3dd3e67b3 100644 --- a/src/sockets.cpp +++ b/src/sockets.cpp @@ -419,7 +419,7 @@ Socket::Socket(int s, bool i, int type) this->sock = socket(this->ipv6 ? AF_INET6 : AF_INET, type, 0); else this->sock = s; - this->SetNonBlocking(); + this->SetBlocking(false); SocketEngine::Sockets[this->sock] = this; SocketEngine::Change(this, true, SF_READABLE); } @@ -443,16 +443,13 @@ bool Socket::IsIPv6() const return ipv6; } -bool Socket::SetBlocking() +bool Socket::SetBlocking(bool state) { int flags = fcntl(this->GetFD(), F_GETFL, 0); - return !fcntl(this->GetFD(), F_SETFL, flags & ~O_NONBLOCK); -} - -bool Socket::SetNonBlocking() -{ - int flags = fcntl(this->GetFD(), F_GETFL, 0); - return !fcntl(this->GetFD(), F_SETFL, flags | O_NONBLOCK); + if (state) + return !fcntl(this->GetFD(), F_SETFL, flags & ~O_NONBLOCK); + else + return !fcntl(this->GetFD(), F_SETFL, flags | O_NONBLOCK); } void Socket::Bind(const Anope::string &ip, int port) @@ -481,7 +478,7 @@ void Socket::ProcessError() ListenSocket::ListenSocket(const Anope::string &bindip, int port, bool i) { - this->SetNonBlocking(); + this->SetBlocking(false); const char op = 1; setsockopt(this->GetFD(), SOL_SOCKET, SO_REUSEADDR, &op, sizeof(op)); diff --git a/src/xline.cpp b/src/xline.cpp index 4ac6581a1..b2fd00974 100644 --- a/src/xline.cpp +++ b/src/xline.cpp @@ -137,10 +137,8 @@ bool XLine::IsRegex() const return !this->mask.empty() && this->mask[0] == '/' && this->mask[this->mask.length() - 1] == '/'; } -Serialize::Data XLine::Serialize() const +void XLine::Serialize(Serialize::Data &data) const { - Serialize::Data data; - data["mask"] << this->mask; data["by"] << this->by; data["created"] << this->created; @@ -149,13 +147,15 @@ Serialize::Data XLine::Serialize() const data["uid"] << this->id; if (this->manager) data["manager"] << this->manager->name; - - return data; } Serializable* XLine::Unserialize(Serializable *obj, Serialize::Data &data) { - ServiceReference<XLineManager> xlm("XLineManager", data["manager"].astr()); + Anope::string smanager; + + data["manager"] >> smanager; + + ServiceReference<XLineManager> xlm("XLineManager", smanager); if (!xlm) return NULL; @@ -176,9 +176,16 @@ Serializable* XLine::Unserialize(Serializable *obj, Serialize::Data &data) } else { + Anope::string smask, sby, sreason, suid; time_t expires; + + data["mask"] >> smask; + data["by"] >> sby; + data["reason"] >> sreason; + data["uid"] >> suid; data["expires"] >> expires; - xl = new XLine(data["mask"].astr(), data["by"].astr(), expires, data["reason"].astr(), data["uid"].astr()); + + xl = new XLine(smask, sby, expires, sreason, suid); xlm->AddXLine(xl); } |