forked from treefrogframework/treefrog-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtapplicationserverbase_unix.cpp
119 lines (96 loc) · 2.99 KB
/
tapplicationserverbase_unix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* Copyright (c) 2010-2017, AOYAMA Kazuharu
* All rights reserved.
*
* This software may be used and distributed according to the terms of
* the New BSD License, which is incorporated herein by reference.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <fcntl.h>
#include <QTcpServer>
#include <QFile>
#include <TSystemGlobal>
#include "tapplicationserverbase.h"
#include "tfcore_unix.h"
void TApplicationServerBase::nativeSocketInit()
{ }
void TApplicationServerBase::nativeSocketCleanup()
{ }
/*!
Listen a port for connections on a socket.
This function must be called in a tfmanager process.
*/
int TApplicationServerBase::nativeListen(const QHostAddress &address, quint16 port, OpenFlag flag)
{
int sd = 0;
QTcpServer server;
if (!server.listen(address, port)) {
tSystemError("Listen failed address:%s port:%d", qPrintable(address.toString()), port);
return sd;
}
sd = duplicateSocket(server.socketDescriptor()); // duplicate
if (flag == CloseOnExec) {
::fcntl(sd, F_SETFD, ::fcntl(sd, F_GETFD) | FD_CLOEXEC);
} else {
::fcntl(sd, F_SETFD, 0); // clear
}
::fcntl(sd, F_SETFL, ::fcntl(sd, F_GETFL) | O_NONBLOCK); // non-block
server.close();
return sd;
}
/*!
Listen for connections on UNIX domain.
*/
int TApplicationServerBase::nativeListen(const QString &fileDomain, OpenFlag flag)
{
int sd = -1;
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = PF_UNIX;
if (sizeof(addr.sun_path) < (uint)fileDomain.toLatin1().size() + 1) {
tSystemError("too long name for UNIX domain socket [%s:%d]", __FILE__, __LINE__);
return sd;
}
strncpy(addr.sun_path, fileDomain.toLatin1().data(), sizeof(addr.sun_path));
// create unix domain socket
sd = ::socket(PF_UNIX, SOCK_STREAM, 0);
if (sd < 0) {
tSystemError("Socket create failed [%s:%d]", __FILE__, __LINE__);
return sd;
}
if (flag == CloseOnExec) {
::fcntl(sd, F_SETFD, FD_CLOEXEC); // set close-on-exec flag
}
::fcntl(sd, F_SETFL, ::fcntl(sd, F_GETFL) | O_NONBLOCK); // non-block
QFile file(fileDomain);
if (file.exists()) {
file.remove();
tSystemWarn("File for UNIX domain socket removed: %s", qPrintable(fileDomain));
}
// Bind
if (::bind(sd, (sockaddr *)&addr, sizeof(sockaddr_un)) < 0) {
tSystemError("Bind failed [%s:%d]", __FILE__, __LINE__);
goto socket_error;
}
file.setPermissions((QFile::Permissions)0x777);
// Listen
if (::listen(sd, 50) < 0) {
tSystemError("Listen failed [%s:%d]", __FILE__, __LINE__);
goto socket_error;
}
return sd;
socket_error:
nativeClose(sd);
return -1;
}
void TApplicationServerBase::nativeClose(int socket)
{
if (socket > 0)
tf_close(socket);
}
int TApplicationServerBase::duplicateSocket(int socketDescriptor)
{
return ::fcntl(socketDescriptor, F_DUPFD, 0); // duplicate
}