summaryrefslogtreecommitdiffstats
path: root/libksirtet/lib/internal.h
blob: f2e752c3b7e86b0df633b490686efe556649f037 (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
#ifndef INTERNAL_H
#define INTERNAL_H

#include <tqtimer.h>
#include <tqstringlist.h>

#include "socket.h"
#include "mp_interface.h"

class MPBoard;
class RemoteHostData;

//-----------------------------------------------------------------------------
class Local
{
 public:
	Local(MPInterface *_interface, TQValueList<MPInterface::Data> &_boards)
	: interface(_interface), ios(_boards.count()), boards(_boards) {}
	virtual ~Local() {}

	virtual uint nbPlayers() const           { return boards.count();  }
	virtual TQString playerName(uint i) const { return boards[i].name; }
	virtual IOBuffer *ioBuffer(uint i) const { return ios[i]; }
	virtual void writeData(bool inverse);
	virtual WritingStream *globalStream() { return 0; }

 protected:
	MPInterface *interface;
	BufferArray  ios;

	void dataError(uint i);
	void readData(bool inverse);
	void treatData();

 private:
	TQValueList<MPInterface::Data> boards;
};

//-----------------------------------------------------------------------------
class Server
{
 public:
	Server(uint _interval);
	virtual ~Server() {}

 protected:
	WritingStream stream;
	TQTimer timer, ctimer;

	virtual void timeout() = 0;
	void serverTimeout();
	void congestion();

 private:
	uint   interval;
};

//-----------------------------------------------------------------------------
class Network : public TQObject, public Local
{
 Q_OBJECT
  

 public:
	Network(MPInterface *_interface, TQValueList<MPInterface::Data> &_boards,
			const TQPtrList<RemoteHostData> &rhd);
	virtual ~Network();

	virtual uint nbPlayers() const;
	TQString playerName(uint i) const;
	IOBuffer *ioBuffer(uint i) const;

 protected slots:
	virtual void notifier(int fd) = 0;

 protected:
	class RemoteData {
	public:
		RemoteData() {}
		Socket      *socket;
		BufferArray *array;
		bool         received;
		TQStringList  names;
	};
	TQValueList<RemoteData> remotes;

	void readError(uint i);
	void writeError(uint i);
	void brokeError(uint i);
	void disconnectHost(uint i, const TQString &msg);
};

//-----------------------------------------------------------------------------
class LocalServer : public TQObject, public Local, public Server
{
 Q_OBJECT
  

 public:
	LocalServer(MPInterface *_interface,
				TQValueList<MPInterface::Data> &_boards, uint _interval);

	WritingStream *globalStream() { return &stream; }

 private slots:
	void timeoutSlot()           { serverTimeout(); }
	void congestionTimeoutSlot() { congestion(); }

 private:
	void timeout() { treatData(); }
};

//-----------------------------------------------------------------------------
class NetworkServer : public Network, public Server
{
 Q_OBJECT
  

 public:
	NetworkServer(MPInterface *_interface,
				  TQValueList<MPInterface::Data> &_boards,
				  const TQPtrList<RemoteHostData> &rhd, uint _interval);

	void writeData(bool inverse);
	WritingStream *globalStream() { return &stream; }

 private slots:
	void timeoutSlot()           { serverTimeout(); }
	void congestionTimeoutSlot() { congestion(); }
	void notifier(int fd);

 private:
	uint nbReceived;

	void lagError();
	void timeout();
};

//-----------------------------------------------------------------------------
class Client : public Network
{
 Q_OBJECT
  

 public:
	Client(MPInterface *_interface, TQValueList<MPInterface::Data> &_boards,
		   const TQPtrList<RemoteHostData> &rhd)
	: Network(_interface, _boards, rhd) {}

	uint nbPlayers() const { return Local::nbPlayers(); }

 private slots:
	void notifier(int fd);
};

#endif // INTERNAL_H