Skip to content

Commit 0fc589e

Browse files
committed
Minor fixes #2
1 parent 47f6638 commit 0fc589e

File tree

4 files changed

+46
-33
lines changed

4 files changed

+46
-33
lines changed

projects/qt-creator/httpserverapp.pro.user

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 3.5.1, 2016-01-09T02:26:09. -->
3+
<!-- Written by QtCreator 3.5.1, 2016-01-10T19:16:23. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>
@@ -61,7 +61,7 @@
6161
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
6262
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
6363
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{c77ee4a2-1c2a-4bac-9185-8378ec4ebf5d}</value>
64-
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
64+
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
6565
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
6666
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
6767
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">

src/Socket.cpp

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ namespace HttpServer
2727
#endif
2828
}
2929

30+
int Socket::getLastError()
31+
{
32+
#ifdef WIN32
33+
return ::WSAGetLastError();
34+
#elif POSIX
35+
return errno;
36+
#else
37+
#error "Undefine platform"
38+
#endif
39+
}
40+
3041
Socket::Socket(): socket_handle(~0)
3142
{
3243

@@ -47,16 +58,16 @@ namespace HttpServer
4758
obj.socket_handle = ~0;
4859
}
4960

50-
System::native_socket_type Socket::open()
61+
bool Socket::open()
5162
{
5263
close();
5364

5465
socket_handle = ::socket(AF_INET, SOCK_STREAM, 0);
5566

56-
return socket_handle;
67+
return is_open();
5768
}
5869

59-
int Socket::close()
70+
bool Socket::close()
6071
{
6172
if (is_open() )
6273
{
@@ -71,15 +82,15 @@ namespace HttpServer
7182
if (0 == result)
7283
{
7384
socket_handle = ~0;
74-
}
7585

76-
return result;
86+
return true;
87+
}
7788
}
7889

79-
return ~0;
90+
return false;
8091
}
8192

82-
int Socket::bind(const int port) const
93+
bool Socket::bind(const int port) const
8394
{
8495
const ::sockaddr_in sock_addr = {
8596
AF_INET,
@@ -88,12 +99,12 @@ namespace HttpServer
8899
0
89100
};
90101

91-
return ::bind(socket_handle, reinterpret_cast<const sockaddr *>(&sock_addr), sizeof(sockaddr_in) );
102+
return 0 == ::bind(socket_handle, reinterpret_cast<const sockaddr *>(&sock_addr), sizeof(sockaddr_in) );
92103
}
93104

94-
int Socket::listen() const
105+
bool Socket::listen() const
95106
{
96-
return ::listen(socket_handle, SOMAXCONN);
107+
return 0 == ::listen(socket_handle, SOMAXCONN);
97108
}
98109

99110
Socket Socket::accept() const
@@ -170,20 +181,20 @@ namespace HttpServer
170181
return Socket(client_socket);
171182
}
172183

173-
int Socket::shutdown() const
184+
bool Socket::shutdown() const
174185
{
175186
if (is_open() )
176187
{
177188
#ifdef WIN32
178-
return ::shutdown(socket_handle, SD_BOTH);
189+
return 0 == ::shutdown(socket_handle, SD_BOTH);
179190
#elif POSIX
180-
return ::shutdown(socket_handle, SHUT_RDWR);
191+
return 0 == ::shutdown(socket_handle, SHUT_RDWR);
181192
#else
182193
#error "Undefine platform"
183194
#endif
184195
}
185196

186-
return -1;
197+
return false;
187198
}
188199

189200
bool Socket::nonblock(const bool isNonBlock) const
@@ -192,32 +203,34 @@ namespace HttpServer
192203
unsigned long value = isNonBlock;
193204
return 0 == ::ioctlsocket(socket_handle, FIONBIO, &value);
194205
#elif POSIX
195-
return -1 != ::fcntl(socket_handle, F_SETFL, isNonBlock ? O_NONBLOCK : O_SYNC);
206+
return ~0 != ::fcntl(socket_handle, F_SETFL, isNonBlock ? O_NONBLOCK : O_SYNC);
196207
#else
197208
#error "Undefine platform"
198209
#endif
199210
}
200211

