FAQ
I'm building rfb client/server packge, all works fine, but i can't
handle one type of message to create rfb proxy.

I'm already have function that reads FrameBufferUpdate message from
rfb server, not i needs to encode it to binary format to send to
another server (like proxy server).
How can i write write method to get binary representation of the data back?
Does somebody can help me?

spec: http://tools.ietf.org/html/rfc6143#section-7.7.1

Read Method for message is :
func (msg *FramebufferUpdateMsg) Read(c *Conn, r io.Reader) (Message, error) {
   m := &FramebufferUpdateMsg{}
   // Read off the padding
   var padding [1]byte
   if err := binary.Read(r, binary.BigEndian, &padding); err != nil {
     return nil, err
   }
   var numRects uint16
   if err := binary.Read(r, binary.BigEndian, &numRects); err != nil {
     return nil, err
   }
   // Build the map of encodings supported
   encMap := make(map[int32]Encoding)
   for _, enc := range c.Encs {
     encMap[enc.Type()] = enc
   }
   // We must always support the raw encoding
   rawEnc := new(RawEncoding)
   encMap[rawEnc.Type()] = rawEnc
   rects := make([]Rectangle, numRects)
   for i := uint16(0); i < numRects; i++ {
     var encodingType int32
     rect := &rects[i]
     data := []interface{}{
       &rect.X,
       &rect.Y,
       &rect.Width,
       &rect.Height,
       &encodingType,
     }
     for _, val := range data {
       if err := binary.Read(r, binary.BigEndian, val); err != nil {
         return nil, err
       }
     }
     enc, ok := encMap[encodingType]
     if !ok {
       return nil, errors.New("unsupported encoding type") //, encodingType)
     }
     var err error
     rect.Enc, err = enc.Read(c, rect, r)
     if err != nil {
       return nil, err
     }
   }
   m.Rectangles = rects
   return m, nil
}

Read method for encoding read (encoding is raw)

type Color struct {
         R, G, B uint16
}

type RawEncoding struct {
   Colors []Color
}

func (*RawEncoding) Read(c *Conn, rect *Rectangle, r io.Reader)
(Encoding, error) {
   bytesPerPixel := c.PixelFormat.BPP / 8
   pixelBytes := make([]uint8, bytesPerPixel)
   var byteOrder binary.ByteOrder = binary.LittleEndian
   if c.PixelFormat.BigEndian {
     byteOrder = binary.BigEndian
   }
   colors := make([]Color, rect.Height*rect.Width)
   for y := uint16(0); y < rect.Height; y++ {
     for x := uint16(0); x < rect.Width; x++ {
       if _, err := io.ReadFull(r, pixelBytes); err != nil {
         return nil, err
       }
       var rawPixel uint32
       if c.PixelFormat.BPP == 8 {
         rawPixel = uint32(pixelBytes[0])
       } else if c.PixelFormat.BPP == 16 {
         rawPixel = uint32(byteOrder.Uint16(pixelBytes))
       } else if c.PixelFormat.BPP == 32 {
         rawPixel = byteOrder.Uint32(pixelBytes)
       }
       color := &colors[x+y]
       if c.PixelFormat.TrueColor {
         color.R = uint16((rawPixel << c.PixelFormat.RedShift) &
uint32(c.PixelFormat.RedMax))
         color.G = uint16((rawPixel << c.PixelFormat.GreenShift) &
uint32(c.PixelFormat.GreenMax))
         color.B = uint16((rawPixel << c.PixelFormat.BlueShift) &
uint32(c.PixelFormat.BlueMax))
       } else {
         *color = c.ColorMap[rawPixel]
       }
     }
   }
   return &RawEncoding{Colors: colors}, nil
}


