-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsg_net.hpp
127 lines (112 loc) · 4.35 KB
/
msg_net.hpp
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
/*
* msg_net.hpp
*
* Netzwork access with message classes
*
* Copyright (c) 2003 by Martin Trautmann ([email protected])
*
* This file may be distributed and/or modified under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
*/
#ifndef __HOLTZ_MESSAGE_NET__
#define __HOLTZ_MESSAGE_NET__
#include "line_net.hpp"
#include <string>
namespace holtz
{
template<class Message> class Message_Network;
template<class Message> class Message_Network_Server;
template<class Message>
class Message_Network_Handler
{
public:
virtual ~Message_Network_Handler() {}
protected:
friend class Message_Network<Message>;
// is called when message arrives
virtual void process_message( Message_Network<Message> *sender,
Message *message ) = 0;
// message must be deleted,
// message==0: invalid message arrived
// is called when connection is established (for Message_Network_Client only)
virtual void on_connect( Message_Network<Message> *sender ) = 0;
// is called when connection was closed or couldn't be established
virtual void on_lost( Message_Network<Message> *sender ) = 0;
// is called when an error occured
virtual void on_error( Message_Network<Message> *sender ) = 0;
};
template<class Message>
class Message_Network : public Line_Network_Handler
{
public:
// line_network will be destroyed by Message_Network
Message_Network( Line_Network *line_network,
Message_Network_Handler<Message> *handler = 0 );
~Message_Network();
void send_message( Message *message ); // message won't be deleted
void flush();
bool is_connected() { return line_network->is_connected(); }
// the following functions return 0 or "" if not connected
std::string get_remote_host() { return line_network->get_remote_host(); }
int get_remote_port() { return line_network->get_remote_port(); }
std::string get_local_host() { return line_network->get_local_host(); }
int get_local_port() { return line_network->get_local_port(); }
void set_handler( Message_Network_Handler<Message> *handler );
void set_auto_flush( bool auto_flush ) { line_network->set_auto_flush(auto_flush); }
Line_Network *get_line_network() { return line_network; }
wxSocketBase *get_socket() { return line_network->get_socket(); }
protected:
virtual void process_line( Line_Network *connection, std::string line );
// line has no '\n' at end
// is called when connection is established (for Line_Network_Client only)
virtual void on_connect( Line_Network *connection );
// is called when connection was closed or couldn't be established
virtual void on_lost( Line_Network *connection );
// is called when an error occured
virtual void on_error( Line_Network *connection );
private:
Line_Network *line_network;
Message_Network_Handler<Message> *handler;
};
template<class Message>
class Message_Network_Client : public Message_Network<Message>
{
public:
Message_Network_Client( std::string host, int port, Message_Network_Handler<Message> *handler,
bool auto_flush = true );
};
template<class Message>
class Message_Network_Server_Handler
{
public:
virtual ~Message_Network_Server_Handler() {}
protected:
friend class Message_Network_Server<Message>;
virtual void new_connection( Message_Network<Message> *message_network ) = 0;
// message_network must be deleted!
// ->set_handler() should be called
};
template<class Message>
class Message_Network_Server : private Line_Network_Server_Handler
{
public:
Message_Network_Server( Message_Network_Server_Handler<Message> *server_handler,
bool auto_flush = true );
~Message_Network_Server();
bool bind( int port ); // returns true if bind to port succeded
int get_port() { return line_server->get_port(); } // returns 0 if not bound
wxSocketBase *get_socket() { return line_server->get_socket(); }
protected:
virtual void new_connection( Line_Network *line_network );
private:
Message_Network_Server_Handler<Message> *server_handler;
Line_Network_Server *line_server;
};
}
#include "msg_net_templ.cpp"
#endif