include/net.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *            net.h
00003  *
00004  *  Sun Sep 11 08:28:02 2005
00005  *  Copyright  2005  Joe Venzon
00006  *  joe@venzon.net
00007  ****************************************************************************/
00008 
00009 /*
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00023  */
00024  
00025 #ifndef _NET_H
00026 
00027 #include <stdio.h>                      // Header File For Standard Input/Output
00028 #include <GL/gl.h>
00029 #include <GL/glu.h>
00030 #include <SDL/SDL.h>
00031 #ifdef __APPLE__
00032 #include <SDL_image/SDL_image.h>
00033 #else
00034 #include <SDL/SDL_image.h>
00035 #endif
00036 #include <string>
00037 #include <iostream>
00038 #include <fstream>
00039 #include <cmath>
00040 #include <cassert>
00041 
00042 #include "replay.h"
00043 
00044 #include "globals.h"
00045 
00046 #ifdef __APPLE__
00047 #include <SDL_net/SDL_net.h>
00048 #else
00049 #include <SDL/SDL_net.h>
00050 #endif
00051 
00052 using namespace std;
00053 
00054 #define NET_DEBUG false
00055 
00056 //#define MAX_PACKET_SIZE 65535
00057 //#define PACKET_BUFFER_LEN 20
00058 
00059 #define MAX_PACKET_SIZE 8192
00060 #define PACKET_BUFFER_LEN 160
00061 
00062 #define UDP_SUCCESS 1
00063 #define UDP_FAILURE -1
00064 #define UDP_TIMEOUT 0
00065 
00066 #define HANDSHAKESTR "vdrift-handshake-v01"
00067 #define HANDSHAKEREPLYSTR "vdrift-handshake-v01-reply"
00068 #define HANDSHAKETIMEOUT 3000
00069 
00070 #define CONTROL_HANDSHAKE 0
00071 
00072 //packet buffer class
00073 class PBUFFER
00074 {
00075 private:
00076         bool valid;
00077         
00078 public:
00079         Uint8 * data;
00080         int len;
00081         bool Valid() {return valid;}
00082         void Put(UDPpacket * newp);
00083         void Clear() {valid = false;}
00084         PBUFFER() {valid = false; data = NULL;}
00085         ~PBUFFER() {if (data != NULL) delete [] data; data=NULL;}
00086 };
00087 
00088 class NET
00089 {
00090 private:
00091         IPaddress remote_ip;
00092         UDPsocket remote_socket;
00093         UDPpacket *pout, *pin;
00094 
00095         bool server;
00096         bool connected;
00097 
00098         PBUFFER buffer[PACKET_BUFFER_LEN];
00099 
00100         //size is in bytes
00101         void WriteToPacket(UDPpacket * p, Uint8 control, void * data, int size);
00102         bool IsEqualToPacket(UDPpacket * p, Uint8 control, void * data, int size);
00103         int UDPSend(UDPpacket * p, int channel);
00104         int UDPRecv(UDPpacket * p, int channel, int timeout);
00105         bool ClientHandshake();
00106         bool ServerHandshake();
00107 
00108         int BufferPacket(UDPpacket * newp);
00109 
00110         unsigned int datacounter_tx;
00111         unsigned int datacounter_rx;
00112 
00113 public:
00114         NET();
00115         ~NET();
00116         void Init();
00117         bool Connect(string host, int port);
00118         void Update();
00119         bool Server() {return server;}
00120         bool Connected();
00121         void Disconnect();
00122         void Send(void * data, int size);
00123         int RecvBlock(void * dest, int destsize, int timeout);
00124         int NumBufferedPackets();
00125         PBUFFER * GetBuffer(int idx) {return &(buffer[idx]);}
00126         int GetMaxBuffers() {return PACKET_BUFFER_LEN;}
00127         int GetTxBytes() {return datacounter_tx;}
00128         int GetRxBytes() {return datacounter_rx;}
00129         int ClearStats() {datacounter_tx = datacounter_rx = 0; return 1;}
00130 };
00131 
00132 #define _NET_H
00133 #endif /* _NET_H */

Generated on Thu Oct 19 04:05:47 2006 by  doxygen 1.4.6