-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
336 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package internal | ||
|
||
import ( | ||
"io" | ||
"unicode/utf8" | ||
) | ||
|
||
// ReadN 精准地读取len(data)个字节, 否则返回错误 | ||
func ReadN(reader io.Reader, data []byte) error { | ||
_, err := io.ReadFull(reader, data) | ||
return err | ||
} | ||
|
||
func WriteN(writer io.Writer, content []byte) error { | ||
_, err := writer.Write(content) | ||
return err | ||
} | ||
|
||
func CheckEncoding(opcode uint8, payload []byte) bool { | ||
switch opcode { | ||
case 1, 8: | ||
return utf8.Valid(payload) | ||
default: | ||
return true | ||
} | ||
} | ||
|
||
type Payload interface { | ||
io.WriterTo | ||
Len() int | ||
CheckEncoding(enabled bool, opcode uint8) bool | ||
} | ||
|
||
type Buffers [][]byte | ||
|
||
func (b Buffers) CheckEncoding(enabled bool, opcode uint8) bool { | ||
if enabled { | ||
for i, _ := range b { | ||
if !CheckEncoding(opcode, b[i]) { | ||
return false | ||
} | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func (b Buffers) Len() int { | ||
var sum = 0 | ||
for i, _ := range b { | ||
sum += len(b[i]) | ||
} | ||
return sum | ||
} | ||
|
||
// WriteTo 可重复写 | ||
func (b Buffers) WriteTo(w io.Writer) (int64, error) { | ||
var n = 0 | ||
for i, _ := range b { | ||
x, err := w.Write(b[i]) | ||
n += x | ||
if err != nil { | ||
return int64(n), err | ||
} | ||
} | ||
return int64(n), nil | ||
} | ||
|
||
type Bytes []byte | ||
|
||
func (b Bytes) CheckEncoding(enabled bool, opcode uint8) bool { | ||
if enabled { | ||
return CheckEncoding(opcode, b) | ||
} | ||
return true | ||
} | ||
|
||
func (b Bytes) Len() int { | ||
return len(b) | ||
} | ||
|
||
// WriteTo 可重复写 | ||
func (b Bytes) WriteTo(w io.Writer) (int64, error) { | ||
n, err := w.Write(b) | ||
return int64(n), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package internal | ||
|
||
import ( | ||
"bytes" | ||
"net" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIOUtil(t *testing.T) { | ||
var as = assert.New(t) | ||
|
||
t.Run("", func(t *testing.T) { | ||
var reader = strings.NewReader("hello") | ||
var p = make([]byte, 5) | ||
var err = ReadN(reader, p) | ||
as.Nil(err) | ||
}) | ||
|
||
t.Run("", func(t *testing.T) { | ||
var writer = bytes.NewBufferString("") | ||
var err = WriteN(writer, nil) | ||
as.NoError(err) | ||
}) | ||
|
||
t.Run("", func(t *testing.T) { | ||
var writer = bytes.NewBufferString("") | ||
var p = []byte("hello") | ||
var err = WriteN(writer, p) | ||
as.NoError(err) | ||
}) | ||
} | ||
|
||
func TestBuffers_WriteTo(t *testing.T) { | ||
t.Run("", func(t *testing.T) { | ||
var b = Buffers{ | ||
[]byte("he"), | ||
[]byte("llo"), | ||
} | ||
var w = bytes.NewBufferString("") | ||
b.WriteTo(w) | ||
n, _ := b.WriteTo(w) | ||
assert.Equal(t, w.String(), "hellohello") | ||
assert.Equal(t, n, int64(5)) | ||
assert.Equal(t, b.Len(), 5) | ||
assert.True(t, b.CheckEncoding(true, 1)) | ||
}) | ||
|
||
t.Run("", func(t *testing.T) { | ||
var conn, _ = net.Pipe() | ||
_ = conn.Close() | ||
var b = Buffers{ | ||
[]byte("he"), | ||
[]byte("llo"), | ||
} | ||
_, err := b.WriteTo(conn) | ||
assert.Error(t, err) | ||
}) | ||
|
||
t.Run("", func(t *testing.T) { | ||
var str = "你好" | ||
var b = Buffers{ | ||
[]byte("he"), | ||
[]byte(str[2:]), | ||
} | ||
assert.False(t, b.CheckEncoding(true, 1)) | ||
}) | ||
} | ||
|
||
func TestBytes_WriteTo(t *testing.T) { | ||
t.Run("", func(t *testing.T) { | ||
var b = Bytes("hello") | ||
var w = bytes.NewBufferString("") | ||
b.WriteTo(w) | ||
n, _ := b.WriteTo(w) | ||
assert.Equal(t, w.String(), "hellohello") | ||
assert.Equal(t, n, int64(5)) | ||
assert.Equal(t, b.Len(), 5) | ||
}) | ||
|
||
t.Run("", func(t *testing.T) { | ||
var str = "你好" | ||
var b = Bytes(str[2:]) | ||
assert.False(t, b.CheckEncoding(true, 1)) | ||
assert.True(t, b.CheckEncoding(false, 1)) | ||
assert.True(t, b.CheckEncoding(true, 2)) | ||
}) | ||
} |
Oops, something went wrong.