Connection -> MConnection, huge refactor. True multiplexing.

This commit is contained in:
Jae Kwon
2014-07-28 01:41:25 -07:00
parent 197c8328c9
commit 34fe442514
19 changed files with 824 additions and 653 deletions
+27 -13
View File
@@ -35,21 +35,35 @@ func (self ByteSlice) WriteTo(w io.Writer) (n int64, err error) {
return int64(n_ + 4), err
}
func ReadByteSliceSafe(r io.Reader) (ByteSlice, error) {
length, err := ReadUInt32Safe(r)
if err != nil {
return nil, err
}
bytes := make([]byte, int(length))
_, err = io.ReadFull(r, bytes)
if err != nil {
return nil, err
}
return bytes, nil
func (self ByteSlice) Reader() io.Reader {
return bytes.NewReader([]byte(self))
}
func ReadByteSlice(r io.Reader) ByteSlice {
bytes, err := ReadByteSliceSafe(r)
func ReadByteSliceSafe(r io.Reader) (bytes ByteSlice, n int64, err error) {
length, n_, err := ReadUInt32Safe(r)
n += n_
if err != nil {
return nil, n, err
}
bytes = make([]byte, int(length))
n__, err := io.ReadFull(r, bytes)
n += int64(n__)
if err != nil {
return nil, n, err
}
return bytes, n, nil
}
func ReadByteSliceN(r io.Reader) (bytes ByteSlice, n int64) {
bytes, n, err := ReadByteSliceSafe(r)
if err != nil {
panic(err)
}
return bytes, n
}
func ReadByteSlice(r io.Reader) (bytes ByteSlice) {
bytes, _, err := ReadByteSliceSafe(r)
if err != nil {
panic(err)
}
+29 -38
View File
@@ -5,21 +5,19 @@ import (
)
const (
TYPE_NIL = Byte(0x00)
TYPE_BYTE = Byte(0x01)
TYPE_INT8 = Byte(0x02)
TYPE_UINT8 = Byte(0x03)
TYPE_INT16 = Byte(0x04)
TYPE_UINT16 = Byte(0x05)
TYPE_INT32 = Byte(0x06)
TYPE_UINT32 = Byte(0x07)
TYPE_INT64 = Byte(0x08)
TYPE_UINT64 = Byte(0x09)
TYPE_NIL = Byte(0x00)
TYPE_BYTE = Byte(0x01)
TYPE_INT8 = Byte(0x02)
TYPE_UINT8 = Byte(0x03)
TYPE_INT16 = Byte(0x04)
TYPE_UINT16 = Byte(0x05)
TYPE_INT32 = Byte(0x06)
TYPE_UINT32 = Byte(0x07)
TYPE_INT64 = Byte(0x08)
TYPE_UINT64 = Byte(0x09)
TYPE_STRING = Byte(0x10)
TYPE_BYTESLICE = Byte(0x11)
TYPE_TIME = Byte(0x20)
TYPE_TIME = Byte(0x20)
)
func GetBinaryType(o Binary) Byte {
@@ -44,57 +42,50 @@ func GetBinaryType(o Binary) Byte {
return TYPE_INT64
case UInt64:
return TYPE_UINT64
case Int:
panic("Int not supported")
case UInt:
panic("UInt not supported")
case String:
return TYPE_STRING
case ByteSlice:
return TYPE_BYTESLICE
case Time:
return TYPE_TIME
default:
panic("Unsupported type")
}
}
func ReadBinary(r io.Reader) Binary {
type_ := ReadByte(r)
func ReadBinaryN(r io.Reader) (o Binary, n int64) {
type_, n_ := ReadByteN(r)
n += n_
switch type_ {
case TYPE_NIL:
return nil
o, n_ = nil, 0
case TYPE_BYTE:
return ReadByte(r)
o, n_ = ReadByteN(r)
case TYPE_INT8:
return ReadInt8(r)
o, n_ = ReadInt8N(r)
case TYPE_UINT8:
return ReadUInt8(r)
o, n_ = ReadUInt8N(r)
case TYPE_INT16:
return ReadInt16(r)
o, n_ = ReadInt16N(r)
case TYPE_UINT16:
return ReadUInt16(r)
o, n_ = ReadUInt16N(r)
case TYPE_INT32:
return ReadInt32(r)
o, n_ = ReadInt32N(r)
case TYPE_UINT32:
return ReadUInt32(r)
o, n_ = ReadUInt32N(r)
case TYPE_INT64:
return ReadInt64(r)
o, n_ = ReadInt64N(r)
case TYPE_UINT64:
return ReadUInt64(r)
o, n_ = ReadUInt64N(r)
case TYPE_STRING:
return ReadString(r)
o, n_ = ReadStringN(r)
case TYPE_BYTESLICE:
return ReadByteSlice(r)
o, n_ = ReadByteSliceN(r)
case TYPE_TIME:
return ReadTime(r)
o, n_ = ReadTimeN(r)
default:
panic("Unsupported type")
}
n += n_
return o, n
}
+117 -113
View File
@@ -40,17 +40,25 @@ func (self Byte) WriteTo(w io.Writer) (int64, error) {
return int64(n), err
}
func ReadByteSafe(r io.Reader) (Byte, error) {
func ReadByteSafe(r io.Reader) (Byte, int64, error) {
buf := [1]byte{0}
_, err := io.ReadFull(r, buf[:])
n, err := io.ReadFull(r, buf[:])
if err != nil {
return 0, err
return 0, int64(n), err
}
return Byte(buf[0]), nil
return Byte(buf[0]), int64(n), nil
}
func ReadByteN(r io.Reader) (Byte, int64) {
b, n, err := ReadByteSafe(r)
if err != nil {
panic(err)
}
return b, n
}
func ReadByte(r io.Reader) Byte {
b, err := ReadByteSafe(r)
b, _, err := ReadByteSafe(r)
if err != nil {
panic(err)
}
@@ -80,17 +88,25 @@ func (self Int8) WriteTo(w io.Writer) (int64, error) {
return int64(n), err
}
func ReadInt8Safe(r io.Reader) (Int8, error) {
func ReadInt8Safe(r io.Reader) (Int8, int64, error) {
buf := [1]byte{0}
_, err := io.ReadFull(r, buf[:])
n, err := io.ReadFull(r, buf[:])
if err != nil {
return Int8(0), err
return Int8(0), int64(n), err
}
return Int8(buf[0]), nil
return Int8(buf[0]), int64(n), nil
}
func ReadInt8N(r io.Reader) (Int8, int64) {
b, n, err := ReadInt8Safe(r)
if err != nil {
panic(err)
}
return b, n
}
func ReadInt8(r io.Reader) Int8 {
b, err := ReadInt8Safe(r)
b, _, err := ReadInt8Safe(r)
if err != nil {
panic(err)
}
@@ -120,17 +136,25 @@ func (self UInt8) WriteTo(w io.Writer) (int64, error) {
return int64(n), err
}
func ReadUInt8Safe(r io.Reader) (UInt8, error) {
func ReadUInt8Safe(r io.Reader) (UInt8, int64, error) {
buf := [1]byte{0}
_, err := io.ReadFull(r, buf[:])
n, err := io.ReadFull(r, buf[:])
if err != nil {
return UInt8(0), err
return UInt8(0), int64(n), err
}
return UInt8(buf[0]), nil
return UInt8(buf[0]), int64(n), nil
}
func ReadUInt8N(r io.Reader) (UInt8, int64) {
b, n, err := ReadUInt8Safe(r)
if err != nil {
panic(err)
}
return b, n
}
func ReadUInt8(r io.Reader) UInt8 {
b, err := ReadUInt8Safe(r)
b, _, err := ReadUInt8Safe(r)
if err != nil {
panic(err)
}
@@ -162,17 +186,25 @@ func (self Int16) WriteTo(w io.Writer) (int64, error) {
return int64(n), err
}
func ReadInt16Safe(r io.Reader) (Int16, error) {
func ReadInt16Safe(r io.Reader) (Int16, int64, error) {
buf := [2]byte{0}
_, err := io.ReadFull(r, buf[:])
n, err := io.ReadFull(r, buf[:])
if err != nil {
return Int16(0), err
return Int16(0), int64(n), err
}
return Int16(binary.LittleEndian.Uint16(buf[:])), nil
return Int16(binary.LittleEndian.Uint16(buf[:])), int64(n), nil
}
func ReadInt16N(r io.Reader) (Int16, int64) {
b, n, err := ReadInt16Safe(r)
if err != nil {
panic(err)
}
return b, n
}
func ReadInt16(r io.Reader) Int16 {
b, err := ReadInt16Safe(r)
b, _, err := ReadInt16Safe(r)
if err != nil {
panic(err)
}
@@ -204,17 +236,25 @@ func (self UInt16) WriteTo(w io.Writer) (int64, error) {
return int64(n), err
}
func ReadUInt16Safe(r io.Reader) (UInt16, error) {
func ReadUInt16Safe(r io.Reader) (UInt16, int64, error) {
buf := [2]byte{0}
_, err := io.ReadFull(r, buf[:])
n, err := io.ReadFull(r, buf[:])
if err != nil {
return UInt16(0), err
return UInt16(0), int64(n), err
}
return UInt16(binary.LittleEndian.Uint16(buf[:])), nil
return UInt16(binary.LittleEndian.Uint16(buf[:])), int64(n), nil
}
func ReadUInt16N(r io.Reader) (UInt16, int64) {
b, n, err := ReadUInt16Safe(r)
if err != nil {
panic(err)
}
return b, n
}
func ReadUInt16(r io.Reader) UInt16 {
b, err := ReadUInt16Safe(r)
b, _, err := ReadUInt16Safe(r)
if err != nil {
panic(err)
}
@@ -246,17 +286,25 @@ func (self Int32) WriteTo(w io.Writer) (int64, error) {
return int64(n), err
}
func ReadInt32Safe(r io.Reader) (Int32, error) {
func ReadInt32Safe(r io.Reader) (Int32, int64, error) {
buf := [4]byte{0}
_, err := io.ReadFull(r, buf[:])
n, err := io.ReadFull(r, buf[:])
if err != nil {
return Int32(0), err
return Int32(0), int64(n), err
}
return Int32(binary.LittleEndian.Uint32(buf[:])), nil
return Int32(binary.LittleEndian.Uint32(buf[:])), int64(n), nil
}
func ReadInt32N(r io.Reader) (Int32, int64) {
b, n, err := ReadInt32Safe(r)
if err != nil {
panic(err)
}
return b, n
}
func ReadInt32(r io.Reader) Int32 {
b, err := ReadInt32Safe(r)
b, _, err := ReadInt32Safe(r)
if err != nil {
panic(err)
}
@@ -288,17 +336,25 @@ func (self UInt32) WriteTo(w io.Writer) (int64, error) {
return int64(n), err
}
func ReadUInt32Safe(r io.Reader) (UInt32, error) {
func ReadUInt32Safe(r io.Reader) (UInt32, int64, error) {
buf := [4]byte{0}
_, err := io.ReadFull(r, buf[:])
n, err := io.ReadFull(r, buf[:])
if err != nil {
return UInt32(0), err
return UInt32(0), int64(n), err
}
return UInt32(binary.LittleEndian.Uint32(buf[:])), nil
return UInt32(binary.LittleEndian.Uint32(buf[:])), int64(n), nil
}
func ReadUInt32N(r io.Reader) (UInt32, int64) {
b, n, err := ReadUInt32Safe(r)
if err != nil {
panic(err)
}
return b, n
}
func ReadUInt32(r io.Reader) UInt32 {
b, err := ReadUInt32Safe(r)
b, _, err := ReadUInt32Safe(r)
if err != nil {
panic(err)
}
@@ -330,17 +386,25 @@ func (self Int64) WriteTo(w io.Writer) (int64, error) {
return int64(n), err
}
func ReadInt64Safe(r io.Reader) (Int64, error) {
func ReadInt64Safe(r io.Reader) (Int64, int64, error) {
buf := [8]byte{0}
_, err := io.ReadFull(r, buf[:])
n, err := io.ReadFull(r, buf[:])
if err != nil {
return Int64(0), err
return Int64(0), int64(n), err
}
return Int64(binary.LittleEndian.Uint64(buf[:])), nil
return Int64(binary.LittleEndian.Uint64(buf[:])), int64(n), nil
}
func ReadInt64N(r io.Reader) (Int64, int64) {
b, n, err := ReadInt64Safe(r)
if err != nil {
panic(err)
}
return b, n
}
func ReadInt64(r io.Reader) Int64 {
b, err := ReadInt64Safe(r)
b, _, err := ReadInt64Safe(r)
if err != nil {
panic(err)
}
@@ -372,87 +436,27 @@ func (self UInt64) WriteTo(w io.Writer) (int64, error) {
return int64(n), err
}
func ReadUInt64Safe(r io.Reader) (UInt64, error) {
func ReadUInt64Safe(r io.Reader) (UInt64, int64, error) {
buf := [8]byte{0}
_, err := io.ReadFull(r, buf[:])
n, err := io.ReadFull(r, buf[:])
if err != nil {
return UInt64(0), err
return UInt64(0), int64(n), err
}
return UInt64(binary.LittleEndian.Uint64(buf[:])), nil
return UInt64(binary.LittleEndian.Uint64(buf[:])), int64(n), nil
}
func ReadUInt64N(r io.Reader) (UInt64, int64) {
b, n, err := ReadUInt64Safe(r)
if err != nil {
panic(err)
}
return b, n
}
func ReadUInt64(r io.Reader) UInt64 {
b, err := ReadUInt64Safe(r)
b, _, err := ReadUInt64Safe(r)
if err != nil {
panic(err)
}
return b
}
// Int
func (self Int) Equals(other Binary) bool {
return self == other
}
func (self Int) Less(other Binary) bool {
if o, ok := other.(Int); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
func (self Int) ByteSize() int {
return 8
}
func (self Int) WriteTo(w io.Writer) (int64, error) {
buf := []byte{0, 0, 0, 0, 0, 0, 0, 0}
binary.LittleEndian.PutUint64(buf, uint64(self))
n, err := w.Write(buf)
return int64(n), err
}
func ReadInt(r io.Reader) Int {
buf := [8]byte{0}
_, err := io.ReadFull(r, buf[:])
if err != nil {
panic(err)
}
return Int(binary.LittleEndian.Uint64(buf[:]))
}
// UInt
func (self UInt) Equals(other Binary) bool {
return self == other
}
func (self UInt) Less(other Binary) bool {
if o, ok := other.(UInt); ok {
return self < o
} else {
panic("Cannot compare unequal types")
}
}
func (self UInt) ByteSize() int {
return 8
}
func (self UInt) WriteTo(w io.Writer) (int64, error) {
buf := []byte{0, 0, 0, 0, 0, 0, 0, 0}
binary.LittleEndian.PutUint64(buf, uint64(self))
n, err := w.Write(buf)
return int64(n), err
}
func ReadUInt(r io.Reader) UInt {
buf := [8]byte{0}
_, err := io.ReadFull(r, buf[:])
if err != nil {
panic(err)
}
return UInt(binary.LittleEndian.Uint64(buf[:]))
}
+18 -8
View File
@@ -32,21 +32,31 @@ func (self String) WriteTo(w io.Writer) (n int64, err error) {
return int64(n_ + 4), err
}
func ReadStringSafe(r io.Reader) (String, error) {
length, err := ReadUInt32Safe(r)
func ReadStringSafe(r io.Reader) (str String, n int64, err error) {
length, n_, err := ReadUInt32Safe(r)
n += n_
if err != nil {
return "", err
return "", n, err
}
bytes := make([]byte, int(length))
_, err = io.ReadFull(r, bytes)
n__, err := io.ReadFull(r, bytes)
n += int64(n__)
if err != nil {
return "", err
return "", n, err
}
return String(bytes), nil
return String(bytes), n, nil
}
func ReadString(r io.Reader) String {
str, err := ReadStringSafe(r)
func ReadStringN(r io.Reader) (str String, n int64) {
str, n, err := ReadStringSafe(r)
if err != nil {
panic(err)
}
return str, n
}
func ReadString(r io.Reader) (str String) {
str, _, err := ReadStringSafe(r)
if err != nil {
panic(err)
}
+21 -1
View File
@@ -33,6 +33,26 @@ func (self Time) WriteTo(w io.Writer) (int64, error) {
return Int64(self.Unix()).WriteTo(w)
}
func ReadTimeSafe(r io.Reader) (Time, int64, error) {
t, n, err := ReadInt64Safe(r)
if err != nil {
return Time{}, n, err
}
return Time{time.Unix(int64(t), 0)}, n, nil
}
func ReadTimeN(r io.Reader) (Time, int64) {
t, n, err := ReadTimeSafe(r)
if err != nil {
panic(err)
}
return t, n
}
func ReadTime(r io.Reader) Time {
return Time{time.Unix(int64(ReadInt64(r)), 0)}
t, _, err := ReadTimeSafe(r)
if err != nil {
panic(err)
}
return t
}