AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Multi-Thread in sockets! Help please! (https://forums.alliedmods.net/showthread.php?t=159311)

johnally 06-15-2011 13:22

Multi-Thread in sockets! Help please!
 
Hey guys, you've been a lot of help to me so far. Thanks to you all.

I was using the webservers and sockets example but noticed that whenever the socket would be listening or waiting for some response, the whole server went gaga(freezed until response received.)

My solution was to try get receiving of data happens in another thread. But how to do that? I did some research and it only came with 'SQLx multi threading'.

Anyone can help me out or propose a better way?.... BIG thanks!

DjOptimuS 06-15-2011 20:19

Re: Multi-Thread in sockets! Help please!
 
would be very usefull to create a Threaded Sockets Module.

Too bad that i don't have the necesary knowledge to acomplish that.

johnally 06-16-2011 10:33

Re: Multi-Thread in sockets! Help please!
 
@DjOptimuS me too..

Well .. I hack'd my way through.. and guess that the most appropriate thing is "not to use socket at all" or "use set_task and make sure data received via sockets ain't event dependent ><" ..
I think sXe-injected use set_task on clientputinserver :) I'll do same!!

If still, someones found a better way.. let us know.

ARES[ro] 06-16-2011 16:49

Re: Multi-Thread in sockets! Help please!
 
socket_hz Tutorial by Hackinzer

DjOptimuS 06-16-2011 22:33

Re: Multi-Thread in sockets! Help please!
 
Sockets_hz helps on listening, but it blocks the server too. I already tested it.

The function socket_unblock isn't working propely, and, besides, you read the code samples ? You must set an infinite loop task, from short time to short time, like 0.3 seconds, or 0.1 on the sample you gave. Isn't ok.

I use sockets to call a HLTV process, using a HLTV code sample finded here on the forums, i adapted the code to Sockets_HZ, but, because of the set task, sometimes i didn't received the challenge id from the HLTV, so Sockets_HZ isn't a solution, a Threaded Sockets Module IS the solution.

ARES[ro] 06-16-2011 23:41

Re: Multi-Thread in sockets! Help please!
 
Well, take a look at Hackinzer's mIRC server!

DjOptimuS 06-17-2011 01:02

Re: Multi-Thread in sockets! Help please!
 
i see that you are from romania, i am from romania too, and i used to work for the biggest hosting provider from romania, and i know that almost all of the cs servers owner, don't understand what socket mean (at least from romania).

If you have the server hosted in Romania, for example a WAR server, and you want to use HLTV on it, but you have the HLTV located in the USA for example, be sure that the HLTV calling functions will hang the server. If you don't know what do you say, don't say it.

johnally 06-17-2011 11:04

Re: Multi-Thread in sockets! Help please!
 
Yep.. I have the socket_hz method commented in my codings :(.. I tried it and still block server and sometimes would require bigger timeout to see changes.

I inspired myself from sXe-injected:
PHP Code:

public client_putinserver(id
{
        
connect_web(id); // The Auth process starts here! <<<<<<---------<<<<<<<
}

public 
send_and_read(idconstring[512], ctype//Where data sent on socket! ctype = 1 or 0 .. 1 means it is client_auth.. and 0.. normal blocking socket reading!
{
    new 
parames[2]
    
parames[0] = id
    write_web
(constring)  // socket_send :)
    //socket_close(g_sckweb) // socket_hz function
    
if (ctype == 1) {
        
parames[1] = // timeout value
        
set_task(2.0"read_web"202parames2"b"// repeated infinite call!
    
} else {
        
parames[1] = 10000000 // timeout value
        
read_web(parames// normal call!
    
}


connect_web function has same structure as in webservers and sockets example except I used the function above to combine "send and read"..
parames[1] holds timeout value for socket_change in read_web function.
set_task is set as "b" infinite.

I use the "remove_task(202)" in the if statement (socket_change)..

As such users can be authenticated and not cause server to freeze.. Hail to sXe-injected! :)

The main change was:
PHP Code:

if (socket_change(g_sckwebparames[1])){
      
remove_task(202//Remove the infinite task!
      // Do something, socket changed :D
} else {
      
// No change in socket!


socket_change made server wait for too long with higher timeouts. Setting a task at a 1-5s interval and a timeout of 1, would ensure that repetitive regular checking of socket would be done while server is not forced to wait for the change to happen. :)

in other words, socket_change(socket, timeout) will make the server waits for a change in socket until it reaches timeout or return true for an actual change!


Cheers,

DjOptimuS 06-17-2011 12:04

Re: Multi-Thread in sockets! Help please!
 
sometimes the sockets returns nothing just because the standard infinite loop is too fast, for example 0.1 or 0.2 set_task to listen the socket.

The Sockets_HZ ideea is good, but only if the socket you need to call is relatively close to your server, or maybe on the same server, but in this case, the standard socket will work, because everything is fast. But the problem appears when you need to call something via socket that have ping over 40-50 ms between them

johnally 06-17-2011 12:13

Re: Multi-Thread in sockets! Help please!
 
Yes.. the issue comes if it's over 40-50ms latency between the two servers :(


All times are GMT -4. The time now is 23:24.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.