Cleanup, fix style, const fix.
This commit is contained in:
+88
-58
@@ -21,7 +21,8 @@
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
EzSockets::EzSockets() {
|
||||
EzSockets::EzSockets()
|
||||
{
|
||||
MAXCON = 5;
|
||||
memset (&addr,0,sizeof(addr)); //Clear the sockaddr_in structure
|
||||
|
||||
@@ -38,23 +39,28 @@ EzSockets::EzSockets() {
|
||||
state = skDISCONNECTED;
|
||||
}
|
||||
|
||||
EzSockets::~EzSockets() {
|
||||
EzSockets::~EzSockets()
|
||||
{
|
||||
close();
|
||||
delete scks;
|
||||
delete times;
|
||||
}
|
||||
|
||||
//Check to see if the socket has been created
|
||||
bool EzSockets::check() {
|
||||
return(sock>0);
|
||||
bool EzSockets::check()
|
||||
{
|
||||
return sock > 0;
|
||||
}
|
||||
|
||||
bool EzSockets::create() {
|
||||
bool EzSockets::create()
|
||||
{
|
||||
return create(IPPROTO_TCP, SOCK_STREAM);
|
||||
}
|
||||
|
||||
bool EzSockets::create(int Protocol) {
|
||||
switch(Protocol) {
|
||||
bool EzSockets::create(int Protocol)
|
||||
{
|
||||
switch(Protocol)
|
||||
{
|
||||
case IPPROTO_TCP:
|
||||
return create(IPPROTO_TCP, SOCK_STREAM);
|
||||
case IPPROTO_UDP:
|
||||
@@ -72,14 +78,16 @@ bool EzSockets::create(int Protocol) {
|
||||
}
|
||||
}
|
||||
|
||||
bool EzSockets::create(int Protocol, int Type) {
|
||||
bool EzSockets::create(int Protocol, int Type)
|
||||
{
|
||||
state = skDISCONNECTED;
|
||||
sock = socket(AF_INET, Type, Protocol);
|
||||
return(sock>0);
|
||||
return sock > 0;
|
||||
}
|
||||
|
||||
|
||||
bool EzSockets::bind(unsigned short port) {
|
||||
bool EzSockets::bind(unsigned short port)
|
||||
{
|
||||
if(!check())
|
||||
return false;
|
||||
|
||||
@@ -87,33 +95,31 @@ bool EzSockets::bind(unsigned short port) {
|
||||
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
addr.sin_port = htons(port);
|
||||
|
||||
return(::bind(sock,(struct sockaddr*)&addr, sizeof(addr))==0);
|
||||
return !::bind(sock,(struct sockaddr*)&addr, sizeof(addr));
|
||||
}
|
||||
|
||||
bool EzSockets::listen() {
|
||||
if(::listen(sock,MAXCON)!=0)
|
||||
bool EzSockets::listen()
|
||||
{
|
||||
if (::listen(sock, MAXCON))
|
||||
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
|
||||
|
||||
bool EzSockets::accept(EzSockets &socket) {
|
||||
bool EzSockets::accept(EzSockets& socket)
|
||||
{
|
||||
if (!blocking && !CanRead())
|
||||
return false;
|
||||
|
||||
int length = sizeof(socket);
|
||||
|
||||
socket.sock = ::accept(sock,(struct sockaddr*) &socket.addr,(socklen_t*) &length);
|
||||
socket.sock = ::accept(sock,(struct sockaddr*) &socket.addr,
|
||||
(socklen_t*) &length);
|
||||
|
||||
if(socket.sock<=0)
|
||||
return false;
|
||||
@@ -122,7 +128,8 @@ bool EzSockets::accept(EzSockets &socket) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void EzSockets::close() {
|
||||
void EzSockets::close()
|
||||
{
|
||||
state = skDISCONNECTED;
|
||||
inBuffer = "";
|
||||
outBuffer = "";
|
||||
@@ -134,12 +141,14 @@ void EzSockets::close() {
|
||||
#endif
|
||||
}
|
||||
|
||||
long EzSockets::uAddr() {
|
||||
long EzSockets::uAddr()
|
||||
{
|
||||
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())
|
||||
return false;
|
||||
|
||||
@@ -157,18 +166,19 @@ bool EzSockets::connect(const std::string& host,unsigned short port) {
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
|
||||
if(::connect(sock,(struct sockaddr*)&addr,sizeof(addr))!=0)
|
||||
if(::connect(sock, (struct sockaddr*)&addr, sizeof(addr)))
|
||||
return false;
|
||||
|
||||
state = skCONNECTED;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EzSockets::CanRead() {
|
||||
bool EzSockets::CanRead()
|
||||
{
|
||||
FD_ZERO(scks);
|
||||
FD_SET((unsigned)sock, scks);
|
||||
|
||||
return(select(sock+1,scks,NULL,NULL,times)>0);
|
||||
return select(sock+1,scks,NULL,NULL,times) > 0;
|
||||
}
|
||||
|
||||
bool EzSockets::IsError() {
|
||||
@@ -185,14 +195,16 @@ bool EzSockets::IsError() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EzSockets::CanWrite() {
|
||||
bool EzSockets::CanWrite()
|
||||
{
|
||||
FD_ZERO(scks);
|
||||
FD_SET((unsigned)sock, scks);
|
||||
|
||||
return(select(sock+1,NULL,scks,NULL,times)>0);
|
||||
return select(sock+1, NULL, scks, NULL, times) > 0;
|
||||
}
|
||||
|
||||
void EzSockets::update() {
|
||||
void EzSockets::update()
|
||||
{
|
||||
if (IsError()) //If socket is in error, don't bother.
|
||||
return;
|
||||
|
||||
@@ -208,7 +220,8 @@ void EzSockets::update() {
|
||||
/*********************\
|
||||
| Raw Data System |
|
||||
\*********************/
|
||||
void EzSockets::SendData(string &outData) {
|
||||
void EzSockets::SendData(const string& outData)
|
||||
{
|
||||
outBuffer.append(outData);
|
||||
if(blocking)
|
||||
while ((outBuffer.length()>0) && !IsError())
|
||||
@@ -217,7 +230,8 @@ void EzSockets::SendData(string &outData) {
|
||||
update();
|
||||
}
|
||||
|
||||
void EzSockets::SendData(const char* data,unsigned int bytes) {
|
||||
void EzSockets::SendData(const char *data, unsigned int bytes)
|
||||
{
|
||||
outBuffer.append(data, bytes);
|
||||
if(blocking)
|
||||
while ((outBuffer.length()>0) && !IsError())
|
||||
@@ -226,13 +240,15 @@ void EzSockets::SendData(const char* data,unsigned int bytes) {
|
||||
update();
|
||||
}
|
||||
|
||||
int EzSockets::ReadData(char* data,unsigned int bytes) {
|
||||
int EzSockets::ReadData(char *data, unsigned int bytes)
|
||||
{
|
||||
int bytesRead = PeekData(data,bytes);
|
||||
inBuffer = inBuffer.substr(bytesRead);
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
int EzSockets::PeekData(char* data,unsigned int bytes) {
|
||||
int EzSockets::PeekData(char *data, unsigned int bytes)
|
||||
{
|
||||
if(blocking)
|
||||
while ((inBuffer.length()<bytes) && !IsError())
|
||||
pUpdateRead();
|
||||
@@ -253,13 +269,15 @@ int EzSockets::PeekData(char* data,unsigned int bytes) {
|
||||
/**********************************\
|
||||
| Packet/Structure Data System |
|
||||
\**********************************/
|
||||
void EzSockets::SendPack(char* data,unsigned int bytes) {
|
||||
void EzSockets::SendPack(const char *data, unsigned int bytes)
|
||||
{
|
||||
unsigned int SendSize = htonl(bytes);
|
||||
SendData((char*)&SendSize,sizeof(int));
|
||||
SendData((const char *)&SendSize, sizeof(int));
|
||||
SendData(data, bytes);
|
||||
}
|
||||
|
||||
int EzSockets::ReadPack(char* data,unsigned int max) {
|
||||
int EzSockets::ReadPack(char *data, unsigned int max)
|
||||
{
|
||||
int size = PeekPack(data, max);
|
||||
|
||||
if (size != -1)
|
||||
@@ -268,14 +286,15 @@ int EzSockets::ReadPack(char* data,unsigned int max) {
|
||||
return size;
|
||||
}
|
||||
|
||||
int EzSockets::PeekPack(char* data,unsigned int max) {
|
||||
int EzSockets::PeekPack(char *data, unsigned int max)
|
||||
{
|
||||
if (CanRead())
|
||||
pUpdateRead();
|
||||
|
||||
if(blocking) {
|
||||
while((inBuffer.length()<4) && !IsError()) {
|
||||
if (blocking)
|
||||
{
|
||||
while ((inBuffer.length()<4) && !IsError())
|
||||
pUpdateRead();
|
||||
}
|
||||
|
||||
if (IsError())
|
||||
return -1;
|
||||
@@ -284,15 +303,15 @@ int EzSockets::PeekPack(char* data,unsigned int max) {
|
||||
if (inBuffer.length()<4)
|
||||
return -1;
|
||||
|
||||
unsigned int size=0;
|
||||
unsigned int size;
|
||||
PeekData((char*)&size, 4);
|
||||
size = ntohl(size);
|
||||
|
||||
if (blocking)
|
||||
while ((inBuffer.length()<(size+4)) && !IsError())
|
||||
while (inBuffer.length()<(size+4) && !IsError())
|
||||
pUpdateRead();
|
||||
else
|
||||
if((inBuffer.length()<(size+4)) || (inBuffer.length()<=4))
|
||||
if (inBuffer.length()<(size+4) || inBuffer.length()<=4)
|
||||
return -1;
|
||||
|
||||
if (IsError())
|
||||
@@ -311,24 +330,29 @@ int EzSockets::PeekPack(char* data,unsigned int max) {
|
||||
/*****************************************\
|
||||
| Null Terminating String Data System |
|
||||
\*****************************************/
|
||||
void EzSockets::SendStr(string& data, char delim) {
|
||||
void EzSockets::SendStr(const string& data, char delim)
|
||||
{
|
||||
char tDr[1];
|
||||
tDr[0] = delim;
|
||||
SendData(data.c_str(), data.length());
|
||||
SendData(tDr, 1);
|
||||
}
|
||||
|
||||
int EzSockets::ReadStr(string& data, char delim) {
|
||||
int EzSockets::ReadStr(string& data, char delim)
|
||||
{
|
||||
int t = PeekStr(data, delim);
|
||||
if (t >= 0)
|
||||
inBuffer = inBuffer.substr(t+1);
|
||||
return t;
|
||||
}
|
||||
|
||||
int EzSockets::PeekStr(string& data, char delim) {
|
||||
int EzSockets::PeekStr(string& data, char delim)
|
||||
{
|
||||
int t = inBuffer.find(delim,0);
|
||||
if(blocking) {
|
||||
while ((t==-1) && !IsError()) {
|
||||
if (blocking)
|
||||
{
|
||||
while (t == -1 && !IsError())
|
||||
{
|
||||
pUpdateRead();
|
||||
t = inBuffer.find(delim, 0);
|
||||
}
|
||||
@@ -344,14 +368,16 @@ int EzSockets::PeekStr(string& data, char delim) {
|
||||
/************************\
|
||||
| Stream Data System |
|
||||
\************************/
|
||||
istream& operator >>(istream &is,EzSockets &obj) {
|
||||
istream& operator>>(istream &is, EzSockets& obj)
|
||||
{
|
||||
string writeString;
|
||||
obj.SendStr(writeString);
|
||||
is >> writeString;
|
||||
return is;
|
||||
}
|
||||
|
||||
ostream& operator <<(ostream &os, EzSockets &obj) {
|
||||
ostream& operator<<(ostream &os, EzSockets &obj)
|
||||
{
|
||||
string readString;
|
||||
obj.ReadStr(readString);
|
||||
os << readString;
|
||||
@@ -362,7 +388,8 @@ ostream& operator <<(ostream &os, EzSockets &obj) {
|
||||
/**************************\
|
||||
| Internal Data System |
|
||||
\**************************/
|
||||
int EzSockets::pUpdateRead() {
|
||||
int EzSockets::pUpdateRead()
|
||||
{
|
||||
char tempData[1024];
|
||||
int bytes = pReadData(tempData);
|
||||
|
||||
@@ -376,7 +403,8 @@ int EzSockets::pUpdateRead() {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
int EzSockets::pUpdateWrite() {
|
||||
int EzSockets::pUpdateWrite()
|
||||
{
|
||||
int bytes = pWriteData(outBuffer.c_str(), outBuffer.length());
|
||||
|
||||
if (bytes > 0)
|
||||
@@ -387,16 +415,18 @@ int EzSockets::pUpdateWrite() {
|
||||
}
|
||||
|
||||
|
||||
int EzSockets::pReadData(char* data) {
|
||||
if((state == skCONNECTED)||(state == skLISTENING))
|
||||
int EzSockets::pReadData(char* data)
|
||||
{
|
||||
if(state == skCONNECTED || state == skLISTENING)
|
||||
return recv(sock, data, 1024, 0);
|
||||
else {
|
||||
|
||||
fromAddr_len = sizeof(sockaddr_in);
|
||||
return recvfrom(sock,data,1024,0,(sockaddr*)&fromAddr,(socklen_t*)&fromAddr_len);
|
||||
}
|
||||
return recvfrom(sock, data, 1024, 0, (sockaddr*)&fromAddr,
|
||||
(socklen_t*)&fromAddr_len);
|
||||
}
|
||||
|
||||
int EzSockets::pWriteData(const char* data,int dataSize) {
|
||||
int EzSockets::pWriteData(const char* data, int dataSize)
|
||||
{
|
||||
return send(sock, data, dataSize, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,8 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class EzSockets {
|
||||
class EzSockets
|
||||
{
|
||||
public:
|
||||
|
||||
EzSockets();
|
||||
@@ -51,7 +52,7 @@ public:
|
||||
bool accept(EzSockets &socket);
|
||||
|
||||
//Connect
|
||||
bool connect(const std::string& host, unsigned short port);
|
||||
bool connect(const string& host, unsigned short port);
|
||||
|
||||
//Kill socket
|
||||
void close();
|
||||
@@ -68,18 +69,18 @@ public:
|
||||
void update();
|
||||
|
||||
//Raw data system
|
||||
void SendData(string & outData);
|
||||
void SendData(const 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);
|
||||
void SendPack(const 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');
|
||||
void SendStr(const string& data, char delim = '\0');
|
||||
int ReadStr(string& data, char delim = '\0');
|
||||
int PeekStr(string& data, char delim = '\0');
|
||||
|
||||
@@ -91,7 +92,8 @@ public:
|
||||
|
||||
|
||||
bool blocking;
|
||||
enum SockState {
|
||||
enum SockState
|
||||
{
|
||||
skDISCONNECTED = 0,
|
||||
skUNDEF1, //Not implemented
|
||||
skLISTENING,
|
||||
|
||||
Reference in New Issue
Block a user