Пример #1
0
// Handles incoming requests:
func (d *Server) handleConnection(ctx context.Context, conn net.Conn) {
	// Make sure this connection count gets released:
	defer func() {
		if err := conn.Close(); err != nil {
			log.Debugf("daemon-loop: connection drop failed: %v", err)
		}

		d.maxConnections <- allowOneConn{}
	}()

	tnl, err := security.NewEllipticTunnel(conn)
	if err != nil {
		log.Error("Tunnel failed", err)
		return
	}

	p := protocol.NewProtocol(tnl, false)

	// Loop until client disconnect or dies otherwise:
	for {
		msg := &wire.Command{}
		if err := p.Recv(msg); err != nil {
			if err != io.EOF {
				log.Warning("daemon-recv: ", err)
			}
			return
		}

		log.Infof("recv: %s: %v", conn.RemoteAddr().String(), msg)
		d.handleCommand(ctx, msg, p)
	}
}
Пример #2
0
// Dial connects to a running daemon instance.
func Dial(port int) (*Client, error) {
	client := &Client{
		Send: make(chan *wire.Command),
		Recv: make(chan *wire.Response),
		quit: make(chan bool, 1),
	}

	addr := fmt.Sprintf("127.0.0.1:%d", port)
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		return nil, err
	}

	client.conn = conn
	tnl, err := security.NewEllipticTunnel(conn)
	if err != nil {
		log.Error("Tunneling failed: ", err)
		return nil, err
	}

	go client.handleMessages(tnl)
	return client, nil
}