MCPcopy Create free account
hub / github.com/Tencent/Hardcoder / createSocket

Method createSocket

libapp2sys/src/main/cpp/localsocket.h:248–355  ·  view source on GitHub ↗

server: remotePath is null or "" client: remotePath is valid

Source from the content-addressed store, hash-verified

246 //server: remotePath is null or ""
247 //client: remotePath is valid
248 int createSocket(const char *localPath, const char *remotePath) {
249 pdbg("createSocket, localPath: %s, remotePath: %s", localPath, remotePath);
250 if (m_fd > 0) {
251 return m_fd;
252 }
253
254 int ret = pipe(m_pipefd);
255 if (ret < 0) {
256 perr("createSocket, create pipe error, ret:%d", ret);
257 return -1;
258 }
259 const int fd = socket(AF_UNIX, SOCK_STREAM, 0);
260 if (fd < 0) {
261 perr("createSocket, socket error, fd:%d", fd);
262 return -1;
263 }
264
265 struct sockaddr_un un;
266 socklen_t len;
267 if (remotePath == NULL || strlen(remotePath) == 0) { //server
268 unlink(localPath);
269
270 memset(&un, 0, sizeof(un));
271 un.sun_family = AF_UNIX;
272 un.sun_path[0] = '\0';
273 memcpy(un.sun_path + 1, localPath, strlen(localPath));
274 len = offsetof(struct sockaddr_un, sun_path) + strlen(localPath) + 1;
275 ret = ::bind(fd, (struct sockaddr *) &un, len);
276 if (ret < 0) {
277 close(fd);
278 perr("createSocket, bind error, ret:%d, fd:%d, localPath:%s", ret, fd, localPath);
279 return -2;
280 }
281
282 ret = listen(fd, 5);
283 if (ret < 0) {
284 close(fd);
285 perr("createSocket, listen error, ret:%d, fd:%d, localPath:%s", ret, fd, localPath);
286 return -3;
287 }
288 m_fd = fd;
289 pwrn("createSocket, create Server socket:%d, localPath:%s", m_fd, localPath);
290 return fd;
291 }
292
293 memset(&un, 0, sizeof(un));
294 un.sun_family = AF_UNIX;
295 un.sun_path[0] = '\0';
296 memcpy(un.sun_path + 1, remotePath, strlen(remotePath));
297 len = offsetof(struct sockaddr_un, sun_path) + strlen(remotePath) + 1;
298
299
300
301// non-blocking connect implementation
302 int opt = 1;
303 //set non-blocking
304 if (ioctl(fd, FIONBIO, &opt) < 0) {
305 close(fd);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected