Fixed compilation with latest Swiften. Using SafeByteArray for communication with backends

This commit is contained in:
HanzZ 2011-06-03 19:40:30 +02:00
parent 52991c925d
commit 877d783276
9 changed files with 52 additions and 61 deletions

View file

@ -400,7 +400,7 @@ static void *notify_user_info(PurpleConnection *gc, const char *who, PurpleNotif
// They weren't able to do anything with that and I don't know what to do too,
// so it's better to hack through it by not trying to forward really broken things...
if (len < 300000 && data) {
photo = Swift::ByteArray(data, len);
photo = Swift::createByteArray(data, len);
// const gchar *ext = (gchar*)purple_buddy_icon_get_extension(icon);
// if (ext) {
// std::string extension(ext);
@ -416,7 +416,7 @@ static void *notify_user_info(PurpleConnection *gc, const char *who, PurpleNotif
}
np->handleVCard(np->m_accounts[account], np->m_vcards[np->m_accounts[account] + name], name, fullName, nickname, photo.toString());
np->handleVCard(np->m_accounts[account], np->m_vcards[np->m_accounts[account] + name], name, fullName, nickname, Swift::byteArrayToString(photo));
np->m_vcards.erase(np->m_accounts[account] + name);
return NULL;

View file

@ -15,7 +15,8 @@ class DummyUserRegistry : public Swift::UserRegistry {
public:
DummyUserRegistry() {}
virtual bool isValidUserPassword(const Swift::JID&, const std::string&) const {
virtual bool isValidUserPassword(const Swift::JID&user, const Swift::SafeByteArray&) const {
onPasswordValid(user.toString());
return true;
}
};

View file

@ -74,7 +74,7 @@ class NetworkPlugin {
void handleJoinRoomPayload(const std::string &payload);
void handleLeaveRoomPayload(const std::string &payload);
void handleVCardPayload(const std::string &payload);
void handleDataRead(const Swift::ByteArray&);
void handleDataRead(const Swift::SafeByteArray&);
void _handleConnected(bool error);
void handleDisconnected();
@ -82,7 +82,7 @@ class NetworkPlugin {
void sendPong();
void pingTimeout();
std::string m_data;
Swift::SafeByteArray m_data;
std::string m_host;
int m_port;
Swift::BoostNetworkFactories *m_factories;

View file

@ -44,7 +44,7 @@ class NetworkPluginServer {
struct Client {
bool pongReceived;
std::list<User *> users;
std::string data;
Swift::SafeByteArray data;
boost::shared_ptr<Swift::Connection> connection;
};
@ -57,7 +57,7 @@ class NetworkPluginServer {
private:
void handleNewClientConnection(boost::shared_ptr<Swift::Connection> c);
void handleSessionFinished(Client *c);
void handleDataRead(Client *c, const Swift::ByteArray&);
void handleDataRead(Client *c, const Swift::SafeByteArray&);
void handleConnectedPayload(const std::string &payload);
void handleDisconnectedPayload(const std::string &payload);

View file

@ -133,8 +133,8 @@ namespace Transport {
void handleConnected();
void handleConnectionError(const Swift::ComponentError &error);
void handlePresence(Swift::Presence::ref presence);
void handleDataRead(const std::string &data);
void handleDataWritten(const std::string &data);
void handleDataRead(const Swift::SafeByteArray &data);
void handleDataWritten(const Swift::SafeByteArray &data);
// void handleDiscoInfoResponse(boost::shared_ptr<Swift::DiscoInfo> info, Swift::ErrorPayload::ref error, const Swift::JID& jid);
void handleCapsChanged(const Swift::JID& jid);

View file

@ -124,6 +124,7 @@ void NetworkPlugin::handleBuddyChanged(const std::string &user, const std::strin
}
void NetworkPlugin::handleConnected(const std::string &user) {
std::cout << "LOGIN SENT\n";
pbnetwork::Connected d;
d.set_user(user);
@ -264,14 +265,15 @@ void NetworkPlugin::handleVCardPayload(const std::string &data) {
handleVCardRequest(payload.username(), payload.buddyname(), payload.id());
}
void NetworkPlugin::handleDataRead(const Swift::ByteArray &data) {
long expected_size = 0;
m_data += data.toString();
// std::cout << "received data; size = " << m_data.size() << "\n";
void NetworkPlugin::handleDataRead(const Swift::SafeByteArray &data) {
m_data.insert(m_data.begin(), data.begin(), data.end());
while (m_data.size() != 0) {
unsigned int expected_size;
if (m_data.size() >= 4) {
expected_size = (((((m_data[0] << 8) | m_data[1]) << 8) | m_data[2]) << 8) | m_data[3];
// std::cout << "expected_size=" << expected_size << "\n";
expected_size = *((unsigned int*) &m_data[0]);
expected_size = ntohl(expected_size);
if (m_data.size() - 4 < expected_size)
return;
}
@ -279,14 +281,12 @@ void NetworkPlugin::handleDataRead(const Swift::ByteArray &data) {
return;
}
std::string msg = m_data.substr(4, expected_size);
m_data.erase(0, 4 + expected_size);
pbnetwork::WrapperMessage wrapper;
if (wrapper.ParseFromString(msg) == false) {
// TODO: ERROR
if (wrapper.ParseFromArray(&m_data[4], expected_size) == false) {
m_data.erase(m_data.begin(), m_data.begin() + 4 + expected_size);
return;
}
m_data.erase(m_data.begin(), m_data.begin() + 4 + expected_size);
switch(wrapper.type()) {
case pbnetwork::WrapperMessage_Type_TYPE_LOGIN:
@ -317,15 +317,9 @@ void NetworkPlugin::handleDataRead(const Swift::ByteArray &data) {
}
void NetworkPlugin::send(const std::string &data) {
std::string header(" ");
// std::cout << data.size() << "\n";
boost::int32_t size = data.size();
for (int i = 0; i != 4; ++i) {
header.at(i) = static_cast<char>(size >> (8 * (3 - i)));
// std::cout << std::hex << (int) header.at(i) << "\n";
}
m_conn->write(Swift::ByteArray(header + data));
char header[4];
*((int*)(header)) = htonl(data.size());
m_conn->write(Swift::createSafeByteArray(std::string(header, 4) + data));
}
void NetworkPlugin::sendPong() {
@ -336,7 +330,7 @@ void NetworkPlugin::sendPong() {
wrap.SerializeToString(&message);
send(message);
// std::cout << "SENDING PONG\n";
std::cout << "SENDING PONG\n";
}
void NetworkPlugin::pingTimeout() {

View file

@ -171,11 +171,12 @@ void NetworkPluginServer::handleSessionFinished(Client *c) {
void NetworkPluginServer::handleConnectedPayload(const std::string &data) {
pbnetwork::Connected payload;
std::cout << "CONNECTED LOGIN 2 " << payload.user() << "\n";
if (payload.ParseFromString(data) == false) {
// TODO: ERROR
return;
}
std::cout << "CONNECTED LOGIN 2 " << payload.user() << "\n";
std::cout << "CONNECTED LOGIN 3 " << payload.user() << "\n";
m_component->m_userRegistry->onPasswordValid(payload.user());
// std::cout << payload.name() << "\n";
}
@ -206,7 +207,7 @@ void NetworkPluginServer::handleVCardPayload(const std::string &data) {
std::cout << "OMG?\n";
boost::shared_ptr<Swift::VCard> vcard(new Swift::VCard());
vcard->setFullName(payload.fullname());
vcard->setPhoto(Swift::ByteArray(payload.photo()));
vcard->setPhoto(Swift::createByteArray(payload.photo()));
vcard->setNickname(payload.nickname());
m_vcardResponder->sendVCard(payload.id(), vcard);
@ -313,16 +314,15 @@ void NetworkPluginServer::handleConvMessagePayload(const std::string &data, bool
conv->handleMessage(msg, payload.nickname());
}
void NetworkPluginServer::handleDataRead(Client *c, const Swift::ByteArray &data) {
long expected_size = 0;
c->data += data.toString();
// std::cout << "received data; size = " << m_data.size() << "\n";
void NetworkPluginServer::handleDataRead(Client *c, const Swift::SafeByteArray &data) {
c->data.insert(c->data.begin(), data.begin(), data.end());
while (c->data.size() != 0) {
unsigned int expected_size;
if (c->data.size() >= 4) {
unsigned char * head = (unsigned char*) c->data.c_str();
expected_size = (((((*head << 8) | *(head + 1)) << 8) | *(head + 2)) << 8) | *(head + 3);
//expected_size = m_data[0];
// std::cout << "expected_size=" << expected_size << "\n";
expected_size = *((unsigned int*) &c->data[0]);
expected_size = ntohl(expected_size);
if (c->data.size() - 4 < expected_size)
return;
}
@ -330,14 +330,12 @@ void NetworkPluginServer::handleDataRead(Client *c, const Swift::ByteArray &data
return;
}
std::string msg = c->data.substr(4, expected_size);
c->data.erase(0, 4 + expected_size);
pbnetwork::WrapperMessage wrapper;
if (wrapper.ParseFromString(msg) == false) {
// TODO: ERROR
if (wrapper.ParseFromArray(&c->data[4], expected_size) == false) {
c->data.erase(c->data.begin(), c->data.begin() + 4 + expected_size);
return;
}
c->data.erase(c->data.begin(), c->data.begin() + 4 + expected_size);
switch(wrapper.type()) {
case pbnetwork::WrapperMessage_Type_TYPE_CONNECTED:
@ -374,11 +372,9 @@ void NetworkPluginServer::handleDataRead(Client *c, const Swift::ByteArray &data
}
void NetworkPluginServer::send(boost::shared_ptr<Swift::Connection> &c, const std::string &data) {
std::string header(" ");
for (int i = 0; i != 4; ++i)
header.at(i) = static_cast<char>(data.size() >> (8 * (3 - i)));
c->write(Swift::ByteArray(header + data));
char header[4];
*((int*)(header)) = htonl(data.size());
c->write(Swift::createSafeByteArray(std::string(header, 4) + data));
}
void NetworkPluginServer::pingTimeout() {

View file

@ -70,7 +70,7 @@ void RosterManager::sendBuddyRosterPush(Buddy *buddy) {
payload->addItem(item);
Swift::SetRosterRequest::ref request = Swift::SetRosterRequest::create(payload, m_component->getIQRouter(), m_user->getJID().toBare());
Swift::SetRosterRequest::ref request = Swift::SetRosterRequest::create(payload, m_user->getJID().toBare(), m_component->getIQRouter());
request->onResponse.connect(boost::bind(&RosterManager::handleBuddyRosterPushResponse, this, _1, buddy->getName()));
request->send();
}
@ -124,10 +124,10 @@ void RosterManager::sendRIE() {
for (std::map<std::string, Buddy *>::const_iterator it = m_buddies.begin(); it != m_buddies.end(); it++) {
Buddy *buddy = (*it).second;
Swift::RosterItemExchangePayload::Item item;
item.jid = buddy->getJID().toBare();
item.name = buddy->getAlias();
item.action = Swift::RosterItemExchangePayload::Add;
item.groups = buddy->getGroups();
item.setJID(buddy->getJID().toBare());
item.setName(buddy->getAlias());
item.setAction(Swift::RosterItemExchangePayload::Item::Add);
item.setGroups(buddy->getGroups());
payload->addItem(item);
}

View file

@ -36,8 +36,8 @@ class MyUserRegistry : public Swift::UserRegistry {
public:
MyUserRegistry(Component *c) {component = c;}
~MyUserRegistry() {}
bool isValidUserPassword(const JID& user, const std::string& password) const {
users[user.toBare().toString()] = password;
bool isValidUserPassword(const JID& user, const Swift::SafeByteArray& password) const {
users[user.toBare().toString()] = Swift::safeByteArrayToString(password);
Swift::Presence::ref response = Swift::Presence::create();
response->setTo(component->getJID());
response->setFrom(user);
@ -165,12 +165,12 @@ void Component::handleConnectionError(const ComponentError &error) {
m_reconnectTimer->start();
}
void Component::handleDataRead(const std::string &data) {
onXMLIn(data);
void Component::handleDataRead(const Swift::SafeByteArray &data) {
onXMLIn(safeByteArrayToString(data));
}
void Component::handleDataWritten(const std::string &data) {
onXMLOut(data);
void Component::handleDataWritten(const Swift::SafeByteArray &data) {
onXMLOut(safeByteArrayToString(data));
}
void Component::handlePresence(Swift::Presence::ref presence) {