~ubuntu-branches/ubuntu/raring/lordsawar/raring

« back to all changes in this revision

Viewing changes to src/network-connection.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese, Barry deFreese, Gonéri Le Bouder
  • Date: 2008-06-17 11:15:26 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080617111526-yjyvu9df50zmpdo0
Tags: 0.0.9-1
[ Barry deFreese ]
* New upstream release.
  + Fixes gcc-4.3 builds so drop ftbfs_gcc-4.3_fix.diff.
  + Add new build-dependency for libgnet-dev.
* Add simple man page for new lordsawar-tile-editor.
* Add desktop file for lordsawar-tile-editor.
* Remove French translation on install.

[ Gonéri Le Bouder ]
* bump Debian Policy to 3.8.0. No change needed.
* fix wording in the 0.0.8-3 entry of the Debian changelog

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2008 Ole Laursen
 
2
//
 
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.
 
7
//
 
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.
 
12
//
 
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 
 
16
//  02110-1301, USA.
 
17
 
 
18
#include "network-connection.h"
 
19
#include <iostream>
 
20
#include <cstring>
 
21
#include <gnet.h>
 
22
 
 
23
#include "network-common.h"
 
24
 
 
25
static void
 
26
event_helper(GConn* conn, GConnEvent* event, gpointer user_data) 
 
27
{
 
28
  static_cast<NetworkConnection *>(user_data)->gotConnectionEvent(conn, event);
 
29
}
 
30
 
 
31
 
 
32
NetworkConnection::NetworkConnection(_GConn *conn)
 
33
{
 
34
  this->conn = conn;
 
35
  if (conn) {
 
36
    gnet_conn_set_callback(conn, &event_helper, this);
 
37
    receiving_message = false;
 
38
    gnet_conn_readn(conn, MESSAGE_SIZE_BYTES);
 
39
  }
 
40
}
 
41
 
 
42
NetworkConnection::~NetworkConnection()
 
43
{
 
44
  if (conn)
 
45
    gnet_conn_delete(conn);
 
46
}
 
47
 
 
48
void NetworkConnection::connectToHost(std::string host, int port)
 
49
{
 
50
  conn = gnet_conn_new(host.c_str(), port, &event_helper, this);
 
51
  if (!conn)
 
52
    ; // FIXME: report error
 
53
 
 
54
  gnet_conn_connect(conn);
 
55
  gnet_conn_set_watch_error(conn, true);
 
56
  gnet_conn_timeout(conn, 30 * 1000);
 
57
}
 
58
 
 
59
void NetworkConnection::send(MessageType type, const std::string &payload)
 
60
{
 
61
  // write the preamble
 
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;
 
67
  
 
68
  gnet_conn_write(conn, buf, MESSAGE_SIZE_BYTES + MESSAGE_PREAMBLE_EXTRA_BYTES);
 
69
 
 
70
  std::cerr << "sending length " << MESSAGE_PREAMBLE_EXTRA_BYTES + payload.size() << std::endl;
 
71
 
 
72
  // write the payload
 
73
  if (!payload.empty())
 
74
    gnet_conn_write(conn, const_cast<gchar *>(payload.data()), payload.size());
 
75
}
 
76
 
 
77
void NetworkConnection::gotConnectionEvent(GConn* conn, GConnEvent* event)
 
78
{
 
79
  switch (event->type)
 
80
  {
 
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);
 
85
    connected.emit();
 
86
    break;
 
87
    
 
88
  case GNET_CONN_READ:
 
89
    if (receiving_message)
 
90
    {
 
91
      if (event->length < 2)
 
92
      {
 
93
        // misbehaving server
 
94
        gnet_conn_disconnect(conn);
 
95
        break;
 
96
      }
 
97
 
 
98
      // protocol version is ignored for now
 
99
      //int protocol_version = event->buffer[0];
 
100
      MessageType type = MessageType(event->buffer[1]);
 
101
      
 
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);
 
106
    }
 
107
    else
 
108
    {
 
109
      g_assert(event->length == MESSAGE_SIZE_BYTES);
 
110
      guint32 val;
 
111
      memcpy(&val, event->buffer, 4);
 
112
      int message_size = g_ntohl(val);
 
113
      
 
114
      std::cerr << "going to read length " << message_size << std::endl;
 
115
      receiving_message = true;
 
116
      gnet_conn_readn(conn, message_size);
 
117
    }
 
118
    break;
 
119
 
 
120
  case GNET_CONN_WRITE:
 
121
    break;
 
122
 
 
123
  case GNET_CONN_CLOSE:
 
124
  case GNET_CONN_TIMEOUT:
 
125
  case GNET_CONN_ERROR:
 
126
    connection_lost.emit();
 
127
    gnet_conn_delete(conn);
 
128
    break;
 
129
 
 
130
  default:
 
131
    g_assert_not_reached ();
 
132
  }
 
133
}