MCPcopy Create free account
hub / github.com/apache/brpc / client_thread

Function client_thread

test/brpc_input_messenger_unittest.cpp:88–147  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

86butil::atomic<size_t> client_index(0);
87
88void* client_thread(void* arg) {
89 ClientMeta* m = (ClientMeta*)arg;
90 size_t offset = 0;
91 m->times = 0;
92 m->bytes = 0;
93 const size_t buf_cap = NMESSAGE * MESSAGE_SIZE;
94 char* buf = (char*)malloc(buf_cap);
95 for (size_t i = 0; i < NMESSAGE; ++i) {
96 memcpy(buf + i * MESSAGE_SIZE, "HULU", 4);
97 // HULU use host byte order directly...
98 *(uint32_t*)(buf + i * MESSAGE_SIZE + 4) = MESSAGE_SIZE - 12;
99 *(uint32_t*)(buf + i * MESSAGE_SIZE + 8) = 4;
100 }
101#ifdef USE_UNIX_DOMAIN_SOCKET
102 const size_t id = client_index.fetch_add(1);
103 char socket_name[64];
104 snprintf(socket_name, sizeof(socket_name), "input_messenger.socket%lu",
105 (id % NEPOLL));
106 butil::fd_guard fd(butil::unix_socket_connect(socket_name));
107 if (fd < 0) {
108 PLOG(FATAL) << "Fail to connect to " << socket_name;
109 return NULL;
110 }
111#else
112 butil::EndPoint point(butil::IP_ANY, 7878);
113 butil::fd_guard fd(butil::tcp_connect(point, NULL));
114 if (fd < 0) {
115 PLOG(FATAL) << "Fail to connect to " << point;
116 return NULL;
117 }
118#endif
119
120 while (!client_stop) {
121 ssize_t n;
122 if (offset == 0) {
123 n = write(fd, buf, buf_cap);
124 } else {
125 iovec v[2];
126 v[0].iov_base = buf + offset;
127 v[0].iov_len = buf_cap - offset;
128 v[1].iov_base = buf;
129 v[1].iov_len = offset;
130 n = writev(fd, v, 2);
131 }
132 if (n < 0) {
133 if (errno != EINTR) {
134 PLOG(FATAL) << "Fail to write fd=" << fd;
135 return NULL;
136 }
137 } else {
138 ++m->times;
139 m->bytes += n;
140 offset += n;
141 if (offset >= buf_cap) {
142 offset -= buf_cap;
143 }
144 }
145 }

Callers

nothing calls this directly

Calls 4

snprintfFunction · 0.85
unix_socket_connectFunction · 0.85
tcp_connectFunction · 0.85
fetch_addMethod · 0.80

Tested by

no test coverage detected