Open source remastering of the Freespace 2 engine
psnet2.cpp File Reference
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <limits.h>
#include <algorithm>
#include "globalincs/pstypes.h"
#include "network/psnet2.h"
#include "network/multi.h"
#include "network/multiutil.h"
#include "network/multilag.h"
#include "osapi/osregistry.h"
#include "io/timer.h"
#include "network/multi_log.h"
#include "network/multi_rate.h"
#include "cmdline/cmdline.h"

Go to the source code of this file.


struct  network_packet
struct  network_packet_buffer
struct  network_packet_buffer_list
struct  reliable_header
struct  reliable_net_sendbuffer
struct  reliable_net_rcvbuffer
struct  reliable_socket


#define WSAGetLastError()   (errno)
#define MAXHOSTNAME   128
#define MAX_RECEIVE_BUFSIZE   4096
#define MAX_SEND_RETRIES   20
#define MAX_LINGER_TIME   0
#define MAXNETBUFFERS   150
#define NETRETRYTIME   0.75f
#define MIN_NET_RETRYTIME   0.2f
#define NETTIMEOUT   30
#define NETBUFFERSIZE   600
#define RNT_ACK   1
#define RNT_DATA   2
#define RNT_DATA_COMP   3
#define RNT_REQ_CONN   4
#define RNT_DISCONNECT   5
#define RNT_HEARTBEAT   6
#define RNT_I_AM_HERE   7
#define RELIABLE_PACKET_HEADER_ONLY_SIZE   (sizeof(reliable_header)-NETBUFFERSIZE)
#define MAX_PING_HISTORY   10
#define CONNECTSEQ   0x142


typedef struct network_packet network_naked_packet
typedef struct network_packet_buffer network_packet_buffer
typedef struct network_packet_buffer_list network_packet_buffer_list


int psnet_is_valid_numeric_ip (char *ip)
void psnet_socket_options (SOCKET sock)
int psnet_init_tcp ()
float psnet_get_time ()
int psnet_get_ip ()
int psnet_init_rel_tcp (int port, int should_listen)
void psnet_rel_close ()
void psnet_buffer_init (network_packet_buffer_list *l)
void psnet_buffer_packet (network_packet_buffer_list *l, ubyte *data, int length, net_addr *from)
int psnet_buffer_get_next (network_packet_buffer_list *l, ubyte *data, int *length, net_addr *from)
int RECVFROM (SOCKET s, char *buf, int len, int flags, sockaddr *from, int *fromlen, int psnet_type)
int SELECT (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout, int psnet_type)
int SENDTO (SOCKET s, char *buf, int len, int flags, sockaddr *to, int tolen, int psnet_type)
void psnet_init (int protocol, int port_num)
void psnet_close ()
int psnet_use_protocol (int protocol)
int psnet_get_network_status ()
char * psnet_addr_to_string (char *text, net_addr *address)
void psnet_string_to_addr (net_addr *address, char *text)
int psnet_same (net_addr *a1, net_addr *a2)
int psnet_send (net_addr *who_to, void *data, int len, int np_index)
int psnet_get (void *data, net_addr *from_addr)
int psnet_broadcast (net_addr *who_to, void *data, int len)
void psnet_flush ()
int psnet_is_valid_ip_string (char *ip_string, int allow_port)
void psnet_rel_send_ack (SOCKADDR *raddr, unsigned int sig, ubyte link_type, float time_sent)
void psnet_rel_close_socket (PSNET_SOCKET_RELIABLE *sockp)
int psnet_rel_check ()
int psnet_rel_send (PSNET_SOCKET_RELIABLE socketid, ubyte *data, int length, int np_index)
int psnet_rel_get (PSNET_SOCKET socketid, ubyte *buffer, int max_len)
void psnet_rel_work ()
int psnet_rel_get_status (PSNET_SOCKET_RELIABLE socketid)
int psnet_rel_check_for_listen (net_addr *from_addr)
void psnet_rel_connect_to_server (PSNET_SOCKET *socket, net_addr *server_addr)
void psnet_mark_received (PSNET_SOCKET_RELIABLE socket)


int Psnet_my_addr_valid
net_addr Psnet_my_addr
ubyte Null_address [6]
int Socket_type
int Can_broadcast
int Tcp_can_broadcast = 0
int Tcp_active = 0
int Network_status
int Tcp_failure_code = 0
int Ras_connected
int Psnet_connection
ushort Psnet_default_port
int Nettimeout = NETTIMEOUT
reliable_socket Reliable_sockets [MAXRELIABLESOCKETS]
SOCKET Unreliable_socket = INVALID_SOCKET
float First_sent_iamhere = 0
float Last_sent_iamhere = 0
unsigned int Serverconn = 0xffffffff
network_packet_buffer_list Psnet_top_buffers [PSNET_NUM_TYPES]

Macro Definition Documentation

#define CONNECTSEQ   0x142

Definition at line 213 of file psnet2.cpp.

#define MAX_LINGER_TIME   0

