2017-04-01 04:19:50 +00:00
|
|
|
package server
|
2017-02-25 05:56:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
|
|
|
//WedgeConn -- A buffered IO infront of a connection allowing peeking, and switching connections.
|
|
|
|
type WedgeConn struct {
|
|
|
|
reader *bufio.Reader
|
|
|
|
net.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
//NewWedgeConn -- Constructor
|
|
|
|
func NewWedgeConn(c net.Conn) (p *WedgeConn) {
|
|
|
|
p = new(WedgeConn)
|
|
|
|
p.reader = bufio.NewReader(c)
|
|
|
|
p.Conn = c
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//NewWedgeConnSize -- Constructor
|
|
|
|
func NewWedgeConnSize(c net.Conn, size int) (p *WedgeConn) {
|
|
|
|
p = new(WedgeConn)
|
|
|
|
p.reader = bufio.NewReaderSize(c, size)
|
|
|
|
p.Conn = c
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-11 20:28:49 +00:00
|
|
|
//Discard - discard a number of bytes, perhaps after peeking at the
|
2017-03-22 22:33:09 +00:00
|
|
|
func (w *WedgeConn) Discard(n int) (int, error) {
|
2017-03-11 20:28:49 +00:00
|
|
|
return w.reader.Discard(n)
|
|
|
|
}
|
|
|
|
|
2017-02-26 18:35:06 +00:00
|
|
|
//Peek - Get a number of bytes outof the buffer, but allow the buffer to be replayed once read
|
2017-02-25 05:56:40 +00:00
|
|
|
func (w *WedgeConn) Peek(n int) ([]byte, error) {
|
|
|
|
return w.reader.Peek(n)
|
|
|
|
}
|
|
|
|
|
2017-03-11 20:28:49 +00:00
|
|
|
//ReadByte -- A normal reader.
|
|
|
|
func (w *WedgeConn) ReadByte() (byte, error) {
|
|
|
|
return w.reader.ReadByte()
|
|
|
|
}
|
|
|
|
|
2017-02-25 05:56:40 +00:00
|
|
|
//Read -- A normal reader.
|
|
|
|
func (w *WedgeConn) Read(p []byte) (int, error) {
|
2017-03-22 22:33:09 +00:00
|
|
|
return w.reader.Read(p)
|
2017-02-25 05:56:40 +00:00
|
|
|
}
|
2017-02-26 18:35:06 +00:00
|
|
|
|
|
|
|
//Buffered --
|
|
|
|
func (w *WedgeConn) Buffered() int {
|
|
|
|
return w.reader.Buffered()
|
|
|
|
}
|
|
|
|
|
|
|
|
//PeekAll --
|
|
|
|
// - get all the chars available
|
|
|
|
// - pass then back
|
2017-03-22 22:33:09 +00:00
|
|
|
func (w *WedgeConn) PeekAll() ([]byte, error) {
|
2017-03-23 23:57:43 +00:00
|
|
|
// We first peek with 1 so that if there is no buffered data the reader will
|
|
|
|
// fill the buffer before we read how much data is buffered.
|
|
|
|
if _, err := w.Peek(1); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-22 22:33:09 +00:00
|
|
|
return w.Peek(w.Buffered())
|
2017-02-26 18:35:06 +00:00
|
|
|
}
|