Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gen.TcpHandle Question #152

Closed
Capybara-1 opened this issue Mar 29, 2023 · 10 comments
Closed

gen.TcpHandle Question #152

Capybara-1 opened this issue Mar 29, 2023 · 10 comments

Comments

@Capybara-1
Copy link

When I find that the incoming packet length of Calling HandlePacket is less than the value I want,I will return the value of left as len (packet), but why is the packet information not received last time overwritten when I receive the next package?

截屏2023-03-29 14 29 06
截屏2023-03-29 14 29 21

@Capybara-1
Copy link
Author

This result is completely different from the result returned in the case comment.

image

@halturin
Copy link
Collaborator

If I do understand your question correctly... The main reason for returning left, await values is that you might receive a packet with the tail of your logical struct and the header of the next one. In this case, gen.TCP will append the tail data to the packet buffer and invoke HandlePacket with the packet containing the full logical struct while keeping the head data for the next packet.

@Capybara-1
Copy link
Author

i get it
thank you

@Capybara-1
Copy link
Author

I send a slice -> [3 217 0 0 26 0 0 0 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90]

I send divided into two this slice

3 seconds at intervals

e.g.
dial.Write(slice[:4])
time.Sleep(time.Second * 3)
dial.Write(slice[4:])

but when when call HandlePacket first call
This branch will be triggered:
plen := len(packet) if plen < headLength { log.Errorf("packet too short %d", plen) return plen, headLength, gen.TCPHandlerStatusOK }
when I send slice[4:]. second call HandlePacket
packet data is -> [26 0 0 0 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90]
截屏2023-03-29 14 50 59

where is my first data?

@halturin
Copy link
Collaborator

not sure if I follow your example. could you please share your HandlePacket code and what you expected to see

@Capybara-1
Copy link
Author

func (this *handle) HandlePacket(process *gen.TCPHandlerProcess, packet []byte, conn *gen.TCPConnection) (int, int, gen.TCPHandlerStatus) {
plen := len(packet)
if plen < headLength {
log.Errorf("packet too short %d", plen)
return plen, headLength, gen.TCPHandlerStatusOK
}
log.Infof("pid %s handle packet: %v", process.Self(), packet)
head := make([]byte, headLength)
copy(head, packet)
code := binary.LittleEndian.Uint32(head)
length := binary.LittleEndian.Uint32(head[4:])
if uint32(plen-headLength) < length {
log.Errorf("packet too short,code is %d cur packet length %d need length %d", code, plen-headLength, length)
return plen - headLength, int(length), gen.TCPHandlerStatusOK
}
body := make([]byte, length)
copy(body, packet[headLength:])
log.Infof("pid %s code: %d length :%d body %s", process.Self(), code, length, string(body))
return plen - headLength - int(length), headLength, gen.TCPHandlerStatusOK
}

I think when packet is not enough(8byte), I will wait next call At this point, I can get the complete data.

But it not,it have next data but first data is lose

@Capybara-1
Copy link
Author

e.g
I send a slice -> [3 217 0 0 26 0 0 0 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90]

I send divided into two this slice

3 seconds at intervals

code:
dial.Write(slice[:4])
time.Sleep(time.Second * 3)
dial.Write(slice[4:])

I know that returning a "packet too short" error on the first call

But on the second call, you must receive the complete data after being sent twice.

It is -> [3 217 0 0 26 0 0 0 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90]

but I receive [26 0 0 0 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90]

I lose [3 217 0 0]

That is the data sent for the first time.

@halturin
Copy link
Collaborator

halturin commented Mar 29, 2023

You lose the first 4 bytes because of a timeout of socket reading which is 3 seconds by default. To see that, you can add a method HandleTimeout to your object *handle. The buffer is clearing on this event, that's where you lost these bytes. To fix your example just decrease your sending timeout.

I'm thinking of fixing it by keeping the data in the buffer even timeout is happening.

@Capybara-1
Copy link
Author

Capybara-1 commented Mar 29, 2023

Thank you. but I have a new question

#153

@halturin
Copy link
Collaborator

halturin commented Apr 2, 2023

done in v2.2.3

@halturin halturin closed this as completed Apr 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants