Version 1 (modified by landauf, 17 years ago) (diff) |
---|
NetworkSocket
This module will probably need a multithreading implementation. Here is a good howto about multithreaded SDL and mutexes. A even better guide for C++ itself: here.
Next Steps:
This is a small description of the NetworkSocket class
/* public: */ /* * The standard constructor: does nothing, thumb */ NetworkSocket(); /* * The constructor that creates the new socket * * @param address: the address informations (SDL_net.h definition) * @param type: the type of the Socket: Server/Client */ NetworkSocket(IPaddress address, NodeType type); /* * This function establishes a TCP/UDP connection to a given server (function argument). * It is called by the NetworkStream. It creates a TCP/UDP socket for the connection. */ void connectToServer(IPaddress ip); /* * Tells the NetworkSocket to listen on a specific port for incoming connections. * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection. */ void listen(unsigned int port); /* * Tears down a TCP/UDP connection. */ void disconnect(); /* * reconnects, restablishes the connection */ void reconnect(); /* * This function writes some bytes (data) to the network connection (if the connection is already * estabilhed) otherwise it just does nothing (silently discarding the data). And writes some * warnings */ int writeBytes(byte* data, int length); /* * Reads in the bytes from the network interface and passes it to the NetworkStream. * This function must internaly be implemented/connected as a thread, since the read * functions of many network libraries are blocking an would therefore block the whole * program. * From outside, the thread shouldn't be accessible at all. * @param data: pointer to the NetworkStream data buffer * @param length: length of the data to read * returns: length of data read */ int readBytes(byte* data, int length); /* * Reads in the bytes from the network interface and passes it to the NetworkStream. * This function must internaly be implemented/connected as a thread, since the read * functions of many network libraries are blocking an would therefore block the whole * program. * From outside, the thread shouldn't be accessible at all. * @param data: pointer to the NetworkStream data buffer * @param length: length of the data to read * returns: 0 if the length of data was not available, else returns length as the number of bytes read */ int readBlock(byte* data, int length);