50 {
51 if (remote_port_ == 0 || remote_ip_.empty()) {
52 AERROR <<
"remote info is invalid!";
53 return false;
54 }
55
56 if (pb_msg == nullptr) {
57 AERROR <<
"proto msg is not ready!";
58 return false;
59 }
60
61 struct sockaddr_in server_addr;
62 server_addr.sin_addr.s_addr = inet_addr(remote_ip_.c_str());
63 server_addr.sin_family = AF_INET;
64 server_addr.sin_port = htons(static_cast<uint16_t>(remote_port_));
65 int sock_fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
66
67 int res =
68 connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
69 if (res < 0) {
70 close(sock_fd);
71 return false;
72 }
73
74 BridgeProtoSerializedBuf<T> proto_buf;
75 proto_buf.Serialize(pb_msg, proto_name_);
76 for (size_t j = 0; j < proto_buf.GetSerializedBufCount(); j++) {
77 ssize_t nbytes = send(sock_fd, proto_buf.GetSerializedBuf(j),
78 proto_buf.GetSerializedBufSize(j), 0);
79 if (nbytes != static_cast<ssize_t>(proto_buf.GetSerializedBufSize(j))) {
80 break;
81 }
82 }
83 close(sock_fd);
84
85 return true;
86}