201-
/* bool Socket::is_nonblock() const
212+
/*
213+
bool Socket::is_nonblock() const
202214
{
203215
#ifdef WIN32
204216
205217
#elif POSIX
206-
int flags = ::fcntl(socket_handle, F_GETFL, 0);
207-
return (flags != -1) && (flags & O_NONBLOCK);
218+
const int flags = ::fcntl(socket_handle, F_GETFL, 0);
219+
return (flags != ~0) && (flags & O_NONBLOCK);
208220
#else
209221
#error "Undefine platform"
210222
#endif
211-
}*/
223+
}
224+
*/
212225

213226
bool Socket::tcp_nodelay(const bool nodelay) const
214227
{
215228
#ifdef WIN32
216229
int flags = nodelay ? 1 : 0;
217-
return 0 == setsockopt(socket_handle, IPPROTO_TCP, TCP_NODELAY, (char *)&flags, sizeof(flags) );
230+
return 0 == setsockopt(socket_handle, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&flags), sizeof(flags) );
218231
#elif POSIX
219232
int flags = nodelay ? 1 : 0;
220-
return 0 == setsockopt(socket_handle, IPPROTO_TCP, TCP_NODELAY, (char *)&flags, sizeof(flags) );
233+
return 0 == setsockopt(socket_handle, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags) );
221234
#else
222235
#error "Undefine platform"
223236
#endif

src/Socket.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace HttpServer
3636
public:
3737
bool static Startup();
3838
bool static Cleanup();
39+
int static getLastError();
3940

4041
public:
4142
Socket();
@@ -45,8 +46,8 @@ namespace HttpServer
4546

4647
~Socket() = default;
4748

48-
System::native_socket_type open();
49-
int close();
49+
bool open();
50+
bool close();
5051

5152
inline bool is_open() const
5253
{
@@ -59,14 +60,14 @@ namespace HttpServer
5960
#endif
6061
}
6162

62-
int bind(const int port) const;
63-
int listen() const;
63+
bool bind(const int port) const;
64+
bool listen() const;
6465

6566
Socket accept() const;
6667
Socket nonblock_accept() const;
6768
Socket nonblock_accept(const std::chrono::milliseconds &timeout) const;
6869

69-
int shutdown() const;
70+
bool shutdown() const;
7071

7172
bool nonblock(const bool isNonBlock = true) const;
7273
// bool is_nonblock() const;

src/System.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ namespace System
2121
::HWND hWnd;
2222
};
2323

24-
::BOOL WINAPI EnumProc(::HWND hWnd, ::LPARAM lParam)
24+
static ::BOOL WINAPI EnumProc(const ::HWND hWnd, const ::LPARAM lParam)
2525
{
26-
EnumData &ed = *reinterpret_cast<EnumData *>(lParam);
26+
EnumData &ed = *reinterpret_cast<EnumData * const>(lParam);
2727

2828
native_processid_type process_id = 0;
2929

@@ -143,7 +143,7 @@ namespace System
143143
const std::string &file_path = filePath;
144144
#endif
145145

146-
::HANDLE hFile = ::CreateFile(file_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
146+
const ::HANDLE hFile = ::CreateFile(file_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
147147

148148
if (INVALID_HANDLE_VALUE == hFile)
149149
{
@@ -187,7 +187,6 @@ namespace System
187187

188188
return true;
189189
#elif POSIX
190-
struct ::tm *clock;
191190
struct ::stat attrib;
192191

193192
if (-1 == ::stat(filePath.c_str(), &attrib) )
@@ -197,7 +196,7 @@ namespace System
197196

198197
*fileSize = attrib.st_size;
199198

200-
clock = ::gmtime(&(attrib.st_mtime) );
199+
struct ::tm *clock = ::gmtime(&(attrib.st_mtime) );
201200

202201
*fileTime = ::mktime(clock);
203202

0 commit comments

Comments
 (0)