Definition at line 132 of file psnet2.cpp.


Definition at line 90 of file psnet2.cpp.

#define MAX_PING_HISTORY   10

Definition at line 169 of file psnet2.cpp.

#define MAX_RECEIVE_BUFSIZE   4096

Definition at line 130 of file psnet2.cpp.

#define MAX_SEND_RETRIES   20

Definition at line 131 of file psnet2.cpp.


Definition at line 86 of file psnet2.cpp.

#define MAXHOSTNAME   128

Definition at line 128 of file psnet2.cpp.

#define MAXNETBUFFERS   150

Definition at line 136 of file psnet2.cpp.


Definition at line 142 of file psnet2.cpp.

#define MIN_NET_RETRYTIME   0.2f

Definition at line 139 of file psnet2.cpp.

#define NETBUFFERSIZE   600

Definition at line 143 of file psnet2.cpp.


Definition at line 141 of file psnet2.cpp.

#define NETRETRYTIME   0.75f

Definition at line 138 of file psnet2.cpp.

#define NETTIMEOUT   30

Definition at line 140 of file psnet2.cpp.


Definition at line 71 of file psnet2.cpp.


Definition at line 72 of file psnet2.cpp.


Definition at line 70 of file psnet2.cpp.


Definition at line 77 of file psnet2.cpp.


Definition at line 78 of file psnet2.cpp.


Definition at line 76 of file psnet2.cpp.


Definition at line 75 of file psnet2.cpp.


Definition at line 79 of file psnet2.cpp.


Definition at line 145 of file psnet2.cpp.

#define RELIABLE_PACKET_HEADER_ONLY_SIZE   (sizeof(reliable_header)-NETBUFFERSIZE)

Definition at line 168 of file psnet2.cpp.

#define RNT_ACK   1

Definition at line 150 of file psnet2.cpp.

#define RNT_DATA   2

Definition at line 151 of file psnet2.cpp.

#define RNT_DATA_COMP   3

Definition at line 152 of file psnet2.cpp.

#define RNT_DISCONNECT   5

Definition at line 154 of file psnet2.cpp.

#define RNT_HEARTBEAT   6

Definition at line 155 of file psnet2.cpp.

#define RNT_I_AM_HERE   7

Definition at line 156 of file psnet2.cpp.

#define RNT_REQ_CONN   4

Definition at line 153 of file psnet2.cpp.

#define WSAGetLastError ( )    (errno)

Definition at line 30 of file psnet2.cpp.

Typedef Documentation

Definition for a non-checksum packet

Structure definition for our packet buffers

Structure for a bunch of network packet buffers

Function Documentation

