func (s *socket) Connect(endpoint string) error { if s.asServer { return ErrInvalidSockAction } parts := strings.Split(endpoint, "://") Connect: netconn, err := net.Dial(parts[0], parts[1]) if err != nil { time.Sleep(s.GetRetry()) goto Connect } zmtpconn := zmtp.NewConnection(netconn) _, err = zmtpconn.Prepare(s.mechanism, s.sockType, s.asServer, nil) if err != nil { return err } conn := &Connection{ netconn: netconn, zmtpconn: zmtpconn, } s.conns = append(s.conns, conn) zmtpconn.Recv(s.messageChan) return nil }
// BindServer accepts a Server interface and an endpoint // in the format <proto>://<address>:<port>. It then attempts // to bind to the endpoint. TODO: change this to starting // a listener on the endpoint that performs handshakes // with any client that connects func BindServer(s Server, endpoint string) (net.Addr, error) { var addr net.Addr parts := strings.Split(endpoint, "://") ln, err := net.Listen(parts[0], parts[1]) if err != nil { return addr, err } netConn, err := ln.Accept() if err != nil { return addr, err } zmtpConn := zmtp.NewConnection(netConn) _, err = zmtpConn.Prepare(s.SecurityMechanism(), s.SocketType(), true, nil) if err != nil { return netConn.LocalAddr(), err } conn := &Connection{ net: netConn, zmtp: zmtpConn, } s.AddConnection(conn) zmtpConn.Recv(s.RecvChannel()) return netConn.LocalAddr(), nil }
// Bind binds to an endpoint. func (s *socket) Bind(endpoint string) (net.Addr, error) { var addr net.Addr if !s.asServer { return addr, ErrInvalidSockAction } parts := strings.Split(endpoint, "://") ln, err := net.Listen(parts[0], parts[1]) if err != nil { return addr, err } netconn, err := ln.Accept() if err != nil { return addr, err } zmtpconn := zmtp.NewConnection(netconn) _, err = zmtpconn.Prepare(s.mechanism, s.sockType, s.asServer, nil) if err != nil { return netconn.LocalAddr(), err } conn := &Connection{ netconn: netconn, zmtpconn: zmtpconn, } s.addConn(conn) zmtpconn.Recv(s.messageChan) return netconn.LocalAddr(), nil }
// ConnectClient accepts a Client interface and an endpoint // in the format <proto>://<address>:<port>. It then attempts // to connect to the endpoint and perform a ZMTP handshake. func ConnectClient(c Client, endpoint string) error { parts := strings.Split(endpoint, "://") Connect: netConn, err := net.Dial(parts[0], parts[1]) if err != nil { time.Sleep(c.RetryInterval()) goto Connect } zmtpConn := zmtp.NewConnection(netConn) _, err = zmtpConn.Prepare(c.SecurityMechanism(), c.SocketType(), false, nil) if err != nil { return err } conn := &Connection{ net: netConn, zmtp: zmtpConn, } c.AddConnection(conn) zmtpConn.Recv(c.RecvChannel()) return nil }