當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Channel.Kill方法代碼示例

本文整理匯總了Golang中github.com/telehash/gogotelehash/e3x.Channel.Kill方法的典型用法代碼示例。如果您正苦於以下問題:Golang Channel.Kill方法的具體用法?Golang Channel.Kill怎麽用?Golang Channel.Kill使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/telehash/gogotelehash/e3x.Channel的用法示例。


在下文中一共展示了Channel.Kill方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: handle_peer

func (mod *module) handle_peer(ch *e3x.Channel) {
	defer ch.Kill()

	log := mod.log.From(ch.RemoteHashname()).To(mod.e.LocalHashname())

	// MUST allow router role
	if mod.config.DisableRouter {
		log.Println("drop: router disabled")
		return
	}

	pkt, err := ch.ReadPacket()
	if err != nil {
		log.Printf("drop: failed to read packet: %s", err)
		return
	}

	peerStr, ok := pkt.Header().GetString("peer")
	if !ok {
		log.Printf("drop: no peer in packet")
		return
	}
	peer := hashname.H(peerStr)

	// MUST have link to either endpoint
	if mod.e.GetExchange(ch.RemoteHashname()) == nil && mod.e.GetExchange(peer) == nil {
		log.Printf("drop: no link to either peer")
		return
	}

	// MUST pass firewall
	if mod.config.AllowPeer != nil && !mod.config.AllowPeer(ch.RemoteHashname(), peer) {
		log.Printf("drop: blocked by firewall")
		return
	}

	ex := mod.e.GetExchange(peer)
	if ex == nil {
		log.Printf("drop: no exchange to target")
		// resolve?
		return
	}

	token := cipherset.ExtractToken(pkt.Body(nil))
	if token != cipherset.ZeroToken {
		// add bridge back to requester
		mod.RouteToken(token, ch.Exchange())
	}

	mod.connect(ex, bufpool.New().Set(pkt.Body(nil)))
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:51,代碼來源:channel_peer.go

示例2: handlePathRequest

func (mod *module) handlePathRequest(c *e3x.Channel) {
	defer c.Kill()

	pkt, err := c.ReadPacket()
	if err != nil {
		return // ignore
	}

	// decode paths known by peer and add them as candidates
	if header, found := pkt.Header().Get("paths"); found {
		data, err := json.Marshal(header)
		if err != nil {
			return // ignore
		}

		var entries []json.RawMessage
		err = json.Unmarshal(data, &entries)
		if err != nil {
			return // ignore
		}

		for _, entry := range entries {
			addr, err := transports.DecodeAddr(entry)
			if err != nil {
			}
			if err == nil {
				c.Exchange().AddPathCandidate(addr)
			}
		}
	}

	var pipes = c.Exchange().KnownPipes()

	for _, pipe := range pipes {
		pkt := &lob.Packet{}
		pkt.Header().Set("path", pipe.RemoteAddr())
		c.WritePacketTo(pkt, pipe)
	}
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:39,代碼來源:module.go

示例3: handle_connect

func (mod *module) handle_connect(ch *e3x.Channel) {
	defer ch.Kill()

	var (
		from        hashname.H
		localIdent  *e3x.Identity
		remoteIdent *e3x.Identity
		handshake   cipherset.Handshake
		innerData   = bufpool.New()
		err         error
	)

	localIdent, err = mod.e.LocalIdentity()
	if err != nil {
		return
	}

	pkt, err := ch.ReadPacket()
	if err != nil {
		return
	}

	pkt.Body(innerData.SetLen(pkt.BodyLen()).RawBytes()[:0])

	inner, err := lob.Decode(innerData)
	if err != nil {
		return
	}

	innerHdr := inner.Header()
	if innerHdr.IsBinary() && len(innerHdr.Bytes) == 1 {
		// handshake
		var (
			csid = innerHdr.Bytes[0]
			key  = localIdent.Keys()[csid]
		)
		if key == nil {
			return
		}

		handshake, err = cipherset.DecryptHandshake(csid, key, inner.Body(nil))
		if err != nil {
			return
		}

		from, err = hashname.FromIntermediates(handshake.Parts())
		if err != nil {
			return
		}

		remoteIdent, err = e3x.NewIdentity(cipherset.Keys{
			handshake.CSID(): handshake.PublicKey(),
		}, handshake.Parts(), nil)
		if err != nil {
			return
		}

	} else {
		// key packet

		var parts = make(cipherset.Parts)
		var csid uint8
		for key, value := range inner.Header().Extra {
			if len(key) != 2 {
				continue
			}

			keyData, err := hex.DecodeString(key)
			if err != nil {
				continue
			}

			partCSID := keyData[0]
			switch v := value.(type) {
			case bool:
				csid = partCSID
			case string:
				parts[partCSID] = v
			}
		}

		hn, err := hashname.FromKeyAndIntermediates(csid, inner.Body(nil), parts)
		if err != nil {
			return
		}

		from = hn

		pubKey, err := cipherset.DecodeKeyBytes(csid, inner.Body(nil), nil)
		if err != nil {
			return
		}

		remoteIdent, err = e3x.NewIdentity(cipherset.Keys{csid: pubKey}, parts, nil)
		if err != nil {
			return
		}
	}

	if from == "" {
//.........這裏部分代碼省略.........
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:101,代碼來源:channel_connect.go


注:本文中的github.com/telehash/gogotelehash/e3x.Channel.Kill方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。