Advertisement
Guest User

Untitled

a guest
Aug 21st, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.76 KB | None | 0 0
  1. // socket extension include file
  2.  
  3. #if defined _socket_included
  4.   #endinput
  5. #endif
  6. #define _socket_included
  7. #include <core>
  8.  
  9. enum SocketType {
  10.     SOCKET_TCP = 1,
  11.     SOCKET_UDP,
  12.     SOCKET_RAW
  13. }
  14.  
  15. #define EMPTY_HOST 1
  16. #define NO_HOST 2
  17. #define CONNECT_ERROR 3
  18. #define SEND_ERROR 4
  19. #define BIND_ERROR 5
  20. #define RECV_ERROR 6
  21. #define LISTEN_ERROR 7
  22.  
  23.  
  24. /*************************************************************************************************/
  25. /******************************************** options ********************************************/
  26. /*************************************************************************************************/
  27.  
  28.  
  29. /**
  30.  * Options available for SocketSetOption()
  31.  *
  32.  * @note modifying these options is not required for normal operation, you can skip the whole
  33.  *       section in most cases.
  34.  */
  35. enum SocketOption {
  36. /**
  37.  * If this option is set the socket extension will try to concatenate SocketReceive callbacks.
  38.  *
  39.  * This will possibly lower the amount of callbacks passed to SourceMod plugins and improve the
  40.  * performance. The socket extension will preserve the packet order.
  41.  *
  42.  * @note this doesn't prevent multiple callbacks, it only reduces them for high load.
  43.  * @note this will not truncate packets below 4096 bytes, setting it lower will be ignored
  44.  * @note set this option if you expect lots of data in a short timeframe
  45.  * @note don't forget to set your buffer sizes at least to the value passed to this function, but
  46.  *       always at least to 4096
  47.  *
  48.  * @param cell_t    0(=default) to disable or max. chunk size including \0 terminator in bytes
  49.  * @return bool true on success
  50.  */
  51.     ConcatenateCallbacks = 1,
  52. /**
  53.  * If this option is set the socket extension will enforce a mutex lock in the GameFrame() hook.
  54.  *
  55.  * This will ensure that callbacks will be processed every gameframe as fast as possible with the
  56.  * drawback of potentially creating lag. It's not recommended to set this option for most cases.
  57.  * If this option is not set the gameframe will be skipped if quietly obtaining a lock fails.
  58.  *
  59.  * @note combine this with CallbacksPerFrame for best performance
  60.  * @note this option will affect all sockets from all plugins, use it with caution!
  61.  *
  62.  * @param bool  whether to force locking or not
  63.  * @return bool true on success
  64.  */
  65.     ForceFrameLock,
  66. /**
  67.  * This will specify the maximum amount of callbacks processed in every gameframe.
  68.  *
  69.  * The default value for this option is 1, setting it higher will possibly increase networking
  70.  * performance but may cause lag if it's set too high.
  71.  * The amount of callbacks actually being processed is limited by not being able to quietly obtain
  72.  * a lock (see ForceFrameLock) and the amount of callbacks in the queue.
  73.  *
  74.  * @note this option will affect all sockets from all plugins, use it with caution!
  75.  *
  76.  * @param cell_t    maximum amount of callbacks per gameframe
  77.  * @return bool     true on success
  78.  */
  79.     CallbacksPerFrame,
  80. /**
  81.  * If this option is set the socket will be allowed to send broadcast messages in case the protocol
  82.  * supports it. This is a wrapper for setting SO_BROADCAST.
  83.  *
  84.  * @param bool  whether to allow broadcasting or not
  85.  * @return bool true on success
  86.  */
  87.     SocketBroadcast,
  88. /**
  89.  * If this option is set SocketBind() will allow reusing local adresses in case the protocol
  90.  * supports it. This is a wrapper for setting SO_REUSEADDR.
  91.  *
  92.  * @param bool  whether to allow broadcasting or not
  93.  * @return bool true on success
  94.  */
  95.     SocketReuseAddr,
  96. /**
  97.  * If this option is set the socket will try to keep the connection alive by periodically sending
  98.  * messages if the protocol supports it. This is a wrapper for setting SO_KEEPALIVE.
  99.  *
  100.  * @param bool  whether to allow broadcasting or not
  101.  * @return bool true on success
  102.  */
  103.     SocketKeepAlive,
  104. /**
  105.  * This option specifies how long a socket will wait if it's being closed and its send buffer is
  106.  * still filled. This is a wrapper for setting SO_LINGER.
  107.  *
  108.  * @param cell_t    0 (=default) to disable or time in s
  109.  * @return bool     true on success
  110.  */
  111.     SocketLinger,
  112. /**
  113.  * If this option is set out-of-band data will be inlined into the normal receive stream. This is a
  114.  * wrapper for setting SO_OOBINLINE.
  115.  *
  116.  * @param bool  whether to inline out-of-band data or not
  117.  * @return bool true on success
  118.  */
  119.     SocketOOBInline,
  120. /**
  121.  * This option specifies how large the send buffer will be. This is a wrapper for setting
  122.  * SO_SNDBUF.
  123.  *
  124.  * @param cell_t    size in bytes
  125.  * @return bool     true on success
  126.  */
  127.     SocketSendBuffer,
  128. /**
  129.  * This option specifies how large the receive buffer will be. This is a wrapper for setting
  130.  * SO_RCVBUF.
  131.  *
  132.  * @param cell_t    size in bytes
  133.  * @return bool     true on success
  134.  */
  135.     SocketReceiveBuffer,
  136. /**
  137.  * If this option is set outgoing messages will ignore the default routing facilities if the
  138.  * protocol implementation supports it. The remote site should be directly connected to the sender.
  139.  * This is a wrapper for setting SO_DONTROUTE.
  140.  *
  141.  * @param bool  whether to skip default routing or not
  142.  * @return bool true on success
  143.  */
  144.     SocketDontRoute,
  145. /**
  146.  * This option specifies the minimum amount of data to receive before processing it. This is a
  147.  * wrapper for setting SO_RCVLOWAT.
  148.  *
  149.  * @note this can probably block the extension, use it with caution!
  150.  *
  151.  * @param cell_t    size in bytes
  152.  * @return bool     true on success
  153.  */
  154.     SocketReceiveLowWatermark,
  155. /**
  156.  * This option specifies how long a socket will try to receive data before it times out and
  157.  * processes the data. This is a wrapper for setting SO_RCVTIMEO.
  158.  *
  159.  * @param cell_t    0 (=default) to disable or time in ms
  160.  * @return bool     true on success
  161.  */
  162.     SocketReceiveTimeout,
  163. /**
  164.  * This option specifies the minimum amount of data required in the send buffer before starting to
  165.  * send it. This is a wrapper for setting SO_SNDLOWAT.
  166.  *
  167.  * @note this can probably block the extension, use it with caution!
  168.  *
  169.  * @param cell_t    size in bytes
  170.  * @return bool     true on success
  171.  */
  172.     SocketSendLowWatermark,
  173. /**
  174.  * This option specifies how long a socket will try to send data before it times out and
  175.  * retries it later. This is a wrapper for setting SO_SNDTIMEO.
  176.  *
  177.  * @param cell_t    0 (=default) to disable or time in ms
  178.  * @return bool     true on success
  179.  */
  180.     SocketSendTimeout,
  181. /**
  182.  * If this option is set the socket extension will display debugging messages in the server console/logs.
  183.  *
  184.  * @param bool  whether to enable debugging or not
  185.  * @return bool true on success
  186.  */
  187.     DebugMode
  188. }
  189.  
  190.  
  191. /*************************************************************************************************/
  192. /******************************************* callbacks *******************************************/
  193. /*************************************************************************************************/
  194.  
  195.  
  196. /**
  197.  * triggered if a normal sockets finished connecting and is ready to be used
  198.  *
  199.  * @param socket    The socket handle pointing to the calling socket
  200.  * @param arg       The argument set by SocketSetArg()
  201.  * @noreturn
  202.  */
  203.  
  204. //funcenum SocketConnectCB
  205. //{
  206. //    public(Handle:socket, any:arg)
  207. //};
  208. typedef SocketConnectCB = function void (Handle socket, any arg);
  209.  
  210. //typedef ConVarChanged = function void (ConVar convar, const char[] oldValue, const char[] newValue);
  211.  
  212. /**
  213.  * triggered if a listening socket received an incoming connection and is ready to be used
  214.  *
  215.  * @note The child-socket won't work until receive-, disconnect-, and errorcallback for it are set.
  216.  *
  217.  * @param Handle    socket      The socket handle pointing to the calling listen-socket
  218.  * @param Handle    newSocket   The socket handle to the newly spawned child socket
  219.  * @param String    remoteIP    The remote IP
  220.  * @param any       arg         The argument set by SocketSetArg() for the listen-socket
  221.  * @noreturn
  222.  */
  223.  
  224. //funcenum SocketIncomingCB
  225. //{
  226. //    public(Handle:socket, Handle:newSocket, const String:remoteIP[], remotePort, any:arg)
  227. //};
  228.  
  229. typedef SocketIncomingCB = function void (Handle socket, Handle newSocket, const char[] remoteIP, int remotePort, any arg);
  230.  
  231. /**
  232.  * triggered if a socket receives data
  233.  *
  234.  * @note This is binary safe if you always use dataSize for operations on receiveData[]
  235.  * @note packets may be split up into multiple chunks -> multiple calls to the receive callback
  236.  * @note if not set otherwise by SocketSetOption(..., ConcatenateCallbacks, ...) receiveData will
  237.  *       never be longer than 4096 characters including \0 terminator
  238.  *
  239.  * @param Handle    socket      The socket handle pointing to the calling socket
  240.  * @param String    receiveData The data which arrived, 0-terminated at receiveData[dataSize]
  241.  * @param cell_t    dataSize    The length of the arrived data excluding the 0-termination
  242.  * @param any       arg         The argument set by SocketSetArg() for the socket
  243.  * @noreturn
  244.  */
  245.  
  246. //funcenum SocketReceiveCB
  247. //{
  248. //    public(Handle:socket, const String:receiveData[], const dataSize, any:arg)
  249. //};
  250.  
  251. typedef SocketReceiveCB = function void (Handle socket, const char[] receiveData,const int dataSize, any arg);
  252.  
  253. /**
  254.  * called after a socket sent all items in its send queue successfully
  255.  *
  256.  * @param Handle    socket      The socket handle pointing to the calling socket
  257.  * @param any       arg         The argument set by SocketSetArg() for the socket
  258.  * @noreturn
  259.  */
  260.  
  261. //funcenum SocketSendqueueEmptyCB
  262. //{
  263. //    public(Handle:socket, any:arg)
  264. //};
  265.  
  266. typedef SocketSendqueueEmptyCB = function void (Handle socket, any arg);
  267.  
  268. /**
  269.  * called if a socket has been properly disconnected by the remote side
  270.  *
  271.  * @note You should call CloseHandle(socket) or reuse the socket before this function ends
  272.  *
  273.  * @param Handle    socket      The socket handle pointing to the calling socket
  274.  * @param any       arg         The argument set by SocketSetArg() for the socket
  275.  * @noreturn
  276.  */
  277.  
  278. //funcenum SocketDisconnectCB
  279. //{
  280. //    public(Handle:socket, any:arg)
  281. //};
  282.  
  283. typedef SocketDisconnectCB = function void (Handle socket, any arg);
  284.  
  285. /**
  286.  * called if an unrecoverable error occured, close the socket without an additional call to a disconnect callback
  287.  *
  288.  * @note You should call CloseHandle(socket) or reuse the socket before this function ends
  289.  *
  290.  * @param Handle    socket      The socket handle pointing to the calling socket
  291.  * @param cell_t    errorType   The error type, see defines above
  292.  * @param cell_t    errorNum    The errno, see errno.h for details
  293.  * @param any       arg         The argument set by SocketSetArg() for the socket
  294.  * @noreturn
  295.  */
  296.  
  297. //funcenum SocketErrorCB
  298. //{
  299. //    public(Handle:socket, const errorType, const errorNum, any:arg)
  300. //};
  301.  
  302. typedef SocketErrorCB = function void(Handle socket, const int errorType, const int errorNum, any arg);
  303.  
  304.  
  305. /*************************************************************************************************/
  306. /******************************************** natives ********************************************/
  307. /*************************************************************************************************/
  308.  
  309.  
  310. /**
  311.  * Returns whether a socket is connected or not.
  312.  *
  313.  * @param   socket  Socket handle to check
  314.  * @return  bool    The connection status
  315.  */
  316. native bool SocketIsConnected(Handle socket);
  317.  
  318.  
  319. /**
  320.  * Creates a new socket.
  321.  *
  322.  * @note this function may be relatively expensive, reuse sockets if possible
  323.  *
  324.  * @param SocketType    protocol    The protocol to use, SOCKET_TCP is default
  325.  * @param SocketErrorCB efunc       The error callback
  326.  * @return Handle                   The socket handle. Returns INVALID_HANDLE on failure
  327.  */
  328. native Handle SocketCreate(SocketType protocol=SOCKET_TCP, SocketErrorCB efunc);
  329.  
  330. /**
  331.  * Binds the socket to a local address
  332.  *
  333.  * @param Handle    socket      The handle of the socket to be used.
  334.  * @param String    hostname    The hostname (or IP) to bind the socket to.
  335.  * @param cell_t    port        The port to bind the socket to.
  336.  * @return bool                 true on success
  337.  */
  338. native bool SocketBind(Handle socket, const char[] hostname, int port);
  339.  
  340. /**
  341.  * Connects a socket
  342.  *
  343.  * @note this native is threaded, it may be still running after it executed, use the connect callback
  344.  * @note invokes the SocketError callback with errorType = CONNECT_ERROR or EMPTY_HOST if it fails
  345.  * @note invokes the SocketConnect callback if it succeeds
  346.  *
  347.  * @param Handle                socket      The handle of the socket to be used.
  348.  * @param SocketConnectCB       cfunc       The connect callback
  349.  * @param SocketReceiveCB       rfunc       The receive callback
  350.  * @param SocketDisconnectCB    dfunc       The disconnect callback
  351.  * @param String                hostname    The hostname (or IP) to connect to.
  352.  * @param cell_t                port        The port to connect to.
  353.  * @noreturn
  354.  */
  355. native void SocketConnect(Handle socket, SocketConnectCB cfunc, SocketReceiveCB rfunc, SocketDisconnectCB dfunc, const char[] hostname, int port);
  356.  
  357. /**
  358.  * Disconnects a socket
  359.  *
  360.  * @note this will not close the handle, the socket will be reset to a state similar to after SocketCreate()
  361.  * @note this won't trigger any disconnect/error callbacks
  362.  *
  363.  * @noreturn
  364.  */
  365. native bool SocketDisconnect(Handle socket);
  366.  
  367. /**
  368.  * Makes a socket listen for incoming connections
  369.  *
  370.  * @param Handle            socket  The handle of the socket to be used.
  371.  * @param SocketIncomingCB  ifunc   The callback for incoming connections
  372.  * @return bool                     true on success
  373.  */
  374. native bool SocketListen(Handle socket, SocketIncomingCB ifunc);
  375.  
  376. /**
  377.  * Sends data through the socket.
  378.  *
  379.  * @note specify size for binary safe operation
  380.  * @note if size is not specified the \0 terminator will not be included
  381.  * @note This native is threaded, it may be still running after it executed (not atomic).
  382.  * @note Use the SendqueueEmpty callback to determine when all data has been successfully sent.
  383.  * @note The socket extension will ensure that the data will be send in the correct order and split
  384.  *          the data if required.
  385.  *
  386.  * @param Handle    socket  The handle of the socket to be used.
  387.  * @param String    data    The data to send.
  388.  * @noreturn
  389.  */
  390. native void SocketSend(Handle socket, const char[] data, int size=-1);
  391.  
  392. /**
  393.  * Sends UDP data through the socket to a specific destination.
  394.  *
  395.  * @note specify size for binary safe operation
  396.  * @note if size is not specified the \0 terminator will not be included
  397.  * @note This native is threaded, it may be still running after it executed (not atomic).
  398.  * @note Use the SendqueueEmpty callback to determine when all data has been successfully sent.
  399.  * @note The socket extension will ensure that the data will be send in the correct order and split
  400.  *          the data if required.
  401.  *
  402.  * @param Handle    socket  The handle of the socket to be used.
  403.  * @param String    data    The data to send.
  404.  * @param String    hostname    The hostname (or IP) to send to.
  405.  * @param cell_t    port        The port to send to.
  406.  * @noreturn
  407.  */
  408. native void SocketSendTo(Handle socket, const char[] data, int size=-1, const char[] hostname, int port);
  409.  
  410. /**
  411.  * Set a socket option.
  412.  *
  413.  * @param Handle        socket  The handle of the socket to be used. May be INVALID_HANDLE if not essential.
  414.  * @param SocketOption  option  The option to modify (see enum SocketOption for details).
  415.  * @param cellt_        value   The value to set the option to.
  416.  * @return cell_t           1 on success.
  417.  */
  418. native int SocketSetOption(Handle socket, SocketOption option, int value);
  419.  
  420.  
  421. /**
  422.  * Defines the callback function for when the socket receives data
  423.  *
  424.  * @note this is only useful and required for child-sockets spawned by listen-sockets
  425.  *       (otherwise you already set it in SocketConnect())
  426.  *
  427.  * @param Handle            socket  The handle of the socket to be used.
  428.  * @param SocketReceiveCB   rfunc   The receive callback
  429.  * @noreturn
  430.  */
  431. native void SocketSetReceiveCallback(Handle socket, SocketReceiveCB rfunc);
  432.  
  433. /**
  434.  * Defines the callback function for when the socket sent all items in its send queue
  435.  *
  436.  * @note this must be called AFTER sending (queueing) the data
  437.  * @note if no send-data is queued this will fire the callback itself
  438.  * @note the callback is guaranteed to fire
  439.  *
  440.  * @param Handle                socket  The handle of the socket to be used.
  441.  * @param SocketDisconnectCB    dfunc   The disconnect callback
  442.  * @noreturn
  443.  */
  444. native void SocketSetSendqueueEmptyCallback(Handle socket, SocketSendqueueEmptyCB sfunc);
  445.  
  446. /**
  447.  * Defines the callback function for when the socket was properly disconnected by the remote side
  448.  *
  449.  * @note this is only useful and required for child-sockets spawned by listen-sockets
  450.  *       (otherwise you already set it in SocketConnect())
  451.  *
  452.  * @param Handle                socket  The handle of the socket to be used.
  453.  * @param SocketDisconnectCB    dfunc   The disconnect callback
  454.  * @noreturn
  455.  */
  456. native void SocketSetDisconnectCallback(Handle socket, SocketDisconnectCB dfunc);
  457.  
  458. /**
  459.  * Defines the callback function for when the socket triggered an error
  460.  *
  461.  * @note this is only useful and required for child-sockets spawned by listen-sockets
  462.  *       (otherwise you already set it in SocketCreate())
  463.  *
  464.  * @param Handle        socket  The handle of the socket to be used.
  465.  * @param SocketErrorCB efunc   The error callback
  466.  * @noreturn
  467.  */
  468. native void SocketSetErrorCallback(Handle socket, SocketErrorCB efunc);
  469.  
  470.  
  471. /**
  472.  * Sets the argument being passed to callbacks
  473.  *
  474.  * @param Handle    socket  The handle of the socket to be used.
  475.  * @param any       arg     The argument to set
  476.  * @noreturn
  477.  */
  478. native void SocketSetArg(Handle socket, any arg);
  479.  
  480. /**
  481.  * Retrieve the local system's hostname as the command "hostname" does.
  482.  *
  483.  * @param dest    Destination string buffer to copy to.
  484.  * @param destLen Destination buffer length (includes null terminator).
  485.  *
  486.  * @return          1 on success
  487.  */
  488. native int SocketGetHostName(char[] dest, int destLen);
  489.  
  490. /**
  491.  * _________________Do not edit below this line!_______________________
  492.  */
  493. public Extension __ext_smsock =
  494. {
  495.     name = "Socket",
  496.     file = "socket.ext",
  497. #if defined AUTOLOAD_EXTENSIONS
  498.     autoload = 1,
  499. #else
  500.     autoload = 0,
  501. #endif
  502. #if defined REQUIRE_EXTENSIONS
  503.     required = 1,
  504. #else
  505.     required = 0,
  506. #endif
  507. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement