SA-MP Forums Archive
[Plugin] Socket plugin (v0.2b released) - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Plugin Development (https://sampforum.blast.hk/forumdisplay.php?fid=18)
+--- Thread: [Plugin] Socket plugin (v0.2b released) (/showthread.php?tid=333934)

Pages: 1 2 3 4 5 6 7 8 9 10


Socket plugin (v0.2b released) - BlueG - 13.04.2012

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



Re: Socket plugin - Shabi RoxX - 13.04.2012

wow nice work keep it up......


Re: Socket plugin - Mauzen - 13.04.2012

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


AW: Socket plugin - C0dy09 - 13.04.2012

wow very nice!


Re: Socket plugin - DRIFT_HUNTER - 13.04.2012

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)


Re: Socket plugin (TCP/UDP) v0.1a - Kar - 13.04.2012

Lovely, finally a great working socket plugin!

Thank you BlueG & Great Job.


Re: Socket plugin (TCP/UDP) v0.1a - AirKite - 13.04.2012

Good job!!!


Re: Socket plugin (TCP/UDP) v0.1a - TheArcher - 13.04.2012

THANK YOU SO MUCH! This is an AMAZING release.

Edit: Is this faster as SampQuery by Wastie?


Re: Socket plugin (TCP/UDP) v0.1a - QuaTTrO - 13.04.2012

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


Re: Socket plugin (TCP/UDP) v0.1a - zSuYaNw - 13.04.2012

Amazing, nice work!


Re: Socket plugin (TCP/UDP) v0.1a - MicroD - 13.04.2012

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



Re: Socket plugin (TCP/UDP) v0.1a - Amit_B - 13.04.2012

Finally. Gonna use it, thanks!


Re: Socket plugin (TCP/UDP) v0.1a - Ricop522 - 13.04.2012

Great job


AW: Socket plugin (TCP/UDP) v0.1a - Nanory - 13.04.2012

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


Re: AW: Socket plugin (TCP/UDP) v0.1a - TheArcher - 13.04.2012

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)


Respuesta: Socket plugin (TCP/UDP) v0.1a - Jovanny - 13.04.2012

Amazing BlueG, rep++


Re: Socket plugin (TCP/UDP) v0.1a - steki. - 13.04.2012

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


Re: Socket plugin (TCP/UDP) v0.1a - Lorenc_ - 14.04.2012

I fucking love you, thanks man.


Re: Socket plugin (TCP/UDP) v0.1a - Crimson - 15.04.2012

Wonderful, Thank you very much.


AW: Socket plugin (TCP/UDP) v0.1a - AlexLS95 - 15.04.2012

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?