FAQ
Is there any way to buffer reads (read a packet into a bytes.Buffer struct)
from something that implements net.PacketConn (UDP etc.) I wish to just get
the contents of the next packet and put it into a dynamically sized buffer.
If I didn't need the address, I could do it this way<http://nf.id.au/buffering-reads-with-bytesbuffer>
.

Search Discussions

  • Dave Cheney at Aug 31, 2012 at 11:24 am
    While net.UDPConn implements Read(), there is no guarantee that reads
    will occur in the same sequence that they were sent. This may be
    problem for your use case.
    On Fri, Aug 31, 2012 at 6:18 PM, liamzebedee wrote:
    Is there any way to buffer reads (read a packet into a bytes.Buffer struct)
    from something that implements net.PacketConn (UDP etc.) I wish to just get
    the contents of the next packet and put it into a dynamically sized buffer.
    If I didn't need the address, I could do it this way.
  • Larry Clapp at Aug 31, 2012 at 1:31 pm
    Hi, Dave, interested bystander here: Do you mean that, due to the nature of
    UDP, there's just no way to guarantee that you'll get packets in the same
    order they were sent?

    liamzebedee: Couldn't you just write your own type and override the Read
    function to do the buffering?

    type MyUDPConn struct {
    UDPConn
    }

    func (c *MyUDPConn) Read(b []byte) (int, error) { // same signature as
    net.UDPConn.Read
    // do buffering here, call c.UDPConn.Read to get the "real" read
    }

    ... Or did you know all that and just wondered of someone else had done it
    already? :)

    -- Larry

    On Friday, August 31, 2012 7:24:33 AM UTC-4, Dave Cheney wrote:

    While net.UDPConn implements Read(), there is no guarantee that reads
    will occur in the same sequence that they were sent. This may be
    problem for your use case.
    On Fri, Aug 31, 2012 at 6:18 PM, liamzebedee wrote:
    Is there any way to buffer reads (read a packet into a bytes.Buffer struct)
    from something that implements net.PacketConn (UDP etc.) I wish to just get
    the contents of the next packet and put it into a dynamically sized buffer.
    If I didn't need the address, I could do it this way.
  • Jim Teeuwen at Aug 31, 2012 at 1:52 pm
    In addition to the out-of-order reception, there is no guarantee that a
    packet arrives at all.
    UDP does not do retransmission of lost packets.. It has no notion of lost
    or received packets at all.

    You will need to build a reliability layer on top of UDP to do this.
    If guaranteed reception in the right order is something you really need,
    it is probably better to just use TCP.

    If you do require UDP and don't mind getting your hands dirty, this might
    be of some help:
    https://github.com/jteeuwen/xudp

    It is still very low level, as it does not automatically resend lost
    packets, but it does keep track
    of ACK'ed and lost packets for you. It notifies you through an event
    handler at which point you
    can handle the situation as you want. It should be relatively easy to build
    an in-order buffered
    reader on top of an xudp.Connection.


    On Friday, August 31, 2012 1:24:33 PM UTC+2, Dave Cheney wrote:

    While net.UDPConn implements Read(), there is no guarantee that reads
    will occur in the same sequence that they were sent. This may be
    problem for your use case.
    On Fri, Aug 31, 2012 at 6:18 PM, liamzebedee wrote:
    Is there any way to buffer reads (read a packet into a bytes.Buffer struct)
    from something that implements net.PacketConn (UDP etc.) I wish to just get
    the contents of the next packet and put it into a dynamically sized buffer.
    If I didn't need the address, I could do it this way.
  • Jim Teeuwen at Aug 31, 2012 at 2:56 pm
    It should also be noted that way this handles packet ACKs, requires that
    you maintain a steady and timed stream of back/forth communication. This
    package was mostly written with multiplayer games in mind. In these cases,
    you are polling for network activity (usually) once every frame. This
    maintains the steady data stream.

    Sending/receiving data as fast as possible, outside of a game loop context
    can be done as-is, but it does mean that you have send empty (nil payload)
    packets on a regular basis in order to keep the ACK synchronisation going.
    On Friday, August 31, 2012 3:45:22 PM UTC+2, Jim Teeuwen wrote:

    In addition to the out-of-order reception, there is no guarantee that a
    packet arrives at all.
    UDP does not do retransmission of lost packets.. It has no notion of lost
    or received packets at all.

    You will need to build a reliability layer on top of UDP to do this.
    If guaranteed reception in the right order is something you really need,
    it is probably better to just use TCP.

    If you do require UDP and don't mind getting your hands dirty, this might
    be of some help:
    https://github.com/jteeuwen/xudp

    It is still very low level, as it does not automatically resend lost
    packets, but it does keep track
    of ACK'ed and lost packets for you. It notifies you through an event
    handler at which point you
    can handle the situation as you want. It should be relatively easy to
    build an in-order buffered
    reader on top of an xudp.Connection.


    On Friday, August 31, 2012 1:24:33 PM UTC+2, Dave Cheney wrote:

    While net.UDPConn implements Read(), there is no guarantee that reads
    will occur in the same sequence that they were sent. This may be
    problem for your use case.
    On Fri, Aug 31, 2012 at 6:18 PM, liamzebedee wrote:
    Is there any way to buffer reads (read a packet into a bytes.Buffer struct)
    from something that implements net.PacketConn (UDP etc.) I wish to just get
    the contents of the next packet and put it into a dynamically sized buffer.
    If I didn't need the address, I could do it this way.
  • Liamzebedee at Sep 1, 2012 at 7:01 am
    I understand UDP does not guarantee in-order delivery (which I don't need
    anyways). What I'm trying to achieve here is being able to read a single
    packet from the connection and store it in a byte array (without defining
    the size of the array). Is there anything anyone can suggest on how to
    achieve this?
    On Friday, August 31, 2012 9:24:33 PM UTC+10, Dave Cheney wrote:

    While net.UDPConn implements Read(), there is no guarantee that reads
    will occur in the same sequence that they were sent. This may be
    problem for your use case.
    On Fri, Aug 31, 2012 at 6:18 PM, liamzebedee wrote:
    Is there any way to buffer reads (read a packet into a bytes.Buffer struct)
    from something that implements net.PacketConn (UDP etc.) I wish to just get
    the contents of the next packet and put it into a dynamically sized buffer.
    If I didn't need the address, I could do it this way.
  • Dave Cheney at Sep 1, 2012 at 10:35 am
    No, all reads from UDP must consume the whole datagram, any extra will
    be discarded. UDP has a maximum datagram size of 65,507 bytes for
    IPv4, however in practice most people get away with using a 1500 or
    4096 byte buffer. If you have control over both ends of the
    transmission you can go further by ensuring the maximum datagram you
    send is ~ 1400, which will avoid ip fragmentation.

    Cheers

    Dave
    On Sat, Sep 1, 2012 at 5:01 PM, liamzebedee wrote:
    I understand UDP does not guarantee in-order delivery (which I don't need
    anyways). What I'm trying to achieve here is being able to read a single
    packet from the connection and store it in a byte array (without defining
    the size of the array). Is there anything anyone can suggest on how to
    achieve this?

    On Friday, August 31, 2012 9:24:33 PM UTC+10, Dave Cheney wrote:

    While net.UDPConn implements Read(), there is no guarantee that reads
    will occur in the same sequence that they were sent. This may be
    problem for your use case.
    On Fri, Aug 31, 2012 at 6:18 PM, liamzebedee wrote:
    Is there any way to buffer reads (read a packet into a bytes.Buffer
    struct)
    from something that implements net.PacketConn (UDP etc.) I wish to just
    get
    the contents of the next packet and put it into a dynamically sized
    buffer.
    If I didn't need the address, I could do it this way.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 31, '12 at 8:18a
activeSep 1, '12 at 10:35a
posts7
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase