Ejemplo n.º 1
0
Archivo: peers.go Proyecto: vgp/gogp
func ServeForever(addr string, seeds []string) {
	// RPC server
	srv := rpc.NewServer()
	ps := PeerServer{&sync.Mutex{}, make([]peer, 0)}
	srv.Register(&ps)

	// Connection server
	listener, err := net.Listen("tcp", addr)
	if err != nil {
		log.Fatal("gogp/peers: Could not set up listen: ", err)
	}
	defer listener.Close()

	// Incoming connections from connection server
	go func() {
		for {
			conn, err := listener.Accept()
			if err != nil {
				log.Fatal("Couldn't receive connection")
			}
			conns, err := muxconn.Split(conn, 2)
			if err != nil {
				log.Fatal("Couldn't mux an inbound connection")
			}
			go srv.ServeConn(conns[0])
			p := peer{rpc.NewClient(conns[1]), time.Now()}
			func() {
				ps.mutex.Lock()
				defer ps.mutex.Unlock()
				ps.peers = append(ps.peers, p)
			}()
		}
	}()

}
Ejemplo n.º 2
0
func DispatchForever(connch <-chan net.Conn, srv *rpc.Server, clientch chan<- *rpc.Client) {
	for conn := range connch {
		muxed, err := muxconn.Split(conn, 2)
		if err != nil {
			log.Println("birpc: Failed to mux incoming connection from", conn.RemoteAddr().String(), "to", conn.LocalAddr().String(), ", dropping")
			continue
		}
		// Server on first muxed conn, client on second
		go srv.ServeConn(muxed[0])
		clientch <- rpc.NewClient(muxed[1])
	}
}
Ejemplo n.º 3
0
func Dial(url string, srv *rpc.Server) (client *rpc.Client, err error) {
	conn, err := direct.Dial(url)
	if err != nil {
		return
	}
	muxed, err := muxconn.Split(conn, 2)
	if err != nil {
		return
	}

	// Server on second, client on first (reverse of above)
	client = rpc.NewClient(muxed[0])
	go srv.ServeConn(muxed[1])
	return
}