Raised This Month: $51 Target: $400
 12% 

Module: Threaded Sockets


Post New Thread Reply   
 
Thread Tools Display Modes
ALIK
New Member
Join Date: Nov 2015
Old 12-16-2015 , 14:14   Re: Module: Threaded Sockets
Reply With Quote #41

Quote:
Originally Posted by Shooting King View Post
Yes
What kind? Examples?
ALIK is offline
Send a message via Skype™ to ALIK
Shooting King
RAAASENGAN
Join Date: Mar 2012
Location: India
Old 12-17-2015 , 07:16   Re: Module: Threaded Sockets
Reply With Quote #42

Quote:
Originally Posted by Nixon133 View Post
Whether also there will be a continuation of the module?
Yes
Quote:
Originally Posted by Nixon133 View Post
Improvement, completion?
Yes, If it can be optimized in anyway which i overlooked, just open a pull req on Github.
__________________
As every time said, don't ever UNDERESTIMATE me.

Donate - Here
Shooting King is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 05-29-2016 , 12:37   Re: Module: Threaded Sockets
Reply With Quote #43

You're right. It's much more appropriate to continue the discussion here.

I previously had a fixed size first packet of 2048B. But I encountered problems where sometimes the HTTP header wouldn't fit and in other cases I managed to get 2 beginnings of chunks inside one "packet".

The solution was to get the header one byte at a time until the end was found and then the rest in packets of 64KB.

Also, if the transfer is chunked (streamed with no fixed size) you have to read the size of the chunk one byte at a time, download the chunk, preferably the number of bytes previously announced by the server and then read the next chunk size byte for byte again. Repeat until the size of the next chunk is 0.

So I need to change the number of bytes to receive on-the-fly.


Other than that I feel it should be possible to include threaded sockets. Not as a requirement, just as an option. Fully automatic of course, set_native_filter() is an amazing native for these things.

Here's the parts from my code:

