Changeset View
Changeset View
Standalone View
Standalone View
source/lobby/glooxwrapper/glooxwrapper.cpp
/* Copyright (C) 2018 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 267 Lines • ▼ Show 20 Lines | |||||
{ | { | ||||
public: | public: | ||||
glooxwrapper::Jingle::SessionHandler* m_Wrapped; | glooxwrapper::Jingle::SessionHandler* m_Wrapped; | ||||
bool m_Owned; | bool m_Owned; | ||||
SessionHandlerWrapper(glooxwrapper::Jingle::SessionHandler* wrapped, bool owned) | SessionHandlerWrapper(glooxwrapper::Jingle::SessionHandler* wrapped, bool owned) | ||||
: m_Wrapped(wrapped), m_Owned(owned) {} | : m_Wrapped(wrapped), m_Owned(owned) {} | ||||
~SessionHandlerWrapper() | |||||
{ | |||||
if (m_Owned) | |||||
delete m_Wrapped; | |||||
} | |||||
virtual void handleSessionAction(gloox::Jingle::Action action, gloox::Jingle::Session* session, const gloox::Jingle::Session::Jingle* jingle) | virtual void handleSessionAction(gloox::Jingle::Action action, gloox::Jingle::Session* session, const gloox::Jingle::Session::Jingle* jingle) | ||||
{ | { | ||||
m_Wrapped->handleSessionAction(action, new glooxwrapper::Jingle::Session(session, false), new glooxwrapper::Jingle::Session::Jingle(jingle, false)); | glooxwrapper::Jingle::Session sessionWrapper(session, false); | ||||
glooxwrapper::Jingle::Session::Jingle jingleWrapper(jingle, false); | |||||
m_Wrapped->handleSessionAction(action, sessionWrapper, jingleWrapper); | |||||
elexis: (nitpick rename because `m_Wrapped` relates to the item wrapped while these variables refer to… | |||||
} | } | ||||
virtual void handleSessionActionError(gloox::Jingle::Action UNUSED(action), gloox::Jingle::Session* UNUSED(session), const gloox::Error* UNUSED(error)) | virtual void handleSessionActionError(gloox::Jingle::Action UNUSED(action), gloox::Jingle::Session* UNUSED(session), const gloox::Error* UNUSED(error)) | ||||
{ | { | ||||
} | } | ||||
virtual void handleIncomingSession(gloox::Jingle::Session* UNUSED(session)) | virtual void handleIncomingSession(gloox::Jingle::Session* UNUSED(session)) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | |||||
void glooxwrapper::Client::disconnect() | void glooxwrapper::Client::disconnect() | ||||
{ | { | ||||
m_Wrapped->disconnect(); | m_Wrapped->disconnect(); | ||||
} | } | ||||
void glooxwrapper::Client::registerStanzaExtension(glooxwrapper::StanzaExtension* ext) | void glooxwrapper::Client::registerStanzaExtension(glooxwrapper::StanzaExtension* ext) | ||||
{ | { | ||||
gloox::StanzaExtension* stanza = new StanzaExtensionWrapper(ext, true); | // ~StanzaExtensionFactory() deletes this new StanzaExtensionWrapper | ||||
m_Wrapped->registerStanzaExtension(stanza); | m_Wrapped->registerStanzaExtension(new StanzaExtensionWrapper(ext, true)); | ||||
} | } | ||||
void glooxwrapper::Client::registerConnectionListener(glooxwrapper::ConnectionListener* hnd) | void glooxwrapper::Client::registerConnectionListener(glooxwrapper::ConnectionListener* hnd) | ||||
{ | { | ||||
gloox::ConnectionListener* listener = new ConnectionListenerWrapper(hnd); | gloox::ConnectionListener* listener = new ConnectionListenerWrapper(hnd); | ||||
m_Wrapped->registerConnectionListener(listener); | m_Wrapped->registerConnectionListener(listener); | ||||
m_Impl->m_ConnectionListeners.push_back(shared_ptr<gloox::ConnectionListener>(listener)); | m_Impl->m_ConnectionListeners.push_back(shared_ptr<gloox::ConnectionListener>(listener)); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 396 Lines • ▼ Show 20 Lines | if (m_Owned) | ||||
delete m_Wrapped; | delete m_Wrapped; | ||||
} | } | ||||
const glooxwrapper::Jingle::Plugin glooxwrapper::Jingle::Plugin::findPlugin(int type) const | const glooxwrapper::Jingle::Plugin glooxwrapper::Jingle::Plugin::findPlugin(int type) const | ||||
{ | { | ||||
return glooxwrapper::Jingle::Plugin(m_Wrapped->findPlugin(type), false); | return glooxwrapper::Jingle::Plugin(m_Wrapped->findPlugin(type), false); | ||||
} | } | ||||
glooxwrapper::Jingle::Content::Content(const string& name, const PluginList& plugins) | |||||
: glooxwrapper::Jingle::Plugin(NULL, false) | |||||
{ | |||||
gloox::Jingle::PluginList glooxPluginList; | |||||
for (const glooxwrapper::Jingle::Plugin* const& plugin: plugins) | |||||
glooxPluginList.push_back(plugin->getWrapped()); | |||||
m_Wrapped = new gloox::Jingle::Content(name.to_string(), glooxPluginList); | |||||
m_Owned = true; | |||||
} | |||||
glooxwrapper::Jingle::Content::Content() | |||||
: glooxwrapper::Jingle::Plugin(NULL, false) | |||||
{ | |||||
m_Wrapped = new gloox::Jingle::Content(); | |||||
m_Owned = true; | |||||
} | |||||
const glooxwrapper::Jingle::PluginList glooxwrapper::Jingle::Session::Jingle::plugins() const | |||||
{ | |||||
glooxwrapper::Jingle::PluginList pluginListWrapper; | |||||
for (const gloox::Jingle::Plugin* const& plugin : m_Wrapped->plugins()) | |||||
pluginListWrapper.push_back(new glooxwrapper::Jingle::Plugin(const_cast<gloox::Jingle::Plugin*>(plugin), false)); | |||||
elexisAuthorUnsubmitted Done Inline ActionsLeak / Unused code: If the unused class wasn't removed (I did that too in D2094), it would probably be cleaner to return references, or tell the caller to delete the plugins. elexis: Leak / Unused code: If the unused class wasn't removed (I did that too in D2094), it would… | |||||
return pluginListWrapper; | |||||
} | |||||
glooxwrapper::Jingle::ICEUDP::Candidate glooxwrapper::Jingle::Session::Jingle::getCandidate() const | glooxwrapper::Jingle::ICEUDP::Candidate glooxwrapper::Jingle::Session::Jingle::getCandidate() const | ||||
{ | { | ||||
const gloox::Jingle::Content* content = static_cast<const gloox::Jingle::Content*>(m_Wrapped->plugins().front()); | const gloox::Jingle::Content* content = static_cast<const gloox::Jingle::Content*>(m_Wrapped->plugins().front()); | ||||
if (!content) | if (!content) | ||||
return glooxwrapper::Jingle::ICEUDP::Candidate(); | return glooxwrapper::Jingle::ICEUDP::Candidate(); | ||||
const gloox::Jingle::ICEUDP* iceUDP = static_cast<const gloox::Jingle::ICEUDP*>(content->findPlugin(gloox::Jingle::PluginICEUDP)); | const gloox::Jingle::ICEUDP* iceUDP = static_cast<const gloox::Jingle::ICEUDP*>(content->findPlugin(gloox::Jingle::PluginICEUDP)); | ||||
if (!iceUDP) | if (!iceUDP) | ||||
return glooxwrapper::Jingle::ICEUDP::Candidate(); | return glooxwrapper::Jingle::ICEUDP::Candidate(); | ||||
gloox::Jingle::ICEUDP::Candidate glooxCandidate = iceUDP->candidates().front(); | gloox::Jingle::ICEUDP::Candidate glooxCandidate = iceUDP->candidates().front(); | ||||
return glooxwrapper::Jingle::ICEUDP::Candidate{glooxCandidate.ip, glooxCandidate.port}; | return glooxwrapper::Jingle::ICEUDP::Candidate{glooxCandidate.ip, glooxCandidate.port}; | ||||
} | } | ||||
glooxwrapper::Jingle::Session::Session(gloox::Jingle::Session* wrapped, bool owned) | |||||
: m_Wrapped(wrapped), m_Owned(owned) | |||||
{ | |||||
} | |||||
glooxwrapper::Jingle::Session::~Session() | |||||
{ | |||||
if (m_Owned) | |||||
delete m_Wrapped; | |||||
} | |||||
bool glooxwrapper::Jingle::Session::sessionInitiate(char* ipStr, u16 port) | bool glooxwrapper::Jingle::Session::sessionInitiate(char* ipStr, u16 port) | ||||
{ | { | ||||
gloox::Jingle::ICEUDP::CandidateList* candidateList = new gloox::Jingle::ICEUDP::CandidateList(); | gloox::Jingle::ICEUDP::CandidateList candidateList; | ||||
candidateList->push_back(gloox::Jingle::ICEUDP::Candidate | candidateList.push_back(gloox::Jingle::ICEUDP::Candidate | ||||
{ | { | ||||
"1", // component_id, | "1", // component_id, | ||||
"1", // foundation | "1", // foundation | ||||
"0", // andidate_generation | "0", // andidate_generation | ||||
"1", // candidate_id | "1", // candidate_id | ||||
ipStr, | ipStr, | ||||
"0", // network | "0", // network | ||||
port, | port, | ||||
0, // priotiry | 0, // priotiry | ||||
"udp", | "udp", | ||||
"", // base_ip | "", // base_ip | ||||
0, // base_port | 0, // base_port | ||||
gloox::Jingle::ICEUDP::ServerReflexive | gloox::Jingle::ICEUDP::ServerReflexive | ||||
}); | }); | ||||
gloox::Jingle::PluginList* pluginList = new gloox::Jingle::PluginList(); | // Content will delete the new ICEUDP plugin in the inherited ~Plugin() | ||||
pluginList->push_back(new gloox::Jingle::ICEUDP(/*local_pwd*/"", /*local_ufrag*/"", *candidateList)); | gloox::Jingle::PluginList pluginList; | ||||
return m_Wrapped->sessionInitiate(new gloox::Jingle::Content(std::string("game-data"), *pluginList)); | pluginList.push_back(new gloox::Jingle::ICEUDP(/*local_pwd*/"", /*local_ufrag*/"", candidateList)); | ||||
} | |||||
glooxwrapper::Jingle::ICEUDP::ICEUDP(glooxwrapper::Jingle::ICEUDP::CandidateList& candidates) | gloox::Jingle::Content* content = new gloox::Jingle::Content(std::string("game-data"), pluginList); | ||||
elexisAuthorUnsubmitted Done Inline ActionsUnused code / Leak: There would have been a destructor nuking m_Wrapped missing here. elexis: Unused code / Leak: There would have been a destructor nuking m_Wrapped missing here. | |||||
: glooxwrapper::Jingle::Plugin(NULL, false) | |||||
{ | |||||
gloox::Jingle::ICEUDP::CandidateList glooxCandidates; | |||||
for (const glooxwrapper::Jingle::ICEUDP::Candidate& candidate : candidates) | |||||
glooxCandidates.push_back(gloox::Jingle::ICEUDP::Candidate | |||||
{ | |||||
"1", // component_id, | |||||
"1", // foundation | |||||
"0", // candidate_generation | |||||
"1", // candidate_id | |||||
candidate.ip.to_string(), | |||||
"0", // network | |||||
candidate.port, | |||||
0, // priority | |||||
"udp", | |||||
"", // base_ip | |||||
0, // base_port | |||||
gloox::Jingle::ICEUDP::ServerReflexive | |||||
}); | |||||
m_Wrapped = new gloox::Jingle::ICEUDP(/*local_pwd*/"", /*local_ufrag*/"", glooxCandidates); | // sessionInitiate deletes the new Content | ||||
elexisAuthorUnsubmitted Done Inline Actionselexis: according to https://camaya.net/api/gloox/classgloox_1_1Jingle_1_1Session. | |||||
m_Owned = true; | return m_Wrapped->sessionInitiate(content); | ||||
} | |||||
glooxwrapper::Jingle::ICEUDP::ICEUDP() | |||||
: glooxwrapper::Jingle::Plugin(NULL, false) | |||||
{ | |||||
m_Wrapped = new gloox::Jingle::ICEUDP(); | |||||
m_Owned = true; | |||||
} | |||||
const glooxwrapper::Jingle::ICEUDP::CandidateList glooxwrapper::Jingle::ICEUDP::candidates() const | |||||
{ | |||||
glooxwrapper::Jingle::ICEUDP::CandidateList candidateListWrapper; | |||||
for (const gloox::Jingle::ICEUDP::Candidate& candidate : static_cast<const gloox::Jingle::ICEUDP*>(m_Wrapped)->candidates()) | |||||
candidateListWrapper.push_back(glooxwrapper::Jingle::ICEUDP::Candidate{candidate.ip, candidate.port}); | |||||
return candidateListWrapper; | |||||
} | } | ||||
glooxwrapper::SessionManager::SessionManager(Client* parent, Jingle::SessionHandler* sh) | glooxwrapper::SessionManager::SessionManager(Client* parent, Jingle::SessionHandler* sh) | ||||
{ | { | ||||
m_HandlerWrapper = new SessionHandlerWrapper(sh, false); | m_HandlerWrapper = new SessionHandlerWrapper(sh, false); | ||||
m_Wrapped = new gloox::Jingle::SessionManager(parent->getWrapped(), m_HandlerWrapper); | m_Wrapped = new gloox::Jingle::SessionManager(parent->getWrapped(), m_HandlerWrapper); | ||||
} | } | ||||
glooxwrapper::SessionManager::~SessionManager() | glooxwrapper::SessionManager::~SessionManager() | ||||
{ | { | ||||
delete m_Wrapped; | delete m_Wrapped; | ||||
delete m_HandlerWrapper; | delete m_HandlerWrapper; | ||||
} | } | ||||
void glooxwrapper::SessionManager::registerPlugins() | void glooxwrapper::SessionManager::registerPlugins() | ||||
{ | { | ||||
// This calls m_factory.registerPlugin (see jinglesessionmanager.cpp), hence | |||||
// ~PluginFactory() will delete these new plugin templates. | |||||
m_Wrapped->registerPlugin(new gloox::Jingle::Content()); | m_Wrapped->registerPlugin(new gloox::Jingle::Content()); | ||||
m_Wrapped->registerPlugin(new gloox::Jingle::ICEUDP()); | m_Wrapped->registerPlugin(new gloox::Jingle::ICEUDP()); | ||||
} | } | ||||
glooxwrapper::Jingle::Session glooxwrapper::SessionManager::createSession(const JID& callee) | glooxwrapper::Jingle::Session glooxwrapper::SessionManager::createSession(const JID& callee) | ||||
{ | { | ||||
// The wrapped gloox SessionManager keeps track of this session and deletes it on ~SessionManager() | |||||
gloox::Jingle::Session* glooxSession = m_Wrapped->createSession(callee.getWrapped(), m_HandlerWrapper); | gloox::Jingle::Session* glooxSession = m_Wrapped->createSession(callee.getWrapped(), m_HandlerWrapper); | ||||
// Hence this may not own the glooxSession, otherwise double-free | |||||
return glooxwrapper::Jingle::Session(glooxSession, false); | return glooxwrapper::Jingle::Session(glooxSession, false); | ||||
elexisAuthorUnsubmitted Done Inline Actionsinline glooxSession, otherwise double-free is redundant (can be done when committing) elexis: inline `glooxSession`, `otherwise double-free` is redundant (can be done when committing) | |||||
} | } |
Wildfire Games · Phabricator
(nitpick rename because m_Wrapped relates to the item wrapped while these variables refer to the wrapper, also "dromedary case" / lower-camel-case)