Skip to content

Commit eb92580

Browse files
authored
Use net.Buffers to write multiple slices to connection
Closes #346.
1 parent 4835f71 commit eb92580

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

conn.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ func (c *Conn) writeFatal(err error) error {
370370
return err
371371
}
372372

373-
func (c *Conn) write(frameType int, deadline time.Time, bufs ...[]byte) error {
373+
func (c *Conn) write(frameType int, deadline time.Time, buf0, buf1 []byte) error {
374374
<-c.mu
375375
defer func() { c.mu <- true }()
376376

@@ -382,15 +382,14 @@ func (c *Conn) write(frameType int, deadline time.Time, bufs ...[]byte) error {
382382
}
383383

384384
c.conn.SetWriteDeadline(deadline)
385-
for _, buf := range bufs {
386-
if len(buf) > 0 {
387-
_, err := c.conn.Write(buf)
388-
if err != nil {
389-
return c.writeFatal(err)
390-
}
391-
}
385+
if len(buf1) == 0 {
386+
_, err = c.conn.Write(buf0)
387+
} else {
388+
err = c.writeBufs(buf0, buf1)
389+
}
390+
if err != nil {
391+
return c.writeFatal(err)
392392
}
393-
394393
if frameType == CloseMessage {
395394
c.writeFatal(ErrCloseSent)
396395
}

conn_write.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2016 The Gorilla WebSocket Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build go1.8
6+
7+
package websocket
8+
9+
import "net"
10+
11+
func (c *Conn) writeBufs(bufs ...[]byte) error {
12+
b := net.Buffers(bufs)
13+
_, err := b.WriteTo(c.conn)
14+
return err
15+
}

conn_write_legacy.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2016 The Gorilla WebSocket Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !go1.8
6+
7+
package websocket
8+
9+
func (c *Conn) writeBufs(bufs ...[]byte) error {
10+
for _, buf := range bufs {
11+
if len(buf) > 0 {
12+
if _, err := c.conn.Write(buf); err != nil {
13+
return err
14+
}
15+
}
16+
}
17+
return nil
18+
}

0 commit comments

Comments
 (0)