cpp utils files, network and a smal util class
This commit is contained in:
parent
ea3dffdb4a
commit
62f50a2f79
13 changed files with 610 additions and 0 deletions
53
net/socketio.h
Normal file
53
net/socketio.h
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#ifndef ZUTIL_NET_SOCKET_IO
|
||||
#define ZUTIL_NET_SOCKET_IO
|
||||
|
||||
#include <iostream>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <string>
|
||||
#include "socketexception.h"
|
||||
using namespace std;
|
||||
|
||||
//const int MAXHOSTNAME = 200;
|
||||
const int MAX_CONNECTIONS = 5;
|
||||
const int SOCKET_BUFFER = 500;
|
||||
const int MSG_PRIO = 0; // defined by dgame
|
||||
|
||||
namespace zutil{
|
||||
class SocketIO{
|
||||
private:
|
||||
int socket_id;
|
||||
sockaddr_in address;
|
||||
public:
|
||||
SocketIO();
|
||||
virtual ~SocketIO();
|
||||
|
||||
// Server initialization
|
||||
bool create();
|
||||
bool bind( const int port );
|
||||
bool listen() const;
|
||||
bool accept( SocketIO& ) const;
|
||||
|
||||
// Client initialization
|
||||
bool connect( const string host, const int port );
|
||||
|
||||
// Data Transimission
|
||||
bool write( const char c ) const;
|
||||
bool write( const string ) const;
|
||||
bool write( const char* buf, const size_t len ) const;
|
||||
|
||||
char read( ) const;
|
||||
int read( string& ) const;
|
||||
int read( char* buf, const size_t len ) const;
|
||||
|
||||
|
||||
void set_non_blocking( const bool );
|
||||
bool is_valid() const { return socket_id != -1; }
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue