summaryrefslogtreecommitdiffstats
path: root/src/Net/Connection.h
blob: 0949ec4dc6f8fd76374089bcca8f72e72bbf6511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * Connection.h
 *
 * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef MAD_NET_CONNECTION_H_
#define MAD_NET_CONNECTION_H_

#include <queue>
#include <gnutls/gnutls.h>
#include <sigc++/signal.h>
#include <poll.h>
#include "Packet.h"

namespace Mad {
namespace Net {

class IPAddress;
class Packet;

class Connection {
  private:
    struct Transmission {
      unsigned long length;
      unsigned long transmitted;

      unsigned char *data;

      sigc::slot<void,const void*,unsigned long> notify;
    };

    enum State {
      DISCONNECTED, HANDSHAKE, CONNECTION_HEADER, PACKET_HEADER, PACKET_DATA, DISCONNECT, BYE
    } state;

    Transmission transR;
    std::queue<Transmission> transS;

    Packet::Data header;

    sigc::signal<void,Connection*,const Packet&> signal;

    void doHandshake();

    void packetHeaderReceiveHandler(const void *data, unsigned long length);
    void packetDataReceiveHandler(const void *data, unsigned long length);

    void doReceive();
    void doSend();

    void doBye();

    void doDisconnect();

    bool receiveComplete() const {
      return (transR.length == transR.transmitted);
    }

    void bye() {
      if(state != DISCONNECT)
        return;

      state = BYE;

      doBye();
    }

    // Prevent shallow copy
    Connection(const Connection &o);
    Connection& operator=(const Connection &o);

  protected:
    struct ConnectionHeader {
      unsigned char m;
      unsigned char a;
      unsigned char d;
      unsigned char type;

      unsigned char versionMajor;
      unsigned char versionMinor;
      unsigned char protVerMin;
      unsigned char protVerMax;
    };

    int sock;
    gnutls_session_t session;

    IPAddress *peer;

    void handshake() {
      if(isConnected())
        return;

      state = HANDSHAKE;

      doHandshake();
    }

    virtual void connectionHeader() = 0;

    bool rawReceive(unsigned long length, const sigc::slot<void,const void*,unsigned long> &notify);
    bool rawSend(const unsigned char *data, unsigned long length);

    bool enterReceiveLoop() {
      if(!isConnected() || isDisconnecting())
        return false;

      state = PACKET_HEADER;

      return rawReceive(sizeof(Packet::Data), sigc::mem_fun(this, &Connection::packetHeaderReceiveHandler));
    }

  public:
    Connection() : state(DISCONNECTED), peer(0) {
      transR.length = transR.transmitted = 0;
      transR.data = 0;
    }

    virtual ~Connection() {
      if(isConnected())
        doDisconnect();

      if(transR.data)
        delete [] transR.data;

      while(!sendQueueEmpty()) {
        delete [] transS.front().data;
        transS.pop();
      }
    }

    bool isConnected() const {return (state != DISCONNECTED);}
    bool isConnecting() const {
      return (state == HANDSHAKE || state == CONNECTION_HEADER);
    }

    bool isDisconnecting() const {
      return (state == DISCONNECT || state == BYE);
    }

    const IPAddress* getPeer() {return peer;}
    int getSocket() const {return sock;}

    void disconnect() {
      if(isConnected() && !isDisconnecting()) {
        state = DISCONNECT;

        if(sendQueueEmpty())
          bye();
      }
    }

    struct pollfd getPollfd() const;

    bool send(const Packet &packet) {
      if(!isConnected() || isConnecting() || isDisconnecting())
        return false;

      return rawSend(reinterpret_cast<const unsigned char*>(packet.getRawData()), packet.getRawDataLength());
    }

    void sendReceive(short events = POLLIN|POLLOUT);

    bool sendQueueEmpty() const {return transS.empty();}

    sigc::signal<void,Connection*,const Packet&> signalReceive() const {return signal;}

    static void init() {
      gnutls_global_init();
    }

    static void deinit() {
      gnutls_global_deinit();
    }
};

}
}

#endif /*MAD_NET_CONNECTION_H_*/