AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Module Coding (https://forums.alliedmods.net/forumdisplay.php?f=9)
-   -   Module: Threaded Sockets (https://forums.alliedmods.net/showthread.php?t=262924)

joropito 06-01-2015 08:24

Re: Module: Threaded Sockets
 
I was checking your code and I have some doubts.

I didn't spent too much time but I can't find where ThreadMain is called from main module.
If I understand correctly, you tried to do the same as on mysql module and other modules you can found. In that case you manage a list of sockets to be polled at each frame or whatever timeframe you want. I can't find that.

Another question is if you have tried your calls to listent/recv/etc on udp sockets. They work slightly different and could broke how you check for incoming data.

Shooting King 06-01-2015 11:12

Re: Module: Threaded Sockets
 
Quote:

Originally Posted by joropito (Post 2303554)
I didn't spent too much time but I can't find where ThreadMain is called from main module.
If I understand correctly, you tried to do the same as on mysql module and other modules you can found. In that case you manage a list of sockets to be polled at each frame or whatever timeframe you want. I can't find that.

When a plugin calls socket_create_t() native, a Thread is created and its Entry function, TheadMain, is attached to that thread. At this moment thread state is set to STATE_IDLE (through the constructor of CThreadedSocket). So in this state, as you can see see from the main post, the Thread does nothing except checking for change in ThreadState.

I don't know exactly how Sqlite threads work, but when i looked at them, they implemented SourceMod Threads, which are not that useful(too complex) in case of Sockets. As you can see, there are no Mutex's no Semaphores etc, etc in this module. I think my Model of implementation is itself synchronous.

Quote:

Originally Posted by joropito (Post 2303554)
Another question is if you have tried your calls to listent/recv/etc on udp sockets. They work slightly different and could broke how you check for incoming data.

Example :D ? Although i will be implementing SendTo and RecvFrom functions in this module as per Destro's request.

Thanks for your time reviewing this shit :d

joropito 06-01-2015 13:40

Re: Module: Threaded Sockets
 
So you have a standalone thread looping for socket handling?

I'm not sure if calling forwards it's safe from another thread. I haven't checked but I guess isn't safe at least at pawn vm. 99% of tests could work, but you may hit some problem.

Shooting King 06-02-2015 04:06

Re: Module: Threaded Sockets
 
Quote:

Originally Posted by joropito (Post 2303676)
So you have a standalone thread looping for socket handling?

Yes. One thread per socket.

Quote:

Originally Posted by joropito (Post 2303676)
I'm not sure if calling forwards it's safe from another thread. I haven't checked but I guess isn't safe at least at pawn vm. 99% of tests could work, but you may hit some problem.

i guess there should't be such a problem. Did you experience such a thing ?

joropito 06-02-2015 16:31

Re: Module: Threaded Sockets
 
Check yourself

https://wiki.alliedmods.net/Pawn_tutorial

Quote:

No thread-safety
Pawn is targetted toward single-thread instances.
My recomendation is to keep things safe.

What you can do is to have an ipc buffer to communicate between main thread and each child thread so you can keep your thread working like now but forwards should be called from main thread.

Shooting King 06-03-2015 10:33

Re: Module: Threaded Sockets
 
If we make such a buffer, when and how do we check for a change in such buffer in main thread ? Do note a point that we are talking about 1% of all cases.

joropito 06-03-2015 11:10

Re: Module: Threaded Sockets
 
Quote:

Originally Posted by Shooting King (Post 2304338)
If we make such a buffer, when and how do we check for a change in such buffer in main thread ? Do note a point that we are talking about 1% of all cases.

StartFrame() is a good start.
Check dlls/mysqlx/threading.cpp on amxmodx source code.

Shooting King 06-03-2015 13:39

Re: Module: Threaded Sockets
 
Quote:

Originally Posted by joropito (Post 2304349)
StartFrame() is a good start.
Check dlls/mysqlx/threading.cpp on amxmodx source code.

Then we will be using much resources just for checking Buffer :| StartFrame is called too many time per second. Any other good alternative ? I really don't want to depend on engine/game functions.

joropito 06-03-2015 14:30

Re: Module: Threaded Sockets
 
You're doing modules, not plugins. On plugins it's expensive in time because stack and calls.

A lot of modules uses StartFrame, it not too expensive in this case.

You have to do in game loop so it should be done in some game call.
If you don't like to use directly StartFrame, do in 1/10 calls of it.
Also you can create a dummy entity with custom Think time where you check for available data, but you will spent 1 entity from your limited max ent number...

Javivi 06-09-2015 08:43

Re: Module: Threaded Sockets
 
Nice job. I was working on an update but this one is way better, so be sure to push it to amxmodx's github.

Feel free to take any code you want to improve your module, as I've seen that you didn't change much of the default code.

https://github.com/Javivi/amxmodx/co...sockets-update

Biggest changes are:
Code:

- WinSock version changed from 1.1 to 2.2.
- Properly check for WinSock initialization on OnAmxxAttach/Detach.
- Now, natives will not be added if we can't start up WinSock.
- socket_open is now IP version agnostic (both IPv4 and IPv6 are
supported).
- Error reporting has been changed on socket_open, now it returns the
actual libc errors instead of the previous made-up errors.
- socket_close now returns a value on success/failure.
- Code rewritten to be more readable, it should be self-explaining now.



All times are GMT -4. The time now is 20:26.

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