dongwo1914 2017-11-17 17:29
浏览 12

net.Conn转换函数应该保留原始数据分割吗?

To clarify, a net.Conn transform() function transforms data write to/read from a connection, an example is as follow:

type TransformedConn struct {
  net.Conn
}

func (c *TransformedConn) Read(p []byte) (int, error) {
  n, err := c.Conn.Read(p)
  transform(p)
  return n, err
}

func (c *TransformedConn) Write(p []byte) (int, error) {
  t := append([]byte{}, p...) // as `p` must not be modified per io.Writer doc
  transform(t)
  return c.Conn.Write(t)
}

Similarly, crypto/cipher.StreamReader and crypto/cipher.StreamWriter perform a similar operation.

As required by io.Writer.Write(), it cannot modify the input buffer. In case the size of the buffer is huge, creating a buffer of the same size will be very expensive. So I'm wondering if it's ok for such transform function to write data out in smaller chunks, to allow us using a smaller scratch space?

I've found such chunking technique will cause some program to fail, especially the ones use io.ReadFull() and io.ReadAtLeast(). Because they implicitly expect that the data to arrive will from a single read() call.

Could someone point me a doc or a spec on how to write such transform functions?

Thanks!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来