FS2_Open
Open source remastering of the Freespace 2 engine
psnet2.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 
11 
12 #ifndef _PSNET2_H
13 #define _PSNET2_H
14 
15 
16 #ifdef _WIN32
17 #include <winsock.h>
18 #else
19 #include <errno.h>
20 #endif
21 
22 #include "globalincs/pstypes.h"
23 
24 // -------------------------------------------------------------------------------------------------------
25 // PSNET 2 DEFINES/VARS
26 //
27 
28 #define NET_NONE 0 // if no protocol is active or none are selected
29 #define NET_TCP 1
30 #define NET_VMT 3
31 
32 // kazan - I think this should raise the ships limit across the network
33 //#define MAX_PACKET_SIZE 4096
34 #define MAX_PACKET_SIZE 512
35 
36 #define DEFAULT_GAME_PORT 7808
37 
38 typedef struct net_addr {
39  uint type; // See NET_ defines above
40  ubyte net_id[4]; // was used for IPX, now unused
41  ubyte addr[6]; // address - IP only uses 4, is 6 because of retail IPX support
42  short port;
43 } net_addr;
44 
45 // define these in such a manner that a call to psnet_send_reliable is exactly the same and the new code in unobtrusive
48 #if defined(_WIN32)
49 typedef int socklen_t;
50 #endif
51 
52 #undef INVALID_SOCKET
53 #define INVALID_SOCKET (PSNET_SOCKET)(~0)
54 
55 // defines for protocol overheads
56 #define UDP_HEADER_SIZE 34
57 #define TCP_HEADER_SIZE 40
58 #define TCP_HEADER_SIZE_COMPRESSED 6
59 
60 // define values for network errors when trying to enter the ready room
61 #define NETWORK_ERROR_NONE 0
62 #define NETWORK_ERROR_NO_TYPE -1
63 #define NETWORK_ERROR_NO_WINSOCK -2
64 #define NETWORK_ERROR_NO_PROTOCOL -3
65 #define NETWORK_ERROR_RELIABLE -4
66 #define NETWORK_ERROR_CONNECT_TO_ISP -5
67 #define NETWORK_ERROR_LAN_AND_RAS -6
68 
69 // psnet packet types
70 #define PSNET_NUM_TYPES 5
71 #define PSNET_TYPE_UNRELIABLE 0
72 #define PSNET_TYPE_RELIABLE 1
73 #define PSNET_TYPE_USER_TRACKER 2
74 #define PSNET_TYPE_GAME_TRACKER 3
75 #define PSNET_TYPE_VALIDATION 4
76 
77 extern net_addr Psnet_my_addr; // address information of this machine
78 extern uint Psnet_my_ip;
79 extern int Psnet_my_addr_valid;
80 
81 extern int Network_status;
82 extern int Tcp_failure_code;
83 
84 extern int Tcp_active;
85 
86 extern int Socket_type; // protocol type in use (see NET_* defines above)
87 
88 // specified their internet connnection type
89 #define NETWORK_CONNECTION_NONE 1
90 #define NETWORK_CONNECTION_DIALUP 2
91 #define NETWORK_CONNECTION_LAN 3
92 
93 extern int Psnet_connection;
94 
96 
97 // Reliable socket states
98 #define RNF_UNUSED 0 // Completely clean socket..
99 #define RNF_CONNECTED 1 // Connected and running fine
100 #define RNF_BROKEN 2 // Broken - disconnected abnormally
101 #define RNF_DISCONNECTED 3 // Disconnected cleanly
102 #define RNF_CONNECTING 4 // We received the connecting message, but haven't told the game yet.
103 #define RNF_LIMBO 5 // between connecting and connected
104 
105 extern SOCKET Unreliable_socket; // all PXO API modules should use this to send and receive on
106 
107 // -------------------------------------------------------------------------------------------------------
108 // PSNET 2 TOP LAYER FUNCTIONS - these functions simply buffer and store packets based upon type (see PSNET_TYPE_* defines)
109 //
110 
111 struct sockaddr;
112 #ifdef _WIN32
113 struct fd_set;
114 #endif
115 struct timeval;
116 
117 // wrappers around select() and recvfrom() for lagging/losing data, and for sorting through different packet types
118 int RECVFROM(uint s, char * buf, int len, int flags, sockaddr *from, int *fromlen, int psnet_type);
119 int SELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set*exceptfds, const timeval* timeout, int psnet_type);
120 
121 // wrappers around sendto to sorting through different packet types
122 int SENDTO(uint s, char * buf, int len, int flags, sockaddr * to, int tolen, int psnet_type);
123 
124 // call this once per frame to read everything off of our socket
126 
127 
128 // -------------------------------------------------------------------------------------------------------
129 // PSNET 2 FUNCTIONS
130 //
131 
132 // initialize psnet to use the specified port
133 void psnet_init(int protocol, int default_port);
134 
135 // shutdown psnet
136 void psnet_close();
137 
138 // set the protocol to use
139 int psnet_use_protocol(int type);
140 
141 // get the status of the network
143 
144 // convert a net_addr to a string
145 char *psnet_addr_to_string( char * text, net_addr * address );
146 
147 // convert a string to a net addr
148 void psnet_string_to_addr( net_addr * address, char * text );
149 
150 // compare 2 addresses
151 int psnet_same( net_addr * a1, net_addr * a2 );
152 
153 // send data unreliably
154 int psnet_send( net_addr * who_to, void * data, int len, int np_index = -1 );
155 
156 // get data from the unreliable socket
157 int psnet_get( void * data, net_addr * from_addr );
158 
159 // broadcast data on unreliable socket
160 int psnet_broadcast( net_addr * who_to, void * data, int len );
161 
162 // flush all sockets
163 void psnet_flush();
164 
165 // if the passed string is a valid IP string
166 int psnet_is_valid_ip_string( char *ip_string, int allow_port=1 );
167 
168 // mark a socket as having received data
170 
171 
172 // -------------------------------------------------------------------------------------------------------
173 // PSNET 2 RELIABLE SOCKET FUNCTIONS
174 //
175 
176 // shutdown a reliable socket
178 
179 // obsolete function - left in for compatibility sake
180 int psnet_rel_check();
181 
182 // send data on the reliable socket
183 int psnet_rel_send(PSNET_SOCKET_RELIABLE socket, ubyte *data, int length, int np_index = -1);
184 
185 // Return codes:
186 // -1 socket not connected
187 // 0 No packet ready to receive
188 // >0 Buffer filled with the number of bytes recieved
189 int psnet_rel_get(PSNET_SOCKET_RELIABLE socket, ubyte *buffer, int max_length);
190 
191 // process all active reliable sockets
192 void psnet_rel_work();
193 
194 // get the status of a reliable socket, see RNF_* defines above
196 
197 // check the listen socket for pending reliable connections
199 
200 // perform a reliable socket connect to the specified server
202 
203 #endif
int psnet_same(net_addr *a1, net_addr *a2)
Definition: psnet2.cpp:785
int psnet_send(net_addr *who_to, void *data, int len, int np_index=-1)
Definition: psnet2.cpp:793
ubyte net_id[4]
Definition: psnet2.h:40
int psnet_get(void *data, net_addr *from_addr)
Definition: psnet2.cpp:881
uint PSNET_SOCKET_RELIABLE
Definition: multi_xfer.h:20
void psnet_mark_received(PSNET_SOCKET_RELIABLE socket)
Definition: psnet2.cpp:2174
int psnet_rel_get_status(PSNET_SOCKET_RELIABLE sock)
Definition: psnet2.cpp:1542
int Tcp_failure_code
Definition: psnet2.cpp:63
SOCKET Unreliable_socket
Definition: psnet2.cpp:208
void psnet_flush()
Definition: psnet2.cpp:927
int psnet_rel_check_for_listen(net_addr *addr)
Definition: psnet2.cpp:1555
void psnet_string_to_addr(net_addr *address, char *text)
Definition: psnet2.cpp:732
void psnet_init(int protocol, int default_port)
Definition: psnet2.cpp:458
uint PSNET_SOCKET
Definition: psnet2.h:46
ushort Psnet_default_port
Definition: psnet2.cpp:67
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: Glext.h:7308
GLenum type
Definition: Gl.h:1492
void psnet_rel_connect_to_server(PSNET_SOCKET_RELIABLE *s, net_addr *server_addr)
Definition: psnet2.cpp:1593
int SELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const timeval *timeout, int psnet_type)
unsigned int uint
Definition: pstypes.h:64
int RECVFROM(uint s, char *buf, int len, int flags, sockaddr *from, int *fromlen, int psnet_type)
int Socket_type
Definition: psnet2.cpp:56
uint PSNET_SOCKET_RELIABLE
Definition: psnet2.h:47
int psnet_get_network_status()
Definition: psnet2.cpp:670
void PSNET_TOP_LAYER_PROCESS()
Definition: psnet2.cpp:360
GLuint buffer
Definition: Glext.h:5492
void psnet_rel_close_socket(PSNET_SOCKET_RELIABLE *sockp)
Definition: psnet2.cpp:1008
GLdouble s
Definition: Glext.h:5321
int psnet_rel_send(PSNET_SOCKET_RELIABLE socket, ubyte *data, int length, int np_index=-1)
Definition: psnet2.cpp:1075
#define SOCKET
Definition: config.h:122
int psnet_is_valid_ip_string(char *ip_string, int allow_port=1)
Definition: psnet2.cpp:938
unsigned char ubyte
Definition: pstypes.h:62
int SENDTO(uint s, char *buf, int len, int flags, sockaddr *to, int tolen, int psnet_type)
uint Psnet_my_ip
GLbitfield GLuint64 timeout
Definition: Glext.h:6725
int Psnet_my_addr_valid
Definition: psnet2.cpp:51
GLbitfield flags
Definition: Glext.h:6722
void psnet_close()
Definition: psnet2.cpp:563
int Tcp_active
Definition: psnet2.cpp:60
int Psnet_connection
Definition: psnet2.cpp:65
int psnet_rel_check()
Definition: psnet2.cpp:1067
char * psnet_addr_to_string(char *text, net_addr *address)
Definition: psnet2.cpp:705
uint type
Definition: psnet2.h:39
unsigned short ushort
Definition: pstypes.h:63
GLenum GLsizei GLenum GLenum const GLvoid * data
Definition: Gl.h:1509
GLenum GLuint GLenum GLsizei length
Definition: Glext.h:5156
int psnet_rel_get(PSNET_SOCKET_RELIABLE socket, ubyte *buffer, int max_length)
Definition: psnet2.cpp:1152
GLenum GLsizei len
Definition: Glext.h:6283
int Network_status
Definition: psnet2.cpp:62
int psnet_broadcast(net_addr *who_to, void *data, int len)
Definition: psnet2.cpp:897
void psnet_rel_work()
Definition: psnet2.cpp:1187
int psnet_use_protocol(int type)
Definition: psnet2.cpp:596
GLenum const GLvoid * addr
Definition: Glext.h:9092
short port
Definition: psnet2.h:42
struct net_addr net_addr
int socklen_t
Definition: tcp_socket.cpp:39
net_addr Psnet_my_addr
Definition: psnet2.cpp:52
GLuint address
Definition: Glext.h:8864