本文整理匯總了Golang中github.com/telehash/gogotelehash/e3x.Channel.WritePacket方法的典型用法代碼示例。如果您正苦於以下問題:Golang Channel.WritePacket方法的具體用法?Golang Channel.WritePacket怎麽用?Golang Channel.WritePacket使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/telehash/gogotelehash/e3x.Channel
的用法示例。
在下文中一共展示了Channel.WritePacket方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: handlePing
func (t *transport) handlePing(ch *e3x.Channel) {
var (
err error
pkt *lob.Packet
id string
alive bool
)
defer ch.Close()
pkt, err = ch.ReadPacket()
if err != nil {
// log error
// tracef("error: %s", err)
return
}
id, _ = pkt.Header().GetString("vn")
rpc := t.lookupRPC(id)
if rpc == nil {
alive = false
} else {
alive = true
}
pkt = &lob.Packet{}
pkt.Header().SetBool("alive", alive)
err = ch.WritePacket(pkt)
if err != nil {
// log error
// tracef("error: %s", err)
return
}
}
示例2: ListVnodes
// Gets a list of the vnodes on the box
func (t *transport) ListVnodes(hn string) ([]*chord.Vnode, error) {
var (
addr *e3x.Addr
ch *e3x.Channel
res []*completeVnode
err error
)
addr = t.lookupAddr(hashname.H(hn))
if addr == nil {
return nil, e3x.ErrNoAddress
}
ch, err = t.e.Open(addr, "chord.list", true)
if err != nil {
return nil, err
}
defer ch.Close()
// ch.SetReadDeadline(time.Now().Add(30*time.Second))
// ch.SetWriteDeadline(time.Now().Add(30*time.Second))
err = ch.WritePacket(&lob.Packet{})
if err != nil {
return nil, err
}
err = json.NewDecoder(newStream(ch)).Decode(&res)
if err != nil {
return nil, err
}
return t.internalVnodes(res), nil
}
示例3: GetPredecessor
// Request a nodes predecessor
func (t *transport) GetPredecessor(vn *chord.Vnode) (*chord.Vnode, error) {
var (
addr *e3x.Addr
ch *e3x.Channel
pkt *lob.Packet
res *completeVnode
err error
)
addr = t.lookupAddr(hashname.H(vn.Host))
if addr == nil {
return nil, e3x.ErrNoAddress
}
ch, err = t.e.Open(addr, "chord.predecessor.get", true)
if err != nil {
return nil, err
}
defer ch.Close()
// ch.SetReadDeadline(time.Now().Add(30*time.Second))
// ch.SetWriteDeadline(time.Now().Add(30*time.Second))
pkt = &lob.Packet{}
pkt.Header().SetString("vn", vn.String())
err = ch.WritePacket(pkt)
if err != nil {
return nil, err
}
err = json.NewDecoder(newStream(ch)).Decode(&res)
if err != nil {
return nil, err
}
if res != nil {
// tracef("GetPredecessor(Vnode(%q)) => Vnode(%q)", vn.String(), res.Id)
}
return t.internalVnode(res), nil
}
示例4: Ping
// Ping a Vnode, check for liveness
func (t *transport) Ping(vn *chord.Vnode) (bool, error) {
var (
addr *e3x.Addr
ch *e3x.Channel
pkt *lob.Packet
alive bool
err error
)
addr = t.lookupAddr(hashname.H(vn.Host))
if addr == nil {
return false, e3x.ErrNoAddress
}
ch, err = t.e.Open(addr, "chord.ping", true)
if err != nil {
return false, err
}
defer ch.Close()
// ch.SetReadDeadline(time.Now().Add(30*time.Second))
// ch.SetWriteDeadline(time.Now().Add(30*time.Second))
pkt = &lob.Packet{}
pkt.Header().SetString("vn", vn.String())
err = ch.WritePacket(pkt)
if err != nil {
return false, err
}
pkt, err = ch.ReadPacket()
if err != nil {
return false, err
}
alive, _ = pkt.Header().GetBool("alive")
// tracef("Ping(Vnode(%q)) => %v", vn.String(), alive)
return alive, nil
}
示例5: handle_see
func (d *dht) handle_see(c *e3x.Channel) {
defer c.Close()
pkt, err := c.ReadPacket()
if err != nil {
return
}
key, ok := pkt.Header().GetString("key")
if !ok {
return
}
h := d.localLookup(key)
pkt = &lob.Packet{}
pkt.Header().Set("see", h)
pkt.Header().SetBool("end", true)
err = c.WritePacket(pkt)
if err != nil {
return
}
}