[Plugin] Socket plugin (v0.2b released)
#1

Sockets v0.2b
Code:
01/08/14: (v0.2b)
- finished SSL related code
- general bugfix
- added socket_sendto()
- fixed socket_send_array()
- added ssl_get_peer_certificate() 
24/06/12: (v0.1b)
- TCP server crash fixed (thanks to Wicko)
- general bugfixes
This plugin allows you to create multiple TCP or UDP sockets. Each socket receives data in a separate thread, which means that the server will not freeze. Once you destroy a socket, the thread will shutdown. If there is further interest I'm going to add support for SSL sockets.

The plugin has many uses, here are some ideas:
- IRC echo bot
- a new RCON tool including chat (based on a TCP server socket, kinda like GUIRCON)
- retrieve SA-MP server data through UDP
- communicate with a PHP script to log/receive data


PAWN Scripting (socket.inc)
pawn Code:
native Socket:socket_create(pType:TCP); // udp & tcp
native socket_bind(Socket:id, ip[]); // udp & tcp
native socket_connect(Socket:id, host[], port); // udp & tcp
native socket_listen(Socket:id, port); // udp & tcp
native socket_stop_listen(Socket:id); // udp & tcp
native socket_destroy(Socket:id); // udp & tcp
native socket_send(Socket:id, data[], len); // udp & tcp
native socket_sendto(Socket:id, const ip[], port, data[], len); // udp only
native socket_send_array(Socket:id, data[], size=sizeof(data)); // udp & tcp
native is_socket_valid(Socket:id); // udp & tcp
native socket_set_max_connections(Socket:id, max_remote_clients); // tcp only
native socket_close_remote_client(Socket:id, remote_clientid); // tcp only
native socket_sendto_remote_client(Socket:id, remote_clientid, data[]); // tcp only
native socket_remote_client_connected(Socket:id, remote_clientid); // tcp only
native get_remote_client_ip(Socket:id, remote_clientid, ip[]); // tcp only

// ssl stuff (requires OpenSSL)

native ssl_init(); // initialize the ssl library
native ssl_create_context(Socket:id, method);
native ssl_connect(Socket:id); // tcp (client only)
native ssl_load_cert_into_context(Socket:id, const certificate[], const private_key[]); // certificate & private_key might be the same .pem file
native ssl_shutdown(Socket:id) = socket_destroy;
native ssl_get_peer_certificate(Socket:id, method, subject[], issuer[], remote_clientid = 0xFFFF);
native ssl_set_accept_timeout(Socket:id, interval); // interval in miliseconds
native ssl_set_mode(Socket:id, mode); // see above SSL_modes (enum)
Callbacks
pawn Code:
// client & server (udp)
forward onUDPReceiveData(Socket:id, data[], data_len, remote_client_ip[], remote_client_port);
// client only (tcp)
forward onSocketAnswer(Socket:id, data[], data_len); // called when socket_connect() has been used and the server sends data
forward onSocketClose(Socket:id);
// server only (tcp)
forward onSocketReceiveData(Socket:id, remote_clientid, data[], data_len); // called when a remote client sends data
forward onSocketRemoteConnect(Socket:id, remote_client[], remote_clientid); // called when a remote client connects to our socket server
forward onSocketRemoteDisconnect(Socket:id, remote_clientid); // called when a remote client disconnects from our socket server
pawn Code:
// connect to an IRC server
new Socket:sock = socket_create(TCP);
if(is_socket_valid(sock)) {
    socket_connect(sock, "foco.us.irc.tl", 6667);
    socket_send(sock, "NICK TestBot\r\n"); // set the nickname
    socket_send(sock, "USER TestBot - - :TestBot\r\n"); // set the username & realname
    socket_send(sock, "MODE TestBot +B\r\n"); // tell the server it's a bot
    socket_send(sock, "JOIN #test_channel\r\n"); // join a specific channel
}

// this callback will now receive all incoming data
public onSocketAnswer(Socket:id, data[])
{
}

Downloads (v0.2a has been skipped)

Note: v0.2b includes SSL related code
Reply
#2

wow nice work keep it up......
Reply
#3

Great, this was one of the missing samp features.
So lets wait for the first cluster servers
Reply
#4

wow very nice!
Reply
#5

I waited so long for these...now i can integrate server chat to my forum
Can someone provide example for sending and listening socket from server and web (PHP)
Reply
#6

Lovely, finally a great working socket plugin!

Thank you BlueG & Great Job.
Reply
#7

Good job!!!
Reply
#8

THANK YOU SO MUCH! This is an AMAZING release.

Edit: Is this faster as SampQuery by Wastie?
Reply
#9

This is amazing. Thank you, i looking for something like this
Reply
#10

Amazing, nice work!
Reply
#11

I've made this with old plugin, this is much better i will continue with this plugin
Reply
#12

Finally. Gonna use it, thanks!
Reply
#13

Great job
Reply
#14

MY DREAMS COME TRUE, finally an Socket Plugin for Windows AND LINUX!
Thank you very much, BlueG, and well done!
Reply
#15

Quote:
Originally Posted by Nanory
View Post
finally an Socket Plugin for Windows AND LINUX!
In 2010 StrickenKid made a socket plugin but it hasn't many functions + this uses TCP & UDP + that plugin was out-dated and unstable (beta version)
Reply
#16

Amazing BlueG, rep++
Reply
#17

Oh, I heard 'bout that. Truly amazing job indeed.
Guess who's doing a custom Admin Panel in Java for their gamemode? aha
Reply
#18

I fucking love you, thanks man.
Reply
#19

Wonderful, Thank you very much.
Reply
#20

Great Plugin! But I've got a problem. With Windows 7 x64 it works really well but on our Linux Ubuntu (v10.04.4) Server everytime I start the server I got this error:

"socket_listen(): Socket has failed to bind on port 776."

pawn Code:
#define SOCKET_PORT 776
new Socket:socketconnection;

//OnGamemodeInit()
socketconnection = socket_create(UDP);
socket_listen(socketconnection, SOCKET_PORT);
Sorry but I nerver worked with sockets so I don't know how to use them. Can someone help me?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)