chunk message writer

This commit is contained in:
xdrm-brackets 2018-05-06 01:05:38 +02:00
parent 361684f634
commit 86dba9aa0b
1 changed files with 25 additions and 9 deletions

View File

@ -10,6 +10,7 @@ var UnmaskedFrameErr = fmt.Errorf("Received unmasked frame")
// Maximum Header Size = Final/OpCode + isMask/Length + Length + Mask // Maximum Header Size = Final/OpCode + isMask/Length + Length + Mask
const maximumHeaderSize = 1 + 1 + 8 + 4 const maximumHeaderSize = 1 + 1 + 8 + 4
const maxWriteChunk = 0x7fff
// Lists websocket close status // Lists websocket close status
type MessageError uint16 type MessageError uint16
@ -28,12 +29,12 @@ const (
type MessageType byte type MessageType byte
const ( const (
CONTINUATION MessageType = 0x0 CONTINUATION MessageType = 0x00
TEXT MessageType = 0x1 TEXT MessageType = 0x01
BINARY MessageType = 0x2 BINARY MessageType = 0x02
CLOSE MessageType = 0x8 CLOSE MessageType = 0x08
PING MessageType = 0x9 PING MessageType = 0x09
PONG MessageType = 0xa PONG MessageType = 0x0a
); );
@ -200,9 +201,24 @@ func (m Message) Send(writer io.Writer) error {
writeBuf = append(writeBuf, header...) writeBuf = append(writeBuf, header...)
writeBuf = append(writeBuf, m.Data[0:m.Size]...) writeBuf = append(writeBuf, m.Data[0:m.Size]...)
/* (4) Send over socket */ /* (4) Send over socket by chunks */
_, err := writer.Write(writeBuf) toWrite := len(header) + int(m.Size)
cursor := 0
for cursor < toWrite {
maxBoundary := cursor+maxWriteChunk
if maxBoundary > toWrite {
maxBoundary = toWrite
}
// Try to wrote (at max 1024 bytes) //
nbwritten, err := writer.Write(writeBuf[cursor:maxBoundary])
if err != nil { return err } if err != nil { return err }
// Update cursor //
cursor += nbwritten
}
return nil return nil
} }