You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

socket_operation.h 3.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Copyright 2021 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_DISTRIBUTED_RPC_TCP_SOCKET_OPERATION_H_
  17. #define MINDSPORE_CCSRC_DISTRIBUTED_RPC_TCP_SOCKET_OPERATION_H_
  18. #include <netinet/in.h>
  19. #include <string>
  20. #include "utils/convert_utils_base.h"
  21. namespace mindspore {
  22. namespace distributed {
  23. namespace rpc {
  24. class Connection;
  25. union SocketAddress {
  26. struct sockaddr sa;
  27. struct sockaddr_in saIn;
  28. struct sockaddr_in6 saIn6;
  29. struct sockaddr_storage saStorage;
  30. };
  31. class SocketOperation {
  32. public:
  33. SocketOperation() = default;
  34. virtual ~SocketOperation() {}
  35. // Lookup the local IP address of the first available network interface.
  36. static std::string GetLocalIP();
  37. static std::string GetIP(const std::string &url);
  38. static uint16_t GetPort(int sock_fd);
  39. // Get the address(ip:port) of the other end of the connection.
  40. static std::string GetPeer(int sock_fd);
  41. // Get socket address of the url.
  42. static bool GetSockAddr(const std::string &url, SocketAddress *addr);
  43. // Create a socket.
  44. static int CreateSocket(sa_family_t family);
  45. // Set socket options.
  46. static int SetSocketOptions(int sock_fd);
  47. static int SetSocketKeepAlive(int fd, int keepalive, int keepidle, int keepinterval, int keepcount);
  48. // Connect to the Socket sock_fd.
  49. static int Connect(int sock_fd, const struct sockaddr *sa, socklen_t saLen, uint16_t *boundPort);
  50. // Close the given connection.
  51. virtual void Close(Connection *connection) = 0;
  52. // Start and listen on the socket represented by the given url.
  53. static int Listen(const std::string &url);
  54. // Accept connection on the server socket.
  55. static int Accept(int sock_fd);
  56. // Call recv with flag MSG_PEEK which means do not delete data in buffer after reading.
  57. virtual int ReceivePeek(Connection *connection, char *recvBuf, uint32_t recvLen) = 0;
  58. // Try to receive messages up to totalRecvLen (for message header).
  59. virtual int Receive(Connection *connection, char *recvBuf, uint32_t totalRecvLen, uint32_t *recvLen) = 0;
  60. // Receive message (for message body).
  61. virtual int ReceiveMessage(Connection *connection, struct msghdr *recvMsg, uint32_t recvLen) = 0;
  62. virtual int SendMessage(Connection *connection, struct msghdr *sendMsg, uint32_t *sendLen) = 0;
  63. // Handle connect and connected events.
  64. virtual void NewConnEventHandler(int fd, uint32_t events, void *context) = 0;
  65. virtual void ConnEstablishedEventHandler(int fd, uint32_t events, void *context) = 0;
  66. };
  67. } // namespace rpc
  68. } // namespace distributed
  69. } // namespace mindspore
  70. #endif