go简单通讯,记录

包含3个文件NetStream.go,client.go,server.go

// NetStream.go
package mynet

import (
    "bytes"
    "encoding/binary"
    "errors"
)

type NetStream struct {
    Data     []byte
    Rpos     int
    Wpos     int
    Capacity int
}

func NewNetStream(capacity int) *NetStream {
    a := &NetStream{
        Rpos:     0,
        Wpos:     0,
        Capacity: capacity,
    }

    a.Data = make([]byte, capacity)
    return a
}

// read
func (self *NetStream) ReadInt16() (int16, error) {
    var p int16
    if self.Rpos+2 > self.Wpos {
        return p, errors.New("not enough read")
    }
    binary.Read(bytes.NewBuffer(self.Data[self.Rpos:]), binary.BigEndian, &p)
    self.Rpos += 2
    return p, nil
}

func (self *NetStream) ReadInt32() (int32, error) {
    var p int32
    if self.Rpos+4 > self.Wpos {
        return p, errors.New("not enough read")
    }

    binary.Read(bytes.NewBuffer(self.Data[self.Rpos:]), binary.BigEndian, &p)
    self.Rpos += 4
    return p, nil
}

func (self *NetStream) GetInt16() (int16, error) {
    var p int16
    if self.Rpos+2 > self.Wpos {
        return p, errors.New("not enough read")
    }

    binary.Read(bytes.NewBuffer(self.Data[self.Rpos:]), binary.BigEndian, &p)
    return p, nil
}

func (self *NetStream) GetInt32() (int32, error) {
    var p int32
    if self.Rpos+4 > self.Wpos {
        return p, errors.New("not enough read")
    }

    binary.Read(bytes.NewBuffer(self.Data[self.Rpos:]), binary.BigEndian, &p)
    return p, nil
}

func (self *NetStream) ReadInt64() (int64, error) {
    var p int64
    if self.Rpos+8 > self.Wpos {
        return p, errors.New("not enough read")
    }

    binary.Read(bytes.NewBuffer(self.Data[self.Rpos:]), binary.BigEndian, &p)
    self.Rpos += 8
    return p, nil
}

func (self *NetStream) ReadBytes(p []byte, offset int, len int) error {
    if self.Rpos+len > self.Wpos {
        return errors.New("not enough read")
    }
    copy(p[offset:offset+len], self.Data[self.Rpos:self.Rpos+len])
    self.Rpos += len
    return nil
}

// write
func (self *NetStream) WriteInt16(p int16) error {
    if self.Wpos+2 > self.Capacity {
        return errors.New("not cap to write")
    }

    buff := bytes.NewBuffer([]byte{})
    binary.Write(buff, binary.BigEndian, p)
    self.WriteByte(buff.Bytes())
    return nil
}

func (self *NetStream) WriteInt32(p int32) error {
    if self.Wpos+4 > self.Capacity {
        return errors.New("not cap to write")
    }
    buff := bytes.NewBuffer([]byte{})
    binary.Write(buff, binary.BigEndian, p)
    self.WriteByte(buff.Bytes())
    return nil
}

func (self *NetStream) WriteInt64(p int64) error {
    if self.Wpos+8 > self.Capacity {
        return errors.New("not cap to write")
    }
    buff := bytes.NewBuffer([]byte{})
    binary.Write(buff, binary.BigEndian, p)
    self.WriteByte(buff.Bytes())
    return nil
}

func (self *NetStream) WriteBytes(p []byte, offset int, len int) error {
    if self.Wpos+len > self.Capacity {
        return errors.New("not cap to write")
    }
    copy(self.Data[self.Wpos:], p[offset:offset+len])
    self.Wpos += len
    return nil
}

func (self *NetStream) WriteByte(p []byte) error {
    if self.Wpos+len(p) > self.Capacity {
        return errors.New("not cap to write")
    }
    copy(self.Data[self.Wpos:], p)
    self.Wpos += len(p)
    return nil
}

func (self *NetStream) ClearRead() {
    if self.Rpos > 0 {
        copy(self.Data[0:], self.Data[self.Rpos:self.Wpos])
        self.Rpos = 0
        self.Wpos = self.Wpos - self.Rpos
    }
}

func (self *NetStream) AvailableNum() int {
    return self.Wpos - self.Rpos
}

func (self *NetStream) PrefixedDataAvailable(len int) bool {
    if self.AvailableNum() < len {
        return false
    }
    var b1 int16
    var b2 int32
    var err error
    if len == 2 {
        b1, err = self.GetInt16()
    } else if len == 4 {
        b2, err = self.GetInt32()
    }

    if err == nil {
        if self.AvailableNum() >= int(b1)+len || self.AvailableNum() >= int(b2)+len {
            return true
        }
    }
    return false
}

func (self *NetStream) PrefixedDataAvailableShort() bool {
    return self.PrefixedDataAvailable(2)
}

func (self *NetStream) PrefixedDataAvailableInt() bool {
    return self.PrefixedDataAvailable(4)
}
//===============================
// client.go
package main

import (
    "fmt"
    "mynet"
    "net"
    "os"
)

func receive(conn net.Conn) {
    dt := mynet.NewNetStream(1024)
    buff := make([]byte, 1024)
    for {
        sz, _ := conn.Read(buff)
        dt.WriteByte(buff[:sz])
        if dt.PrefixedDataAvailableInt() {
            total, _ := dt.ReadInt32()
            cmd, _ := dt.ReadInt16()
            msg := make([]byte, (total - 2))
            dt.ReadBytes(msg, 0, int(total-2))
            fmt.Printf("cmd:%d msg:%s\n", cmd, msg)
        }
    }
}

func main() {
    conn, err := net.Dial("tcp4", "127.0.0.1:5001")
    if err != nil {
        fmt.Println("Error: %s", err.Error())
        os.Exit(1)
    }

    go receive(conn)
    var str string
    for {
        fmt.Scanf("%s", &str)
        if str != "quit" {
            sz := len(str) + 2
            dt := mynet.NewNetStream(sz + 4)
            dt.WriteInt32(int32(sz))
            dt.WriteInt16(int16(11))
            dt.WriteByte([]byte(str))

            conn.Write(dt.Data)
        }
    }

}
//===============================
// server.go
package main

import (
    "fmt"
    "mynet"
    "net"
    "os"
)

func handleConn(conn net.Conn) {
    dt := mynet.NewNetStream(1024)
    buff := make([]byte, 1024)
    for {
        sz, _ := conn.Read(buff)
        dt.WriteByte(buff[:sz])
        if dt.PrefixedDataAvailableInt() {
            total, _ := dt.ReadInt32()

            cmd, _ := dt.ReadInt16()
            msg := make([]byte, (total - 2))
            dt.ReadBytes(msg, 0, int(total-2))
            fmt.Printf("receive cmd:%d msg:%s\n", cmd, msg)

            // 发送回去
            tstr := []byte("haha")
            tsz := len(tstr)
            tmsg := mynet.NewNetStream(tsz + 2 + 4)
            tmsg.WriteInt32(int32(tsz + 2))
            tmsg.WriteInt16(int16(12))
            tmsg.WriteByte(tstr)
            conn.Write(tmsg.Data)
        }
    }
}

func main() {
    listen, err := net.Listen("tcp4", "0.0.0.0:5001")
    if err != nil {
        fmt.Print(err.Error())
        os.Exit(1)
    }
    for {
        conn, err := listen.Accept()
        if err != nil {
            continue
        }

        go handleConn(conn)
    }
}

你可能感兴趣的:(游戏设计,网络通讯)