| 1 | = NetworkSocket = |
| 2 | |
| 3 | This module will probably need a multithreading implementation. [http://wiki.delphigl.com/index.php/SDL_Thread_Beispiel Here] is a good howto about multithreaded SDL and mutexes. A even better guide for C++ itself: [http://www.libsdl.org/intro.de/usingthreads.html here]. |
| 4 | |
| 5 | Next Steps: |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | This is a small description of the NetworkSocket class |
| 11 | |
| 12 | {{{ |
| 13 | |
| 14 | /* public: */ |
| 15 | |
| 16 | /* |
| 17 | * The standard constructor: does nothing, thumb |
| 18 | */ |
| 19 | NetworkSocket(); |
| 20 | |
| 21 | |
| 22 | /* |
| 23 | * The constructor that creates the new socket |
| 24 | * |
| 25 | * @param address: the address informations (SDL_net.h definition) |
| 26 | * @param type: the type of the Socket: Server/Client |
| 27 | */ |
| 28 | NetworkSocket(IPaddress address, NodeType type); |
| 29 | |
| 30 | |
| 31 | /* |
| 32 | * This function establishes a TCP/UDP connection to a given server (function argument). |
| 33 | * It is called by the NetworkStream. It creates a TCP/UDP socket for the connection. |
| 34 | */ |
| 35 | void connectToServer(IPaddress ip); |
| 36 | |
| 37 | |
| 38 | /* |
| 39 | * Tells the NetworkSocket to listen on a specific port for incoming connections. |
| 40 | * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection. |
| 41 | */ |
| 42 | void listen(unsigned int port); |
| 43 | |
| 44 | |
| 45 | /* |
| 46 | * Tears down a TCP/UDP connection. |
| 47 | */ |
| 48 | void disconnect(); |
| 49 | |
| 50 | |
| 51 | /* |
| 52 | * reconnects, restablishes the connection |
| 53 | */ |
| 54 | void reconnect(); |
| 55 | |
| 56 | |
| 57 | /* |
| 58 | * This function writes some bytes (data) to the network connection (if the connection is already |
| 59 | * estabilhed) otherwise it just does nothing (silently discarding the data). And writes some |
| 60 | * warnings |
| 61 | */ |
| 62 | int writeBytes(byte* data, int length); |
| 63 | |
| 64 | |
| 65 | /* |
| 66 | * Reads in the bytes from the network interface and passes it to the NetworkStream. |
| 67 | * This function must internaly be implemented/connected as a thread, since the read |
| 68 | * functions of many network libraries are blocking an would therefore block the whole |
| 69 | * program. |
| 70 | * From outside, the thread shouldn't be accessible at all. |
| 71 | * @param data: pointer to the NetworkStream data buffer |
| 72 | * @param length: length of the data to read |
| 73 | * returns: length of data read |
| 74 | */ |
| 75 | int readBytes(byte* data, int length); |
| 76 | |
| 77 | |
| 78 | /* |
| 79 | * Reads in the bytes from the network interface and passes it to the NetworkStream. |
| 80 | * This function must internaly be implemented/connected as a thread, since the read |
| 81 | * functions of many network libraries are blocking an would therefore block the whole |
| 82 | * program. |
| 83 | * From outside, the thread shouldn't be accessible at all. |
| 84 | * @param data: pointer to the NetworkStream data buffer |
| 85 | * @param length: length of the data to read |
| 86 | * returns: 0 if the length of data was not available, else returns length as the number of bytes read |
| 87 | */ |
| 88 | int readBlock(byte* data, int length); |
| 89 | |
| 90 | |
| 91 | |
| 92 | }}} |