Update syntax

This commit is contained in:
JoinedSenses
2019-11-07 14:47:40 -05:00
parent 5ef5451874
commit a7744212d2
5 changed files with 193 additions and 179 deletions
+15 -12
View File
@@ -3,7 +3,10 @@
#include <sourcemod>
#include <socket>
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 <modfolder>/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);
+17 -14
View File
@@ -3,7 +3,10 @@
#include <sourcemod>
#include <socket>
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<Socket>(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);
+107 -104
View File
@@ -5,7 +5,10 @@
#include <sourcemod>
#include <socket>
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<Socket>(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<count; i++) {
socket[i] = SocketCreate(SOCKET_TCP, OnSocketErrorTrap);
for (int count = 1; count <= 20; count++) {
for (int i = 0; i < count; i++) {
socket[i] = new Socket(SOCKET_TCP, OnSocketErrorTrap);
}
for (new i=0; i<count; i++) {
for (int i = 0; i < count; i++) {
CloseHandle(socket[i]);
}
}
goalsReached++;
g_iGoalsReached++;
}
public Action:selfTest3Terminate(Handle:timer) {
if (goalsReached == 1 && trapsReached == 0) {
public Action selfTest3Terminate(Handle timer) {
if (g_iGoalsReached == 1 && g_iTrapsReached == 0) {
PrintToServer("* |socket selftest| ** test #3 passed **");
selfTest4();
} else {
@@ -152,97 +155,97 @@ public Action:selfTest3Terminate(Handle:timer) {
* - connect
* - listen
*/
selfTest4() {
goalsReached = 0;
trapsReached = 0;
void selfTest4() {
g_iGoalsReached = 0;
g_iTrapsReached = 0;
CreateTimer(5.0, selfTest4Terminate);
PrintToServer("* |socket selftest| test #4 running");
new Handle:listenSocket = SocketCreate(SOCKET_TCP, OnSocketErrorTrap);
Socket listenSocket = new Socket(SOCKET_TCP, OnSocketErrorTrap);
SocketSetOption(listenSocket, SocketReuseAddr, 1);
listenSocket.SetOption(SocketReuseAddr, 1);
SocketBind(listenSocket, "0.0.0.0", 12345);
SocketListen(listenSocket, Test3_OnListenSocketIncoming);
listenSocket.Bind("0.0.0.0", 12345);
listenSocket.Listen(Test3_OnListenSocketIncoming);
decl Handle:socket[8];
Socket socket[8];
for (new count=1; count <= 8; count++) {
for (new i=0; i<count; i++) {
socket[i] = SocketCreate(SOCKET_TCP, OnSocketErrorTrap);
for (int count = 1; count <= 8; count++) {
for (int i = 0; i < count; i++) {
socket[i] = new Socket(SOCKET_TCP, OnSocketErrorTrap);
}
for (new i=0; i<count; i++) {
SocketConnect(socket[i], OnSocketConnectGoal, OnSocketReceiveTrap, OnSocketDisconnectGoal, "127.0.0.1", 12345);
for (int i = 0; i < count; i++) {
socket[i].Connect(OnSocketConnectGoal, OnSocketReceiveTrap, OnSocketDisconnectGoal, "127.0.0.1", 12345);
}
}
CloseHandle(listenSocket);
goalsReached++;
g_iGoalsReached++;
}
public Action:selfTest4Terminate(Handle:timer) {
if (goalsReached == 109 && trapsReached == 0) {
public Action selfTest4Terminate(Handle timer) {
if (g_iGoalsReached == 109 && g_iTrapsReached == 0) {
PrintToServer("* |socket selftest| ** test #4 passed **");
selfTest5();
} else {
PrintToServer("* |socket selftest| ** test #4 failed ** (%d goals of 109)", goalsReached);
PrintToServer("* |socket selftest| ** test #4 failed ** (%d goals of 109)", g_iGoalsReached);
}
return Plugin_Stop;
}
selfTest5() {
void selfTest5() {
}
// ------------------------------------- TEST 1 callbacks -------------------------------------
public Test1_OnListenSocketIncoming(Handle:socket, Handle:newSocket, String:remoteIP[], remotePort, any:arg) {
SocketSetReceiveCallback(newSocket, OnSocketReceiveTrap);
SocketSetDisconnectCallback(newSocket, OnSocketDisconnectTrap);
SocketSetErrorCallback(newSocket, OnSocketErrorTrap);
public void Test1_OnListenSocketIncoming(Socket socket, Socket newSocket, char[] remoteIP, int remotePort, any arg) {
newSocket.SetReceiveCallback(OnSocketReceiveTrap);
newSocket.SetDisconnectCallback(OnSocketDisconnectTrap);
newSocket.SetErrorCallback(OnSocketErrorTrap);
SocketSend(newSocket, "\x00abc\x00def\x01\x02\x03\x04", 12);
SocketSetSendqueueEmptyCallback(newSocket, Test1_OnChildSocketSQEmpty);
newSocket.Send("\x00abc\x00def\x01\x02\x03\x04", 12);
newSocket.SetSendqueueEmptyCallback(Test1_OnChildSocketSQEmpty);
PrintToServer("* |socket selftest| goal Test1_OnListenSocketIncoming reached");
goalsReached++;
g_iGoalsReached++;
// close listening socket
CloseHandle(socket);
PrintToServer("* |socket selftest| goal Test1_OnListenSocketIncoming - CloseHandle reached");
goalsReached++;
g_iGoalsReached++;
}
public Test1_OnChildSocketSQEmpty(Handle:socket, any:arg) {
public void Test1_OnChildSocketSQEmpty(Socket socket, any arg) {
CloseHandle(socket);
PrintToServer("* |socket selftest| goal Test1_OnChildSocketSQEmpty reached");
goalsReached++;
g_iGoalsReached++;
}
new String:recvBuffer[128];
new recvBufferPos = 0;
char g_sRecvBuffer[128];
int g_iRecvBufferPos = 0;
public Test1_OnReceiveSocketReceive(Handle:socket, String:receiveData[], const dataSize, any:arg) {
public void Test1_OnReceiveSocketReceive(Socket socket, char[] receiveData, const int dataSize, any arg) {
PrintToServer("received %d bytes", dataSize);
if (recvBufferPos < 512) {
for (new i=0; i<dataSize && recvBufferPos<sizeof(recvBuffer); i++, recvBufferPos++) {
recvBuffer[recvBufferPos] = receiveData[i];
if (g_iRecvBufferPos < 512) {
for (int i = 0; i < dataSize && g_iRecvBufferPos < sizeof(g_sRecvBuffer); i++, g_iRecvBufferPos++) {
g_sRecvBuffer[g_iRecvBufferPos] = receiveData[i];
}
}
}
public Test1_OnReceiveSocketDisconnect(Handle:socket, any:arg) {
public void Test1_OnReceiveSocketDisconnect(Socket socket, any arg) {
PrintToServer("* |socket selftest| goal Test1_OnReceiveSocketDisconnect reached");
goalsReached++;
g_iGoalsReached++;
new String:cmp[] = "\x00abc\x00def\x01\x02\x03\x04";
new i;
for (i=0; i<recvBufferPos && i<12; i++) {
if (recvBuffer[i] != cmp[i]) {
char cmp[] = "\x00abc\x00def\x01\x02\x03\x04";
int i;
for (i = 0; i < g_iRecvBufferPos && i < 12; i++) {
if (g_sRecvBuffer[i] != cmp[i]) {
PrintToServer("comparison failed");
break;
}
@@ -252,57 +255,57 @@ public Test1_OnReceiveSocketDisconnect(Handle:socket, any:arg) {
if (i == 12) {
PrintToServer("* |socket selftest| goal Test1_OnReceiveSocketDisconnect - i=12 reached");
goalsReached++;
g_iGoalsReached++;
} else {
PrintToServer("* |socket selftest| trap Test1_OnReceiveSocketDisconnect - i=%d!=12 triggered", i);
trapsReached++;
g_iTrapsReached++;
}
}
// ------------------------------------- TEST 3 callbacks -------------------------------------
public Test3_OnListenSocketIncoming(Handle:socket, Handle:newSocket, String:remoteIP[], remotePort, any:arg) {
public void Test3_OnListenSocketIncoming(Socket socket, Socket newSocket, char[] remoteIP, int remotePort, any arg) {
CloseHandle(newSocket);
goalsReached++;
g_iGoalsReached++;
}
// ------------------------------------- common callbacks -------------------------------------
public OnSocketConnectGoal(Handle:socket, any:arg) {
goalsReached++;
public void OnSocketConnectGoal(Socket socket, any arg) {
g_iGoalsReached++;
}
public OnSocketReceiveGoal(Handle:socket, String:receiveData[], const dataSize, any:arg) {
goalsReached++;
public void OnSocketReceiveGoal(Socket socket, char[] receiveData, const int dataSize, any arg) {
g_iGoalsReached++;
}
public OnSocketDisconnectGoal(Handle:socket, any:arg) {
goalsReached++;
public void OnSocketDisconnectGoal(Socket socket, any arg) {
g_iGoalsReached++;
}
public OnSocketErrorGoal(Handle:socket, const errorType, const errorNum, any:arg) {
goalsReached++;
public void OnSocketErrorGoal(Socket socket, const int errorType, const int errorNum, any arg) {
g_iGoalsReached++;
}
public OnSocketIncomingGoal(Handle:socket, Handle:newSocket, String:remoteIP[], remotePort, any:arg) {
goalsReached++;
public void OnSocketIncomingGoal(Socket socket, Socket newSocket, char[] remoteIP, int remotePort, any arg) {
g_iGoalsReached++;
}
public OnSocketConnectTrap(Handle:socket, any:arg) {
public void OnSocketConnectTrap(Socket socket, any arg) {
PrintToServer("* |socket selftest| trap OnSocketConnectTrap triggered");
trapsReached++;
g_iTrapsReached++;
}
public OnSocketReceiveTrap(Handle:socket, String:receiveData[], const dataSize, any:arg) {
public void OnSocketReceiveTrap(Socket socket, char[] receiveData, const int dataSize, any arg) {
PrintToServer("* |socket selftest| trap OnSocketReceiveTrap triggered (%d bytes received)", dataSize);
trapsReached++;
g_iTrapsReached++;
}
public OnSocketDisconnectTrap(Handle:socket, any:arg) {
public void OnSocketDisconnectTrap(Socket socket, any arg) {
PrintToServer("* |socket selftest| trap OnSocketDisconnectTrap triggered");
trapsReached++;
g_iTrapsReached++;
}
public OnSocketErrorTrap(Handle:socket, const errorType, const errorNum, any:arg) {
public void OnSocketErrorTrap(Socket socket, const int errorType, const int errorNum, any arg) {
PrintToServer("* |socket selftest| trap OnSocketErrorTrap triggered (error %d, errno %d)", errorType, errorNum);
trapsReached++;
g_iTrapsReached++;
}
public OnSocketIncomingTrap(Handle:socket, Handle:newSocket, String:remoteIP[], remotePort, any:arg) {
public void OnSocketIncomingTrap(Socket socket, Socket newSocket, char[] remoteIP, int remotePort, any arg) {
PrintToServer("* |socket selftest| trap OnSocketIncomingTrap triggered");
trapsReached++;
g_iTrapsReached++;
}
// EOF
+39 -36
View File
@@ -5,7 +5,10 @@
#include <sourcemod>
#include <socket>
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<Socket>(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<dataSize && recvBufferPos<sizeof(recvBuffer); i++, recvBufferPos++) {
recvBuffer[recvBufferPos] = receiveData[i];
if (g_irecvBufferPos < 512) {
for (int i = 0; i < dataSize && g_irecvBufferPos < sizeof(g_sRecvBuffer); i++, g_irecvBufferPos++) {
g_sRecvBuffer[g_irecvBufferPos] = receiveData[i];
}
}
// 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 OnCSocketDisconnect(Handle:socket, any:arg) {
new String:cmp[] = "\x00abc\x00def\x01\x02\x03\x04";
new i;
for (i=0; i<recvBufferPos && i<12; i++) {
if (recvBuffer[i] != cmp[i]) {
public void OnCSocketDisconnect(Socket socket, any arg) {
char[] cmp = "\x00abc\x00def\x01\x02\x03\x04";
int i;
for (i = 0; i < g_irecvBufferPos && i < 12; i++) {
if (g_sRecvBuffer[i] != cmp[i]) {
PrintToServer("comparison failed");
break;
}
}
PrintToServer("comparison finished at pos %d/%d", i, recvBufferPos);
PrintToServer("comparison finished at pos %d/%d", i, g_irecvBufferPos);
CloseHandle(socket);
}
public OnCSocketError(Handle:socket, const errorType, const errorNum, any:arg) {
public void OnCSocketError(Socket socket, const int errorType, const int errorNum, any arg) {
// a socket error occured
LogError("connect socket error %d (errno %d)", errorType, errorNum);
+15 -13
View File
@@ -1,12 +1,14 @@
// http://forums.alliedmods.net/showpost.php?p=717597&postcount=67
#pragma semicolon 1
#include <sourcemod>
#include <socket>
#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<Socket>(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 <modfolder>/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);