Ejemplo n.º 1
0
func newPeer() *Peer {
	init_mutex.Lock()
	defer init_mutex.Unlock()
	if has_init == 0 {
		C.dpx_init()
	}

	peer := C.dpx_peer_new()
	has_init++

	p := &Peer{
		peer: peer,
	}

	runtime.SetFinalizer(p, func(x *Peer) {
		if int(C.dpx_peer_closed(x.peer)) == 0 {
			// memleak. drop the reference and leave it be.
			return
		}
		C.dpx_peer_free(p.peer)

		go func() {
			init_mutex.Lock()
			defer init_mutex.Unlock()
			has_init--

			if has_init == 0 {
				C.dpx_cleanup()
			}
		}()
	})

	return p
}
Ejemplo n.º 2
0
func (p *Peer) Closed() bool {
	return int(C.dpx_peer_closed(p.peer)) != 0
}