Guest User

socket.ini

a guest
Jun 20th, 2020
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.02 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. typeset SocketConnectCB
  204. {
  205. function void (Handle socket, any arg);
  206. };
  207.  
  208. /**
  209. * triggered if a listening socket received an incoming connection and is ready to be used
  210. *
  211. * @note The child-socket won't work until receive-, disconnect-, and errorcallback for it are set.
  212. *
  213. * @param Handle socket The socket handle pointing to the calling listen-socket
  214. * @param Handle newSocket The socket handle to the newly spawned child socket
  215. * @param String remoteIP The remote IP
  216. * @param any arg The argument set by SocketSetArg() for the listen-socket
  217. * @noreturn
  218. */
  219. typeset SocketIncomingCB
  220. {
  221. function void (Handle socket, Handle newSocket, const char remoteIP[], remotePort, any arg);
  222. };
  223.  
  224. /**
  225. * triggered if a socket receives data
  226. *
  227. * @note This is binary safe if you always use dataSize for operations on receiveData[]
  228. * @note packets may be split up into multiple chunks -> multiple calls to the receive callback
  229. * @note if not set otherwise by SocketSetOption(..., ConcatenateCallbacks, ...) receiveData will
  230. * never be longer than 4096 characters including \0 terminator
  231. *
  232. * @param Handle socket The socket handle pointing to the calling socket
  233. * @param String receiveData The data which arrived, 0-terminated at receiveData[dataSize]
  234. * @param cell_t dataSize The length of the arrived data excluding the 0-termination
  235. * @param any arg The argument set by SocketSetArg() for the socket
  236. * @noreturn
  237. */
  238. typeset SocketReceiveCB
  239. {
  240. function void (Handle socket, const char receiveData[], const dataSize, any arg)
  241. };
  242.  
  243. /**
  244. * called after a socket sent all items in its send queue successfully
  245. *
  246. * @param Handle socket The socket handle pointing to the calling socket
  247. * @param any arg The argument set by SocketSetArg() for the socket
  248. * @noreturn
  249. */
  250. typeset SocketSendqueueEmptyCB
  251. {
  252. function void (Handle socket, any arg);
  253. };
  254.  
  255. /**
  256. * called if a socket has been properly disconnected by the remote side
  257. *
  258. * @note You should call CloseHandle(socket) or reuse the socket before this function ends
  259. *
  260. * @param Handle socket The socket handle pointing to the calling socket
  261. * @param any arg The argument set by SocketSetArg() for the socket
  262. * @noreturn
  263. */
  264. typeset SocketDisconnectCB
  265. {
  266. function void (Handle socket, any arg);
  267. };
  268.  
  269. /**
  270. * called if an unrecoverable error occured, close the socket without an additional call to a disconnect callback
  271. *
  272. * @note You should call CloseHandle(socket) or reuse the socket before this function ends
  273. *
  274. * @param Handle socket The socket handle pointing to the calling socket
  275. * @param cell_t errorType The error type, see defines above
  276. * @param cell_t errorNum The errno, see errno.h for details
  277. * @param any arg The argument set by SocketSetArg() for the socket
  278. * @noreturn
  279. */
  280. typeset SocketErrorCB
  281. {
  282. function void (Handle socket, const errorType, const errorNum, any arg);
  283. };
  284.  
  285.  
  286. /*************************************************************************************************/
  287. /******************************************** natives ********************************************/
  288. /*************************************************************************************************/
  289.  
  290.  
  291. /**
  292. * Returns whether a socket is connected or not.
  293. *
  294. * @param socket Socket handle to check
  295. * @return bool The connection status
  296. */
  297. native bool:SocketIsConnected(Handle:socket);
  298.  
  299.  
  300. /**
  301. * Creates a new socket.
  302. *
  303. * @note this function may be relatively expensive, reuse sockets if possible
  304. *
  305. * @param SocketType protocol The protocol to use, SOCKET_TCP is default
  306. * @param SocketErrorCB efunc The error callback
  307. * @return Handle The socket handle. Returns INVALID_HANDLE on failure
  308. */
  309. native Handle:SocketCreate(SocketType:protocol=SOCKET_TCP, SocketErrorCB:efunc);
  310.  
  311. /**
  312. * Binds the socket to a local address
  313. *
  314. * @param Handle socket The handle of the socket to be used.
  315. * @param String hostname The hostname (or IP) to bind the socket to.
  316. * @param cell_t port The port to bind the socket to.
  317. * @return bool true on success
  318. */
  319. native bool:SocketBind(Handle:socket, const String:hostname[], port);
  320.  
  321. /**
  322. * Connects a socket
  323. *
  324. * @note this native is threaded, it may be still running after it executed, use the connect callback
  325. * @note invokes the SocketError callback with errorType = CONNECT_ERROR or EMPTY_HOST if it fails
  326. * @note invokes the SocketConnect callback if it succeeds
  327. *
  328. * @param Handle socket The handle of the socket to be used.
  329. * @param SocketConnectCB cfunc The connect callback
  330. * @param SocketReceiveCB rfunc The receive callback
  331. * @param SocketDisconnectCB dfunc The disconnect callback
  332. * @param String hostname The hostname (or IP) to connect to.
  333. * @param cell_t port The port to connect to.
  334. * @noreturn
  335. */
  336. native SocketConnect(Handle:socket, SocketConnectCB:cfunc, SocketReceiveCB:rfunc, SocketDisconnectCB:dfunc, const String:hostname[], port);
  337.  
  338. /**
  339. * Disconnects a socket
  340. *
  341. * @note this will not close the handle, the socket will be reset to a state similar to after SocketCreate()
  342. * @note this won't trigger any disconnect/error callbacks
  343. *
  344. * @noreturn
  345. */
  346. native bool:SocketDisconnect(Handle:socket);
  347.  
  348. /**
  349. * Makes a socket listen for incoming connections
  350. *
  351. * @param Handle socket The handle of the socket to be used.
  352. * @param SocketIncomingCB ifunc The callback for incoming connections
  353. * @return bool true on success
  354. */
  355. native bool:SocketListen(Handle:socket, SocketIncomingCB:ifunc);
  356.  
  357. /**
  358. * Sends data through the socket.
  359. *
  360. * @note specify size for binary safe operation
  361. * @note if size is not specified the \0 terminator will not be included
  362. * @note This native is threaded, it may be still running after it executed (not atomic).
  363. * @note Use the SendqueueEmpty callback to determine when all data has been successfully sent.
  364. * @note The socket extension will ensure that the data will be send in the correct order and split
  365. * the data if required.
  366. *
  367. * @param Handle socket The handle of the socket to be used.
  368. * @param String data The data to send.
  369. * @noreturn
  370. */
  371. native SocketSend(Handle:socket, const String:data[], size=-1);
  372.  
  373. /**
  374. * Sends UDP data through the socket to a specific destination.
  375. *
  376. * @note specify size for binary safe operation
  377. * @note if size is not specified the \0 terminator will not be included
  378. * @note This native is threaded, it may be still running after it executed (not atomic).
  379. * @note Use the SendqueueEmpty callback to determine when all data has been successfully sent.
  380. * @note The socket extension will ensure that the data will be send in the correct order and split
  381. * the data if required.
  382. *
  383. * @param Handle socket The handle of the socket to be used.
  384. * @param String data The data to send.
  385. * @param String hostname The hostname (or IP) to send to.
  386. * @param cell_t port The port to send to.
  387. * @noreturn
  388. */
  389. native SocketSendTo(Handle:socket, const String:data[], size=-1, const String:hostname[], port);
  390.  
  391. /**
  392. * Set a socket option.
  393. *
  394. * @param Handle socket The handle of the socket to be used. May be INVALID_HANDLE if not essential.
  395. * @param SocketOption option The option to modify (see enum SocketOption for details).
  396. * @param cellt_ value The value to set the option to.
  397. * @return cell_t 1 on success.
  398. */
  399. native SocketSetOption(Handle:socket, SocketOption:option, value);
  400.  
  401.  
  402. /**
  403. * Defines the callback function for when the socket receives data
  404. *
  405. * @note this is only useful and required for child-sockets spawned by listen-sockets
  406. * (otherwise you already set it in SocketConnect())
  407. *
  408. * @param Handle socket The handle of the socket to be used.
  409. * @param SocketReceiveCB rfunc The receive callback
  410. * @noreturn
  411. */
  412. native SocketSetReceiveCallback(Handle:socket, SocketReceiveCB:rfunc);
  413.  
  414. /**
  415. * Defines the callback function for when the socket sent all items in its send queue
  416. *
  417. * @note this must be called AFTER sending (queueing) the data
  418. * @note if no send-data is queued this will fire the callback itself
  419. * @note the callback is guaranteed to fire
  420. *
  421. * @param Handle socket The handle of the socket to be used.
  422. * @param SocketDisconnectCB dfunc The disconnect callback
  423. * @noreturn
  424. */
  425. native SocketSetSendqueueEmptyCallback(Handle:socket, SocketSendqueueEmptyCB:sfunc);
  426.  
  427. /**
  428. * Defines the callback function for when the socket was properly disconnected by the remote side
  429. *
  430. * @note this is only useful and required for child-sockets spawned by listen-sockets
  431. * (otherwise you already set it in SocketConnect())
  432. *
  433. * @param Handle socket The handle of the socket to be used.
  434. * @param SocketDisconnectCB dfunc The disconnect callback
  435. * @noreturn
  436. */
  437. native SocketSetDisconnectCallback(Handle:socket, SocketDisconnectCB:dfunc);
  438.  
  439. /**
  440. * Defines the callback function for when the socket triggered an error
  441. *
  442. * @note this is only useful and required for child-sockets spawned by listen-sockets
  443. * (otherwise you already set it in SocketCreate())
  444. *
  445. * @param Handle socket The handle of the socket to be used.
  446. * @param SocketErrorCB efunc The error callback
  447. * @noreturn
  448. */
  449. native SocketSetErrorCallback(Handle:socket, SocketErrorCB:efunc);
  450.  
  451.  
  452. /**
  453. * Sets the argument being passed to callbacks
  454. *
  455. * @param Handle socket The handle of the socket to be used.
  456. * @param any arg The argument to set
  457. * @noreturn
  458. */
  459. native SocketSetArg(Handle:socket, any:arg);
  460.  
  461. /**
  462. * Retrieve the local system's hostname as the command "hostname" does.
  463. *
  464. * @param dest Destination string buffer to copy to.
  465. * @param destLen Destination buffer length (includes null terminator).
  466. *
  467. * @return 1 on success
  468. */
  469. native SocketGetHostName(String:dest[], destLen);
  470.  
  471. /**
  472. * _________________Do not edit below this line!_______________________
  473. */
  474. public Extension:__ext_smsock =
  475. {
  476. name = "Socket",
  477. file = "socket.ext",
  478. #if defined AUTOLOAD_EXTENSIONS
  479. autoload = 1,
  480. #else
  481. autoload = 0,
  482. #endif
  483. #if defined REQUIRE_EXTENSIONS
  484. required = 1,
  485. #else
  486. required = 0,
  487. #endif
  488. };
Add Comment
Please, Sign In to add comment