--
Vasiliy Tolstov,
e-mail: v.tolstov@selfip.ru
jabber: vase@selfip.ru

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Vasiliy Tolstov at Aug 20, 2013 at 6:40 pm

    2013/8/20 Vasiliy Tolstov <v.tolstov@selfip.ru>:
    I'm building rfb client/server packge, all works fine, but i can't
    handle one type of message to create rfb proxy.

    I'm already have function that reads FrameBufferUpdate message from
    rfb server, not i needs to encode it to binary format to send to
    another server (like proxy server).
    How can i write write method to get binary representation of the data back?
    Does somebody can help me?

    Package sources https://bitbucket.org/vtolstov/vkvm/src

    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Aug 21, 2013 at 6:13 am
    As i'm understand my main problem that i can't understand
    In case of reading i have:
        color.R = uint16((rawPixel << c.PixelFormat.RedShift) &
    uint32(c.PixelFormat.RedMax))
             color.G = uint16((rawPixel << c.PixelFormat.GreenShift) &
    uint32(c.PixelFormat.GreenMax))
             color.B = uint16((rawPixel << c.PixelFormat.BlueShift) &
    uint32(c.PixelFormat.BlueMax))

    In case of writing i need to convert color.R/color.G/color.B to uint32
    but i can't understand how can i revert reading operation.

    2013/8/20 Vasiliy Tolstov <v.tolstov@selfip.ru>:
    2013/8/20 Vasiliy Tolstov <v.tolstov@selfip.ru>:
    I'm building rfb client/server packge, all works fine, but i can't
    handle one type of message to create rfb proxy.

    I'm already have function that reads FrameBufferUpdate message from
    rfb server, not i needs to encode it to binary format to send to
    another server (like proxy server).
    How can i write write method to get binary representation of the data back?
    Does somebody can help me?

    Package sources https://bitbucket.org/vtolstov/vkvm/src

    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru


    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gerard at Aug 21, 2013 at 7:38 am
    The Color type overflows "var rawPixel uint32" with 3x uint16 (which
    together is 48 bits)

    That's probably why there is for each color an offset and a max size
    specified.

    I think I solved the issue with this goplay<http://play.golang.org/p/uRDWuOaQFN>sample. (this should probably be solved easier but I don't have a lot of
    knowledge about bit manipulation)

    Take a look at the shifting with maxR/maxG/maxB because they cap the values
    of color.R/color.G/color.B. Without this operation the values overflow.

    On Tuesday, August 20, 2013 4:46:47 PM UTC+2, Vasiliy Tolstov wrote:

    I'm building rfb client/server packge, all works fine, but i can't
    handle one type of message to create rfb proxy.

    I'm already have function that reads FrameBufferUpdate message from
    rfb server, not i needs to encode it to binary format to send to
    another server (like proxy server).
    How can i write write method to get binary representation of the data
    back?
    Does somebody can help me?

    spec: http://tools.ietf.org/html/rfc6143#section-7.7.1

    Read Method for message is :
    func (msg *FramebufferUpdateMsg) Read(c *Conn, r io.Reader) (Message,
    error) {
    m := &FramebufferUpdateMsg{}
    // Read off the padding
    var padding [1]byte
    if err := binary.Read(r, binary.BigEndian, &padding); err != nil {
    return nil, err
    }
    var numRects uint16
    if err := binary.Read(r, binary.BigEndian, &numRects); err != nil {
    return nil, err
    }
    // Build the map of encodings supported
    encMap := make(map[int32]Encoding)
    for _, enc := range c.Encs {
    encMap[enc.Type()] = enc
    }
    // We must always support the raw encoding
    rawEnc := new(RawEncoding)
    encMap[rawEnc.Type()] = rawEnc
    rects := make([]Rectangle, numRects)
    for i := uint16(0); i < numRects; i++ {
    var encodingType int32
    rect := &rects[i]
    data := []interface{}{
    &rect.X,
    &rect.Y,
    &rect.Width,
    &rect.Height,
    &encodingType,
    }
    for _, val := range data {
    if err := binary.Read(r, binary.BigEndian, val); err != nil {
    return nil, err
    }
    }
    enc, ok := encMap[encodingType]
    if !ok {
    return nil, errors.New("unsupported encoding type") //,
    encodingType)
    }
    var err error
    rect.Enc, err = enc.Read(c, rect, r)
    if err != nil {
    return nil, err
    }
    }
    m.Rectangles = rects
    return m, nil
    }

    Read method for encoding read (encoding is raw)

    type Color struct {
    R, G, B uint16
    }

    type RawEncoding struct {
    Colors []Color
    }

    func (*RawEncoding) Read(c *Conn, rect *Rectangle, r io.Reader)
    (Encoding, error) {
    bytesPerPixel := c.PixelFormat.BPP / 8
    pixelBytes := make([]uint8, bytesPerPixel)
    var byteOrder binary.ByteOrder = binary.LittleEndian
    if c.PixelFormat.BigEndian {
    byteOrder = binary.BigEndian
    }
    colors := make([]Color, rect.Height*rect.Width)
    for y := uint16(0); y < rect.Height; y++ {
    for x := uint16(0); x < rect.Width; x++ {
    if _, err := io.ReadFull(r, pixelBytes); err != nil {
    return nil, err
    }
    var rawPixel uint32
    if c.PixelFormat.BPP == 8 {
    rawPixel = uint32(pixelBytes[0])
    } else if c.PixelFormat.BPP == 16 {
    rawPixel = uint32(byteOrder.Uint16(pixelBytes))
    } else if c.PixelFormat.BPP == 32 {
    rawPixel = byteOrder.Uint32(pixelBytes)
    }
    color := &colors[x+y]
    if c.PixelFormat.TrueColor {
    color.R = uint16((rawPixel << c.PixelFormat.RedShift) &
    uint32(c.PixelFormat.RedMax))
    color.G = uint16((rawPixel << c.PixelFormat.GreenShift) &
    uint32(c.PixelFormat.GreenMax))
    color.B = uint16((rawPixel << c.PixelFormat.BlueShift) &
    uint32(c.PixelFormat.BlueMax))
    } else {
    *color = c.ColorMap[rawPixel]
    }
    }
    }
    return &RawEncoding{Colors: colors}, nil
    }


    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru <javascript:>
    jabber: va...@selfip.ru <javascript:>
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Aug 21, 2013 at 7:49 am

    2013/8/21 Gerard <gvdschoot@gmail.com>:
    The Color type overflows "var rawPixel uint32" with 3x uint16 (which
    together is 48 bits)

    That's probably why there is for each color an offset and a max size
    specified.

    I think I solved the issue with this goplay sample. (this should probably be
    solved easier but I don't have a lot of knowledge about bit manipulation)

    Take a look at the shifting with maxR/maxG/maxB because they cap the values
    of color.R/color.G/color.B. Without this operation the values overflow.

    Hmm. Very big thanks. Can you says me if the spec says me
    http://tools.ietf.org/html/rfc6143#section-7.4
    what i need to do to read/write pixel ?



    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gerard at Aug 21, 2013 at 8:35 am
    I updated the goplay <http://play.golang.org/p/0QTHlBtal4> so it uses the
    max mask correctly (line 29..31). Note that I didn't read the RFC when I
    looked at the code. So the code doesn't match it (regarding endinaness
    etc.).

    The read/write of the pixel array is (I think) described in "7.6.1.
    FramebufferUpdate".

    On Wednesday, August 21, 2013 9:49:27 AM UTC+2, Vasiliy Tolstov wrote:

    2013/8/21 Gerard <gvds...@gmail.com <javascript:>>:
    The Color type overflows "var rawPixel uint32" with 3x uint16 (which
    together is 48 bits)

    That's probably why there is for each color an offset and a max size
    specified.

    I think I solved the issue with this goplay sample. (this should
    probably be
    solved easier but I don't have a lot of knowledge about bit
    manipulation)
    Take a look at the shifting with maxR/maxG/maxB because they cap the values
    of color.R/color.G/color.B. Without this operation the values overflow.

    Hmm. Very big thanks. Can you says me if the spec says me
    http://tools.ietf.org/html/rfc6143#section-7.4
    what i need to do to read/write pixel ?



    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru <javascript:>
    jabber: va...@selfip.ru <javascript:>
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gerard at Aug 21, 2013 at 9:37 am
    I haven't looked at it, but I think you can use binary.Write() for the
    Color type when speed isn't an issue. This makes it easier with the
    endianess.

    In $7.4.1 it is said that "bits-per-pixel must be 8, 16, or 32." So define
    3 types of Colors (Color8, Color16 and Color32). That in combination with
    binary.Write might solve it. However this is only possible when and
    red-shift, green-shift and blue-shift are ordinary values (magnitudes of 8)
    and red-max, green-max, blue-max can be ignored.

    The encodings (described in $7.7 can be added at a higher level (other
    Write / Read functions)

    On Wednesday, August 21, 2013 10:35:06 AM UTC+2, Gerard wrote:

    I updated the goplay <http://play.golang.org/p/0QTHlBtal4> so it uses the
    max mask correctly (line 29..31). Note that I didn't read the RFC when I
    looked at the code. So the code doesn't match it (regarding endinaness
    etc.).

    The read/write of the pixel array is (I think) described in "7.6.1.
    FramebufferUpdate".

    On Wednesday, August 21, 2013 9:49:27 AM UTC+2, Vasiliy Tolstov wrote:

    2013/8/21 Gerard <gvds...@gmail.com>:
    The Color type overflows "var rawPixel uint32" with 3x uint16 (which
    together is 48 bits)

    That's probably why there is for each color an offset and a max size
    specified.

    I think I solved the issue with this goplay sample. (this should
    probably be
    solved easier but I don't have a lot of knowledge about bit
    manipulation)
    Take a look at the shifting with maxR/maxG/maxB because they cap the values
    of color.R/color.G/color.B. Without this operation the values overflow.

    Hmm. Very big thanks. Can you says me if the spec says me
    http://tools.ietf.org/html/rfc6143#section-7.4
    what i need to do to read/write pixel ?



    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru
    jabber: va...@selfip.ru
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Aug 21, 2013 at 10:01 am
    I'm not needed now colo8, color16, only color32

    now i'm write this
    https://bitbucket.org/vtolstov/vkvm/src/master/vnc/encoding.go?at=master#cl-48

    but rfb output does not like input =)

    wireshark says that real server send to me 21954+55570 but i'm only
    send after parsing is about 4162 bytes.
    =(

    2013/8/21 Gerard <gvdschoot@gmail.com>:
    I haven't looked at it, but I think you can use binary.Write() for the Color
    type when speed isn't an issue. This makes it easier with the endianess.

    In $7.4.1 it is said that "bits-per-pixel must be 8, 16, or 32." So define 3
    types of Colors (Color8, Color16 and Color32). That in combination with
    binary.Write might solve it. However this is only possible when and
    red-shift, green-shift and blue-shift are ordinary values (magnitudes of 8)
    and red-max, green-max, blue-max can be ignored.

    The encodings (described in $7.7 can be added at a higher level (other Write
    / Read functions)

    On Wednesday, August 21, 2013 10:35:06 AM UTC+2, Gerard wrote:

    I updated the goplay so it uses the max mask correctly (line 29..31). Note
    that I didn't read the RFC when I looked at the code. So the code doesn't
    match it (regarding endinaness etc.).

    The read/write of the pixel array is (I think) described in "7.6.1.
    FramebufferUpdate".

    On Wednesday, August 21, 2013 9:49:27 AM UTC+2, Vasiliy Tolstov wrote:

    2013/8/21 Gerard <gvds...@gmail.com>:
    The Color type overflows "var rawPixel uint32" with 3x uint16 (which
    together is 48 bits)

    That's probably why there is for each color an offset and a max size
    specified.

    I think I solved the issue with this goplay sample. (this should
    probably be
    solved easier but I don't have a lot of knowledge about bit
    manipulation)

    Take a look at the shifting with maxR/maxG/maxB because they cap the
    values
    of color.R/color.G/color.B. Without this operation the values overflow.

    Hmm. Very big thanks. Can you says me if the spec says me
    http://tools.ietf.org/html/rfc6143#section-7.4
    what i need to do to read/write pixel ?



    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru
    jabber: va...@selfip.ru
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gerard at Aug 21, 2013 at 4:12 pm
    Take a look at this goplay <http://play.golang.org/p/RGN3QYIcuV>.

    It fixes the bug in line 99 of encoding.go (the slice with [x*rect.Width+y]
    instead of [x+y])

    Also it uses encoding.Write only once, and then with a pixbuf.
    Because the pixbuf slice can contain []uint8, []uint16 or []uint32, this
    needs to be checked. So I added a check function (which calls a create
    function if the check fails)

    When you write the pixbuf to the conn you know the size of it exactly
    (which is len(pixbuf) * size of uint8, uint16 or uint32)

    And one last thing. If you change BigEndian and TrueColor in the PixelFormat
    struct to uint8, then the entire struct can also be read from / written to
    a buffer with binary.Read() / binary.Write()
    On Wednesday, August 21, 2013 12:01:37 PM UTC+2, Vasiliy Tolstov wrote:

    I'm not needed now colo8, color16, only color32

    now i'm write this

    https://bitbucket.org/vtolstov/vkvm/src/master/vnc/encoding.go?at=master#cl-48

    but rfb output does not like input =)

    wireshark says that real server send to me 21954+55570 but i'm only
    send after parsing is about 4162 bytes.
    =(

    2013/8/21 Gerard <gvds...@gmail.com <javascript:>>:
    I haven't looked at it, but I think you can use binary.Write() for the Color
    type when speed isn't an issue. This makes it easier with the endianess.

    In $7.4.1 it is said that "bits-per-pixel must be 8, 16, or 32." So define 3
    types of Colors (Color8, Color16 and Color32). That in combination with
    binary.Write might solve it. However this is only possible when and
    red-shift, green-shift and blue-shift are ordinary values (magnitudes of 8)
    and red-max, green-max, blue-max can be ignored.

    The encodings (described in $7.7 can be added at a higher level (other Write
    / Read functions)

    On Wednesday, August 21, 2013 10:35:06 AM UTC+2, Gerard wrote:

    I updated the goplay so it uses the max mask correctly (line 29..31).
    Note
    that I didn't read the RFC when I looked at the code. So the code
    doesn't
    match it (regarding endinaness etc.).

    The read/write of the pixel array is (I think) described in "7.6.1.
    FramebufferUpdate".

    On Wednesday, August 21, 2013 9:49:27 AM UTC+2, Vasiliy Tolstov wrote:

    2013/8/21 Gerard <gvds...@gmail.com>:
    The Color type overflows "var rawPixel uint32" with 3x uint16 (which
    together is 48 bits)

    That's probably why there is for each color an offset and a max size
    specified.

    I think I solved the issue with this goplay sample. (this should
    probably be
    solved easier but I don't have a lot of knowledge about bit
    manipulation)

    Take a look at the shifting with maxR/maxG/maxB because they cap the
    values
    of color.R/color.G/color.B. Without this operation the values
    overflow.

    Hmm. Very big thanks. Can you says me if the spec says me
    http://tools.ietf.org/html/rfc6143#section-7.4
    what i need to do to read/write pixel ?



    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru
    jabber: va...@selfip.ru
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru <javascript:>
    jabber: va...@selfip.ru <javascript:>
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Aug 21, 2013 at 9:21 pm

    2013/8/21 Gerard <gvdschoot@gmail.com>:
    Take a look at this goplay.

    It fixes the bug in line 99 of encoding.go (the slice with [x*rect.Width+y]
    instead of [x+y])

    Also it uses encoding.Write only once, and then with a pixbuf.
    Because the pixbuf slice can contain []uint8, []uint16 or []uint32, this
    needs to be checked. So I added a check function (which calls a create
    function if the check fails)

    When you write the pixbuf to the conn you know the size of it exactly (which
    is len(pixbuf) * size of uint8, uint16 or uint32)

    And one last thing. If you change BigEndian and TrueColor in the PixelFormat
    struct to uint8, then the entire struct can also be read from / written to a
    buffer with binary.Read() / binary.Write()

    Very big thanks for help!
    Firstly - i'm rewrite some code to correct errors and doing go build fine.
    But now i get panic after writing rectangle. Please check again
    https://bitbucket.org/vtolstov/vkvm/src/master/vnc/encoding.go?at=master

    panic: runtime error: index out of range

    goroutine 7 [running]:
    _/home/vase/devel/vkvm/vnc.(*RawEncoding).Write(0xc200079a20,
    0xc20009b000, 0xc2000882e0, 0xc200079ae0, 0xc200072a80, ...)
             /home/vase/devel/vkvm/vnc/encoding.go:171 +0x614
    _/home/vase/devel/vkvm/vnc.(*FramebufferUpdateMsg).Write(0xc200088160,
    0xc20009b000, 0xc2000795a0, 0xc2000000b0, 0xc2000000b0, ...)
             /home/vase/devel/vkvm/vnc/message.go:435 +0x5d7
    _/home/vase/devel/vkvm/vnc.(*Conn).serverServe(0xc20009b000)
             /home/vase/devel/vkvm/vnc/server.go:125 +0x534
    created by _/home/vase/devel/vkvm/vnc.(*Server).Serve
             /home/vase/devel/vkvm/vnc/server.go:73 +0x159


    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gerard at Aug 22, 2013 at 5:26 am
    Oops. That was my mistake. The index is probably x+y*rect.Width.

    Please take a look at the attached diff.

    I have to note however that my approach is only an idea. I think it's a lot
    faster, but the best method is to benchmark the code. And to be honest, I
    don't like the interface{} a lot. It has too many type checks / conversions
    / bloat. But I don't know a better way.
    On Tuesday, August 20, 2013 4:46:47 PM UTC+2, Vasiliy Tolstov wrote:

    I'm building rfb client/server packge, all works fine, but i can't
    handle one type of message to create rfb proxy.

    I'm already have function that reads FrameBufferUpdate message from
    rfb server, not i needs to encode it to binary format to send to
    another server (like proxy server).
    How can i write write method to get binary representation of the data
    back?
    Does somebody can help me?

    spec: http://tools.ietf.org/html/rfc6143#section-7.7.1

    Read Method for message is :
    func (msg *FramebufferUpdateMsg) Read(c *Conn, r io.Reader) (Message,
    error) {
    m := &FramebufferUpdateMsg{}
    // Read off the padding
    var padding [1]byte
    if err := binary.Read(r, binary.BigEndian, &padding); err != nil {
    return nil, err
    }
    var numRects uint16
    if err := binary.Read(r, binary.BigEndian, &numRects); err != nil {
    return nil, err
    }
    // Build the map of encodings supported
    encMap := make(map[int32]Encoding)
    for _, enc := range c.Encs {
    encMap[enc.Type()] = enc
    }
    // We must always support the raw encoding
    rawEnc := new(RawEncoding)
    encMap[rawEnc.Type()] = rawEnc
    rects := make([]Rectangle, numRects)
    for i := uint16(0); i < numRects; i++ {
    var encodingType int32
    rect := &rects[i]
    data := []interface{}{
    &rect.X,
    &rect.Y,
    &rect.Width,
    &rect.Height,
    &encodingType,
    }
    for _, val := range data {
    if err := binary.Read(r, binary.BigEndian, val); err != nil {
    return nil, err
    }
    }
    enc, ok := encMap[encodingType]
    if !ok {
    return nil, errors.New("unsupported encoding type") //,
    encodingType)
    }
    var err error
    rect.Enc, err = enc.Read(c, rect, r)
    if err != nil {
    return nil, err
    }
    }
    m.Rectangles = rects
    return m, nil
    }

    Read method for encoding read (encoding is raw)

    type Color struct {
    R, G, B uint16
    }

    type RawEncoding struct {
    Colors []Color
    }

    func (*RawEncoding) Read(c *Conn, rect *Rectangle, r io.Reader)
    (Encoding, error) {
    bytesPerPixel := c.PixelFormat.BPP / 8
    pixelBytes := make([]uint8, bytesPerPixel)
    var byteOrder binary.ByteOrder = binary.LittleEndian
    if c.PixelFormat.BigEndian {
    byteOrder = binary.BigEndian
    }
    colors := make([]Color, rect.Height*rect.Width)
    for y := uint16(0); y < rect.Height; y++ {
    for x := uint16(0); x < rect.Width; x++ {
    if _, err := io.ReadFull(r, pixelBytes); err != nil {
    return nil, err
    }
    var rawPixel uint32
    if c.PixelFormat.BPP == 8 {
    rawPixel = uint32(pixelBytes[0])
    } else if c.PixelFormat.BPP == 16 {
    rawPixel = uint32(byteOrder.Uint16(pixelBytes))
    } else if c.PixelFormat.BPP == 32 {
    rawPixel = byteOrder.Uint32(pixelBytes)
    }
    color := &colors[x+y]
    if c.PixelFormat.TrueColor {
    color.R = uint16((rawPixel << c.PixelFormat.RedShift) &
    uint32(c.PixelFormat.RedMax))
    color.G = uint16((rawPixel << c.PixelFormat.GreenShift) &
    uint32(c.PixelFormat.GreenMax))
    color.B = uint16((rawPixel << c.PixelFormat.BlueShift) &
    uint32(c.PixelFormat.BlueMax))
    } else {
    *color = c.ColorMap[rawPixel]
    }
    }
    }
    return &RawEncoding{Colors: colors}, nil
    }


    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru <javascript:>
    jabber: va...@selfip.ru <javascript:>
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Aug 22, 2013 at 7:40 am
    Hmm. No not works. Thanks for patch, but i thins errors that means
    that interface is nil, because of checkbuff initialize pixbuf from
    interface and its type not uint6/16/32 may be add to switch default
    case and create pixbuf by default to []uint32 ?

    2013/8/22 Gerard <gvdschoot@gmail.com>:
    Oops. That was my mistake. The index is probably x+y*rect.Width.

    Please take a look at the attached diff.

    I have to note however that my approach is only an idea. I think it's a lot
    faster, but the best method is to benchmark the code. And to be honest, I
    don't like the interface{} a lot. It has too many type checks / conversions
    / bloat. But I don't know a better way.

    On Tuesday, August 20, 2013 4:46:47 PM UTC+2, Vasiliy Tolstov wrote:

    I'm building rfb client/server packge, all works fine, but i can't
    handle one type of message to create rfb proxy.

    I'm already have function that reads FrameBufferUpdate message from
    rfb server, not i needs to encode it to binary format to send to
    another server (like proxy server).
    How can i write write method to get binary representation of the data
    back?
    Does somebody can help me?

    spec: http://tools.ietf.org/html/rfc6143#section-7.7.1

    Read Method for message is :
    func (msg *FramebufferUpdateMsg) Read(c *Conn, r io.Reader) (Message,
    error) {
    m := &FramebufferUpdateMsg{}
    // Read off the padding
    var padding [1]byte
    if err := binary.Read(r, binary.BigEndian, &padding); err != nil {
    return nil, err
    }
    var numRects uint16
    if err := binary.Read(r, binary.BigEndian, &numRects); err != nil {
    return nil, err
    }
    // Build the map of encodings supported
    encMap := make(map[int32]Encoding)
    for _, enc := range c.Encs {
    encMap[enc.Type()] = enc
    }
    // We must always support the raw encoding
    rawEnc := new(RawEncoding)
    encMap[rawEnc.Type()] = rawEnc
    rects := make([]Rectangle, numRects)
    for i := uint16(0); i < numRects; i++ {
    var encodingType int32
    rect := &rects[i]
    data := []interface{}{
    &rect.X,
    &rect.Y,
    &rect.Width,
    &rect.Height,
    &encodingType,
    }
    for _, val := range data {
    if err := binary.Read(r, binary.BigEndian, val); err != nil {
    return nil, err
    }
    }
    enc, ok := encMap[encodingType]
    if !ok {
    return nil, errors.New("unsupported encoding type") //,
    encodingType)
    }
    var err error
    rect.Enc, err = enc.Read(c, rect, r)
    if err != nil {
    return nil, err
    }
    }
    m.Rectangles = rects
    return m, nil
    }

    Read method for encoding read (encoding is raw)

    type Color struct {
    R, G, B uint16
    }

    type RawEncoding struct {
    Colors []Color
    }

    func (*RawEncoding) Read(c *Conn, rect *Rectangle, r io.Reader)
    (Encoding, error) {
    bytesPerPixel := c.PixelFormat.BPP / 8
    pixelBytes := make([]uint8, bytesPerPixel)
    var byteOrder binary.ByteOrder = binary.LittleEndian
    if c.PixelFormat.BigEndian {
    byteOrder = binary.BigEndian
    }
    colors := make([]Color, rect.Height*rect.Width)
    for y := uint16(0); y < rect.Height; y++ {
    for x := uint16(0); x < rect.Width; x++ {
    if _, err := io.ReadFull(r, pixelBytes); err != nil {
    return nil, err
    }
    var rawPixel uint32
    if c.PixelFormat.BPP == 8 {
    rawPixel = uint32(pixelBytes[0])
    } else if c.PixelFormat.BPP == 16 {
    rawPixel = uint32(byteOrder.Uint16(pixelBytes))
    } else if c.PixelFormat.BPP == 32 {
    rawPixel = byteOrder.Uint32(pixelBytes)
    }
    color := &colors[x+y]
    if c.PixelFormat.TrueColor {
    color.R = uint16((rawPixel << c.PixelFormat.RedShift) &
    uint32(c.PixelFormat.RedMax))
    color.G = uint16((rawPixel << c.PixelFormat.GreenShift) &
    uint32(c.PixelFormat.GreenMax))
    color.B = uint16((rawPixel << c.PixelFormat.BlueShift) &
    uint32(c.PixelFormat.BlueMax))
    } else {
    *color = c.ColorMap[rawPixel]
    }
    }
    }
    return &RawEncoding{Colors: colors}, nil
    }


    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru
    jabber: va...@selfip.ru
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gerard at Aug 22, 2013 at 8:14 am
    Well, in that case it's a lot simpler.

    var pixbuf []uint32

    func (enc *RawEncoding) Write(c *Conn, rect *Rectangle, w io.Writer) error {
    var byteOrder binary.ByteOrder = binary.LittleEndian
    if c.PixelFormat.BigEndian {
    byteOrder = binary.BigEndian
    }
    colors := enc.Colors

    bufsize := int(rect.Width*rect.Height)
    if len(pixbuf) != bufsize {
    pixbuf = make([]uint32, bufsize)
    }

    if !c.PixelFormat.TrueColor || c.PixelFormat.BPP != 32 {
    return fmt.Errorf("TODO: BPP of %d", c.PixelFormat.BPP)
    }

    for y := uint16(0); y < rect.Height; y++ {
    for x := uint16(0); x < rect.Width; x++ {
    index := int(x + y*rect.Height)
    color := &colors[index]
    pixbuf[index] =
    uint32(color.R)<<c.PixelFormat.RedShift |
    uint32(color.G)<<c.PixelFormat.GreenShift |
    uint32(color.B)<<c.PixelFormat.BlueShift
    }
    }
    if err := binary.Write(w, byteOrder, pixbuf); err != nil {
    return err
    }
    return nil
    }
    On Thursday, August 22, 2013 9:39:51 AM UTC+2, Vasiliy Tolstov wrote:

    Hmm. No not works. Thanks for patch, but i thins errors that means
    that interface is nil, because of checkbuff initialize pixbuf from
    interface and its type not uint6/16/32 may be add to switch default
    case and create pixbuf by default to []uint32 ?

    2013/8/22 Gerard <gvds...@gmail.com <javascript:>>:
    Oops. That was my mistake. The index is probably x+y*rect.Width.

    Please take a look at the attached diff.

    I have to note however that my approach is only an idea. I think it's a lot
    faster, but the best method is to benchmark the code. And to be honest, I
    don't like the interface{} a lot. It has too many type checks /
    conversions
    / bloat. But I don't know a better way.

    On Tuesday, August 20, 2013 4:46:47 PM UTC+2, Vasiliy Tolstov wrote:

    I'm building rfb client/server packge, all works fine, but i can't
    handle one type of message to create rfb proxy.

    I'm already have function that reads FrameBufferUpdate message from
    rfb server, not i needs to encode it to binary format to send to
    another server (like proxy server).
    How can i write write method to get binary representation of the data
    back?
    Does somebody can help me?

    spec: http://tools.ietf.org/html/rfc6143#section-7.7.1

    Read Method for message is :
    func (msg *FramebufferUpdateMsg) Read(c *Conn, r io.Reader) (Message,
    error) {
    m := &FramebufferUpdateMsg{}
    // Read off the padding
    var padding [1]byte
    if err := binary.Read(r, binary.BigEndian, &padding); err != nil {
    return nil, err
    }
    var numRects uint16
    if err := binary.Read(r, binary.BigEndian, &numRects); err != nil {
    return nil, err
    }
    // Build the map of encodings supported
    encMap := make(map[int32]Encoding)
    for _, enc := range c.Encs {
    encMap[enc.Type()] = enc
    }
    // We must always support the raw encoding
    rawEnc := new(RawEncoding)
    encMap[rawEnc.Type()] = rawEnc
    rects := make([]Rectangle, numRects)
    for i := uint16(0); i < numRects; i++ {
    var encodingType int32
    rect := &rects[i]
    data := []interface{}{
    &rect.X,
    &rect.Y,
    &rect.Width,
    &rect.Height,
    &encodingType,
    }
    for _, val := range data {
    if err := binary.Read(r, binary.BigEndian, val); err != nil {
    return nil, err
    }
    }
    enc, ok := encMap[encodingType]
    if !ok {
    return nil, errors.New("unsupported encoding type") //,
    encodingType)
    }
    var err error
    rect.Enc, err = enc.Read(c, rect, r)
    if err != nil {
    return nil, err
    }
    }
    m.Rectangles = rects
    return m, nil
    }

    Read method for encoding read (encoding is raw)

    type Color struct {
    R, G, B uint16
    }

    type RawEncoding struct {
    Colors []Color
    }

    func (*RawEncoding) Read(c *Conn, rect *Rectangle, r io.Reader)
    (Encoding, error) {
    bytesPerPixel := c.PixelFormat.BPP / 8
    pixelBytes := make([]uint8, bytesPerPixel)
    var byteOrder binary.ByteOrder = binary.LittleEndian
    if c.PixelFormat.BigEndian {
    byteOrder = binary.BigEndian
    }
    colors := make([]Color, rect.Height*rect.Width)
    for y := uint16(0); y < rect.Height; y++ {
    for x := uint16(0); x < rect.Width; x++ {
    if _, err := io.ReadFull(r, pixelBytes); err != nil {
    return nil, err
    }
    var rawPixel uint32
    if c.PixelFormat.BPP == 8 {
    rawPixel = uint32(pixelBytes[0])
    } else if c.PixelFormat.BPP == 16 {
    rawPixel = uint32(byteOrder.Uint16(pixelBytes))
    } else if c.PixelFormat.BPP == 32 {
    rawPixel = byteOrder.Uint32(pixelBytes)
    }
    color := &colors[x+y]
    if c.PixelFormat.TrueColor {
    color.R = uint16((rawPixel << c.PixelFormat.RedShift) &
    uint32(c.PixelFormat.RedMax))
    color.G = uint16((rawPixel << c.PixelFormat.GreenShift) &
    uint32(c.PixelFormat.GreenMax))
    color.B = uint16((rawPixel << c.PixelFormat.BlueShift) &
    uint32(c.PixelFormat.BlueMax))
    } else {
    *color = c.ColorMap[rawPixel]
    }
    }
    }
    return &RawEncoding{Colors: colors}, nil
    }


    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru
    jabber: va...@selfip.ru
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru <javascript:>
    jabber: va...@selfip.ru <javascript:>
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Aug 22, 2013 at 10:12 am

    2013/8/22 Gerard <gvdschoot@gmail.com>:
    Well, in that case it's a lot simpler.
    Hmm. But panic still exists. Is that possible that Reader function have errors?

    panic: runtime error: index out of range

    goroutine 7 [running]:
    _/home/vase/devel/vkvm/vnc.(*RawEncoding).Write(0xc20007aa20,
    0xc20009a000, 0xc2000872e0, 0xc20007aae0, 0xc200073a80, ...)
             /home/vase/devel/vkvm/vnc/encoding.go:178 +0x329
    _/home/vase/devel/vkvm/vnc.(*FramebufferUpdateMsg).Write(0xc200087160,
    0xc20009a000, 0xc20007a5a0, 0xc2000000b0, 0xc2000000b0, ...)
             /home/vase/devel/vkvm/vnc/message.go:435 +0x5d7
    _/home/vase/devel/vkvm/vnc.(*Conn).serverServe(0xc20009a000)
             /home/vase/devel/vkvm/vnc/server.go:125 +0x534
    created by _/home/vase/devel/vkvm/vnc.(*Server).Serve
             /home/vase/devel/vkvm/vnc/server.go:73 +0x159


    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gerard at Aug 22, 2013 at 4:03 pm
    The Read function is now a bit simplified. Maybe you can also apply this to
    the Write function. The simplification doesn't mean less code. But the size
    of rawPixel and the TrueColor boolean is now handled at a higher level
    switch. That means that the for loops have less code to deal with.

    The pixelBufferU32 variable is added so that no memory is allocated with
    each Read(). The same can be created for []uint8 / []uint16 if that's
    required.

    The right shifts ">>" are also simplified. It isn't necessary to have it
    uint32 after the shift. This can stay uint16.


    var (
    pixelBufferU32 []uint32
    )

    func (*RawEncoding) Read(c *Conn, rect *Rectangle, r io.Reader) (Encoding,
    error) {
    m := &RawEncoding{}
    bufferSize := int(rect.Width * rect.Height)

    var byteOrder binary.ByteOrder = binary.LittleEndian
    if c.PixelFormat.BigEndian {
    byteOrder = binary.BigEndian
    }

    colors := make([]Color, rect.Height*rect.Width)

    switch {

    case c.PixelFormat.TrueColor == false :
      // Todo

    case c.PixelFormat.TrueColor && c.PixelFormat.BPP == 8 :

    // Todo
      case c.PixelFormat.TrueColor && c.PixelFormat.BPP == 16 :

    // Todo
      case c.PixelFormat.TrueColor && c.PixelFormat.BPP == 32 :
      if len(pixelBufferU32) != bufferSize {
    pixelBufferU32 = make([]uint32, bufferSize)
    }
      if err := binary.Read(r, byteOrder, &pixelBufferU32); err != nil {
    return nil, err
    }

    for y := uint16(0); y < rect.Height; y++ {
    for x := uint16(0); x < rect.Width; x++ {
    index := x + y*rect.Height

    rawPixel := pixelBufferU32[index]
    color := &colors[index]

    color.R = uint16(rawPixel >> c.PixelFormat.RedShift) & c.PixelFormat.RedMax
    color.G = uint16(rawPixel >> c.PixelFormat.GreenShift) &
    c.PixelFormat.GreenMax
    color.B = uint16(rawPixel >> c.PixelFormat.BlueShift) &
    c.PixelFormat.BlueMax
    }
    }
    }
      m.Colors = colors

    return m, nil
    }

    On Thursday, August 22, 2013 12:12:10 PM UTC+2, Vasiliy Tolstov wrote:

    2013/8/22 Gerard <gvds...@gmail.com <javascript:>>:
    Well, in that case it's a lot simpler.
    Hmm. But panic still exists. Is that possible that Reader function have
    errors?

    panic: runtime error: index out of range

    goroutine 7 [running]:
    _/home/vase/devel/vkvm/vnc.(*RawEncoding).Write(0xc20007aa20,
    0xc20009a000, 0xc2000872e0, 0xc20007aae0, 0xc200073a80, ...)
    /home/vase/devel/vkvm/vnc/encoding.go:178 +0x329
    _/home/vase/devel/vkvm/vnc.(*FramebufferUpdateMsg).Write(0xc200087160,
    0xc20009a000, 0xc20007a5a0, 0xc2000000b0, 0xc2000000b0, ...)
    /home/vase/devel/vkvm/vnc/message.go:435 +0x5d7
    _/home/vase/devel/vkvm/vnc.(*Conn).serverServe(0xc20009a000)
    /home/vase/devel/vkvm/vnc/server.go:125 +0x534
    created by _/home/vase/devel/vkvm/vnc.(*Server).Serve
    /home/vase/devel/vkvm/vnc/server.go:73 +0x159


    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru <javascript:>
    jabber: va...@selfip.ru <javascript:>
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Aug 22, 2013 at 4:17 pm

    2013/8/22 Gerard <gvdschoot@gmail.com>:
    he Read function is now a bit simplified. Maybe you can also apply this to
    the Write function. The simplification doesn't mean less code. But the size
    of rawPixel and the TrueColor boolean is now handled at a higher level
    switch. That means that the for loops have less code to deal with.

    The pixelBufferU32 variable is added so that no memory is allocated with
    each Read(). The same can be created for []uint8 / []uint16 if that's
    required.

    The right shifts ">>" are also simplified. It isn't necessary to have it
    uint32 after the shift. This can stay uint16.

    Main problem still exists: program exited with panic
    (https://bitbucket.org/vtolstov/vkvm/src/master/vnc/encoding.go?at=master):
    server<-client: &{PixelFormat:0xc2000565a0}
    client->server: &{PixelFormat:0xc2000565a0}
    server<-client: &{Encs:[0xc2000873e0 0xc200000050]}
    client->server: &{Encs:[0xc2000873e0 0xc200000050]}
    server<-client: &{Incremental:0 X:0 Y:0 Width:640 Height:480}
    client->server: &{Incremental:0 X:0 Y:0 Width:640 Height:480}
    panic: runtime error: index out of range

    goroutine 6 [running]:
    _/home/vase/devel/vkvm/vnc.(*RawEncoding).Read(0xc2000871e0,
    0xc20009a800, 0xc200087200, 0xc20007a5d0, 0xc200000168, ...)
             /home/vase/devel/vkvm/vnc/encoding.go:202 +0x459
    _/home/vase/devel/vkvm/vnc.(*FramebufferUpdateMsg).Read(0xc200087280,
    0xc20009a800, 0xc20007a5d0, 0xc200000168, 0xc200000168, ...)
             /home/vase/devel/vkvm/vnc/message.go:395 +0x777
    _/home/vase/devel/vkvm/vnc.func·001()
             /home/vase/devel/vkvm/vnc/client.go:223 +0x130
    created by _/home/vase/devel/vkvm/vnc.(*Conn).clientServe
             /home/vase/devel/vkvm/vnc/client.go:232 +0x418




    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gerard at Aug 22, 2013 at 4:38 pm
    In the error you see this line:

          /home/vase/devel/vkvm/vnc/encoding.go:202 +0x459

    If you look at the code you see that it's the index of the buffer that's
    cause the error.

    On line 200 the index variable is created. Change that to: index := x +
    y*rect.Width

    That should do the trick ;-)

    On Tuesday, August 20, 2013 4:46:47 PM UTC+2, Vasiliy Tolstov wrote:

    I'm building rfb client/server packge, all works fine, but i can't
    handle one type of message to create rfb proxy.

    I'm already have function that reads FrameBufferUpdate message from
    rfb server, not i needs to encode it to binary format to send to
    another server (like proxy server).
    How can i write write method to get binary representation of the data
    back?
    Does somebody can help me?

    spec: http://tools.ietf.org/html/rfc6143#section-7.7.1

    Read Method for message is :
    func (msg *FramebufferUpdateMsg) Read(c *Conn, r io.Reader) (Message,
    error) {
    m := &FramebufferUpdateMsg{}
    // Read off the padding
    var padding [1]byte
    if err := binary.Read(r, binary.BigEndian, &padding); err != nil {
    return nil, err
    }
    var numRects uint16
    if err := binary.Read(r, binary.BigEndian, &numRects); err != nil {
    return nil, err
    }
    // Build the map of encodings supported
    encMap := make(map[int32]Encoding)
    for _, enc := range c.Encs {
    encMap[enc.Type()] = enc
    }
    // We must always support the raw encoding
    rawEnc := new(RawEncoding)
    encMap[rawEnc.Type()] = rawEnc
    rects := make([]Rectangle, numRects)
    for i := uint16(0); i < numRects; i++ {
    var encodingType int32
    rect := &rects[i]
    data := []interface{}{
    &rect.X,
    &rect.Y,
    &rect.Width,
    &rect.Height,
    &encodingType,
    }
    for _, val := range data {
    if err := binary.Read(r, binary.BigEndian, val); err != nil {
    return nil, err
    }
    }
    enc, ok := encMap[encodingType]
    if !ok {
    return nil, errors.New("unsupported encoding type") //,
    encodingType)
    }
    var err error
    rect.Enc, err = enc.Read(c, rect, r)
    if err != nil {
    return nil, err
    }
    }
    m.Rectangles = rects
    return m, nil
    }

    Read method for encoding read (encoding is raw)

    type Color struct {
    R, G, B uint16
    }

    type RawEncoding struct {
    Colors []Color
    }

    func (*RawEncoding) Read(c *Conn, rect *Rectangle, r io.Reader)
    (Encoding, error) {
    bytesPerPixel := c.PixelFormat.BPP / 8
    pixelBytes := make([]uint8, bytesPerPixel)
    var byteOrder binary.ByteOrder = binary.LittleEndian
    if c.PixelFormat.BigEndian {
    byteOrder = binary.BigEndian
    }
    colors := make([]Color, rect.Height*rect.Width)
    for y := uint16(0); y < rect.Height; y++ {
    for x := uint16(0); x < rect.Width; x++ {
    if _, err := io.ReadFull(r, pixelBytes); err != nil {
    return nil, err
    }
    var rawPixel uint32
    if c.PixelFormat.BPP == 8 {
    rawPixel = uint32(pixelBytes[0])
    } else if c.PixelFormat.BPP == 16 {
    rawPixel = uint32(byteOrder.Uint16(pixelBytes))
    } else if c.PixelFormat.BPP == 32 {
    rawPixel = byteOrder.Uint32(pixelBytes)
    }
    color := &colors[x+y]
    if c.PixelFormat.TrueColor {
    color.R = uint16((rawPixel << c.PixelFormat.RedShift) &
    uint32(c.PixelFormat.RedMax))
    color.G = uint16((rawPixel << c.PixelFormat.GreenShift) &
    uint32(c.PixelFormat.GreenMax))
    color.B = uint16((rawPixel << c.PixelFormat.BlueShift) &
    uint32(c.PixelFormat.BlueMax))
    } else {
    *color = c.ColorMap[rawPixel]
    }
    }
    }
    return &RawEncoding{Colors: colors}, nil
    }


    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru <javascript:>
    jabber: va...@selfip.ru <javascript:>
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Aug 22, 2013 at 4:47 pm

    2013/8/22 Gerard <gvdschoot@gmail.com>:
    In the error you see this line:

    /home/vase/devel/vkvm/vnc/encoding.go:202 +0x459

    If you look at the code you see that it's the index of the buffer that's
    cause the error.

    On line 200 the index variable is created. Change that to: index := x +
    y*rect.Width

    That should do the trick ;-)

    Nothing changed =(

    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gerard at Aug 22, 2013 at 5:04 pm
    It's like the pilgrims walk. Three steps forward, two down.

    If you use the range, it should work.

    Apply this also for Write()

    But don't use the same buffers for Read() and Write().


    func (*RawEncoding) Read(c *Conn, rect *Rectangle, r io.Reader) (Encoding,
    error) {
    m := &RawEncoding{}
    bufferSize := int(rect.Width * rect.Height)

    var byteOrder binary.ByteOrder = binary.LittleEndian
    if c.PixelFormat.BigEndian {
    byteOrder = binary.BigEndian
    }

    colors := make([]Color, int(rect.Height*rect.Width))

    switch {

    case c.PixelFormat.TrueColor == false:

    // Todo

    case c.PixelFormat.TrueColor && c.PixelFormat.BPP == 8:

    // Todo

    case c.PixelFormat.TrueColor && c.PixelFormat.BPP == 16:

    // Todo

    case c.PixelFormat.TrueColor && c.PixelFormat.BPP == 32:

    if len(pixelBufferU32) != bufferSize {
    pixelBufferU32 = make([]uint32, bufferSize)
    }

    if err := binary.Read(r, byteOrder, &pixelBufferU32); err != nil {
    return nil, err
    }

    for index, rawPixel := range pixelBufferU32 {
    color := &colors[index]

    color.R = uint16(rawPixel>>c.PixelFormat.RedShift) & c.PixelFormat.RedMax
    color.G = uint16(rawPixel>>c.PixelFormat.GreenShift) &
    c.PixelFormat.GreenMax
    color.B = uint16(rawPixel>>c.PixelFormat.BlueShift) & c.PixelFormat.BlueMax
    }
    }

    m.Colors = colors
    return m, nil
    }




    On Thursday, August 22, 2013 6:47:30 PM UTC+2, Vasiliy Tolstov wrote:

    2013/8/22 Gerard <gvds...@gmail.com <javascript:>>:
    In the error you see this line:

    /home/vase/devel/vkvm/vnc/encoding.go:202 +0x459

    If you look at the code you see that it's the index of the buffer that's
    cause the error.

    On line 200 the index variable is created. Change that to: index := x +
    y*rect.Width

    That should do the trick ;-)

    Nothing changed =(

    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru <javascript:>
    jabber: va...@selfip.ru <javascript:>
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Aug 22, 2013 at 5:27 pm

    2013/8/22 Gerard <gvdschoot@gmail.com>:
    It's like the pilgrims walk. Three steps forward, two down.

    If you use the range, it should work.

    Apply this also for Write()

    But don't use the same buffers for Read() and Write().

    Thanks. I'm rewrite code, now panic dissapeared, but now i get full
    black window without any text on it.

    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gerard at Aug 22, 2013 at 6:01 pm
    The pilgrims walk is of course three steps forward and two steps backwards
    thus taking five steps in order to advance one.

    The only thing I can really think about is that the switch in the Read()
    function has some case statements that don't have an error printing (only
    TODO) and there should be a default case in that switch (with error
    printing).
    On Thursday, August 22, 2013 7:26:58 PM UTC+2, Vasiliy Tolstov wrote:

    2013/8/22 Gerard <gvds...@gmail.com <javascript:>>:
    It's like the pilgrims walk. Three steps forward, two down.

    If you use the range, it should work.

    Apply this also for Write()

    But don't use the same buffers for Read() and Write().

    Thanks. I'm rewrite code, now panic dissapeared, but now i get full
    black window without any text on it.

    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru <javascript:>
    jabber: va...@selfip.ru <javascript:>
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Aug 22, 2013 at 6:14 pm

    2013/8/22 Gerard <gvdschoot@gmail.com>:
    The pilgrims walk is of course three steps forward and two steps backwards
    thus taking five steps in order to advance one.

    The only thing I can really think about is that the switch in the Read()
    function has some case statements that don't have an error printing (only
    TODO) and there should be a default case in that switch (with error
    printing).

    I'm add printf, when i run code nothing display about cases in
    read/write functions.

    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gerard at Aug 22, 2013 at 6:27 pm
    Coding is always puzzling. I don't think I can help you with this problem.
    Maybe someone else.
    On Thursday, August 22, 2013 8:14:36 PM UTC+2, Vasiliy Tolstov wrote:

    2013/8/22 Gerard <gvds...@gmail.com <javascript:>>:
    The pilgrims walk is of course three steps forward and two steps backwards
    thus taking five steps in order to advance one.

    The only thing I can really think about is that the switch in the Read()
    function has some case statements that don't have an error printing (only
    TODO) and there should be a default case in that switch (with error
    printing).

    I'm add printf, when i run code nothing display about cases in
    read/write functions.

    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru <javascript:>
    jabber: va...@selfip.ru <javascript:>
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Aug 22, 2013 at 7:00 pm

    On Aug 22, 2013 10:27 PM, "Gerard" wrote:
    Coding is always puzzling. I don't think I can help you with this
    problem. Maybe someone else.
    Big thanks for help.
    On Thursday, August 22, 2013 8:14:36 PM UTC+2, Vasiliy Tolstov wrote:

    2013/8/22 Gerard <gvds...@gmail.com>:
    The pilgrims walk is of course three steps forward and two steps
    backwards
    thus taking five steps in order to advance one.

    The only thing I can really think about is that the switch in the
    Read()
    function has some case statements that don't have an error printing
    (only
    TODO) and there should be a default case in that switch (with error
    printing).

    I'm add printf, when i run code nothing display about cases in
    read/write functions.

    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru
    jabber: va...@selfip.ru
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Aug 22, 2013 at 7:07 pm

    On Aug 22, 2013 10:27 PM, "Gerard" wrote:
    Coding is always puzzling. I don't think I can help you with this
    problem. Maybe someone else.
    >

    Hmm I think I forgot that rect bytes contains pixels not width*height, but
    bpp/8*width*height.
    That means buffer size needs multiply to 4.
    On Thursday, August 22, 2013 8:14:36 PM UTC+2, Vasiliy Tolstov wrote:

    2013/8/22 Gerard <gvds...@gmail.com>:
    The pilgrims walk is of course three steps forward and two steps
    backwards
    thus taking five steps in order to advance one.

    The only thing I can really think about is that the switch in the
    Read()
    function has some case statements that don't have an error printing
    (only
    TODO) and there should be a default case in that switch (with error
    printing).

    I'm add printf, when i run code nothing display about cases in
    read/write functions.

    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru
    jabber: va...@selfip.ru
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gerard at Aug 22, 2013 at 7:27 pm

    On Thursday, August 22, 2013 9:06:55 PM UTC+2, Vasiliy Tolstov wrote:

    On Aug 22, 2013 10:27 PM, "Gerard" <gvds...@gmail.com <javascript:>>
    wrote:
    Coding is always puzzling. I don't think I can help you with this
    problem. Maybe someone else.
    Hmm I think I forgot that rect bytes contains pixels not width*height, but
    bpp/8*width*height.
    That means buffer size needs multiply to 4.
    That would be true if the buffer type was []byte. However, the buffer is
    []uint32 (in case BPP = 32)
    On Thursday, August 22, 2013 8:14:36 PM UTC+2, Vasiliy Tolstov wrote:

    2013/8/22 Gerard <gvds...@gmail.com>:
    The pilgrims walk is of course three steps forward and two steps
    backwards
    thus taking five steps in order to advance one.

    The only thing I can really think about is that the switch in the
    Read()
    function has some case statements that don't have an error printing
    (only
    TODO) and there should be a default case in that switch (with error
    printing).

    I'm add printf, when i run code nothing display about cases in
    read/write functions.

    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru
    jabber: va...@selfip.ru
    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Aug 22, 2013 at 10:09 pm

    2013/8/22 Gerard <gvdschoot@gmail.com>:
    That would be true if the buffer type was []byte. However, the buffer is
    []uint32 (in case BPP = 32)

    =(. But i think erros in case of size that i read from reader...

    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gerard at Aug 23, 2013 at 7:39 am
    Could you please enable the issue tracker for the bitbucket repository? See
    this<https://confluence.atlassian.com/display/BITBUCKET/Use+the+issue+tracker>link fow how to enable this. Then we can continue the discussion over there
    with a new issue.
    On Friday, August 23, 2013 12:09:21 AM UTC+2, Vasiliy Tolstov wrote:

    2013/8/22 Gerard <gvds...@gmail.com <javascript:>>:
    That would be true if the buffer type was []byte. However, the buffer is
    []uint32 (in case BPP = 32)

    =(. But i think erros in case of size that i read from reader...

    --
    Vasiliy Tolstov,
    e-mail: v.to...@selfip.ru <javascript:>
    jabber: va...@selfip.ru <javascript:>
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Aug 23, 2013 at 9:36 am

    2013/8/23 Gerard <gvdschoot@gmail.com>:
    Could you please enable the issue tracker for the bitbucket repository? See
    this link fow how to enable this. Then we can continue the discussion over
    there with a new issue.

    Thanks https://bitbucket.org/vtolstov/vkvm/issue/1/vnc-encodinggo-not-work-properly
    I'm create public issue tracker.

    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 20, '13 at 2:47p
activeAug 23, '13 at 9:36a
posts29
users2
websitegolang.org

2 users in discussion

Vasiliy Tolstov: 16 posts Gerard: 13 posts

People

Translate

site design / logo © 2022 Grokbase