This document assumes a certain degree of knowledge of TCP/IP and socket programming.
To use the NET library, you'll need to '#include
net
and all #define
tokens start with NET
. Words within a class or function
name are Capitalised and NOT separated with underscores. Words
within #define
tokens may be separated with underscores
to make them readable.
class netAddress class netBuffer |__ class netMessage class netGuid class netSocket |__ class netChannel |__ class netBufferChannel | |__ class netChat | |__ class netMessageChannel | |__ class netMonitorServer
class netBuffer
class netBuffer { public: netBuffer ( int max_length ) ; ~netBuffer () ; int getLength() const ; int getMaxLength() const ; char* getData() ; void remove () ; void remove (int pos, int n) ; bool append (const char* s, int n) ; bool append (int n) ; } ;
class netMessage
class netMessage : public netBuffer { public: netMessage ( const char* s, int n ) ; netMessage ( int type, int to_id, int from_id ) ; int getType () const ; int getToID () const ; int getFromID () const ; void geta ( void* a, int n ) const ; // a=array; array should already be serialized void puta ( const void* a, int n ) ; int getch () const ; void putch ( int c ) ; bool getb () const ; void putb ( bool b ) ; int getw () const ; void putw ( int i ) ; int geti () const ; void puti ( int i ) ; void gets ( char* s, int n ) const ; void puts ( const char* s ) ; void print ( FILE *fd = stderr ) const ; };
class netAddress
class netAddress { public: netAddress () ; netAddress ( const char* host, int port ) ; void set ( const char* host, int port ) ; const char* getHost () const ; int getPort () const ; static const char* getLocalHost () ; bool getBroadcast () const ; } ;When reading from the network using one of the higher level classes, you can use a netAddress with an empty string as the 'host' to mean "accept data from any host". When writing to the network, the reserved host name '<broadcast>' (the < and > are literally there) will cause the message to be broadcast to all machines on your subnet.
Otherwise, use either the machines' network name - or it's IP address (as an ASCII string).
class netSocket
class netSocket { public: netSocket () ; virtual ~netSocket () ; int getHandle () const ; void setHandle (int handle) ; bool open ( bool stream=true ) ; int bind ( cchar* host, int port ) ; int listen ( int backlog ) ; int accept ( netAddress* addr ) ; int connect ( cchar* host, int port ) ; int send ( const void * buffer, int size, int flags = 0 ) ; int sendto ( const void * buffer, int size, int flags, const netAddress* to ) ; int recv ( void * buffer, int size, int flags = 0 ) ; int recvfrom ( void * buffer, int size, int flags, netAddress* from ) ; void close ( void ) ; void setBlocking ( bool blocking ) ; } ;
Sender:netInit () ; netSocket *sock = new netSocket () ; sock -> open ( false ) ; sock -> setBlocking ( false ) ; sock -> connect ( host, port ) ; while ( !done ) sock -> send ( msg, len, 0 ); sock -> close () ; |
Reciever:netInit () ; netSocket *sock = new netSocket () ; sock -> open ( false ) ; sock -> setBlocking ( false ) ; sock -> bind ( host, port ) ; while ( !done ) if ( (len = sock -> recv(msg, maxlen, 0)) >= 0 ) ...use the data... sock -> close () ; |
class netChannel
class netChannel : public netSocket { public: netChannel () ; virtual ~netChannel () ; void setHandle (int s, bool is_connected = true); bool isConnected () const ; bool isClosed () const ; void shouldDelete () ; // -------------------------------------------------- // socket methods // -------------------------------------------------- bool open ( bool stream=true ) ; int listen ( int backlog ) ; int connect ( cchar* host, int port ) ; int send ( const void * buf, int size, int flags = 0 ) ; int recv ( void * buf, int size, int flags = 0 ) ; void close ( void ) ; // poll() eligibility predicates virtual bool readable (void) ; virtual bool writable (void) ; // -------------------------------------------------- // event handlers // -------------------------------------------------- void handleReadEvent (void); void handleWriteEvent (void); // These are meant to be overridden. virtual void handleConnect (void) ; virtual void handleRead (void) ; virtual void handleWrite (void) ; virtual void handleClose (void) ; virtual void handleAccept (void) ; virtual void handleError (int error) ; static bool poll (u32 timeout = 0 ) ; static void loop (u32 timeout = 0 ) ; };
class netBufferChannel
class netBufferChannel : public netChannel { public: netBufferChannel (int in_buffer_size = 512, int out_buffer_size = 4096) ; void closeWhenDone (void) ; virtual bool bufferSend (const char* msg, int msg_len) ; virtual void handleBufferRead (netBuffer& buffer) ; };
class netChat
The handle_buffer_read() method looks at the input stream for the current 'terminator' (usually '\r\n' for single-line responses, '\r\n.\r\n' for multi-line
class netChat : public netBufferChannel { public: netChat () ; void setTerminator (const char* t); const char* getTerminator (void); bool push (const char* s) ; virtual void collectIncomingData (const char* s, int n) ; virtual void foundTerminator (void) ; };
class netMessageChannel
class netMessageChannel : public netBufferChannel { public: netMessageChannel () ; bool sendMessage ( const netMessage& msg ) ; virtual void handleMessage ( const netMessage& msg ) ; };
class netMonitorServer
class netMonitorServer : public netChannel { public: netMonitorServer( cchar* _name, int port ) ; ~netMonitorServer() ; cchar* getPassword () const ; void setPassword ( cchar* string ) ; void setPrompt ( cchar* string ) ; void setCommandFunc ( void (*func)(cchar*, netMonitorChannel*) ) ; } ;
![]() | Dave McClurg <dpm@efn.org> |