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 17 #ifndef MINDSPORE_CCSRC_DISTRIBUTED_RPC_TCP_SOCKET_OPERATION_H_ 18 #define MINDSPORE_CCSRC_DISTRIBUTED_RPC_TCP_SOCKET_OPERATION_H_ 19 20 #include <netinet/in.h> 21 #include <string> 22 #include "utils/convert_utils_base.h" 23 24 namespace mindspore { 25 namespace distributed { 26 namespace rpc { 27 // This is the return value that represents the address is in use errno. 28 constexpr int kAddressInUseError = -2; 29 30 class Connection; 31 32 union SocketAddress { 33 struct sockaddr sa; 34 struct sockaddr_in saIn; 35 struct sockaddr_in6 saIn6; 36 struct sockaddr_storage saStorage; 37 }; 38 39 class SocketOperation { 40 public: 41 SocketOperation() = default; ~SocketOperation()42 virtual ~SocketOperation() {} 43 Initialize()44 virtual bool Initialize() { return true; } 45 46 // Lookup the local IP address of the first available network interface. 47 static std::string GetLocalIP(); 48 49 static std::string GetIP(const std::string &url); 50 51 // Get ip and port of the specified socket fd. 52 static std::string GetIP(int sock_fd); 53 static uint16_t GetPort(int sock_fd); 54 55 // Get the address(ip:port) of the other end of the connection. 56 static std::string GetPeer(int sock_fd); 57 58 // Get socket address of the url. 59 static bool GetSockAddr(const std::string &url, SocketAddress *addr); 60 61 // Create a socket. 62 static int CreateSocket(sa_family_t family); 63 64 // Set socket options. 65 static int SetSocketOptions(int sock_fd); 66 static int SetSocketKeepAlive(int fd, int keepalive, int keepidle, int keepinterval, int keepcount); 67 68 // Connect to the Socket sock_fd. 69 static int Connect(int sock_fd, const struct sockaddr *sa, socklen_t saLen, uint16_t *boundPort); 70 71 // Get interface name for specified socket address. 72 static std::string GetInterfaceName(SocketAddress *const addr); 73 74 // Close the given connection. 75 virtual void Close(Connection *connection) = 0; 76 77 // Start and listen on the socket represented by the given url. 78 static int Listen(const std::string &url); 79 80 // Accept connection on the server socket. 81 static int Accept(int sock_fd); 82 83 // Call recv with flag MSG_PEEK which means do not delete data in buffer after reading. 84 virtual ssize_t ReceivePeek(Connection *connection, char *recvBuf, uint32_t recvLen) = 0; 85 86 // Try to receive messages up to totalRecvLen (for message header). 87 virtual int Receive(Connection *connection, char *recvBuf, size_t totalRecvLen, size_t *recvLen) = 0; 88 89 // Receive message (for message body). 90 virtual int ReceiveMessage(Connection *connection, struct msghdr *recvMsg, size_t totalRecvLen, size_t *recvLen) = 0; 91 92 virtual int SendMessage(Connection *connection, struct msghdr *sendMsg, size_t totalSendLen, size_t *sendLen) = 0; 93 94 // Handle connect and connected events. 95 virtual void NewConnEventHandler(int fd, uint32_t events, void *context) = 0; 96 virtual void ConnEstablishedEventHandler(int fd, uint32_t events, void *context) = 0; 97 }; 98 } // namespace rpc 99 } // namespace distributed 100 } // namespace mindspore 101 102 #endif 103