2004-03-16 22:34:48 +00:00
|
|
|
/*******************************************
|
2004-05-24 13:59:05 +00:00
|
|
|
EzSockets - a multiplatform generic
|
|
|
|
|
sockets class (Version 2)
|
|
|
|
|
Original code by Josh Allen
|
|
|
|
|
Modified by Charles Lohr
|
2004-03-16 22:34:48 +00:00
|
|
|
|
2004-05-24 13:59:05 +00:00
|
|
|
All contents of this file Copyright
|
|
|
|
|
2003-2004 Charles Lohr
|
|
|
|
|
Joshua Allen
|
|
|
|
|
|
|
|
|
|
This file may be used under what
|
|
|
|
|
license the StepMania project is using.
|
|
|
|
|
|
2004-03-16 22:34:48 +00:00
|
|
|
********************************************/
|
2004-05-24 13:59:05 +00:00
|
|
|
|
2004-03-16 22:34:48 +00:00
|
|
|
#ifndef __EZSOCKETS_H
|
|
|
|
|
#define __EZSOCKETS_H
|
|
|
|
|
|
2004-05-24 13:59:05 +00:00
|
|
|
#include <sstream>
|
2004-03-16 22:34:48 +00:00
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
2004-05-24 13:59:05 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <ctype.h>
|
2004-03-16 22:34:48 +00:00
|
|
|
|
|
|
|
|
#if defined(WIN32)
|
|
|
|
|
#include <winsock2.h>
|
|
|
|
|
#else
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <netdb.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-04-05 15:43:33 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
2004-03-16 22:34:48 +00:00
|
|
|
class EzSockets {
|
2004-05-24 13:59:05 +00:00
|
|
|
public:
|
2004-03-16 22:34:48 +00:00
|
|
|
|
2004-05-24 13:59:05 +00:00
|
|
|
EzSockets();
|
|
|
|
|
~EzSockets();
|
|
|
|
|
|
|
|
|
|
//Crate the socket
|
|
|
|
|
int create();
|
|
|
|
|
|
|
|
|
|
//Bind Socket to local port
|
|
|
|
|
bool bind(unsigned short port);
|
|
|
|
|
|
|
|
|
|
//Listen with socket
|
|
|
|
|
bool listen();
|
|
|
|
|
|
|
|
|
|
//Accept incomming socket
|
|
|
|
|
bool accept(EzSockets &socket);
|
|
|
|
|
|
|
|
|
|
//Connect
|
|
|
|
|
bool connect(const std::string& host, unsigned short port);
|
|
|
|
|
|
|
|
|
|
//Kill socket
|
|
|
|
|
void close();
|
|
|
|
|
|
|
|
|
|
//see if socket has been created
|
|
|
|
|
bool check();
|
|
|
|
|
|
|
|
|
|
long uAddr();
|
|
|
|
|
|
|
|
|
|
bool CanRead();
|
|
|
|
|
bool IsError();
|
|
|
|
|
bool CanWrite();
|
|
|
|
|
|
|
|
|
|
void update();
|
|
|
|
|
|
|
|
|
|
//Raw data system
|
|
|
|
|
void SendData(string & outData);
|
|
|
|
|
void SendData(const char *data, unsigned int bytes);
|
|
|
|
|
int ReadData(char *data, unsigned int bytes);
|
|
|
|
|
int PeekData(char *data, unsigned int bytes);
|
|
|
|
|
|
|
|
|
|
//Packet system (for structures and classes)
|
|
|
|
|
void SendPack(char * data, unsigned int bytes);
|
|
|
|
|
int ReadPack(char * data, unsigned int max);
|
|
|
|
|
int PeekPack(char * data, unsigned int max);
|
|
|
|
|
|
|
|
|
|
//String (Flash) system / Null-terminated strings
|
|
|
|
|
void SendStr(string & data, char delim = '\0');
|
|
|
|
|
int ReadStr(string & data, char delim = '\0');
|
|
|
|
|
int PeekStr(string & data, char delim = '\0');
|
2004-03-16 22:34:48 +00:00
|
|
|
|
|
|
|
|
|
2004-05-24 13:59:05 +00:00
|
|
|
//Operators
|
|
|
|
|
char operator[] (int i); //Access buffer
|
|
|
|
|
friend istream& operator >>(istream &is,EzSockets &obj);
|
|
|
|
|
friend ostream& operator <<(ostream &os,const EzSockets &obj);
|
2004-03-16 22:34:48 +00:00
|
|
|
|
|
|
|
|
|
2004-05-24 13:59:05 +00:00
|
|
|
bool blocking;
|
2004-04-05 15:43:33 +00:00
|
|
|
|
|
|
|
|
|
2004-05-24 13:59:05 +00:00
|
|
|
//The following possibly should be private.
|
|
|
|
|
string inBuffer;
|
|
|
|
|
string outBuffer;
|
|
|
|
|
|
|
|
|
|
int pUpdateWrite();
|
|
|
|
|
int pUpdateRead();
|
|
|
|
|
|
|
|
|
|
int pReadData(char * data);
|
|
|
|
|
int pWriteData(const char * data, int dataSize);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enum SockState {
|
|
|
|
|
skDISCONNECTED = 0,
|
|
|
|
|
skUNDEF1, //Not implemented
|
|
|
|
|
skLISTENING,
|
|
|
|
|
skUNDEF3, //Not implemented
|
|
|
|
|
skUNDEF4, //Not implemented
|
|
|
|
|
skUNDEF5, //Not implemented
|
|
|
|
|
skUNDEF6, //Not implemented
|
|
|
|
|
skCONNECTED,
|
|
|
|
|
skERROR
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SockState state;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
//Only necessiary in windows
|
|
|
|
|
#if defined(WIN32)
|
|
|
|
|
WSADATA wsda;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
int MAXCON;
|
|
|
|
|
int sock;
|
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Used for Select() command
|
|
|
|
|
fd_set * scks;
|
|
|
|
|
timeval * times;
|
|
|
|
|
|
|
|
|
|
//Buffers
|
2004-03-16 22:34:48 +00:00
|
|
|
};
|
|
|
|
|
|
2004-05-24 13:59:05 +00:00
|
|
|
istream& operator >>(istream &is,EzSockets &obj);
|
|
|
|
|
ostream& operator <<(ostream &os,EzSockets &obj);
|
|
|
|
|
|
|
|
|
|
|
2004-03-16 22:34:48 +00:00
|
|
|
#endif
|