Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
greubel
Visitor

Re: Help with roStreamSocket data

Can you clear the message port and have it revert back to sync ?
I believe I tried this and it didn't.
0 Kudos
RokuMEmerson
Visitor

Re: Help with roStreamSocket data

Setting a socket's message port back to invalid after having set it to a real port is coded to set the socket's behavior back to sync, and an off-the-cuff connect() test that does so works for me.
Our sample apps and the tests in the test suite either leave it sync or set it to async once.
0 Kudos
greubel
Visitor

Re: Help with roStreamSocket data

What I tried is:

1. connect() - host
2. send() - request
3. set msg port
4. receive() - multiple blocks
5. clear msg port to invalid
6. send() - another request

The send at 6, always returned 0 bytes
0 Kudos
RokuMEmerson
Visitor

Re: Help with roStreamSocket data

That same test of using sync for connect() and sendStr(), setting a message port for async receiveStr(), and then setting the socket's message port to invalid for another sendStr() worked fine for me. Could we get a post of a short executable sample that shows the problem?
0 Kudos
greubel
Visitor

Re: Help with roStreamSocket data

OK, I hope this shows something !



--- Passes if connect and initialize socket every time ---

* Connect failed 0 times
Time connect = 7
Time send = 9
recv ok = true
Recv[ 0] = 1642 y = 1642
Recv[ 1] = 384 y = 384
Time recv = 2016
Total recv 2028 append time = 2017

* Connect failed 0 times
Time connect = 7
Time send = 8
recv ok = true
Recv[ 0] = 3288 y = 3288
Time recv = 2019
Total recv 3289 append time = 2020

* Connect failed 0 times
Time connect = 8
Time send = 10
recv ok = true
Recv[ 0] = 861 y = 861
Time recv = 2029
Total recv 862 append time = 2030

--- Fails if try and reuse socket ---
*
Test_Send
* Connect failed 0 times
Time connect = 10
Time send = 12
recv ok = true
Recv[ 0] = 2026 y = 2026
Time recv = 2032
Total recv 2027 append time = 2033
*
Test_Send
Time send = 2
recv ok = true <-------- 2 recieve also fails ????
Time recv = 6
*
Test_Send
sent 0 of 836
status = 0

>>>>> Send Failed <<<<<

----------------------------------------------------------------------

Function Test_Send( host as string, obuf as object, ibuf as object )
? "Test_Send"

tcp = m.tcp
if tcp = invalid
tcp = TCP_Connect( host )
if tcp <> invalid
? "Time connect = " m.timer.TotalMilliseconds()
else
? "Connect failed for " host
Disp( "Failed to Connect to "+Chr(10)+host )
return invalid
end if
m.tcp = tcp
end if

tcp.setMessagePort( invalid )
tcp.notifyReadable( false )

z = obuf.Count()
obuf[z] = 0

x = tcp.send( obuf, 0, z )
if x <> z
? "sent " x " of " z
? "status = " tcp.status()
Disp( "Send Failed" )
return invalid
end if
? "Time send = " m.timer.TotalMilliseconds()

port = createobject( "roMessagePort" )
tcp.setMessagePort( port )
tcp.notifyReadable( true )

ok = tcp.eOK()
? "recv ok = " ok
while ok
ev = wait( 100, port )
if type(ev) = "roSocketEvent"
if ev.getSocketID() = tcp.getID()
if tcp.isReadable()
x = tcp.getCountRcvBuf()
if x = 0 exit while
e = ibuf.Count()
ibuf.Push( createobject( "roByteArray" ))
ibuf[e][x] = 0
y = tcp.receive( ibuf[e], 0, x )
? "Recv[" e "] = " x " y = " y
if y <> x
Disp( "Receive Failed" )
return invalid
end if
end if
end if
end if
end while
? "Time recv = " m.timer.TotalMilliseconds()

End Function

Function TCP_Connect( host as string ) as object

fail = 0
while 1
adrs = createobject( "roSocketAddress" )
adrs.setAddress( host )

tcp = CreateObject( "roStreamSocket" )
tcp.setSendToAddress( adrs )

tcp.connect()
if tcp.isConnected()
exit while
else
fail = fail + 1
end if

tcp.Close()
tcp = invalid
if fail = 100 exit while
if fail > 10 sleep(10)
end while
? "* Connect failed " fail " times"
return tcp

End Function

----------------------------------------------------------------------
0 Kudos
RokuMEmerson
Visitor

Re: Help with roStreamSocket data

By some of the comments in the output, it seems like the sample code might be trying to reuse a closed socket or a previously connected socket?
It doesn't have a main that calls the functions, so I can't try it myself either.
Are we still trying to determine whether sync send works on a socket after async receive?

The test code I used to demonstrate the case I thought we were discussing is below. Can you tell me what it's missing from what you're trying to do?

Script:
function main()
HttpGet = "GET / HTTP/1.1" + chr(13) + chr(10) + chr(13) + chr(10)
socket = CreateObject("roStreamSocket")
server = CreateObject("roSocketAddress")
server.setAddress("www.roku.com:80")
socket.setSendToAddress(server)

' sync
socket.connect()
print "Test: sent"; socket.sendStr(HttpGet); " bytes"

' async
port = CreateObject("roMessagePort")
socket.setMessagePort(port)
socket.notifyReadable(true)
msg = wait(5 * 1000, port)
if type(msg)="roSocketEvent" and socket.isReadable()
input = socket.receiveStr(1000)
print "Test: received"; Len(input); " bytes" '; ", input:"; chr(10); input
end if

' sync
socket.setMessagePort(invalid)
print "Test: sent"; socket.sendStr(HttpGet); " bytes"
end function


Output:
------ Running ------
Test: sent 18 bytes
Test: received 513 bytes
Test: sent 18 bytes
0 Kudos