Header:
Code:
while ( gBufferPos < sizeof gDataBuffer && socket_recv(gInformation[gIndex][_hSocket], gDataBuffer[gBufferPos], 2) ) { // Get one byte

Reading chunk size:
Code:
socket_recv(gInformation[gIndex][_hSocket], tempdata, 2); // Get one byte

Chunked, Large transfers (>2GB) and regular transfers:
Code:
    static stTempLarge[16];     new tempsize;         if ( gInformation[gIndex][_Status] & STATUS_CHUNKED_TRANSFER )         tempsize = min(gInformation[gIndex][_EndOfChunk] - gInformation[gIndex][_BytesReceived] + 1, sizeof gDataBuffer); // Get the remaining bytes of the chunk unless it's too big, then try to fill the buffer.     else if ( gInformation[gIndex][_Status] & STATUS_LARGE_SIZE ) {         large_add(stTempLarge, sizeof stTempLarge, gInformation[gIndex][_FilesizeLarge], sizeof gInformation[][_FilesizeLarge]);         large_sub(stTempLarge, sizeof stTempLarge, gInformation[gIndex][_BytesReceivedLarge], sizeof gInformation[][_BytesReceivedLarge]);         large_add(stTempLarge, sizeof stTempLarge, gOneLarge, sizeof gOneLarge);                 if ( large_comp(stTempLarge, sizeof stTempLarge, gBufferSizeLarge, sizeof gBufferSizeLarge) == 1 ) // Compares how much is remaining on the download and if it's bigger than the buffer, try to fill the buffer.             tempsize = sizeof gDataBuffer;         else             tempsize = large_toint(stTempLarge, sizeof stTempLarge); // Otherwise try getting whatever's left.     }     else         tempsize = min(gInformation[gIndex][_Filesize] - gInformation[gIndex][_BytesReceived] + 1, sizeof gDataBuffer); // Get the remaining bytes unless too much, then try to fill buffer.     gBufferPos = 0;     if ( ( gBufferLen = socket_recv(gInformation[gIndex][_hSocket], gDataBuffer, tempsize) ) <= 0 ) { // Execute.
__________________

Last edited by Black Rose; 05-29-2016 at 13:25.
Black Rose is offline
Shooting King
RAAASENGAN
Join Date: Mar 2012
Location: India
Old 05-30-2016 , 07:51   Re: Module: Threaded Sockets
Reply With Quote #44

I still dont understand whats the problem with native socket_recv_t( const iThreadHandle, const CallBackHandler[], const iRecvDataLen ); ._. You can change iRecvDataLen 1B or 10B or 10GB your wish.
__________________
As every time said, don't ever UNDERESTIMATE me.

Donate - Here
Shooting King is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 05-30-2016 , 13:54   Re: Module: Threaded Sockets
Reply With Quote #45

You call socket_recv_t 1 time.
The handler is called several times. At this point the datalen is set and cannot be changed. I need to parse the data sent by the server and react to that depending on the type of transfer. If the transfer is chunked I have to dynamically set the datalen to hit the end of the chunk perfectly.

en.wikipedia.org/wiki/Chunked_transfer_encoding#Example
__________________

Last edited by Black Rose; 05-30-2016 at 13:55.
Black Rose is offline
Shooting King
RAAASENGAN
Join Date: Mar 2012
Location: India
Old 05-30-2016 , 15:56   Re: Module: Threaded Sockets
Reply With Quote #46

Oh ! I get it now. Try v1.1.
__________________
As every time said, don't ever UNDERESTIMATE me.

Donate - Here
Shooting King is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 05-30-2016 , 16:29   Re: Module: Threaded Sockets
Reply With Quote #47

Perfect. I love the implementation. I'll get to work as soon as possible. Hopefully this week.
__________________
Black Rose is offline
tenub
Junior Member
Join Date: Aug 2013
Old 08-31-2016 , 11:16   Re: Module: Threaded Sockets
Reply With Quote #48

In the download example, what happens if multiple "/test" commands are executed at the same time since the thread handle is global? I ask because I am curious how to handle sending concurrent data to a Redis server, ie. when a server changes map and player info for up to 32 players is sent. How can we handle race conditions / locking / waiting? Sorry, I am new.
tenub is offline
Shooting King
RAAASENGAN
Join Date: Mar 2012
Location: India
Old 08-31-2016 , 12:04   Re: Module: Threaded Sockets
Reply With Quote #49

If the /test is executed multiple times(i.e before completion of file download), Multiple threads will be created overriding g_iThreadHandle with the same call backs, causing improper execution of callback functions with different handles, will not work as you have expected, ofcourse its not the way the API is intended to be used.

In this Module's Threading model, there are no bottlenecks, so no race conditions/locking. One unique handle per thread, One socket per thread.

Your possible models (to be used in your plugin),
Sequential Model -> Create a thread, sendData of player1, In the callback, sendData of player2, again in callback send data of player3,....
Or
Concurrent/Parallel Model -> Create 32 Threads (if you have enough system resources), Send data of one PLayer1 from Thread1, PLayer2 on Thread2, and so on...., but don't expect the order to be same on the other endpoint, like player2 Data may be received first or player3-player1-player2 or some other combination.

In Either case, There will be no race/locking in the Module.
__________________
As every time said, don't ever UNDERESTIMATE me.

Donate - Here
Shooting King is offline
tenub
Junior Member
Join Date: Aug 2013
Old 08-31-2016 , 14:40   Re: Module: Threaded Sockets
Reply With Quote #50

Quote:
Originally Posted by Shooting King View Post
Concurrent/Parallel Model -> Create 32 Threads (if you have enough system resources), Send data of one PLayer1 from Thread1, PLayer2 on Thread2, and so on...., but don't expect the order to be same on the other endpoint, like player2 Data may be received first or player3-player1-player2 or some other combination.

In Either case, There will be no race/locking in the Module.
I see now. For each possible connection I would need to create unique handles. One more question if you wouldn't mind. Consider hooking into player "say" events to deliver a payload to the same Redis server in order to broadcast in-game chat to the web. How would you go about this without throttling player chat, ie. forcing a player to only send 1 message every X seconds (even then there is no guarantee the thread will complete in time for the next message from the same player)? Consider also the worst case scenario of 32 players each sending a message every 200ms or so.

Last edited by tenub; 08-31-2016 at 14:41.
tenub is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 16:35.


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