Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
tcp.h
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/****************************************************************************\
20TCP Neal Kettler neal@westwood.com
21
22\****************************************************************************/
23
24#ifndef TCP_HEADER
25#define TCP_HEADER
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <ctype.h>
30#include <errno.h>
31#include <string.h>
32#include <assert.h>
33
34#ifdef _WINDOWS
35
36#include <winsock.h>
37#include <io.h>
38#define close _close
39#define read _read
40#define write _write
41
42#else //UNIX
43#include <netdb.h>
44#include <sys/types.h>
45#include <sys/socket.h>
46#include <netinet/in.h>
47#include <arpa/inet.h>
48#include <unistd.h>
49#include <sys/time.h>
50#include <fcntl.h>
51#include <limits.h>
52
53typedef signed int SOCKET;
54
55#endif
56
57
58#ifdef AIX
59#include <sys/select.h>
60#endif
61
62#define DEFAULT_PROTOCOL 0
63
64#include "wlib/wstypes.h"
65#include "wlib/wdebug.h"
66#include "wlib/wtime.h"
67
68class TCP
69{
70
71// DATA ---------------
72
73private:
74 int mode; // client or server
75 sint32 fd; // the primary FD
76
77 uint32 myIP; // after bind myIP & myPort will be
78 uint16 myPort; // whatever we bound to
79
80 struct sockaddr_in addr;
81 int maxFD; // value of the biggest FD
82 int clientCount; // how many clients open
83
84
85 sint32 inputDelay; // default delay for semi-blocking reads
86 sint32 outputDelay; // default delay for semi-blocking writes
87
88 enum ConnectionState
89 {
90 CLOSED,
91 CONNECTING,
92 CONNECTED
93 } connectionState; // What state is client FD in
94
95public:
96
97 enum
98 {
99 CLIENT = 1,
101 };
102
103 // These defines specify a system independent way to
104 // get error codes for socket services.
105 enum
106 {
107 OK, // Everything's cool
108 UNKNOWN, // There was an error of unknown type
109 ISCONN, // The socket is already connected
110 INPROGRESS, // The socket is non-blocking and the operation
111 // isn't done yet
112 ALREADY, // The socket is already attempting a connection
113 // but isn't done yet
114 AGAIN, // Try again.
115 ADDRINUSE, // Address already in use
116 ADDRNOTAVAIL, // That address is not available on the remote host
117 BADF, // Not a valid FD
118 CONNREFUSED, // Connection was refused
119 INTR, // Operation was interrupted
120 NOTSOCK, // FD wasn't a socket
121 PIPE, // That operation just made a SIGPIPE
122 WOULDBLOCK, // That operation would block
123 INVAL, // Invalid
124 TIMEDOUT // Timeout
125 };
126
127 // for client list (if this is a server)
129
130
131// CODE ----------------
132
133public:
134 TCP(int newMode);
135 TCP(int newMode,sint16 socket);
136 ~TCP();
137 bit8 Bind(uint32 IP,uint16 port,bit8 reuseAddr=FALSE);
138 bit8 Bind(char *Host,uint16 port,bit8 reuseAddr=FALSE);
139
140 sint32 GetMaxFD(void);
141
142 bit8 Connect(uint32 IP,uint16 port);
143 bit8 Connect(char *Host,uint16 port);
145 bit8 ConnectAsync(char *Host,uint16 port);
146
147 bit8 IsConnected(sint32 whichFD=0);
148
149 sint32 GetFD(void);
150 sint32 GetClientCount(void) { return(clientCount); }
151
152 // Get IP or Port of a connected endpoint
153 uint32 GetRemoteIP(sint32 whichFD=0);
154 uint16 GetRemotePort(sint32 whichFD=0);
155
156 sint32 GetConnection(void);
157 sint32 GetConnection(struct sockaddr *clientAddr);
158 void WaitWrite(sint32 whichFD=0);
159 bit8 CanWrite(sint32 whichFD=0);
160 sint32 Write(const uint8 *msg,uint32 len,sint32 whichFD=0);
161 sint32 WriteNB(uint8 *msg,uint32 len,sint32 whichFD=0);
163 sint32 WriteString(char *msg,sint32 whichFD=0);
164 sint32 Printf(sint32 whichFD,const char *format,...);
165 sint32 Read(uint8 *msg,uint32 len,sint32 whichFD=0);
166 sint32 TimedRead(uint8 *msg,uint32 len,int seconds,sint32 whichFD=0);
167 sint32 Peek(uint8 *msg,uint32 len,sint32 whichFD=0);
169
170 char *Gets(char *string,int n,int whichFD=0);
171
172 // Wait on all sockets (or a specified one)
173 // return when ready for reading (or timeout occurs)
174 int Wait(sint32 sec,sint32 usec,fd_set &returnSet,sint32 whichFD=0);
175 int Wait(sint32 sec,sint32 usec,fd_set &inputSet,fd_set &returnSet);
176
177 int GetStatus(void);
178 void ClearStatus(void);
179
180 //sint32 GetSockStatus(sint32 whichFD=0);
181
182 // give up ownership of the socket without closing it
183 void DisownSocket(void);
184
185 sint32 Close(sint32 whichFD=0);
186 sint32 CloseAll(void); // close all sockets (same as close for client)
187
188 sint32 SetBlocking(bit8 block,sint32 whichFD=0);
189
190 // Set default delays for semi-blocking reads & writes
191 // default input = 5, output = 5
192 // this is new and not used everywhere
193 //
194 bit8 SetInputDelay(sint32 delay) { inputDelay=delay; return(TRUE); };
195 bit8 SetOutputDelay(sint32 delay) { outputDelay=delay; return(TRUE); };
196
197};
198
199#endif
#define TRUE
Definition BaseType.h:109
#define FALSE
Definition BaseType.h:113
char bit8
Definition wstypes.h:61
unsigned short uint16
Definition bittype.h:45
unsigned long uint32
Definition bittype.h:46
signed short sint16
Definition bittype.h:50
signed long sint32
Definition bittype.h:51
unsigned char uint8
Definition bittype.h:44
bit8 SetInputDelay(sint32 delay)
Definition tcp.h:194
sint32 TimedRead(uint8 *msg, uint32 len, int seconds, sint32 whichFD=0)
Definition tcp.cpp:548
bit8 ConnectAsync(uint32 IP, uint16 port)
Definition tcp.cpp:1074
int Wait(sint32 sec, sint32 usec, fd_set &returnSet, sint32 whichFD=0)
Definition tcp.cpp:736
TCP(int newMode)
Definition tcp.cpp:108
sint32 SetBlocking(bit8 block, sint32 whichFD=0)
Definition tcp.cpp:168
fd_set clientList
Definition tcp.h:128
sint32 GetMaxFD(void)
Definition tcp.cpp:199
void DisownSocket(void)
Definition tcp.cpp:679
sint32 Close(sint32 whichFD=0)
Definition tcp.cpp:690
bit8 CanWrite(sint32 whichFD=0)
Definition tcp.cpp:858
bit8 Connect(uint32 IP, uint16 port)
Definition tcp.cpp:987
sint32 GetConnection(void)
Definition tcp.cpp:1204
uint16 GetRemotePort(sint32 whichFD=0)
Definition tcp.cpp:399
sint32 Peek(uint8 *msg, uint32 len, sint32 whichFD=0)
Definition tcp.cpp:576
sint32 EncapsulatedRead(uint8 *msg, uint32 len, sint32 whichFD=0)
Definition tcp.cpp:605
sint32 GetClientCount(void)
Definition tcp.h:150
sint32 EncapsulatedWrite(uint8 *msg, uint32 len, sint32 whichFD=0)
Definition tcp.cpp:257
sint32 WriteNB(uint8 *msg, uint32 len, sint32 whichFD=0)
Definition tcp.cpp:233
uint32 GetRemoteIP(sint32 whichFD=0)
Definition tcp.cpp:378
sint32 WriteString(char *msg, sint32 whichFD=0)
Definition tcp.cpp:310
sint32 Write(const uint8 *msg, uint32 len, sint32 whichFD=0)
Definition tcp.cpp:210
int GetStatus(void)
Definition tcp.cpp:1167
sint32 CloseAll(void)
Definition tcp.cpp:660
bit8 IsConnected(sint32 whichFD=0)
Definition tcp.cpp:419
void ClearStatus(void)
Definition tcp.cpp:1160
sint32 Printf(sint32 whichFD, const char *format,...)
Definition tcp.cpp:341
sint32 Read(uint8 *msg, uint32 len, sint32 whichFD=0)
Definition tcp.cpp:512
~TCP()
Definition tcp.cpp:155
void WaitWrite(sint32 whichFD=0)
Definition tcp.cpp:826
bit8 SetOutputDelay(sint32 delay)
Definition tcp.h:195
@ SERVER
Definition tcp.h:100
@ CLIENT
Definition tcp.h:99
@ AGAIN
Definition tcp.h:114
@ OK
Definition tcp.h:107
@ INPROGRESS
Definition tcp.h:110
@ CONNREFUSED
Definition tcp.h:118
@ INTR
Definition tcp.h:119
@ ADDRNOTAVAIL
Definition tcp.h:116
@ INVAL
Definition tcp.h:123
@ NOTSOCK
Definition tcp.h:120
@ UNKNOWN
Definition tcp.h:108
@ BADF
Definition tcp.h:117
@ ISCONN
Definition tcp.h:109
@ ADDRINUSE
Definition tcp.h:115
@ PIPE
Definition tcp.h:121
@ TIMEDOUT
Definition tcp.h:124
@ WOULDBLOCK
Definition tcp.h:122
@ ALREADY
Definition tcp.h:112
bit8 Bind(uint32 IP, uint16 port, bit8 reuseAddr=FALSE)
Definition tcp.cpp:905
sint32 GetFD(void)
Definition tcp.cpp:161
char * Gets(char *string, int n, int whichFD=0)
Definition tcp.cpp:465
signed int SOCKET
Definition tcp.h:53
MSG msg
Definition patch.cpp:409