char* psnet_addr_to_string ( char *  text,
net_addr address 

Convert a net_addr to a string

Definition at line 705 of file psnet2.cpp.

int psnet_broadcast ( net_addr who_to,
void data,
int  len 

Broadcast data on unreliable socket

Definition at line 897 of file psnet2.cpp.

int psnet_buffer_get_next ( network_packet_buffer_list l,
ubyte data,
int length,
net_addr from 

Get the index of the next packet in order!

Definition at line 1861 of file psnet2.cpp.

void psnet_buffer_init ( network_packet_buffer_list l)

Initialize the buffering system

Definition at line 1804 of file psnet2.cpp.

void psnet_buffer_packet ( network_packet_buffer_list l,
ubyte data,
int  length,
net_addr from 

Buffer a packet (maintain order!)

Definition at line 1825 of file psnet2.cpp.

void psnet_close ( )

Shutdown psnet

Definition at line 563 of file psnet2.cpp.

void psnet_flush ( )

Flush all sockets

Definition at line 927 of file psnet2.cpp.

int psnet_get ( void data,
net_addr from_addr 

Get data from the unreliable socket

Definition at line 881 of file psnet2.cpp.

int psnet_get_ip ( )

Returns the IP address of this computer

Definition at line 1751 of file psnet2.cpp.

int psnet_get_network_status ( )

Get the status of the network

Definition at line 670 of file psnet2.cpp.

float psnet_get_time ( )

Get time in seconds

Definition at line 2166 of file psnet2.cpp.

void psnet_init ( int  protocol,
int  port_num 

Initialize psnet to use the specified port

Definition at line 458 of file psnet2.cpp.

int psnet_init_rel_tcp ( int  port,
int  should_listen 

Initialize reliable sockets

Definition at line 1777 of file psnet2.cpp.

int psnet_init_tcp ( )

Initialize tcp socket

Definition at line 2125 of file psnet2.cpp.

int psnet_is_valid_ip_string ( char *  ip_string,
int  allow_port 

If the passed string is a valid IP string

Definition at line 938 of file psnet2.cpp.

int psnet_is_valid_numeric_ip ( char *  ip)

If the string is a legally formatted IP string

Definition at line 1904 of file psnet2.cpp.

void psnet_mark_received ( PSNET_SOCKET_RELIABLE  socket)

Mark a socket as having received data

Definition at line 2174 of file psnet2.cpp.

int psnet_rel_check ( )

Definition at line 1067 of file psnet2.cpp.

int psnet_rel_check_for_listen ( net_addr from_addr)

Checks the Listen_socket for possibly incoming requests to be connected.

0 on error or nothing waiting. 1 if we should try to accept

Definition at line 1555 of file psnet2.cpp.

void psnet_rel_close ( )

Definition at line 1783 of file psnet2.cpp.

void psnet_rel_close_socket ( PSNET_SOCKET_RELIABLE sockp)

Function to shutdown and close the given socket.

It takes a couple of things into consideration when closing, such as possibly reiniting reliable sockets if they are closed here.

Definition at line 1008 of file psnet2.cpp.

void psnet_rel_connect_to_server ( PSNET_SOCKET socket,
net_addr server_addr 

Attempt to connect() to the server's tcp socket. socket parameter is simply assigned to the Reliable_socket socket created in psnet_init

Definition at line 1593 of file psnet2.cpp.

int psnet_rel_get ( PSNET_SOCKET  socketid,
ubyte buffer,
int  max_len 

Definition at line 1152 of file psnet2.cpp.

int psnet_rel_get_status ( PSNET_SOCKET_RELIABLE  socketid)

Get the status of a reliable socket, see RNF_* defines above

Definition at line 1542 of file psnet2.cpp.

int psnet_rel_send ( PSNET_SOCKET_RELIABLE  socketid,
ubyte data,
int  length,
int  np_index 

Send data reliably

Definition at line 1075 of file psnet2.cpp.

void psnet_rel_send_ack ( SOCKADDR raddr,
unsigned int  sig,
ubyte  link_type,
float  time_sent 

Definition at line 975 of file psnet2.cpp.

void psnet_rel_work ( )

Process all active reliable sockets

Definition at line 1187 of file psnet2.cpp.

int psnet_same ( net_addr a1,
net_addr a2 

Compare 2 addresses

Definition at line 785 of file psnet2.cpp.

int psnet_send ( net_addr who_to,
void data,
int  len,
int  np_index 

Send data unreliably

Definition at line 793 of file psnet2.cpp.

void psnet_socket_options ( SOCKET  sock)

Set some options on a socket

Definition at line 2088 of file psnet2.cpp.

void psnet_string_to_addr ( net_addr address,
char *  text 

Convert a string to a net addr

Definition at line 732 of file psnet2.cpp.


Call this once per frame to read everything off of our socket

Definition at line 360 of file psnet2.cpp.

int psnet_use_protocol ( int  protocol)

Set the protocol to use

Definition at line 596 of file psnet2.cpp.

char *  buf,
int  len,
int  flags,
sockaddr *  from,
int fromlen,
int  psnet_type 

Wrappers around select() and recvfrom() for lagging/losing data

Definition at line 271 of file psnet2.cpp.

int SELECT ( int  nfds,
fd_set *  readfds,
fd_set *  writefds,
fd_set *  exceptfds,
struct timeval *  timeout,
int  psnet_type 

Wrappers around select() and recvfrom() for lagging/losing data

Definition at line 317 of file psnet2.cpp.

char *  buf,
int  len,
int  flags,
sockaddr *  to,
int  tolen,
int  psnet_type 

Wrappers around sendto to sorting through different packet types

Definition at line 345 of file psnet2.cpp.

Variable Documentation

int Can_broadcast

Definition at line 57 of file psnet2.cpp.

float First_sent_iamhere = 0

Definition at line 210 of file psnet2.cpp.

float Last_sent_iamhere = 0

Definition at line 211 of file psnet2.cpp.

int Nettimeout = NETTIMEOUT

Definition at line 147 of file psnet2.cpp.

int Network_status

Definition at line 62 of file psnet2.cpp.

ubyte Null_address[6]

Definition at line 54 of file psnet2.cpp.

int Psnet_connection

Definition at line 65 of file psnet2.cpp.

ushort Psnet_default_port

Definition at line 67 of file psnet2.cpp.

net_addr Psnet_my_addr

Definition at line 52 of file psnet2.cpp.

int Psnet_my_addr_valid

Definition at line 51 of file psnet2.cpp.

Definition at line 221 of file psnet2.cpp.

int Ras_connected

Definition at line 64 of file psnet2.cpp.

Definition at line 202 of file psnet2.cpp.

unsigned int Serverconn = 0xffffffff

Definition at line 215 of file psnet2.cpp.

int Socket_type

Definition at line 56 of file psnet2.cpp.

int Tcp_active = 0

Definition at line 60 of file psnet2.cpp.

int Tcp_can_broadcast = 0

Definition at line 58 of file psnet2.cpp.

int Tcp_failure_code = 0

Definition at line 63 of file psnet2.cpp.


Definition at line 205 of file psnet2.cpp.

SOCKET Unreliable_socket = INVALID_SOCKET

Definition at line 208 of file psnet2.cpp.