endpoints.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package rpc
  17. import (
  18. "net"
  19. "strings"
  20. "github.com/ethereum/go-ethereum/log"
  21. )
  22. // StartIPCEndpoint starts an IPC endpoint.
  23. func StartIPCEndpoint(ipcEndpoint string, apis []API) (net.Listener, *Server, error) {
  24. // Register all the APIs exposed by the services.
  25. var (
  26. handler = NewServer()
  27. regMap = make(map[string]struct{})
  28. registered []string
  29. )
  30. for _, api := range apis {
  31. if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
  32. log.Info("IPC registration failed", "namespace", api.Namespace, "error", err)
  33. return nil, nil, err
  34. }
  35. if _, ok := regMap[api.Namespace]; !ok {
  36. registered = append(registered, api.Namespace)
  37. regMap[api.Namespace] = struct{}{}
  38. }
  39. }
  40. log.Debug("IPCs registered", "namespaces", strings.Join(registered, ","))
  41. // All APIs registered, start the IPC listener.
  42. listener, err := ipcListen(ipcEndpoint)
  43. if err != nil {
  44. return nil, nil, err
  45. }
  46. go handler.ServeListener(listener)
  47. return listener, handler, nil
  48. }