Commit 3f0febcd authored by Adrien Dorsaz's avatar Adrien Dorsaz

fix typo on webscoket signal close*d* and add close() method to client; fix...

fix typo on webscoket signal close*d* and add close() method to client; fix assert error on message receive
parent 2c824edd
......@@ -11,7 +11,7 @@
class IHttpClient
{
public:
sigc::signal<void> websocket_close;
sigc::signal<void> websocket_closed;
sigc::signal<void> websocket_closing;
sigc::signal<void> websocket_connect;
sigc::signal<void, ustring> websocket_error;
......
......@@ -13,7 +13,7 @@ class SoupHttpClient : public IHttpClient {
void _websocket_on_message(gint type, GBytes *message);
void _websocket_on_error(GError *error);
void _websocket_on_closing();
void _websocket_on_close();
void _websocket_on_closed();
public:
SoupHttpClient();
~SoupHttpClient();
......@@ -27,5 +27,5 @@ public:
static void websocket_on_message(SoupWebsocketConnection *self, gint type, GBytes *message, gpointer user_data);
static void websocket_on_error(SoupWebsocketConnection *self, GError *error, gpointer user_data);
static void websocket_on_closing(SoupWebsocketConnection *self, gpointer user_data);
static void websocket_on_close(SoupWebsocketConnection *self, gpointer user_data);
static void websocket_on_closed(SoupWebsocketConnection *self, gpointer user_data);
};
......@@ -31,6 +31,11 @@ namespace Xmpp {
FramedStream _stream;
void webSocketURI();
void on_closed();
void on_closing();
void on_connection();
void on_error(ustring error);
void on_message(int type, Glib::RefPtr<Glib::Bytes> message);
public:
void store_password(ustring password);
Client(Account account, std::unique_ptr<IHttpClient> httpClient, std::shared_ptr<ISecretClient> secretClient);
......@@ -38,10 +43,6 @@ namespace Xmpp {
Client & operator=(Client && other);
~Client();
void connect();
void on_close();
void on_closing();
void on_connection();
void on_error(ustring error);
void on_message(int type, Glib::RefPtr<Glib::Bytes> message);
void close();
};
}
......@@ -83,7 +83,7 @@ SoupHttpClient::_websocket_on_connection(GAsyncResult *result)
g_signal_connect(_websocketConnection, "message", G_CALLBACK(&SoupHttpClient::websocket_on_message), this);
g_signal_connect(_websocketConnection, "error", G_CALLBACK(&SoupHttpClient::websocket_on_error), this);
g_signal_connect(_websocketConnection, "closing", G_CALLBACK(&SoupHttpClient::websocket_on_closing), this);
g_signal_connect(_websocketConnection, "closed", G_CALLBACK(&SoupHttpClient::websocket_on_close), this);
g_signal_connect(_websocketConnection, "closed", G_CALLBACK(&SoupHttpClient::websocket_on_closed), this);
websocket_connect();
}
......@@ -97,7 +97,7 @@ SoupHttpClient::websocket_on_message([[maybe_unused]] SoupWebsocketConnection *s
void
SoupHttpClient::_websocket_on_message(gint type, GBytes *message)
{
websocket_message(type, Glib::wrap(message, false));
websocket_message(type, Glib::wrap(message, true));
}
void
......@@ -127,14 +127,14 @@ SoupHttpClient::_websocket_on_closing()
}
void
SoupHttpClient::websocket_on_close([[maybe_unused]] SoupWebsocketConnection *self, gpointer user_data)
SoupHttpClient::websocket_on_closed([[maybe_unused]] SoupWebsocketConnection *self, gpointer user_data)
{
static_cast<SoupHttpClient*>(user_data)->_websocket_on_close();
static_cast<SoupHttpClient*>(user_data)->_websocket_on_closed();
}
void
SoupHttpClient::_websocket_on_close()
SoupHttpClient::_websocket_on_closed()
{
websocket_close();
websocket_closed();
}
......@@ -55,7 +55,7 @@ void
Xmpp::Client::connect()
{
// Connect slots to http client signals
_httpClient->websocket_close.connect(sigc::mem_fun(*this, &Xmpp::Client::on_close));
_httpClient->websocket_closed.connect(sigc::mem_fun(*this, &Xmpp::Client::on_closed));
_httpClient->websocket_closing.connect(sigc::mem_fun(*this, &Xmpp::Client::on_closing));
_httpClient->websocket_connect.connect(sigc::mem_fun(*this, &Xmpp::Client::on_connection));
_httpClient->websocket_error.connect(sigc::mem_fun(*this, &Xmpp::Client::on_error));
......@@ -66,6 +66,12 @@ Xmpp::Client::connect()
_httpClient->websocket_connect_server(_webSocketURI);
}
void
Xmpp::Client::close()
{
_httpClient->websocket_send_message(_stream.close());
}
void
Xmpp::Client::webSocketURI()
{
......@@ -119,7 +125,7 @@ Xmpp::Client::store_password(ustring password)
}
void
Xmpp::Client::on_close()
Xmpp::Client::on_closed()
{
}
......@@ -137,7 +143,10 @@ Xmpp::Client::on_connection()
void
Xmpp::Client::on_error(ustring error)
{
std::cout << "Error: "s << error << std::endl;
if (Project::Constants::Debug)
{
std::cout << "Error: "s << error << std::endl;
}
}
void
......@@ -152,13 +161,21 @@ Xmpp::Client::on_message(int type, Glib::RefPtr<Glib::Bytes> message)
if (ptr)
{
_dom_parser.parse_memory_raw(ptr, size);
if (_dom_parser)
{
isValid = true;
std::cout << _dom_parser.get_document()->write_to_string_formatted() << std::endl;
}
try {
_dom_parser.parse_memory_raw(ptr, size);
if (_dom_parser)
{
isValid = true;
}
}
catch(...)
{
if (Project::Constants::Debug)
{
std::cout << "Failed to parse XML document"s << std::endl;
}
}
}
if (!isValid)
......@@ -166,7 +183,11 @@ Xmpp::Client::on_message(int type, Glib::RefPtr<Glib::Bytes> message)
_httpClient->websocket_send_close(SOUP_WEBSOCKET_CLOSE_BAD_DATA , "unable to parse received message");
}
std::cout << "Message received: " << std::endl;
auto document = _dom_parser.get_document();
if (Project::Constants::Debug)
{
std::cout << "Message received: " << std::endl << document->write_to_string_formatted() << std::endl;
}
}
else
{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment