1 | #ifndef __SOCK_HPP_ |
---|
2 | #define __SOCK_HPP_ |
---|
3 | |
---|
4 | extern const char notify_signature[]; |
---|
5 | extern const char notify_response[]; |
---|
6 | |
---|
7 | class net_address |
---|
8 | { |
---|
9 | public : |
---|
10 | enum protocol { IP, IPX, MODEM, NETBIOS, OTHER }; |
---|
11 | virtual protocol protocol_type() const = 0; |
---|
12 | virtual int equal(const net_address *who) const = 0; |
---|
13 | int operator==(const net_address &who) { return equal(&who); } |
---|
14 | virtual int set_port(int port) = 0; |
---|
15 | virtual int get_port() = 0; |
---|
16 | virtual void print() { ; } |
---|
17 | virtual net_address *copy() = 0; |
---|
18 | virtual void store_string(char *st, int st_length) = 0; // this should be able to be used get_node_address() |
---|
19 | virtual ~net_address() {;} |
---|
20 | } ; |
---|
21 | |
---|
22 | |
---|
23 | class net_socket |
---|
24 | { |
---|
25 | public : |
---|
26 | enum socket_type { SOCKET_SECURE, SOCKET_FAST } ; |
---|
27 | |
---|
28 | virtual int error() = 0; |
---|
29 | virtual int ready_to_read() = 0; |
---|
30 | virtual int ready_to_write() = 0; |
---|
31 | virtual int write(void const *buf, int size, net_address *addr=0) = 0; |
---|
32 | virtual int read(void *buf, int size, net_address **addr=0) = 0; |
---|
33 | virtual int get_fd() = 0; |
---|
34 | virtual ~net_socket() { ; } |
---|
35 | virtual void read_selectable() { ; } |
---|
36 | virtual void read_unselectable() { ; } |
---|
37 | virtual void write_selectable() { ; } |
---|
38 | virtual void write_unselectable() { ; } |
---|
39 | virtual int listen(int port) { return 0; } |
---|
40 | virtual net_socket *accept(net_address *&from) { from=0; return 0; } |
---|
41 | } ; |
---|
42 | |
---|
43 | class net_protocol |
---|
44 | { |
---|
45 | public : |
---|
46 | enum debug_type |
---|
47 | { DB_OFF, // no debug printing |
---|
48 | DB_MAJOR_EVENT, // print major events |
---|
49 | DB_IMPORTANT_EVENT, // anything that would tell where a lockup occures |
---|
50 | DB_MINOR_EVENT } ; // anything you can think off |
---|
51 | |
---|
52 | private : |
---|
53 | debug_type debug_setting; |
---|
54 | public : |
---|
55 | |
---|
56 | enum connect_flags |
---|
57 | { READ_WRITE, // flags for connect to server |
---|
58 | WRITE_ONLY } ; |
---|
59 | |
---|
60 | |
---|
61 | int debug_level(debug_type min_level) { return min_level<=debug_setting; } |
---|
62 | void set_debug_printing(debug_type level) { debug_setting=level; } |
---|
63 | |
---|
64 | static net_protocol *first; |
---|
65 | net_protocol *next; |
---|
66 | |
---|
67 | virtual net_address *get_local_address() = 0; |
---|
68 | virtual net_address *get_node_address(char const *&server_name, int def_port, int force_port) = 0; |
---|
69 | virtual net_socket *connect_to_server(net_address *addr, |
---|
70 | net_socket::socket_type sock_type=net_socket::SOCKET_SECURE) =0; |
---|
71 | virtual net_socket *create_listen_socket(int port, net_socket::socket_type sock_type) = 0; |
---|
72 | virtual int installed() = 0; |
---|
73 | virtual char const *name() = 0; |
---|
74 | virtual int select(int block) = 0; // return # of sockets available for read & writing |
---|
75 | virtual void cleanup() { ; } // should do any needed pre-exit cleanup stuff |
---|
76 | net_socket *connect_to_server(char const *&server_name, int port, int force_port=0, |
---|
77 | net_socket::socket_type sock_type=net_socket::SOCKET_SECURE); |
---|
78 | |
---|
79 | // Notification methods |
---|
80 | virtual net_socket *start_notify(int port, void *data, int len) { return 0; } |
---|
81 | virtual void end_notify() {} |
---|
82 | // for protocols that don't know how to find address without help, start_notify creates |
---|
83 | // a fast/connectionless socket to respond to find address request packets. After start_notify, |
---|
84 | // protocol::select should automatically forward the specified data to all requesters. |
---|
85 | // find_address should "broadcast" a notification request packet to all addresses it can |
---|
86 | // and wait for any responses |
---|
87 | |
---|
88 | // Find methods |
---|
89 | virtual net_address *find_address(int port, char *name) { return 0; } // name should be a 256 byte buffer |
---|
90 | virtual void reset_find_list() { ; } |
---|
91 | |
---|
92 | net_protocol() { next=first; first=this; debug_setting=DB_OFF; } |
---|
93 | virtual ~net_protocol() { cleanup(); } |
---|
94 | } ; |
---|
95 | |
---|
96 | #endif |
---|
97 | |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | |
---|