summaryrefslogtreecommitdiff
path: root/src/common/network.h
blob: d7b463a2ff43d5d1103e51dcbfa12b96928dd41c (plain) (blame)
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
#ifndef _rA_NETWORK_H_
#define _rA_NETWORK_H_

#include <netinet/in.h>
#include "../common/cbasetypes.h"
#include "../common/netbuffer.h" 
#include "../common/evdp.h"

#ifndef MAXCONN
#define MAXCONN 16384
#endif


typedef struct SESSION{
	EVDP_DATA	evdp_data;	// Must be always the frist member! (some evdp's may rely on this fact)

	// Connection Type	
	enum{ NST_FREE=0, NST_LISTENER = 1, NST_CLIENT=2, NST_OUTGOING=3}	type;

	// Flags / Settings.
	bool v6; // is v6?
	bool disconnect_in_progress;	// To prevent stack overflows / recursive calls.
	
	
	union{ // union to save memory.
		struct sockaddr_in	v4;
		struct sockaddr_in6	v6;
	}addr;
	

	// "lowlevel" Handlers
	// (Implemented by the protocol specific parser)
	//
	bool (*onRecv)(int32 fd);	// return false = disconnect
	bool (*onSend)(int32 fd);	// return false = disconnect

	// Event Handlers for LISTENER type sockets 
	//
	// onConnect  gets Called when a connection has been 
	//	successfully accepted.
	//	Session entry is available in this Handler!
	//	A returncode of false will reejct the connection (disconnect)
	//	Note: When rejecting a connection in onConnect by returning false
	//		  The onDisconnect handler wont get called!
	//	Note: the onConnect Handler is also responsible for setting
	//		  the appropriate netparser (which implements onRecv/onSend..) [protocol specific]
	//	
	// onDisconnect  gets called when a connection gets disconnected 
	//				 (by peer as well as by core)
	//
	bool (*onConnect)(int32 fd);	// return false = disconnect (wont accept)
	void (*onDisconnect)(int32 fd);


	// 
	// Parser specific data
	//
	void *netparser_data;	// incase of RO Packet Parser, pointer to packet len table (uint16array)
	void (*onPacketComplete)(int32 fd, uint16 op, uint16 len, netbuf buf);
	

	//
	// Buffers
	// 
	struct{
		enum NETREADSTATE { NRS_WAITOP = 0,	NRS_WAITLEN = 1,	NRS_WAITDATA = 2}	state;
		
		uint32	head_left;
		uint16	head[2];
		
		netbuf	buf;
	} read;

	struct{
		uint32	max_outstanding;
		uint32	n_outstanding;
		
		uint32	dataPos;
				
		netbuf	buf, buf_last;				
	} write;
	
	// Application Level data Pointer
	// (required for backward compatibility with previous athena socket system.)
	void *data;
				
} SESSION;


/**
 * Subsystem Initialization / Finalization.
 *
 */
void network_init();
void network_final();


/**
 * Will do the net work :) ..
 */
void network_do();


/** 
 * Adds a new listner.
 *
 * @param v6	v6 listner? 
 * @param *addr	the address to listen on.
 * @param port	port to listen on
 *
 * @return -1 on error  otherwise  the identifier of the new listener.
 */
int32 network_addlistener(bool v6,  const char *addr,  uint16 port);


/**
 * Tries to establish an outgoing connection.
 *
 * @param v6		operate with IPv6 addresses?
 * @param addr		the address to connect to
 * @param port		the port to connect to
 * @param from_addr	the address to connect from (local source / optional if auto  -> NULL)
 * @param from_port the port to connect from (local source / optional if auto  -> 0)
 * @param onConnectionEstablishedHandler	callback that gets called when the connection is established.
 * @param onConnectionLooseHandler			callback that gets called when the connection gets disconnected (or the connection couldnt be established)
 *
 * @return -1 on error  otherwise  the identifier of the new connection
 */
int32 network_connect(bool v6,
						const char *addr,
						uint16 port,
						const char *from_addr,
						uint16 from_port,
						bool (*onConnectionEstablishedHandler)(int32 fd),
						void (*onConnectionLooseHandler)(int32 fd)
);

						

/**
 * Disconnects the given connection
 *
 * @param fd  connection identifier.
 *
 * @Note:
 * 	- onDisconnect callback gets called! 
 *	- cleares (returns) all assigned buffers
 *
 */
void network_disconnect(int32 fd);


/** 
 * Attach's a netbuffer at the end of sending queue to the given connection
 *
 * @param fd	connection identifier
 * @param buf	netbuffer to attach.
 */
void network_send(int32 fd,  netbuf buf);


/**
 * Sets the parser to RO Protocol like Packet Parser.
 * 
 * @param fd				connection identifier
 * @param *packetlentable	pointer to array of uint16 in size of UINT16_MAX,
 * @param onComplteProc		callback for packet completion.
 *
 * @note:
 * 	PacketLen Table Fromat:
 *	each element's offsets represents th ro opcode.
 *	value is length.
 *	a length of 0  means the packet is dynamic.
 *	a length of UINT16_MAX means the packet is unknown.
 *
 *	Static Packets must contain their hader in len so (0x64 ==  55 ..)
 *
 */
void network_parser_set_ro(int32 fd,
							int16 *packetlentable,
							void (*onPacketCompleteProc)(int32 fd,  uint16 op,  uint16 len,  netbuf buf) 
							);
#define ROPACKET_UNKNOWN UINT16_MAX
#define ROPACKET_DYNLEN 0




#endif