示例#1
0
func ZappyMustCompress(inb []byte) (outb []byte) {
	outb, err := zappy.Encode(nil, inb)
	if err != nil {
		panic(err)
	}
	return outb
}
示例#2
0
func newConn(sock *net.UDPConn, local, remote net.Addr, id int) *Conn {
	sock.SetDeadline(time.Time{})
	conn := &Conn{conn: sock, local: local, remote: remote, closed: false, quit: make(chan bool), tmp: make([]byte, CacheBuffSize*2), tmp2: make([]byte, CacheBuffSize), sendChan: make(chan string, 10), checkCanWrite: make(chan chan bool), readChan: make(chan cache), overTime: time.Now().Unix() + 30, fecWriteId: 0, fecSendC: 0}
	debug("create", id)
	conn.kcp = ikcp.Ikcp_create(uint32(id), conn)
	conn.kcp.Output = udp_output
	conn.SetKcp(DefaultKcpSetting())
	if *bCompress {
		conn.compressCache = make([]byte, CacheBuffSize*2)
		conn.compressSendChan = make(chan []byte, 100)
		go func() {
			for {
				select {
				case b := <-conn.compressSendChan:
					enc, er := zappy.Encode(conn.compressCache, b)
					if er != nil {
						log.Println("compress error", er.Error())
						go conn.Close()
						break
					}
					//log.Println("compress", len(b), len(enc))
					conn.conn.WriteTo(enc, conn.remote)
				case <-conn.quit:
					return
				}
			}
		}()
	}
	return conn
}
示例#3
0
func Test(t *testing.T) {

	if n := len(value100); n != 100 {
		t.Fatal(n)
	}

	c, err := zappy.Encode(nil, value100)
	if err != nil {
		t.Fatal(err)
	}

	if n := len(c); n != 50 {
		t.Fatal(n)
	}
}
示例#4
0
func (a *Allocator) makeUsedBlock(dst []byte, b []byte) (w []byte, rqAtoms int, cc byte, err error) {
	cc = tagNotCompressed
	w = b

	var n int
	if n = len(b); n > maxRq {
		return nil, 0, 0, &ErrINVAL{"Allocator.makeUsedBlock: content size out of limits", n}
	}

	rqAtoms = n2atoms(n)
	if a.Compress && n > 14 { // attempt compression
		if dst, err = zappy.Encode(dst, b); err != nil {
			return
		}

		n2 := len(dst)
		if rqAtoms2 := n2atoms(n2); rqAtoms2 < rqAtoms { // compression saved at least a single atom
			w, n, rqAtoms, cc = dst, n2, rqAtoms2, tagCompressed
		}
	}
	return
}