sockets_hz.inc
Original include source with line numbers.
| 1 | /* |
| 2 | * |
| 3 | * AMX Mod X Module |
| 4 | * Basic Socket Functions |
| 5 | * |
| 6 | * Based on code by Codebase from Ivan, [email protected] (AMX 0.9.3),Olaf Reusch, [email protected] (AMXX 0.16, AMX 0.96) |
| 7 | * Modified by Jйrфme Andrieu hackziner ( [email protected] ) |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #if defined _socket_hz_included |
| 12 | #endinput |
| 13 | #endif |
| 14 | #define _socket_hz_included |
| 15 | |
| 16 | #if AMXX_VERSION_NUM >= 175 |
| 17 | #pragma reqlib sockets_hz |
| 18 | #if !defined AMXMODX_NOAUTOLOAD |
| 19 | #pragma loadlib sockets_hz |
| 20 | #endif |
| 21 | #else |
| 22 | #pragma library socket_hz |
| 23 | #endif |
| 24 | |
| 25 | // Use SOCKET_TCP for TCP Socket connections |
| 26 | |
| 27 | #define SOCKET_TCP 1 |
| 28 | |
| 29 | // Use SOCKET_UDP for UDP Socket connections |
| 30 | |
| 31 | #define SOCKET_UDP 2 |
| 32 | |
| 33 | /* Opens a new connection to hostname:port via protocol (either SOCKET_TCP or SOCKET_UDP), |
| 34 | * returns a socket (positive) or negative or zero on error. |
| 35 | * States of error: |
| 36 | * 0 - no error |
| 37 | * 1 - error while creating socket |
| 38 | * 2 - couldn't resolve hostname |
| 39 | * 3 - couldn't connect to given hostname:port |
| 40 | */ |
| 41 | |
| 42 | native socket_open(const _hostname[], _port, _protocol = SOCKET_TCP, &_error); |
| 43 | |
| 44 | /* Opens a new listenning connection bind to hostname:port via protocol (either SOCKET_TCP or SOCKET_UDP), |
| 45 | * returns a socket (positive) or negative or zero on error. |
| 46 | * States of error: |
| 47 | * 0 - no error |
| 48 | * 1 - error while creating socket |
| 49 | * 2 - couldn't resolve hostname |
| 50 | * 3 - couldn't connect to given hostname:port |
| 51 | */ |
| 52 | |
| 53 | native socket_listen(const _hostname[], _port, _protocol = SOCKET_TCP, &_error); |
| 54 | |
| 55 | /* Closes a Socket */ |
| 56 | |
| 57 | native socket_close(_socket); |
| 58 | |
| 59 | /* Accept a connection on a listenning socket |
| 60 | * returns a socket (positive) or negative or zero on error. */ |
| 61 | |
| 62 | native socket_accept(_socket); |
| 63 | |
| 64 | /* Set a socket in unblocking mode */ |
| 65 | |
| 66 | native socket_unblock(_socket); |
| 67 | |
| 68 | /* Recieves Data to string with the given length */ |
| 69 | |
| 70 | native socket_recv(_socket, _data[], _length); |
| 71 | |
| 72 | /* Sends data to the Socket */ |
| 73 | |
| 74 | native socket_send(_socket, const _data[], _length); |
| 75 | |
| 76 | /* Same as socket_send but Data can contain null bytes */ |
| 77 | |
| 78 | native socket_send2(_socket, const _data[], _length); |
| 79 | |
| 80 | /* This function will return true if the state (buffer content) have changed within the last recieve or |
| 81 | * the timeout, where timeout is a value in µSeconds, (1 sec =1000000 µsec). |
| 82 | * Use to check if new data is in your socket. */ |
| 83 | |
| 84 | native socket_change(_socket, _timeout=100000); |
| 85 | |
| 86 | |
| 87 | /* The getpeername function retrieves the address of the peer to which a socket is connected |
| 88 | * family - address family |
| 89 | * data - up to 14 bytes of direct address */ |
| 90 | |
| 91 | native socket_getpeername(_socket, &family , data[] , length); |