Coder Social home page Coder Social logo

gen.TcpHandle Question about ergo HOT 10 CLOSED

DustOak avatar DustOak commented on May 30, 2024
gen.TcpHandle Question

from ergo.

Comments (10)

DustOak avatar DustOak commented on May 30, 2024

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

image

from ergo.

halturin avatar halturin commented on May 30, 2024

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.

from ergo.

DustOak avatar DustOak commented on May 30, 2024

i get it
thank you

from ergo.

DustOak avatar DustOak commented on May 30, 2024

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?

from ergo.

halturin avatar halturin commented on May 30, 2024

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

from ergo.

DustOak avatar DustOak commented on May 30, 2024

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

from ergo.

DustOak avatar DustOak commented on May 30, 2024

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.

from ergo.

halturin avatar halturin commented on May 30, 2024

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.

from ergo.

DustOak avatar DustOak commented on May 30, 2024

Thank you. but I have a new question

#153

from ergo.

halturin avatar halturin commented on May 30, 2024

done in v2.2.3

from ergo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.