00001 // OMICRON ENGINE HEADER FILE 00002 // 00003 // -------------------------------------------------------------------------- 00004 // Copyright (C) 2001-2002 by Bjoern Paetzel <kolrabi@gmx.de> 00005 // 00006 // This file is part of the Omicron Engine. 00007 // 00008 // The Omicron Engine is free software; you can redistribute it and/or modify 00009 // it under the terms of the GNU General Public License as published by the 00010 // Free Software Foundation; either version 2 of the License, or (at your 00011 // option) any later version. 00012 // 00013 // The Omicron Engine is distributed in the hope that it will be useful, but 00014 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 00015 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00016 // for more details. 00017 // 00018 // You should have received a copy of the GNU General Public License along 00019 // with The Omicron Engine; if not, write to the Free Software Foundation, 00020 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 // 00022 // -------------------------------------------------------------------------- 00023 // Last modified: $Date: 2002/12/07 19:02:17 $ 00024 // By : $Author: kolrabi $ 00025 // $Id: net.h,v 1.1.1.1 2002/12/07 19:02:17 kolrabi Exp $ 00026 00027 /* 00028 00029 $Log: net.h,v $ 00030 Revision 1.1.1.1 2002/12/07 19:02:17 kolrabi 00031 initial release 00032 00033 00034 */ 00035 00043 #include "forward.h" 00044 00045 #if defined(WIN32) 00046 00047 //# include <winsock2.h> 00048 00049 # define socklen_t int 00050 00051 #else 00052 00053 # include <netdb.h> 00054 # include <sys/types.h> 00055 # include <sys/socket.h> 00056 # include <netinet/in.h> 00057 # include <arpa/inet.h> 00058 00059 // winsock like definitions 00060 00061 # define SOCKET int 00062 # define INVALID_SOCKET -1 00063 # define SOCKET_ERROR -1 00064 # define HOSTENT struct hostent 00065 # define SOCKADDR_IN struct sockaddr_in 00066 00067 typedef struct sockaddr SOCKADDR; 00068 typedef SOCKADDR* LPSOCKADDR; 00069 00070 # define closesocket(a) close(a) 00071 # define TIMEVAL struct timeval 00072 00073 #endif 00074 00075 /**************************************************************************** 00076 **************************************************************************** 00077 * DEFINES ****************************************************************** 00078 **************************************************************************** 00079 ****************************************************************************/ 00080 00081 #define NETMSG_PING 0x0000 00082 #define NETMSG_PONG 0x0001 00083 #define NETMSG_USERDEFINED 0x1000 00084 00085 #define NETSTAT_CONNECTING 1 00086 #define NETSTAT_CONNECTED 2 00087 #define NETSTAT_DISCONNECTED 3 00088 00090 typedef struct net_message_header_s 00091 { 00092 ushort type; 00093 ulong len; 00094 ulong time; 00095 // char[len - sizeof(net_message_header_t)] bytes of data following 00096 } net_message_header_t; 00097 00099 typedef struct net_message_ping_s 00100 { 00101 net_message_header_t header; 00102 } net_message_ping_t; 00103 00105 typedef struct net_message_pong_s 00106 { 00107 net_message_header_t header; 00108 } net_message_pong_t; 00109 00111 class net_client_c 00112 { 00113 protected: 00114 00116 friend class net_manager_c; 00117 00118 ulong id; 00119 00120 SOCKET sock; 00121 SOCKADDR_IN laddress; 00122 00123 list_c<char> *queue; 00124 ulong queuelen; 00125 00126 ulong status; 00127 ulong latency; 00128 00129 public: 00130 00131 net_client_c(); 00132 virtual ~net_client_c(); 00133 00134 void 00135 send_message(net_message_header_t *msg, bool reliable = true); 00136 00137 void 00138 receive_message(); 00139 00140 ulong 00141 get_latency() { AssertThisV; return latency; }; 00142 }; 00143 00145 class net_manager_c 00146 { 00147 protected: 00148 00150 friend class net_client_c; 00151 00153 // server part 00154 00155 array_c <net_client_c> *clients; 00156 00157 SOCKET sv_socket; 00158 00159 ulong sv_lastupdate; 00160 ulong sv_gametime; 00161 00163 // client part 00164 00165 SOCKET cl_socket; 00166 00167 ulong frametime; 00168 00169 ulong cl_lastupdate; 00170 ulong cl_connecttime; 00171 00172 list_c<char> *cl_queue; 00173 ulong cl_queuelen; 00174 00175 #if defined(WIN32) 00176 00177 WSADATA wsadata; 00178 00179 #endif 00180 00181 bool isserver; 00182 bool isclient; 00183 00184 public: 00185 00186 net_manager_c(); 00187 ~net_manager_c(); 00188 00190 // server part 00191 00192 void 00193 set_frametime(ulong t) { AssertThis; frametime = t; }; 00194 00195 void 00196 start_listen(ushort port, bool local); 00197 00198 void 00199 stop_listen(); 00200 00201 float 00202 sv_get_framefrac() 00203 { 00204 AssertThisV; 00205 return ((float)(_time_get()-sv_lastupdate)/(float)frametime); 00206 } 00207 00208 void 00209 broadcast(net_message_header_t *msg); 00210 00211 void 00212 unicast(ulong clnum, net_message_header_t *msg); 00213 00215 // client part 00216 00217 float 00218 cl_get_framefrac() 00219 { 00220 AssertThisV; 00221 return ((float)(_time_get()-cl_lastupdate)/(float)frametime); 00222 } 00223 00224 bool 00225 connect_to_server(char *name, ushort port); 00226 00227 void 00228 disconnect_from_server(); 00229 00230 void 00231 cl_sendmessage(net_message_header_t *msg); 00232 00233 void 00234 cl_receivemessage(net_message_header_t *msg); 00235 00237 // common part 00238 00239 void 00240 update(); 00241 };
1.2.18