Not sure. Let me try to explain what I’m doing w/ some examples…
[code]function sendPacket(responseExpected as boolean, targetContent as memoryBlock, timeOut as uInt64, byRef replyContent as memoryBlock) as boolean
dim processedPacket as memoryBlock
dim expireTime as uInt64
expireTime = (timeOut * 60) + ticks '[timeOut] in seconds
expectingResponse = responseExpected
responsePacket = NULL_STRING
receivedAck = false
if (targetContent = NULL_STRING) or (targetContent.size = 0) or (targetContent.size > 999999) then
errorFramework.logError(ERROR_SILENT, system.logLevelAlert, “coreTcp/sendPacket - invalid content requirements”)
expectingResponse = false
return false
end if
if not constructPacket(false, responseExpected, targetContent, processedPacket) then 'error constructing packet
errorFramework.logError(ERROR_SILENT, system.logLevelAlert, “coreTcp/sendPacket - packet construct error”)
return false
else 'packet constructed successfully; send constructed packet to remote connection
me.sendMessage(0, processedPacket)
idleTimer.reset 'sent packet; reset idle timer
end if
// - wait for successful send, error, or timeout
do until (me.bytesLeftToSend = 0) or (me.readError) or (me.writeError) or (ticks > expireTime)
app.doEvents()
loop
if (me.writeError) or (me.readError) then 'tcp socket read/write error
errorFramework.logError(ERROR_SILENT, system.logLevelAlert, “coreTcp/sendPacket - readError/writeError [send]”)
expectingResponse = false
return false
elseif (ticks > expireTime) then 'timeout flagged; specified by [timeOut]
errorFramework.logError(ERROR_SILENT, system.logLevelAlert, “coreTcp/sendPacket - timeOut [send]”)
expectingResponse = false
return false
elseif (me.bytesLeftToSend = 0) then 'packet successfully sent
if responseExpected then 'packet expects a response from remote connection
// - wait for successful response, error, or timeout
do until (responsePacket <> NULL_STRING) or (me.readError) or (me.writeError) or (ticks > expireTime)
app.doEvents()
loop
if (me.writeError) or (me.readError) then 'tcp socket read/write error
errorFramework.logError(ERROR_SILENT, system.logLevelAlert, "coreTcp/sendPacket - readError/writeError [response]")
expectingResponse = false
return false
elseif (ticks > expireTime) then 'timeout flagged; specified by [timeOut]
errorFramework.logError(ERROR_SILENT, system.logLevelAlert, "coreTcp/sendPacket - timeOut [response]")
expectingResponse = false
return false
elseif (responsePacket <> NULL_STRING) then 'response successfully received
expectingResponse = false
replyContent = responsePacket 'return response data
return true
end if
else 'packet only expects an acknowledgement from remote connection
expectingResponse = false
replyContent = NULL_STRING
do until (receivedAck = true) or (me.readError) or (me.writeError) or (ticks > expireTime)
app.doEvents()
loop
if (me.writeError) or (me.readError) then 'tcp socket read/write error
errorFramework.logError(ERROR_SILENT, system.logLevelAlert, "coreTcp/sendPacket - readError/writeError [acknowledge]")
return false
elseif (ticks > expireTime) then 'timeout flagged; specified by [timeOut]
errorFramework.logError(ERROR_SILENT, system.logLevelAlert, "coreTcp/sendPacket - timeOut [acknowledge]")
return false
elseif (receivedAck = true) then 'acknowledgement successfully received
return true
end if
end if
end if
end function[/code]
to send a packet in other areas of my code, this is what I use…
if tcpFramework.sendPacket(true, "SEND ME SOMETHING", 15, tcpResponse) then
tcpFramework.sessionId = targetSessionId + outgoingPacket 'set [:coreTcp/sessionId] with constructed 18-digit value
else
// - could not get response; handle error here
end if
The tcpFramework (which is an easyTcpSocket) populates the ‘responsePacket’ when the remote system returns a value. When it does, this method passes that byRef to ‘replyContent’.
It’s important in this process that data is returned with that sendPacket (unless an error occurs, which I handle) before continuing on to the next line of code… (which is likely more sendPacket commands, checks & validations, etc.). It has to be processed in order.