UDP Support
This commit is contained in:
+329
-382
@@ -1,458 +1,405 @@
|
|||||||
/*******************************************
|
/*******************************************************************\
|
||||||
ezsockets.cpp -- Header for sockets.cpp
|
| ezsockets.cpp: EzSockets Class Source |
|
||||||
Designed by Josh Allen and Charles
|
| Designed by Josh Allen, Charles Lohr and Adam Lowman. |
|
||||||
Lohr. Socket programming methods based
|
| Socket programming methods based on Charles Lohr's EZW progam. |
|
||||||
on Charles Lohr's EZW progam.
|
| Modified by Charles Lohr for use with Windows-Based OSes. |
|
||||||
|
| UDP/NON-TCP Support by Adam Lowman. |
|
||||||
Modified by Charles Lohr for use with
|
\*******************************************************************/
|
||||||
Windows-Based OSes.
|
|
||||||
********************************************/
|
|
||||||
|
|
||||||
// StepMania only includes
|
|
||||||
#include "global.h"
|
|
||||||
|
|
||||||
#include "ezsockets.h"
|
#include "ezsockets.h"
|
||||||
|
|
||||||
// We need the WinSock32 Library on Windows
|
#include "global.h" // StepMania only includes
|
||||||
|
|
||||||
#if defined(_XBOX)
|
#if defined(_XBOX)
|
||||||
#elif defined(_WINDOWS)
|
#elif defined(_WINDOWS) // We need the WinSock32 Library on Windows
|
||||||
#pragma comment(lib,"wsock32.lib")
|
#pragma comment(lib,"wsock32.lib")
|
||||||
#else
|
#else
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
EzSockets::EzSockets()
|
EzSockets::EzSockets() {
|
||||||
{
|
MAXCON = 5;
|
||||||
MAXCON = 5;
|
memset (&addr,0,sizeof(addr)); //Clear the sockaddr_in structure
|
||||||
memset ( &addr, 0, sizeof(addr) ); //Clear the sockaddr_in structure
|
|
||||||
|
|
||||||
// Windows REQUIRES WinSock Startup
|
#if defined(_WINDOWS) || defined(_XBOX) // Windows REQUIRES WinSock Startup
|
||||||
#if defined(_WINDOWS) || defined(_XBOX)
|
WSAStartup( MAKEWORD(1,1), &wsda );
|
||||||
WSAStartup( MAKEWORD(1,1), &wsda );
|
#endif
|
||||||
|
|
||||||
|
sock = -1;
|
||||||
|
blocking = true;
|
||||||
|
scks = new fd_set;
|
||||||
|
times = new timeval;
|
||||||
|
times->tv_sec = 0;
|
||||||
|
times->tv_usec = 0;
|
||||||
|
state = skDISCONNECTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
EzSockets::~EzSockets() {
|
||||||
|
close();
|
||||||
|
delete scks;
|
||||||
|
delete times;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Check to see if the socket has been created
|
||||||
|
bool EzSockets::check() {
|
||||||
|
return(sock>0);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EzSockets::create() {
|
||||||
|
return create(IPPROTO_TCP, SOCK_STREAM);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EzSockets::create(int Protocol) {
|
||||||
|
switch(Protocol) {
|
||||||
|
case IPPROTO_TCP:
|
||||||
|
return create(IPPROTO_TCP, SOCK_STREAM);
|
||||||
|
case IPPROTO_UDP:
|
||||||
|
return create(IPPROTO_UDP, SOCK_DGRAM);
|
||||||
|
default:
|
||||||
|
return create(Protocol, SOCK_RAW);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EzSockets::create(int Protocol, int Type) {
|
||||||
|
state = skDISCONNECTED;
|
||||||
|
sock = socket(AF_INET, Type, Protocol);
|
||||||
|
return(sock>0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool EzSockets::bind(unsigned short port) {
|
||||||
|
if(!check())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
|
addr.sin_port = htons(port);
|
||||||
|
|
||||||
|
return(::bind(sock,(struct sockaddr*)&addr, sizeof(addr))>0);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EzSockets::listen() {
|
||||||
|
if(::listen(sock,MAXCON)<=0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
state = skLISTENING;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/************************************************************************\
|
||||||
|
| To whomever edited this: |
|
||||||
|
| Please do not assume LINUX is defined if you're compiling on Linux! |
|
||||||
|
| It will not compile properly. |
|
||||||
|
\************************************************************************/
|
||||||
|
#if defined(WIN32) || defined(DARWIN) // glibc already has socklen_t defined
|
||||||
|
typedef int socklen_t;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
sock = -1;
|
bool EzSockets::accept(EzSockets &socket) {
|
||||||
blocking=true;
|
int length = sizeof(socket);
|
||||||
|
|
||||||
|
socket.sock = ::accept(sock,(struct sockaddr*) &socket.addr,(socklen_t*) &length);
|
||||||
|
|
||||||
scks = new fd_set;
|
if(socket.sock<=0)
|
||||||
times = new timeval;
|
return false;
|
||||||
|
|
||||||
|
socket.state = skCONNECTED;
|
||||||
times->tv_sec = 0;
|
return true;
|
||||||
times->tv_usec = 0;
|
|
||||||
|
|
||||||
state = skDISCONNECTED;
|
|
||||||
}
|
|
||||||
|
|
||||||
EzSockets::~EzSockets()
|
|
||||||
{
|
|
||||||
close();
|
|
||||||
delete scks;
|
|
||||||
delete times;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool EzSockets::check()
|
|
||||||
{
|
|
||||||
//Check to see if the socket has been created
|
|
||||||
if ( sock < 0 )
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
int EzSockets::create()
|
|
||||||
{
|
|
||||||
state = skDISCONNECTED;
|
|
||||||
sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
|
|
||||||
return (true);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool EzSockets::bind( unsigned short port )
|
|
||||||
{
|
|
||||||
if ( !check() )
|
|
||||||
return false;
|
|
||||||
|
|
||||||
addr.sin_family = AF_INET;
|
|
||||||
addr.sin_addr.s_addr = htonl( INADDR_ANY );
|
|
||||||
addr.sin_port = htons( port );
|
|
||||||
|
|
||||||
int desc = ::bind( sock, (struct sockaddr *)&addr, sizeof(addr) );
|
|
||||||
if ( desc < 0 )
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool EzSockets::listen()
|
|
||||||
{
|
|
||||||
int desc = ::listen( sock, MAXCON );
|
|
||||||
|
|
||||||
if ( desc < 0 )
|
|
||||||
return false;
|
|
||||||
|
|
||||||
state = skLISTENING;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// glibc already has socklen_t defined
|
|
||||||
// to whoever edited this:
|
|
||||||
// please do not assume LINUX is defined if you're compiling on linux!
|
|
||||||
// it will not compile properly.
|
|
||||||
#if defined(WIN32) || defined(DARWIN)
|
|
||||||
typedef int socklen_t;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
bool EzSockets::accept( EzSockets &socket )
|
|
||||||
{
|
|
||||||
// Windows wants it defined as a signed int
|
|
||||||
// As does everything else -- Steve
|
|
||||||
int length = sizeof( socket );
|
|
||||||
|
|
||||||
socket.sock = ::accept( sock, (struct sockaddr *)&socket.addr,(socklen_t *)&length );
|
|
||||||
socket.state = skCONNECTED;
|
|
||||||
if ( socket.sock < 0 )
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EzSockets::close() {
|
void EzSockets::close() {
|
||||||
state = skDISCONNECTED;
|
state = skDISCONNECTED;
|
||||||
inBuffer = "";
|
inBuffer = "";
|
||||||
outBuffer = "";
|
outBuffer = "";
|
||||||
// The close socket command is different in Windows
|
|
||||||
#if defined(WIN32)
|
#if defined(WIN32) // The close socket command is different in Windows
|
||||||
::closesocket( sock );
|
::closesocket(sock);
|
||||||
#else
|
#else
|
||||||
::close( sock );
|
::close(sock);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
long EzSockets::uAddr()
|
long EzSockets::uAddr() {
|
||||||
{
|
return addr.sin_addr.s_addr;
|
||||||
return addr.sin_addr.s_addr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool EzSockets::connect( const std::string& host, unsigned short port )
|
bool EzSockets::connect(const std::string& host,unsigned short port) {
|
||||||
{
|
if(!check())
|
||||||
if (! check() )
|
return false;
|
||||||
return false;
|
|
||||||
|
|
||||||
#if defined(_XBOX)
|
#if defined(_XBOX)
|
||||||
// FIXME: Xbox doesn't have gethostbyname or any way to get a hostent.
|
// FIXME: Xbox doesn't have gethostbyname or any way to get a hostent.
|
||||||
// Investigate the samples and figure out how this is supposed to work.
|
// Investigate the samples and figure out how this is supposed to work.
|
||||||
return false;
|
return false;
|
||||||
#elif defined(_WINDOWS)
|
#elif defined(_WINDOWS)
|
||||||
struct hostent* phe;
|
struct hostent* phe;
|
||||||
|
phe = gethostbyname(host.c_str());
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_addr.s_addr = (unsigned long) *((LPIN_ADDR*)phe->h_addr_list);
|
||||||
|
addr.sin_port = htons(port);
|
||||||
|
#else
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_port = htons(port);
|
||||||
|
inet_pton(AF_INET,host.c_str(),&addr.sin_addr);
|
||||||
|
#endif
|
||||||
|
|
||||||
addr.sin_family = AF_INET;
|
if(::connect(sock,(struct sockaddr*)&addr,sizeof(addr))<=0)
|
||||||
addr.sin_port = htons( port );
|
return false;
|
||||||
phe = gethostbyname( host.c_str() );
|
|
||||||
addr.sin_addr = *( (LPIN_ADDR)*phe->h_addr_list );
|
|
||||||
|
|
||||||
int desc = ::connect( sock, (struct sockaddr *)&addr, sizeof(addr) );
|
state = skCONNECTED;
|
||||||
if (!desc)
|
return true;
|
||||||
state = skCONNECTED;
|
}
|
||||||
return !desc;
|
|
||||||
#else
|
|
||||||
addr.sin_family = AF_INET;
|
|
||||||
addr.sin_port = htons( port );
|
|
||||||
inet_pton( AF_INET, host.c_str(), &addr.sin_addr );
|
|
||||||
|
|
||||||
int desc = ::connect( sock, (struct sockaddr *)&addr, sizeof(addr) );
|
bool EzSockets::CanRead() {
|
||||||
if (!desc)
|
FD_ZERO(scks);
|
||||||
state = skCONNECTED;
|
FD_SET((unsigned)sock,scks);
|
||||||
return !desc;
|
|
||||||
#endif
|
return(select(sock+1,scks,NULL,NULL,times)>0);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EzSockets::IsError() {
|
||||||
|
if(state == skERROR)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
FD_ZERO(scks);
|
||||||
|
FD_SET((unsigned)sock,scks);
|
||||||
|
|
||||||
|
return(select(sock+1,NULL,NULL,scks,times)<=0);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EzSockets::CanWrite() {
|
||||||
|
FD_ZERO(scks);
|
||||||
|
FD_SET((unsigned)sock,scks);
|
||||||
|
|
||||||
|
return(select(sock+1,NULL,scks,NULL,times)>0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EzSockets::update() {
|
||||||
|
if(state == skERROR) //If socket is in error, don't bother.
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(IsError()) {
|
||||||
|
state=skERROR;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(CanRead() && !IsError()) //Check for Reading
|
||||||
|
if(pUpdateRead()<1)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (CanWrite() && (outBuffer.length()>0))
|
||||||
|
pUpdateWrite();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*********************\
|
||||||
bool EzSockets::CanRead()
|
| Raw Data System |
|
||||||
{
|
\*********************/
|
||||||
FD_ZERO(scks);
|
void EzSockets::SendData(string &outData) {
|
||||||
FD_SET((unsigned)sock,scks);
|
outBuffer.append(outData);
|
||||||
|
if(blocking)
|
||||||
return select(sock+1, scks, NULL, NULL, times) > 0;
|
while ((outBuffer.length()>0) && !IsError())
|
||||||
|
pUpdateWrite();
|
||||||
|
else
|
||||||
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EzSockets::IsError()
|
void EzSockets::SendData(const char* data,unsigned int bytes) {
|
||||||
{
|
outBuffer.append(data,bytes);
|
||||||
if (state == skERROR)
|
if(blocking)
|
||||||
return true;
|
while ((outBuffer.length()>0) && !IsError())
|
||||||
|
pUpdateWrite();
|
||||||
FD_ZERO(scks);
|
else
|
||||||
FD_SET((unsigned)sock,scks);
|
update();
|
||||||
|
|
||||||
/* What is this trying to do? Errors are returned by read/write functions, not via
|
|
||||||
* the select() exception field. */
|
|
||||||
return !!select(sock+1, NULL, NULL, scks, times);
|
|
||||||
}
|
|
||||||
bool EzSockets::CanWrite()
|
|
||||||
{
|
|
||||||
FD_ZERO(scks);
|
|
||||||
FD_SET((unsigned)sock,scks);
|
|
||||||
|
|
||||||
return select(0, NULL, scks, NULL, times) > 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EzSockets::update()
|
int EzSockets::ReadData(char* data,unsigned int bytes) {
|
||||||
{
|
int bytesRead = PeekData(data,bytes);
|
||||||
if (state==skERROR)
|
inBuffer = inBuffer.substr(bytesRead);
|
||||||
return;
|
return bytesRead;
|
||||||
//If socket is in error, don't bother.
|
}
|
||||||
|
|
||||||
if (IsError())
|
int EzSockets::PeekData(char* data,unsigned int bytes) {
|
||||||
{
|
if(blocking)
|
||||||
state=skERROR;
|
while ((inBuffer.length()<bytes) && !IsError())
|
||||||
return;
|
pUpdateRead();
|
||||||
}
|
else
|
||||||
|
|
||||||
//Check for reading
|
|
||||||
while (CanRead() && !IsError())
|
while (CanRead() && !IsError())
|
||||||
if (pUpdateRead()<1)
|
if (pUpdateRead()<1)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (CanWrite() && (outBuffer.length()>0))
|
int bytesRead = bytes;
|
||||||
pUpdateWrite();
|
if(inBuffer.length()<bytes)
|
||||||
|
bytesRead = inBuffer.length();
|
||||||
|
memcpy(data,inBuffer.c_str(),bytesRead);
|
||||||
|
|
||||||
|
return bytesRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**********************************\
|
||||||
//Raw data system
|
| Packet/Structure Data System |
|
||||||
void EzSockets::SendData(string & outData)
|
\**********************************/
|
||||||
{
|
void EzSockets::SendPack(char* data,unsigned int bytes) {
|
||||||
outBuffer.append(outData);
|
unsigned int SendSize = htonl(bytes);
|
||||||
if (blocking)
|
SendData((char*)&SendSize,sizeof(int));
|
||||||
while ((outBuffer.length()>0) && !IsError())
|
SendData(data,bytes);
|
||||||
pUpdateWrite();
|
|
||||||
else
|
|
||||||
update();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EzSockets::SendData(const char *data, unsigned int bytes)
|
int EzSockets::ReadPack(char* data,unsigned int max) {
|
||||||
{
|
int size = PeekPack(data,max);
|
||||||
outBuffer.append(data,bytes);
|
if(size!=-1)
|
||||||
if (blocking)
|
inBuffer=inBuffer.substr(size+4);
|
||||||
while ((outBuffer.length()>0) && !IsError())
|
|
||||||
pUpdateWrite();
|
return size;
|
||||||
else
|
|
||||||
update();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int EzSockets::ReadData(char *data, unsigned int bytes)
|
int EzSockets::PeekPack(char* data,unsigned int max) {
|
||||||
{
|
if(CanRead())
|
||||||
int bytesRead = PeekData(data,bytes);
|
pUpdateRead();
|
||||||
|
|
||||||
inBuffer = inBuffer.substr(bytesRead);
|
if(blocking) {
|
||||||
|
while((inBuffer.length()<4)&& !IsError())
|
||||||
|
pUpdateRead();
|
||||||
|
|
||||||
return bytesRead;
|
if(IsError())
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int size=0;
|
||||||
|
PeekData((char*)&size,4);
|
||||||
|
size=ntohl(size);
|
||||||
|
|
||||||
|
if(blocking)
|
||||||
|
while ((inBuffer.length()<(size+4)) && !IsError())
|
||||||
|
pUpdateRead();
|
||||||
|
else if(inBuffer.length()>3)
|
||||||
|
if((inBuffer.length()<(size+4)) || (inBuffer.length()<=4))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
string tBuff(inBuffer.substr(4,size));
|
||||||
|
if(tBuff.length()>max)
|
||||||
|
tBuff.substr(0,max);
|
||||||
|
memcpy (data,tBuff.c_str(),tBuff.length());
|
||||||
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
int EzSockets::PeekData(char *data, unsigned int bytes)
|
|
||||||
{
|
|
||||||
if (blocking)
|
|
||||||
while ((inBuffer.length()<bytes) && !IsError())
|
|
||||||
pUpdateRead();
|
|
||||||
else
|
|
||||||
while (CanRead()&&!IsError())
|
|
||||||
if (pUpdateRead()<1)
|
|
||||||
break;
|
|
||||||
|
|
||||||
int bytesRead = bytes;
|
/*****************************************\
|
||||||
if (inBuffer.length()<bytes)
|
| Null Terminating String Data System |
|
||||||
bytesRead = inBuffer.length();
|
\*****************************************/
|
||||||
|
void EzSockets::SendStr(string& data, char delim) {
|
||||||
memcpy(data,inBuffer.c_str(),bytesRead);
|
char tDr[1];
|
||||||
|
tDr[0] = delim;
|
||||||
return bytesRead;
|
SendData(data.c_str(),data.length());
|
||||||
|
SendData(tDr,1);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Packet system (for structures and classes)
|
int EzSockets::ReadStr(string& data, char delim) {
|
||||||
void EzSockets::SendPack(char * data, unsigned int bytes)
|
int t = PeekStr(data,delim);
|
||||||
{
|
if(t>=0)
|
||||||
unsigned int SendSize = htonl(bytes);
|
inBuffer = inBuffer.substr(t+1);
|
||||||
SendData ((char *)&SendSize,sizeof(int));
|
return t;
|
||||||
SendData (data,bytes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int EzSockets::ReadPack(char * data, unsigned int max)
|
int EzSockets::PeekStr(string& data, char delim) {
|
||||||
{
|
int t = inBuffer.find(delim,0);
|
||||||
int size = PeekPack(data,max);
|
if(blocking) {
|
||||||
if (size!=-1)
|
while ((t==-1) && !IsError()) {
|
||||||
inBuffer = inBuffer.substr(size+4);
|
pUpdateRead();
|
||||||
return size;
|
t = inBuffer.find(delim,0);
|
||||||
}
|
|
||||||
|
|
||||||
int EzSockets::PeekPack(char * data, unsigned int max)
|
|
||||||
{
|
|
||||||
if (CanRead())
|
|
||||||
pUpdateRead();
|
|
||||||
|
|
||||||
if (blocking)
|
|
||||||
{
|
|
||||||
while ((inBuffer.length()<4)&& !IsError())
|
|
||||||
pUpdateRead();
|
|
||||||
|
|
||||||
if (IsError())
|
|
||||||
return (-1);
|
|
||||||
|
|
||||||
// LOG->Info("TTY:%d",inBuffer.length()
|
|
||||||
unsigned int size=0;
|
|
||||||
PeekData((char*)&size,4);
|
|
||||||
size = ntohl(size);
|
|
||||||
// LOG->Info("TXA:%d",size);
|
|
||||||
while ((inBuffer.length()<(size+4)) && !IsError())
|
|
||||||
pUpdateRead();
|
|
||||||
|
|
||||||
string tBuff(inBuffer.substr(4,size));
|
|
||||||
|
|
||||||
if (tBuff.length()>max)
|
|
||||||
tBuff.substr(0,max);
|
|
||||||
|
|
||||||
memcpy (data,tBuff.c_str(),tBuff.length());
|
|
||||||
|
|
||||||
return (size);
|
|
||||||
}
|
}
|
||||||
else if (inBuffer.length()>3)
|
data = inBuffer.substr(0,t);
|
||||||
{
|
}
|
||||||
unsigned int size=0;
|
|
||||||
PeekData((char*)&size,4);
|
|
||||||
size = ntohl(size);
|
|
||||||
if ((inBuffer.length()<(size+4)) || (inBuffer.length()<=4))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
string tBuff(inBuffer.substr(4,size));
|
if(t>=0)
|
||||||
|
data = inBuffer.substr(0,t);
|
||||||
if (tBuff.length()>max)
|
return t;
|
||||||
tBuff.substr(0,max);
|
|
||||||
|
|
||||||
memcpy (data,tBuff.c_str(),tBuff.length());
|
|
||||||
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//String (Flash) system / Null-terminated strings
|
/************************\
|
||||||
void EzSockets::SendStr(string & data, char delim)
|
| Stream Data System |
|
||||||
{
|
\************************/
|
||||||
char tDr[1];
|
istream& operator >>(istream &is,EzSockets &obj) {
|
||||||
tDr[0] = delim;
|
string writeString;
|
||||||
SendData(data.c_str(),data.length());
|
obj.SendStr(writeString);
|
||||||
SendData(tDr,1);
|
is >> writeString;
|
||||||
|
return is;
|
||||||
}
|
}
|
||||||
|
|
||||||
int EzSockets::ReadStr(string & data, char delim)
|
ostream& operator <<(ostream &os, EzSockets &obj) {
|
||||||
{
|
string readString;
|
||||||
int t = PeekStr (data, delim);
|
obj.ReadStr(readString);
|
||||||
if (t!=-1)
|
os << readString;
|
||||||
inBuffer = inBuffer.substr(t+1);
|
return os;
|
||||||
return t;
|
|
||||||
}
|
|
||||||
int EzSockets::PeekStr(string & data, char delim)
|
|
||||||
{
|
|
||||||
int t;
|
|
||||||
t = inBuffer.find(delim,0);
|
|
||||||
if (blocking)
|
|
||||||
{
|
|
||||||
while ((t==-1) && !IsError())
|
|
||||||
{
|
|
||||||
pUpdateRead();
|
|
||||||
t = inBuffer.find(delim,0);
|
|
||||||
}
|
|
||||||
data = inBuffer.substr(0,t);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (t == -1)
|
|
||||||
return -1;
|
|
||||||
data = inBuffer.substr(0,t);
|
|
||||||
}
|
|
||||||
return t;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**************************\
|
||||||
|
| Internal Data System |
|
||||||
|
\**************************/
|
||||||
|
int EzSockets::pUpdateRead() {
|
||||||
|
char tempData[1024];
|
||||||
|
int bytes = pReadData(tempData);
|
||||||
|
|
||||||
|
if(bytes>0)
|
||||||
int EzSockets::pUpdateRead()
|
inBuffer.append(tempData,bytes);
|
||||||
{
|
else
|
||||||
char tempData[1024];
|
state = skERROR;
|
||||||
int bytes = pReadData(tempData);
|
return bytes;
|
||||||
if (bytes>0)
|
|
||||||
inBuffer.append(tempData,bytes);
|
|
||||||
|
|
||||||
//You cannot read - bytes!
|
|
||||||
//0 bytes may happen under non-blocking circuimstances
|
|
||||||
if (bytes<0)
|
|
||||||
state = skERROR;
|
|
||||||
|
|
||||||
return bytes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int EzSockets::pUpdateWrite()
|
int EzSockets::pUpdateWrite() {
|
||||||
{
|
int bytes = pWriteData(outBuffer.c_str(),outBuffer.length());
|
||||||
int bytes = pWriteData(outBuffer.c_str(),outBuffer.length());
|
|
||||||
if (bytes>0)
|
if (bytes>0)
|
||||||
outBuffer = outBuffer.substr(bytes);
|
outBuffer = outBuffer.substr(bytes);
|
||||||
if (bytes<0)
|
else
|
||||||
state = skERROR;
|
state = skERROR;
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int EzSockets::pReadData(char * data)
|
int EzSockets::pReadData(char* data) {
|
||||||
{
|
if((state == skCONNECTED)||(state == skLISTENING))
|
||||||
return recv( sock, data,1024, 0 );
|
return recv(sock,data,1024,0);
|
||||||
|
else {
|
||||||
|
fromAddr_len = sizeof(SOCKADDR_IN);
|
||||||
|
return recvfrom(sock,data,1024,0,(SOCKADDR*)&fromAddr,(int*)&fromAddr_len);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int EzSockets::pWriteData(const char * data, int dataSize)
|
int EzSockets::pWriteData(const char* data,int dataSize) {
|
||||||
{
|
return send(sock,data,dataSize,0);
|
||||||
return send( sock, data, dataSize, 0 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
istream& operator >>(istream &is,EzSockets &obj)
|
//*****************************************************************************\\
|
||||||
{
|
// (c) 2003-2004 Josh Allen, Charles Lohr, and Adam Lowman. \\
|
||||||
string writeString;
|
// All rights reserved. \\
|
||||||
obj.SendStr(writeString);
|
// \\
|
||||||
is>>writeString;
|
// Permission is hereby granted, free of charge, to any person obtaining a \\
|
||||||
return is;
|
// copy of this software and associated documentation files (the \\
|
||||||
}
|
// "Software"), to deal in the Software without restriction, including \\
|
||||||
ostream& operator <<(ostream &os, EzSockets &obj)
|
// without limitation the rights to use, copy, modify, merge, publish, \\
|
||||||
{
|
// distribute, and/or sell copies of the Software, and to permit persons to \\
|
||||||
string readString;
|
// whom the Software is furnished to do so, provided that the above \\
|
||||||
obj.ReadStr(readString);
|
// copyright notice(s) and this permission notice appear in all copies of \\
|
||||||
os<<readString;
|
// the Software and that both the above copyright notice(s) and this \\
|
||||||
return os;
|
// permission notice appear in supporting documentation. \\
|
||||||
}
|
// \\
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS \\
|
||||||
/*
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF \\
|
||||||
* (c) 2003-2004 Charles Lohr, Josh Allen
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF \\
|
||||||
* All rights reserved.
|
// THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS \\
|
||||||
*
|
// INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT \\
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
// OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS \\
|
||||||
* copy of this software and associated documentation files (the
|
// OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR \\
|
||||||
* "Software"), to deal in the Software without restriction, including
|
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR \\
|
||||||
* without limitation the rights to use, copy, modify, merge, publish,
|
// PERFORMANCE OF THIS SOFTWARE. \\
|
||||||
* distribute, and/or sell copies of the Software, and to permit persons to
|
//*****************************************************************************\\
|
||||||
* whom the Software is furnished to do so, provided that the above
|
|
||||||
* copyright notice(s) and this permission notice appear in all copies of
|
|
||||||
* the Software and that both the above copyright notice(s) and this
|
|
||||||
* permission notice appear in supporting documentation.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
||||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
|
|
||||||
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
|
|
||||||
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
|
|
||||||
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
||||||
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
||||||
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
||||||
* PERFORMANCE OF THIS SOFTWARE.
|
|
||||||
*/
|
|
||||||
+46
-39
@@ -1,4 +1,10 @@
|
|||||||
/* EzSockets - BSD sockets class */
|
/*******************************************************************\
|
||||||
|
| ezsockets.h: EzSockets Class Header |
|
||||||
|
| Designed by Josh Allen, Charles Lohr and Adam Lowman. |
|
||||||
|
| Socket programming methods based on Charles Lohr's EZW progam. |
|
||||||
|
| Modified by Charles Lohr for use with Windows-Based OSes. |
|
||||||
|
| UDP/NON-TCP Support by Adam Lowman. |
|
||||||
|
\*******************************************************************/
|
||||||
|
|
||||||
#ifndef EZSOCKETS_H
|
#ifndef EZSOCKETS_H
|
||||||
#define EZSOCKETS_H
|
#define EZSOCKETS_H
|
||||||
@@ -26,7 +32,9 @@ public:
|
|||||||
~EzSockets();
|
~EzSockets();
|
||||||
|
|
||||||
//Crate the socket
|
//Crate the socket
|
||||||
int create();
|
bool create();
|
||||||
|
bool create(int Protocol);
|
||||||
|
bool create(int Protocol, int Type);
|
||||||
|
|
||||||
//Bind Socket to local port
|
//Bind Socket to local port
|
||||||
bool bind(unsigned short port);
|
bool bind(unsigned short port);
|
||||||
@@ -78,19 +86,6 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
bool blocking;
|
bool blocking;
|
||||||
|
|
||||||
|
|
||||||
//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 {
|
enum SockState {
|
||||||
skDISCONNECTED = 0,
|
skDISCONNECTED = 0,
|
||||||
skUNDEF1, //Not implemented
|
skUNDEF1, //Not implemented
|
||||||
@@ -102,6 +97,18 @@ public:
|
|||||||
skCONNECTED,
|
skCONNECTED,
|
||||||
skERROR
|
skERROR
|
||||||
};
|
};
|
||||||
|
SOCKADDR_IN fromAddr;
|
||||||
|
unsigned long fromAddr_len;
|
||||||
|
|
||||||
|
//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);
|
||||||
|
|
||||||
SockState state;
|
SockState state;
|
||||||
|
|
||||||
@@ -130,27 +137,27 @@ private:
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
//*****************************************************************************\\
|
||||||
* (c) 2003-2004 Charles Lohr, Josh Allen
|
// (c) 2003-2004 Josh Allen, Charles Lohr, and Adam Lowman. \\
|
||||||
* All rights reserved.
|
// All rights reserved. \\
|
||||||
*
|
// \\
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
// Permission is hereby granted, free of charge, to any person obtaining a \\
|
||||||
* copy of this software and associated documentation files (the
|
// copy of this software and associated documentation files (the \\
|
||||||
* "Software"), to deal in the Software without restriction, including
|
// "Software"), to deal in the Software without restriction, including \\
|
||||||
* without limitation the rights to use, copy, modify, merge, publish,
|
// without limitation the rights to use, copy, modify, merge, publish, \\
|
||||||
* distribute, and/or sell copies of the Software, and to permit persons to
|
// distribute, and/or sell copies of the Software, and to permit persons to \\
|
||||||
* whom the Software is furnished to do so, provided that the above
|
// whom the Software is furnished to do so, provided that the above \\
|
||||||
* copyright notice(s) and this permission notice appear in all copies of
|
// copyright notice(s) and this permission notice appear in all copies of \\
|
||||||
* the Software and that both the above copyright notice(s) and this
|
// the Software and that both the above copyright notice(s) and this \\
|
||||||
* permission notice appear in supporting documentation.
|
// permission notice appear in supporting documentation. \\
|
||||||
*
|
// \\
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS \\
|
||||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF \\
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF \\
|
||||||
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
|
// THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS \\
|
||||||
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
|
// INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT \\
|
||||||
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
// OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS \\
|
||||||
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
// OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR \\
|
||||||
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR \\
|
||||||
* PERFORMANCE OF THIS SOFTWARE.
|
// PERFORMANCE OF THIS SOFTWARE. \\
|
||||||
*/
|
//*****************************************************************************\\
|
||||||
|
|||||||
Reference in New Issue
Block a user