forked from wahjam/wahjam
/
netmsg.h
executable file
·120 lines (84 loc) · 2.81 KB
/
netmsg.h
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
/*
Copyright (C) 2005-2007 Cockos Incorporated
Wahjam 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 2 of the License, or
(at your option) any later version.
Wahjam 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 Wahjam; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
This header provides the declarations for the Net_Messsage class, and
Net_Connection class (handles sending and receiving Net_Messages)
*/
#ifndef _NETMSG_H_
#define _NETMSG_H_
#include <time.h>
#include <QTcpSocket>
#include <QHostAddress>
#include "../WDL/queue.h"
#define NET_MESSAGE_MAX_SIZE 16384
#define NET_CON_MAX_MESSAGES 512
#define MESSAGE_KEEPALIVE 0xfd
#define MESSAGE_EXTENDED 0xfe
#define MESSAGE_INVALID 0xff
#define NET_CON_KEEPALIVE_RATE 3
class Net_Message
{
public:
Net_Message() : m_parsepos(0), m_refcnt(0), m_type(MESSAGE_INVALID)
{
}
~Net_Message()
{
}
void set_type(int type) { m_type=type; }
int get_type() { return m_type; }
void set_size(int newsize) { m_hb.Resize(newsize); }
int get_size() { return m_hb.GetSize(); }
void *get_data() { return m_hb.Get(); }
int parseMessageHeader(void *data, int len); // returns bytes used, if any (or 0 if more data needed), or -1 if invalid
int parseBytesNeeded();
int parseAddBytes(void *data, int len); // returns bytes actually added
int makeMessageHeader(void *data); // makes message header, returns length. data should be at least 16 bytes to be safe
void addRef() { ++m_refcnt; }
void releaseRef() { if (--m_refcnt < 1) delete this; }
private:
int m_parsepos;
int m_refcnt;
int m_type;
WDL_HeapBuf m_hb;
};
class Net_Connection
{
public:
Net_Connection(QTcpSocket *sock) : m_error(0), m_recvstate(0), m_recvmsg(0), m_sock(sock)
{
SetKeepAlive(0);
}
~Net_Connection();
Net_Message *Run(int *wantsleep=0);
int Send(Net_Message *msg); // -1 on error, i.e. queue full
int GetStatus(); // returns <0 on error, 0 on normal, 1 on disconnect
QHostAddress GetRemoteAddr();
void SetKeepAlive(int interval)
{
m_keepalive=interval?interval:NET_CON_KEEPALIVE_RATE;
m_last_send=m_last_recv=time(NULL);
}
void Kill();
private:
int m_error;
int m_keepalive;
time_t m_last_send;
time_t m_last_recv;
int m_recvstate;
Net_Message *m_recvmsg;
QTcpSocket *m_sock;
};
#endif