Problem with ReceiveData

Discuss any general programming issues here
User avatar
airsoftsoftwair
Posts: 3221
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Problem with ReceiveData

Post by airsoftsoftwair » Tue Apr 30, 2019 8:59 pm

msu wrote:
Mon Apr 22, 2019 9:23 pm
Here's an example:

Server:

Code: Select all

CreateServer(1,2900)
Function p_ConnectFunc(msg)
	 Switch(msg.action)
		Case "OnDisconnect":
			CloseConnection(msg.id)
 		Case "OnConnect":
  			SendData(msg.clientid,"Hallo Klient")
 	EndSwitch
EndFunction

InstallEventHandler({OnConnect = p_ConnectFunc, OnDisconnect = p_ConnectFunc})
Repeat
CheckEvent
Forever

Client:

Code: Select all

OpenConnection(1, "localhost", 2900) 
a$ = ReceiveData(1, #RECEIVEBYTES, 200)
NPrint(a$)
WaitLeftMouse
The client displays the message after 10 seconds (NetworkTimeout default). :(

Exactly the same behavior as with the database server. :(
Thanks for posting those code snippets. I can confirm the behaviour here. That's not how it should behave. I'll fix this so that #RECEIVEBYTES returns immediately as soon as there is no more data, as documented.

User avatar
msu
Posts: 44
Joined: Mon Jun 13, 2016 11:36 am
Location: Sinzig/Germany

Re: Problem with ReceiveData

Post by msu » Tue Apr 30, 2019 9:26 pm

Thank you!

User avatar
airsoftsoftwair
Posts: 3221
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Problem with ReceiveData

Post by airsoftsoftwair » Mon Jun 03, 2019 4:54 pm

I've taken a closer look now what's going on under the hood here and I can confirm that #RECEIVEBYTES indeed will always block until the timeout has occurred if there's not enough data in the queue.

However, you might get what you want by using #RECEIVEALL and setting its untilterm argument to FALSE. This should be a way to receive everything that is currently in the queue without any blocking, e.g. do the following:

Code: Select all

a$, len = ReceiveData(1, #RECEIVEALL, False)
; now check "len" to see how many bytes you've got and call #RECEIVEALL again if you need more data
Does that solve the problem now?

User avatar
msu
Posts: 44
Joined: Mon Jun 13, 2016 11:36 am
Location: Sinzig/Germany

Re: Problem with ReceiveData

Post by msu » Tue Jun 04, 2019 2:18 pm

Hi Andreas,
Meanwhile, I solved the problem. :D
It does not matter if you use #RECEIVEBYTES or #RECEIVEALL (untillterm = TRUE), both works.

This code here is an absolute performance killer for large amounts of data:

Code: Select all

part$, c = ReceiveData(ConnectionID, #RECEIVEBYTES, 5)
		Answer$ = Answer$ .. part$
The larger the string variable, the slower the program becomes. :o

Using a Table is much more efficient.
But you should not use the table library, it is just as slow as big string variables!

Here's a working example for other Hollywood programmers.

The server sends messages in the following format: Length of the message followed by the message.

Code: Select all

;SERVER
CreateServer(1,2900)
Function p_ConnectFunc(msg)
	Switch(msg.action)
		Case "OnDisconnect":
			CloseConnection(msg.id)
 		Case "OnConnect":
			For a = 0 To 100000
				SendData(msg.clientid,"5Hello")
			Next
			SendData(msg.clientid,"3end")
 	EndSwitch
EndFunction

InstallEventHandler({OnConnect = p_ConnectFunc, OnDisconnect = p_ConnectFunc})
Repeat
CheckEvent
Forever
This client uses #RECEIVEBYTES.

Code: Select all

;CLIENT 1

OpenConnection(1, "localhost", 2900) 
b$ = {}
c = 0

StartTimer(1)
Repeat
	a$ = ReceiveData(1, #RECEIVEBYTES, 1)
	a$ = ReceiveData(1, #RECEIVEBYTES, ToNumber(a$))
	b$[c] = a$
	c = c + 1
Until a$ = "end" 

DebugPrint("Needed time: ", GetTimer(1))
DebugPrint("Number of received messages: ", TableItems(b$))
This client uses #RECEIVEALL with untillterm.

Code: Select all

;CLIENT 2

OpenConnection(1, "localhost", 2900) 
c$ = {}

Function p_Receive()
	Repeat
		a$, len = ReceiveData(1, #RECEIVEALL, False)
		b$ = b$ .. a$

		While StrLen(b$) <> 0
			Size = ToNumber(LeftStr(b$, 1))
			If StrLen(b$) >= Size + 1
				b$ = UnrightStr(b$, 1)
				c$[c] = LeftStr(b$, Size)
				If c$[c] = "end" Then e = 1
				b$ = UnrightStr(b$, Size)
				c = c + 1
			EndIf	
		Wend
	Until len = 0
EndFunction

InstallEventHandler({OnReceiveData = p_Receive})
StartTimer(1)
While e = 0
	CheckEvent
Wend
DebugPrint("Needed time: ", GetTimer(1))
DebugPrint("Number of received messages: ", TableItems(c$))
End
Client 2 with #RECEIVEALL is slightly faster.
However, it only works reliably using Network Events.

Greetings, Michael
Last edited by msu on Tue Jun 04, 2019 2:31 pm, edited 1 time in total.

User avatar
Clyde
Posts: 198
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

Re: Problem with ReceiveData

Post by Clyde » Tue Jun 04, 2019 2:27 pm

Nice, that you sorted it out! Thanks for sharing your code.

But, what do you mean by "spreadsheet" and "table library"? Thanks!
Currently using: Hollywood 8 with Windows IDE

User avatar
msu
Posts: 44
Joined: Mon Jun 13, 2016 11:36 am
Location: Sinzig/Germany

Re: Problem with ReceiveData

Post by msu » Tue Jun 04, 2019 2:32 pm

Clyde wrote:
Tue Jun 04, 2019 2:27 pm
But, what do you mean by "spreadsheet" and "table library"? Thanks!
Thank you, have corrected that.

User avatar
Clyde
Posts: 198
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

Re: Problem with ReceiveData

Post by Clyde » Tue Jun 04, 2019 2:37 pm

Thanks!

But I still don't know what "the table library" is!? Sorry, if I am too dumb to understand or unexperienced in Hollywood. :-( I know the {} tables but I don't that there is a table library (built in to Hollywood?).

Thanks for your patience! :-)
Currently using: Hollywood 8 with Windows IDE

User avatar
msu
Posts: 44
Joined: Mon Jun 13, 2016 11:36 am
Location: Sinzig/Germany

Re: Problem with ReceiveData

Post by msu » Tue Jun 04, 2019 2:41 pm

Clyde wrote:
Tue Jun 04, 2019 2:37 pm
But I still don't know what "the table library" is!? Sorry,
https://www.hollywood-mal.com/docs/html ... tml#active

User avatar
Clyde
Posts: 198
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

Re: Problem with ReceiveData

Post by Clyde » Tue Jun 04, 2019 2:58 pm

Alright, I see, thanks. I know these functions for manipulation a table (but wasn't aware of the term "library"). So you mean you should use a table as a data structure but don't use functions to manipulate the table in order to get the speed gain, right!?
Currently using: Hollywood 8 with Windows IDE

User avatar
msu
Posts: 44
Joined: Mon Jun 13, 2016 11:36 am
Location: Sinzig/Germany

Re: Problem with ReceiveData

Post by msu » Tue Jun 04, 2019 3:15 pm

Clyde wrote:
Tue Jun 04, 2019 2:58 pm
So you mean you should use a table as a data structure but don't use functions to manipulate the table in order to get the speed gain, right!?
Right, but only in terms of very large amounts of data.

Post Reply