13.04.2012, 10:41
(
Last edited by BlueG; 01/08/2014 at 11:38 AM.
)
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
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)
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
- Windows
- v0.1b http://files.g-stylezzz.com/socket_p...01b/socket.dll
- v0.2b https://github.com/pBlueG/Socket/releases/tag/v0.2b (Github mirror)
- Linux
- v0.1b http://files.g-stylezzz.com/socket_p...v01b/socket.so
- v0.2b https://github.com/pBlueG/Socket/releases/tag/v0.2b (Github mirror)
- Include
- Tools, Scripts
- Source:
- v0.1b http://files.g-stylezzz.com/socket_p...t_v01b_src.rar
- v0.2b https://github.com/pBlueG/Socket (Source browser), http://github.com/pBlueG/Socket/archive/master.zip (.zip archive)