1
// Copyright (C) 2008 Ole Laursen
3
// This program is free software; you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation; either version 2 of the License, or
6
// (at your option) any later version.
8
// This program is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
// GNU Library General Public License for more details.
13
// You should have received a copy of the GNU General Public License
14
// along with this program; if not, write to the Free Software
15
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
#include "network-connection.h"
23
#include "network-common.h"
26
event_helper(GConn* conn, GConnEvent* event, gpointer user_data)
28
static_cast<NetworkConnection *>(user_data)->gotConnectionEvent(conn, event);
32
NetworkConnection::NetworkConnection(_GConn *conn)
36
gnet_conn_set_callback(conn, &event_helper, this);
37
receiving_message = false;
38
gnet_conn_readn(conn, MESSAGE_SIZE_BYTES);
42
NetworkConnection::~NetworkConnection()
45
gnet_conn_delete(conn);
48
void NetworkConnection::connectToHost(std::string host, int port)
50
conn = gnet_conn_new(host.c_str(), port, &event_helper, this);
52
; // FIXME: report error
54
gnet_conn_connect(conn);
55
gnet_conn_set_watch_error(conn, true);
56
gnet_conn_timeout(conn, 30 * 1000);
59
void NetworkConnection::send(MessageType type, const std::string &payload)
62
gchar buf[MESSAGE_SIZE_BYTES + MESSAGE_PREAMBLE_EXTRA_BYTES];
63
guint32 l = g_htonl(MESSAGE_PREAMBLE_EXTRA_BYTES + payload.size());
64
memcpy(buf, &l, MESSAGE_SIZE_BYTES);
65
buf[MESSAGE_SIZE_BYTES] = MESSAGE_PROTOCOL_VERSION;
66
buf[MESSAGE_SIZE_BYTES + 1] = type;
68
gnet_conn_write(conn, buf, MESSAGE_SIZE_BYTES + MESSAGE_PREAMBLE_EXTRA_BYTES);
70
std::cerr << "sending length " << MESSAGE_PREAMBLE_EXTRA_BYTES + payload.size() << std::endl;
74
gnet_conn_write(conn, const_cast<gchar *>(payload.data()), payload.size());
77
void NetworkConnection::gotConnectionEvent(GConn* conn, GConnEvent* event)
81
case GNET_CONN_CONNECT:
82
gnet_conn_timeout(conn, 0); // stop timeout
83
receiving_message = false;
84
gnet_conn_readn(conn, MESSAGE_SIZE_BYTES);
89
if (receiving_message)
91
if (event->length < 2)
94
gnet_conn_disconnect(conn);
98
// protocol version is ignored for now
99
//int protocol_version = event->buffer[0];
100
MessageType type = MessageType(event->buffer[1]);
102
got_message.emit(type, std::string(event->buffer + MESSAGE_PREAMBLE_EXTRA_BYTES,
103
event->length - MESSAGE_PREAMBLE_EXTRA_BYTES));
104
receiving_message = false;
105
gnet_conn_readn(conn, MESSAGE_SIZE_BYTES);
109
g_assert(event->length == MESSAGE_SIZE_BYTES);
111
memcpy(&val, event->buffer, 4);
112
int message_size = g_ntohl(val);
114
std::cerr << "going to read length " << message_size << std::endl;
115
receiving_message = true;
116
gnet_conn_readn(conn, message_size);
120
case GNET_CONN_WRITE:
123
case GNET_CONN_CLOSE:
124
case GNET_CONN_TIMEOUT:
125
case GNET_CONN_ERROR:
126
connection_lost.emit();
127
gnet_conn_delete(conn);
131
g_assert_not_reached ();