SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Plugin Development

Reply
 
Thread Tools Display Modes
Old 13/04/2012, 10:41 AM   #1
BlueG
Beta Tester
 
BlueG's Avatar
 
Join Date: Sep 2007
Location: Los Angeles, CA
Posts: 231
Reputation: 294
Default Socket plugin (v0.2b released)

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)
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
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

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

Last edited by BlueG; 01/08/2014 at 11:38 AM.
BlueG is offline   Reply With Quote
Old 13/04/2012, 10:44 AM   #2
Shabi RoxX
Gangsta
 
Shabi RoxX's Avatar
 
Join Date: Jan 2012
Location: ____Pakistan______ Playing Since: 5-April-2008 Facebook:Shabi RoxX
Posts: 552
Reputation: 131
Default Re: Socket plugin

wow nice work keep it up......
Shabi RoxX is offline   Reply With Quote
Old 13/04/2012, 10:45 AM   #3
Mauzen
Godfather
 
Mauzen's Avatar
 
Join Date: Jun 2007
Location: Western Germany
Posts: 5,069
Reputation: 1505
Default Re: Socket plugin

Great, this was one of the missing samp features.
So lets wait for the first cluster servers
Mauzen is offline   Reply With Quote
Old 13/04/2012, 10:51 AM   #4
C0dy09
Little Clucker
 
C0dy09's Avatar
 
Join Date: Oct 2011
Location: Germany
Posts: 37
Reputation: 0
Default AW: Socket plugin

wow very nice!
C0dy09 is offline   Reply With Quote
Old 13/04/2012, 11:00 AM   #5
DRIFT_HUNTER
High-roller
 
Join Date: Oct 2009
Posts: 2,067
Reputation: 126
Default Re: Socket plugin

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)
__________________
Путин here,
Путин there,
Путин просто everywhere.
DRIFT_HUNTER is offline   Reply With Quote
Old 13/04/2012, 11:31 AM   #6
Kar
High-roller
 
Kar's Avatar
 
Join Date: May 2010
Location: Black Asylum
Posts: 3,329
Reputation: 422
Default Re: Socket plugin (TCP/UDP) v0.1a

Lovely, finally a great working socket plugin!

Thank you BlueG & Great Job.
__________________
Kar is offline   Reply With Quote
Old 13/04/2012, 11:46 AM   #7
AirKite
Gangsta
 
AirKite's Avatar
 
Join Date: Feb 2009
Posts: 740
Reputation: 19
Default Re: Socket plugin (TCP/UDP) v0.1a

Good job!!!
AirKite is offline   Reply With Quote
Old 13/04/2012, 11:50 AM   #8
TheArcher
High-roller
 
TheArcher's Avatar
 
Join Date: Dec 2009
Location: Home
Posts: 2,458
Reputation: 247
Default Re: Socket plugin (TCP/UDP) v0.1a

THANK YOU SO MUCH! This is an AMAZING release.

Edit: Is this faster as SampQuery by Wastie?
__________________
TheArcher is offline   Reply With Quote
Old 13/04/2012, 12:08 PM   #9
QuaTTrO
Gangsta
 
QuaTTrO's Avatar
 
Join Date: Aug 2009
Posts: 564
Reputation: 66
Default Re: Socket plugin (TCP/UDP) v0.1a

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


QuaTTrO is offline   Reply With Quote
Old 13/04/2012, 12:14 PM   #10
zSuYaNw
Godfather
 
zSuYaNw's Avatar
 
Join Date: Apr 2010
Location: Santa Catarina
Posts: 6,825
Reputation: 655
Default Re: Socket plugin (TCP/UDP) v0.1a

Amazing, nice work!
zSuYaNw is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Socket Plugin] Retrieving input stream iRage Scripting Help 0 08/01/2014 09:11 AM
Question about socket plugin Kirollos Scripting Help 6 13/07/2012 08:33 PM
Socket Plugin 147147147 Español/Spanish 5 07/03/2012 10:22 PM


All times are GMT. The time now is 07:26 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.