From a7744212d2580efd8584e928ae962105c0ef8010 Mon Sep 17 00:00:00 2001 From: JoinedSenses Date: Thu, 7 Nov 2019 14:47:40 -0500 Subject: [PATCH] Update syntax --- examples/example.sp | 27 ++--- examples/listenexample.sp | 31 +++--- examples/selftest.sp | 211 +++++++++++++++++++------------------- examples/sendto_test.sp | 75 +++++++------- examples/sockettest.sp | 28 ++--- 5 files changed, 193 insertions(+), 179 deletions(-) diff --git a/examples/example.sp b/examples/example.sp index 0ba8972..141c70e 100644 --- a/examples/example.sp +++ b/examples/example.sp @@ -3,7 +3,10 @@ #include #include -public Plugin:myinfo = { +#pragma newdecls required +#pragma semicolon 1 + +public Plugin myinfo = { name = "socket example", author = "Player", description = "This example demonstrates downloading a http file with the socket extension", @@ -11,33 +14,33 @@ public Plugin:myinfo = { url = "http://www.player.to/" }; -public OnPluginStart() { +public void OnPluginStart() { // create a new tcp socket - new Handle:socket = SocketCreate(SOCKET_TCP, OnSocketError); + Socket socket = new Socket(SOCKET_TCP, OnSocketError); // open a file handle for writing the result - new Handle:hFile = OpenFile("dl.htm", "wb"); + File hFile = OpenFile("dl.htm", "wb"); // pass the file handle to the callbacks - SocketSetArg(socket, hFile); + socket.SetArg(hFile); // connect the socket - SocketConnect(socket, OnSocketConnected, OnSocketReceive, OnSocketDisconnected, "www.sourcemod.net", 80) + socket.Connect(OnSocketConnected, OnSocketReceive, OnSocketDisconnected, "www.sourcemod.net", 80); } -public OnSocketConnected(Handle:socket, any:arg) { +public void OnSocketConnected(Socket socket, any arg) { // socket is connected, send the http request - decl String:requestStr[100]; + char requestStr[100]; Format(requestStr, sizeof(requestStr), "GET /%s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", "index.php", "www.sourcemod.net"); - SocketSend(socket, requestStr); + socket.Send(requestStr); } -public OnSocketReceive(Handle:socket, String:receiveData[], const dataSize, any:hFile) { +public void OnSocketReceive(Socket socket, char[] receiveData, const int dataSize, any hFile) { // receive another chunk and write it to /dl.htm // we could strip the http response header here, but for example's sake we'll leave it in WriteFileString(hFile, receiveData, false); } -public OnSocketDisconnected(Handle:socket, any:hFile) { +public void OnSocketDisconnected(Socket socket, any hFile) { // Connection: close advises the webserver to close the connection when the transfer is finished // we're done here @@ -45,7 +48,7 @@ public OnSocketDisconnected(Handle:socket, any:hFile) { CloseHandle(socket); } -public OnSocketError(Handle:socket, const errorType, const errorNum, any:hFile) { +public void OnSocketError(Socket socket, const int errorType, const int errorNum, any hFile) { // a socket error occured LogError("socket error %d (errno %d)", errorType, errorNum); diff --git a/examples/listenexample.sp b/examples/listenexample.sp index 812b649..ff1a56c 100644 --- a/examples/listenexample.sp +++ b/examples/listenexample.sp @@ -3,7 +3,10 @@ #include #include -public Plugin:myinfo = { +#pragma newdecls required +#pragma semicolon 1 + +public Plugin myinfo = { name = "listen socket example", author = "Player", description = "This example provides a simple echo server", @@ -11,52 +14,52 @@ public Plugin:myinfo = { url = "http://www.player.to/" }; -public OnPluginStart() { +public void OnPluginStart() { // enable socket debugging (only for testing purposes!) - SocketSetOption(INVALID_HANDLE, DebugMode, 1); + view_as(null).SetOption(DebugMode, 1); // create a new tcp socket - new Handle:socket = SocketCreate(SOCKET_TCP, OnSocketError); + Socket socket = new Socket(SOCKET_TCP, OnSocketError); // bind the socket to all interfaces, port 50000 SocketBind(socket, "0.0.0.0", 50000); // let the socket listen for incoming connections SocketListen(socket, OnSocketIncoming); } -public OnSocketIncoming(Handle:socket, Handle:newSocket, String:remoteIP[], remotePort, any:arg) { +public void OnSocketIncoming(Socket socket, Socket newSocket, char[] remoteIP, int remotePort, any arg) { PrintToServer("%s:%d connected", remoteIP, remotePort); // setup callbacks required to 'enable' newSocket // newSocket won't process data until these callbacks are set - SocketSetReceiveCallback(newSocket, OnChildSocketReceive); - SocketSetDisconnectCallback(newSocket, OnChildSocketDisconnected); - SocketSetErrorCallback(newSocket, OnChildSocketError); + newSocket.SetReceiveCallback(OnChildSocketReceive); + newSocket.SetDisconnectCallback(OnChildSocketDisconnected); + newSocket.SetErrorCallback(OnChildSocketError); - SocketSend(newSocket, "send quit to quit\n"); + newSocket.Send("send quit to quit\n"); } -public OnSocketError(Handle:socket, const errorType, const errorNum, any:ary) { +public void OnSocketError(Socket socket, const int errorType, const int errorNum, any arg) { // a socket error occured LogError("socket error %d (errno %d)", errorType, errorNum); CloseHandle(socket); } -public OnChildSocketReceive(Handle:socket, String:receiveData[], const dataSize, any:hFile) { +public void OnChildSocketReceive(Socket socket, char[] receiveData, const int dataSize, any hFile) { // send (echo) the received data back - SocketSend(socket, receiveData); + socket.Send(receiveData); // close the connection/socket/handle if it matches quit if (strncmp(receiveData, "quit", 4) == 0) CloseHandle(socket); } -public OnChildSocketDisconnected(Handle:socket, any:hFile) { +public void OnChildSocketDisconnected(Socket socket, any hFile) { // remote side disconnected CloseHandle(socket); } -public OnChildSocketError(Handle:socket, const errorType, const errorNum, any:ary) { +public void OnChildSocketError(Socket socket, const int errorType, const int errorNum, any ary) { // a socket error occured LogError("child socket error %d (errno %d)", errorType, errorNum); diff --git a/examples/selftest.sp b/examples/selftest.sp index a1658ed..4d55b67 100644 --- a/examples/selftest.sp +++ b/examples/selftest.sp @@ -5,7 +5,10 @@ #include #include -public Plugin:myinfo = { +#pragma newdecls required +#pragma semicolon 1 + +public Plugin myinfo = { name = "socket extension selftest", author = "Player", description = "basic functionality testing", @@ -13,15 +16,15 @@ public Plugin:myinfo = { url = "http://www.player.to/" }; -new goalsReached; -new trapsReached; +int g_iGoalsReached; +int g_iTrapsReached; -new test; -public OnGameFrame() { - if (test == 0) { - test++; +int g_iTest; +public void OnGameFrame() { + if (g_iTest == 0) { + g_iTest++; - SocketSetOption(INVALID_HANDLE, DebugMode, 1); + view_as(null).SetOption(DebugMode, 1); selfTest1(); } @@ -44,25 +47,25 @@ public OnGameFrame() { * - A socket which tries to connect to the listening socket and receives data * - the child socket which sends the data */ -selfTest1() { - goalsReached = 0; - trapsReached = 0; +void selfTest1() { + g_iGoalsReached = 0; + g_iTrapsReached = 0; CreateTimer(3.0, selfTest1Terminate); PrintToServer("* |socket selftest| test #1 running"); - new Handle:socket = SocketCreate(SOCKET_TCP, OnSocketErrorTrap); + Socket socket = new Socket(SOCKET_TCP, OnSocketErrorTrap); - SocketSetOption(socket, SocketReuseAddr, 1); + socket.SetOption(SocketReuseAddr, 1); - SocketBind(socket, "0.0.0.0", 12345); - SocketListen(socket, Test1_OnListenSocketIncoming); + socket.Bind("0.0.0.0", 12345); + socket.Listen(Test1_OnListenSocketIncoming); - new Handle:socket2 = SocketCreate(SOCKET_TCP, OnSocketErrorTrap); - SocketConnect(socket2, OnSocketConnectGoal, Test1_OnReceiveSocketReceive, Test1_OnReceiveSocketDisconnect, "127.0.0.1", 12345); + Socket socket2 = new Socket(SOCKET_TCP, OnSocketErrorTrap); + socket2.Connect(OnSocketConnectGoal, Test1_OnReceiveSocketReceive, Test1_OnReceiveSocketDisconnect, "127.0.0.1", 12345); } -public Action:selfTest1Terminate(Handle:timer) { - if (goalsReached == 6 && trapsReached == 0) { +public Action selfTest1Terminate(Handle timer) { + if (g_iGoalsReached == 6 && g_iTrapsReached == 0) { PrintToServer("* |socket selftest| ** test #1 passed **"); selfTest2(); } else { @@ -83,26 +86,26 @@ public Action:selfTest1Terminate(Handle:timer) { * This test is using one socket: * - Listening socket on port 12345 */ -selfTest2() { - goalsReached = 0; - trapsReached = 0; +void selfTest2() { + g_iGoalsReached = 0; + g_iTrapsReached = 0; CreateTimer(4.0, selfTest2Terminate); PrintToServer("* |socket selftest| test #2 running"); - new Handle:socket = SocketCreate(SOCKET_TCP, OnSocketErrorTrap); + Socket socket = new Socket(SOCKET_TCP, OnSocketErrorTrap); - SocketSetOption(socket, SocketReuseAddr, 1); + socket.SetOption(SocketReuseAddr, 1); - SocketBind(socket, "0.0.0.0", 12345); - SocketListen(socket, OnSocketIncomingTrap); + socket.Bind("0.0.0.0", 12345); + socket.Listen(OnSocketIncomingTrap); CloseHandle(socket); - goalsReached++; + g_iGoalsReached++; } -public Action:selfTest2Terminate(Handle:timer) { - if (goalsReached == 1 && trapsReached == 0) { +public Action selfTest2Terminate(Handle timer) { + if (g_iGoalsReached == 1 && g_iTrapsReached == 0) { PrintToServer("* |socket selftest| ** test #2 passed **"); selfTest3(); } else { @@ -112,29 +115,29 @@ public Action:selfTest2Terminate(Handle:timer) { return Plugin_Stop; } -selfTest3() { - goalsReached = 0; - trapsReached = 0; +void selfTest3() { + g_iGoalsReached = 0; + g_iTrapsReached = 0; CreateTimer(1.0, selfTest3Terminate); PrintToServer("* |socket selftest| test #3 running"); - decl Handle:socket[20]; + Socket socket[20]; - for (new count=1; count <= 20; count++) { - for (new i=0; i #include -public Plugin:myinfo = { +#pragma newdecls required +#pragma semicolon 1 + +public Plugin myinfo = { name = "socket extension sendto selftest", author = "Player", description = "basic functionality testing", @@ -13,101 +16,101 @@ public Plugin:myinfo = { url = "http://www.player.to/" }; -public OnPluginStart() { - SocketSetOption(INVALID_HANDLE, DebugMode, 1); +public void OnPluginStart() { + view_as(null).SetOption(DebugMode, 1); - new port = 12346; + int port = 12346; - new Handle:socket = SocketCreate(SOCKET_UDP, OnLSocketError); + Socket socket = new Socket(SOCKET_UDP, OnLSocketError); - SocketBind(socket, "0.0.0.0", port); - SocketListen(socket, OnLSocketIncoming); + socket.Bind("0.0.0.0", port); + socket.Listen(OnLSocketIncoming); - new Handle:socket2 = SocketCreate(SOCKET_UDP, OnCSocketError); + Socket socket2 = new Socket(SOCKET_UDP, OnCSocketError); //SocketConnect(socket2, OnCSocketConnect, OnCSocketReceive, OnCSocketDisconnect, "127.0.0.1", port); } -public OnLSocketIncoming(Handle:socket, Handle:newSocket, String:remoteIP[], remotePort, any:arg) { +public void OnLSocketIncoming(Socket socket, Socket newSocket, char[] remoteIP, int remotePort, any arg) { PrintToServer("%s:%d connected", remoteIP, remotePort); - SocketSetReceiveCallback(newSocket, OnChildSocketReceive); - SocketSetDisconnectCallback(newSocket, OnChildSocketDisconnect); - SocketSetErrorCallback(newSocket, OnChildSocketError); + newSocket.SetReceiveCallback(OnChildSocketReceive); + newSocket.SetDisconnectCallback(OnChildSocketDisconnect); + newSocket.SetErrorCallback(OnChildSocketError); - SocketSend(newSocket, "\x00abc\x00def\x01\x02\x03\x04", 12); - SocketSetSendqueueEmptyCallback(newSocket, OnChildSocketSQEmpty); + newSocket.Send("\x00abc\x00def\x01\x02\x03\x04", 12); + newSocket.SetSendqueueEmptyCallback(OnChildSocketSQEmpty); } -public OnLSocketError(Handle:socket, const errorType, const errorNum, any:arg) { +public void OnLSocketError(Socket socket, const int errorType, const int errorNum, any arg) { LogError("listen socket error %d (errno %d)", errorType, errorNum); CloseHandle(socket); } -public OnChildSocketReceive(Handle:socket, String:receiveData[], const dataSize, any:arg) { +public void OnChildSocketReceive(Socket socket, char[] receiveData, const int dataSize, any arg) { // send (echo) the received data back - //SocketSend(socket, receiveData); + //socket.Send(receiveData); // close the connection/socket/handle if it matches quit //if (strncmp(receiveData, "quit", 4) == 0) CloseHandle(socket); } -public OnChildSocketSQEmpty(Handle:socket, any:arg) { +public void OnChildSocketSQEmpty(Socket socket, any arg) { PrintToServer("sq empty"); CloseHandle(socket); } -public OnChildSocketDisconnect(Handle:socket, any:arg) { +public void OnChildSocketDisconnect(Socket socket, any arg) { // remote side disconnected PrintToServer("disc"); CloseHandle(socket); } -public OnChildSocketError(Handle:socket, const errorType, const errorNum, any:arg) { +public void OnChildSocketError(Socket socket, const int errorType, const int errorNum, any arg) { // a socket error occured LogError("child socket error %d (errno %d)", errorType, errorNum); CloseHandle(socket); } -public OnCSocketConnect(Handle:socket, any:arg) { +public void OnCSocketConnect(Socket socket, any arg) { // send (echo) the received data back - //SocketSend(socket, receiveData); + //socket.Send(receiveData); // close the connection/socket/handle if it matches quit //if (strncmp(receiveData, "quit", 4) == 0) CloseHandle(socket); } -new String:recvBuffer[128]; -new recvBufferPos = 0; +char g_sRecvBuffer[128]; +int g_irecvBufferPos = 0; -public OnCSocketReceive(Handle:socket, String:receiveData[], const dataSize, any:arg) { +public void OnCSocketReceive(Socket socket, char[] receiveData, const int dataSize, any arg) { PrintToServer("received %d bytes", dataSize); - if (recvBufferPos < 512) { - for (new i=0; i #include +#pragma newdecls required +#pragma semicolon 1 + #define PLUGIN_VERSION "1.0.0" -public Plugin:myinfo = +public Plugin myinfo = { name = "Socket Test", author = "Unknown", @@ -15,42 +17,42 @@ public Plugin:myinfo = url = "" } -public OnPluginStart() +public void OnPluginStart() { - SocketSetOption(INVALID_HANDLE,DebugMode,1); + view_as(null).SetOption(DebugMode,1); RegAdminCmd("sm_socket_test", CommandTest, ADMFLAG_CONVARS, "test"); } -public Action:CommandTest(client, args) +public Action CommandTest(int client, int args) { // create a new tcp socket - new Handle:socket = SocketCreate(SOCKET_TCP, OnSocketError); + Socket socket = new Socket(SOCKET_TCP, OnSocketError); // open a file handle for writing the result - //new Handle:hFile = OpenFile("dl.htm", "wb"); + //new Socket hFile = OpenFile("dl.htm", "wb"); // pass the file handle to the callbacks //SocketSetArg(socket, hFile); // connect the socket - SocketConnect(socket, OnSocketConnected, OnSocketReceive, OnSocketDisconnected, "www.sourcemod.net", 80); + socket.Connect(OnSocketConnected, OnSocketReceive, OnSocketDisconnected, "www.sourcemod.net", 80); return Plugin_Handled; } -public OnSocketConnected(Handle:socket, any:hFile) { +public void OnSocketConnected(Socket socket, any hFile) { // socket is connected, send the http request - decl String:requestStr[100]; + char requestStr[100]; Format(requestStr, sizeof(requestStr), "GET /%s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", "index.php", "www.sourcemod.net"); SocketSend(socket, requestStr); } -public OnSocketReceive(Handle:socket, String:receiveData[], const dataSize, any:hFile) { +public void OnSocketReceive(Socket socket, char[] receiveData, const int dataSize, any hFile) { // receive another chunk and write it to /dl.htm // we could strip the http response header here, but for example's sake we'll leave it in //WriteFileString(hFile, receiveData, false); } -public OnSocketDisconnected(Handle:socket, any:hFile) { +public void OnSocketDisconnected(Socket socket, any hFile) { // Connection: close advises the webserver to close the connection when the transfer is finished // we're done here @@ -58,7 +60,7 @@ public OnSocketDisconnected(Handle:socket, any:hFile) { CloseHandle(socket); } -public OnSocketError(Handle:socket, const errorType, const errorNum, any:hFile) { +public void OnSocketError(Socket socket, const int errorType, const int errorNum, any hFile) { // a socket error occured LogError("socket error %d (errno %d)", errorType, errorNum);