AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Extensions (https://forums.alliedmods.net/forumdisplay.php?f=134)
-   -   [EXTENSION] Sockets (2.0.0) (https://forums.alliedmods.net/showthread.php?t=54643)

Olly 04-30-2007 16:27

[EXTENSION] Sockets (2.0.0)
 
3 Attachment(s)
Hi,
I have made this extension so that your server can communicate with other servers, and send and receive data between them. This can be used for downloading through HTTP, creating an irc bot, querying another game server.. List goes on :P

Any bugs then get me in #sourcemod :oops:

Credits:
BAILOPAN, PimpinJuice, devicenull, and anyone else i missed from #sourcemod :D

Install:
1. Extract 'sockets.ext.dll' to the 'addons/sourcemod/extensions' folder.
2. Extract 'sockets.inc' to the 'addons/sourcemod/scripting/include' folder.


WINDOWS ONLY AT THE MOMENT, LINUX WILL BE TOMOROW


Changed Natives:

/**
* Creates a new socket.
*
* @param protocol The type of socket to create, TCP (default), or UDP
* @param SocketRecieve:rfunc The recieve callback.
* @param SocketError:efunc The error callback.
* @return Handle The socket handle. Returns INVALID_HANDLE on failure
*/
native Handle:CreateSocket(SocketType:protocol=SOCKE T_TCP, SocketRecieve:rfunc, SocketError:efunc);

This has now changed, the SocketRecive, and SocketError callbacks are now paramaters of this native.


/**
* Convert a string to an MD5 hash (used for irc-relay)
*
* @param String:input The input string
* @param String:output The output string
* @param size The length of the output buffer
*/
native md5HashString(String:in[], String:out[], size);

This has nothing todo with sockets, but i needed in in IRC-Relay because SQLite sucks balls



public(String:recieveData[], const dataSize, Handle:arg)

The socketrevieve callback signature is now as above


Plugin Example
I couldnt be bothered to write a new example plugin. But you can check my new IRC Relay which i will be posting after this.

Enjoy :up:

Change Log
Quote:

1.0.0.0
  • Initial Release :D
1.0.0.1
  • Minor changes (never released)
1.0.1.0
  • Huge overhaull of everything in the plugin (consider this the first release)
1.0.2.0
  • Fixed lots of null pointer crashes ><
  • Fixed the native name in the include for SocketClose
  • Fixed functag
  • Compiled for linux
1.2.0.0
  • Skipped a load of version numbers (because i can)
  • Added SetConnectedCallback, and SetDisconnectCallback
  • CreateSocket now returns a Handle type which needs to be passed to all natives
  • Removed lots of the BS comments :P
  • Hopefully fixed lots of crashes in linux
  • Added arg argument for all callbacks to identify the socket that called back
2.0.0
  • Fixed lots of bugs
  • Fixed problem with the way i setup the callbacks (thanks bailo)
  • Changed the way that the natives work, less 'set callack' natives, so it works much better
  • The SocketReceive native now has the data that was got from the socket in one of the paramaters, instead of needing to specify a global string of a fixed size.
  • Compiled for Orange-Box


API 04-30-2007 21:37

Re: [EXTENSION] Sockets
 
Great job :)
I will probably find some use for it soon.

sslice 04-30-2007 21:49

Re: [EXTENSION] Sockets
 
Nice, although I would like to see this implemented in a more SM-ish way. Using a Handle and then some callbacks for the different socket interactions like connect, disconnect, receive, maybe error, etc.

Edit: Also maybe use the same naming convention used in SourceMod, rather than all lowercase and underscores.

Olly 05-01-2007 07:38

Re: [EXTENSION] Sockets
 
Quote:

Originally Posted by sslice (Post 471810)
Nice, although I would like to see this implemented in a more SM-ish way. Using a Handle and then some callbacks for the different socket interactions like connect, disconnect, receive, maybe error, etc.

Edit: Also maybe use the same naming convention used in SourceMod, rather than all lowercase and underscores.

As i said in my first post, this was just the basics that are implimented in the origional sockets extension. I will start to add more advanced things in the near future. But i just wanted to release the basics first, so if anyone needs, they can start writing the plugin sooner rather than later.

But I will be adding callbacks and handles soon :)

API 05-01-2007 09:43

Re: [EXTENSION] Sockets
 
No nix'? If you need help with nix build, ask me.

Olly 05-04-2007 18:32

Re: [EXTENSION] Sockets
 
Ok I am doing a major reworking of this plugin.

* Callbacks
* Seperate connect & open socket
* Fixed naming (sslice :P)
* Added bind
* Gonna add some stuff i havnt written yet :)
* And i have linux now, so i will be able to provide one when the time comes

Lets just call the current extension a practice run :)

sslice 05-05-2007 00:25

Re: [EXTENSION] Sockets
 
Possibly add accept and listen as well? Might not want to give plugins the ability to serve connections though.

Olly 05-05-2007 20:08

Re: [EXTENSION] Sockets
 
added to my todo list :)

A little note.
This extension is going to change quite drasticly in the next version. So at the moment, pretend this extension isnt here

Olly 05-05-2007 22:01

Re: [EXTENSION] Sockets
 
These are the revised natives for Sockets Ext. There is no more recieve native, because receiving data is handled internally by the extension, but when data is received it will call the recieve callback so stuff can be done to the received data.

I still have some more functions to add, but cant think of them off hand right now, its like 3am :P


Code:

// Error type codes
#define EMPTY_HOST 1
#define NO_HOST 2
#define CONNECT_ERROR 3
#define SEND_ERROR 4
#define BIND_ERROR 5
// Protocols
#define SOCKET_TCP 1
#define SOCKET_UDP 2
 #define SOCKET_RAW 3

funcenum SocketError
{
    public(errorType, errorNum)
};

funcenum SocketRecieve
{
    // Not decided yet ><
};

/**
 * Creates a new socket.
 *
 * @param    protocol            The type of socket to create, TCP (default), or UDP
 * @return    socket                The socket id (handle).
 */
native CreateSocket(protocol=SOCKET_TCP);

/**
 * Closes the specified socket.
 *
 * @param    socket                The id (handle) of the socket to close.
 */
native CloseSocket(socket);

/**
 * Defines the callback function when an error occurs
 *
 * @param    socket                The id (handle) of the socket to set the callback for.
 * @param    function            The function to use as callback.
 */
native SetErrorCallback(socket, function);

/**
 * Defines the callback function for when data is received though the socket
 *
 * @param    socket                The id (handle) of the socket to set the callback for.
 * @param    function            The function to use as callback.
 */
native SetReceiveCallback(socket, function);

/**
 * Defines the callback function when an error occurs
 *
 * @param    socket                The id (handle) of the socket to connect.
 * @param    hostname            The hostname (or IP) to connect to.
 * @param    port                The port to connect to.
 * @return  true on success
 */
native ConnectSocket(socket, String:hostname[], port);

/**
 * Binds the socket to a local address
 *
 * @param    socket                The id (handle) of the socket to connect.
 * @param    hostname            The hostname (or IP) to bind the socket to.
 * @param    port                The port to bind the socket to.
 * @return  true on success
 */
native SocketBind(socket, String:hostname[], port);

/**
 * Sends a command through the socket.
 *
 * @param    socket            The id (handle) of the socket to send to.
 * @param    command            The command to send to the server.
 * @return                    The length of data sent.
 */
native SocketSend(socket, String:command[]);


devicenull 05-05-2007 23:28

Re: [EXTENSION] Sockets
 
Use handles!

I have some code that uses them, grab me on IRC.


All times are GMT -4. The time now is 14:05.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.