moved udp connection socket and fixed invalid handle probably

This commit is contained in:
jenzur 2020-09-23 11:48:11 +02:00
parent 3afffae783
commit 3b7bf8d3f0
2 changed files with 37 additions and 30 deletions

View File

@ -53,12 +53,9 @@ public void OnPluginStart()
HookEntityOutput("trigger_teleport", "OnTrigger", trigger_teleport); HookEntityOutput("trigger_teleport", "OnTrigger", trigger_teleport);
HookEntityOutput("trigger_teleport", "OnStartTouch", trigger_teleport); HookEntityOutput("trigger_teleport", "OnStartTouch", trigger_teleport);
//socket otherwise declare in public OnConfigsExecuted(){} //UDP connection
Handle socket = SocketCreate(SOCKET_UDP, OnSocketError); connect_socket();
SocketSetOption(socket, SocketReuseAddr, 1);
SocketBind(socket, "127.0.0.1", 48475);
connect(socket);
global_socket = socket;
reset_target_human_tp_coord(); reset_target_human_tp_coord();
targethuman = 0; targethuman = 0;
} }
@ -319,7 +316,7 @@ public Action recursive_pressing(Handle timer, any data)
if (targeteam == 3) if (targeteam == 3)
{ {
float min_distance_target_human = 12800.0; float min_distance_target_human = 12800.0;
float min_enemy_distance = min_distance_target_human * 8; float min_enemy_distance = min_distance_target_human * 15;
if (0 < dist_target <= min_distance_target_human && enemy_distance > min_enemy_distance) if (0 < dist_target <= min_distance_target_human && enemy_distance > min_enemy_distance)
{ {
chasing_enemy = true; chasing_enemy = true;
@ -339,8 +336,8 @@ public Action recursive_pressing(Handle timer, any data)
//zombie aiming for human //zombie aiming for human
float present_bot_coords[3]; float present_bot_coords[3];
GetClientAbsOrigin(present, present_bot_coords); GetClientAbsOrigin(present, present_bot_coords);
z_axis = FloatAbs(Pow(present_bot_coords[2] - pos[2], 5.0)); z_axis = FloatAbs(Pow(present_bot_coords[2] - pos[2], 2.0));
float z_axis_cap = 10.0; float z_axis_cap = 20.0;
if ((targeteam == 2 && 0 < enemy_distance && z_axis < z_axis_cap) || dist_target == -1.0) if ((targeteam == 2 && 0 < enemy_distance && z_axis < z_axis_cap) || dist_target == -1.0)
{ {
chasing_enemy = true; chasing_enemy = true;
@ -685,14 +682,21 @@ public void OnClientPostAdminCheck(int client)
public OnSocketError(Handle socket, const int errorType, const int errorNum, any args) public OnSocketError(Handle socket, const int errorType, const int errorNum, any args)
{ {
CloseHandle(socket);
LogError("[MR] Socket error: %d (errno %d)", errorType, errorNum); LogError("[MR] Socket error: %d (errno %d)", errorType, errorNum);
CreateTimer(120.0, TimerConnect, socket, TIMER_HNDL_CLOSE); CreateTimer(10.0, TimerConnect, INVALID_HANDLE, TIMER_HNDL_CLOSE);
} }
stock void connect(Handle socket) stock void connect_socket()
{ {
if (!SocketIsConnected(socket)) if (global_socket == INVALID_HANDLE || !SocketIsConnected(global_socket))
SocketConnect(socket, OnSocketConnected, OnSocketReceive, OnSocketDisconnected, "127.0.0.1", 48475); {
//socket otherwise declare in public OnConfigsExecuted(){}
global_socket = SocketCreate(SOCKET_UDP, OnSocketError);
SocketSetOption(global_socket, SocketReuseAddr, 1);
SocketBind(global_socket, "127.0.0.1", 48475);
SocketConnect(global_socket, OnSocketConnected, OnSocketReceive, OnSocketDisconnected, "127.0.0.1", 48475);
}
} }
public void OnClientDisconnect(int client) public void OnClientDisconnect(int client)
@ -741,12 +745,12 @@ public OnSocketReceive(Handle socket, char[] receiveData, const dataSize, any hF
public OnSocketDisconnected(Handle socket, any arg) public OnSocketDisconnected(Handle socket, any arg)
{ {
CreateTimer(120.0, TimerConnect, socket, TIMER_HNDL_CLOSE); CreateTimer(10.0, TimerConnect, INVALID_HANDLE, TIMER_HNDL_CLOSE);
} }
public Action TimerConnect(Handle timer, any arg) public Action TimerConnect(Handle timer, any arg)
{ {
connect(arg); connect_socket();
return Plugin_Handled; return Plugin_Handled;
} }

View File

@ -745,6 +745,8 @@ public void SQL_SelectTop_Callback(Handle db, Handle results, const char[] error
} }
Menu menu = new Menu(MenuHandler1); Menu menu = new Menu(MenuHandler1);
menu.SetTitle("Maptimer: %s", g_cMapname); menu.SetTitle("Maptimer: %s", g_cMapname);
if (results != INVALID_HANDLE)
{
while (SQL_GetRowCount(results) > 0 && SQL_FetchRow(results)) while (SQL_GetRowCount(results) > 0 && SQL_FetchRow(results))
{ {
l_iPosition++; l_iPosition++;
@ -761,6 +763,7 @@ public void SQL_SelectTop_Callback(Handle db, Handle results, const char[] error
} }
menu.ExitButton = true; menu.ExitButton = true;
menu.Display(iclient, 0); menu.Display(iclient, 0);
}
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
// Purpose: // Purpose: