Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revised the style of error handling #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 18 additions & 32 deletions netio/netio.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,35 @@ func WritePacket(output io.Writer, packet interface{}) error {
// type switch
switch value := val.(type) {
case uint16:
_, err := Write(output, utils.Uint16ToBytes(value))
if err != nil {
if _, err := Write(output, utils.Uint16ToBytes(value)); err != nil {
return err
}
case uint32:
_, err := Write(output, utils.Uint32ToBytes(value))
if err != nil {
if _, err := Write(output, utils.Uint32ToBytes(value)); err != nil {
return err
}
case uint64:
_, err := Write(output, utils.Uint64ToBytes(value))
if err != nil {
if _, err := Write(output, utils.Uint64ToBytes(value)); err != nil {
return err
}
case string:
_, err := Write(output, []byte(value))
if err != nil {
if _, err := Write(output, []byte(value)); err != nil {
return err
}
case []byte:
_, err := Write(output, value)
if err != nil {
if _, err := Write(output, value); err != nil {
return err
}
case [2]byte:
_, err := Write(output, value[0:])
if err != nil {
if _, err := Write(output, value[0:]); err != nil {
return err
}
case [4]byte:
_, err := Write(output, value[0:])
if err != nil {
if _, err := Write(output, value[0:]); err != nil {
return err
}
case [32]byte:
_, err := Write(output, value[0:])
if err != nil {
if _, err := Write(output, value[0:]); err != nil {
return err
}
default:
Expand Down Expand Up @@ -108,22 +100,19 @@ func ReadPacket(input io.Reader, packet interface{}) error {
}
case uint16:
var buf [2]byte
_, err := Read(input, buf[0:])
if err != nil {
if _, err := Read(input, buf[0:]); err != nil {
return err
}
f.SetUint(uint64(utils.BytesToUint16(buf[0:])))
case uint32:
var buf [4]byte
_, err := Read(input, buf[0:])
if err != nil {
if _, err := Read(input, buf[0:]); err != nil {
return err
}
f.SetUint(uint64(utils.BytesToUint32(buf[0:])))
case uint64:
var buf [8]byte
_, err := Read(input, buf[0:])
if err != nil {
if _, err := Read(input, buf[0:]); err != nil {
return err
}
f.SetUint(uint64(utils.BytesToUint64(buf[0:])))
Expand All @@ -148,30 +137,28 @@ func ReadPacket(input io.Reader, packet interface{}) error {
return nil
}
buf := make([]byte, length)
_, err := Read(input, buf[0:])
if err != nil {
if _, err := Read(input, buf[0:]); err != nil {
return err
}

f.SetBytes(buf)
}
case [2]byte:
var buf [2]byte
_, err := Read(input, buf[0:])
if err != nil {
if _, err := Read(input, buf[0:]); err != nil {
return err
}
f.Set(reflect.ValueOf(buf))
case [4]byte:
var buf [4]byte
_, err := Read(input, buf[0:])
if err != nil {
if _, err := Read(input, buf[0:]); err != nil {
return err
}

f.Set(reflect.ValueOf(buf))
case [32]byte:
var buf [32]byte
_, err := Read(input, buf[0:])
if err != nil {
if _, err := Read(input, buf[0:]); err != nil {
return err
}
// 使用reflect给array类型赋值的方法
Expand Down Expand Up @@ -208,8 +195,7 @@ func readUntilSeparator(input io.Reader, separator string) (string, error) {
i := 0
var one [1]byte
for {
_, err := Read(input, one[0:])
if err != nil {
if _, err := Read(input, one[0:]); err != nil {
return "", err
}
if kmp.Pattern[i] == one[0] {
Expand Down