Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
5bb2d64d
Commit
5bb2d64d
authored
Jan 15, 2013
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SocketError: merge duplicate FormatMessage() calls
Make a class that contains the formatting code and the buffer.
parent
bc66dc45
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
26 deletions
+36
-26
SocketError.hxx
src/SocketError.hxx
+36
-26
No files found.
src/SocketError.hxx
View file @
5bb2d64d
...
...
@@ -87,27 +87,49 @@ IsSocketErrorClosed(socket_error_t code)
#endif
}
/**
* Helper class that formats a socket error message into a
* human-readable string. On Windows, a buffer is necessary for this,
* and this class hosts the buffer.
*/
class
SocketErrorMessage
{
#ifdef WIN32
char
msg
[
256
];
#else
const
char
*
const
msg
;
#endif
public
:
#ifdef WIN32
explicit
SocketErrorMessage
(
socket_error_t
code
=
GetSocketError
())
{
DWORD
nbytes
=
FormatMessage
(
FORMAT_MESSAGE_FROM_SYSTEM
|
FORMAT_MESSAGE_IGNORE_INSERTS
|
FORMAT_MESSAGE_MAX_WIDTH_MASK
,
NULL
,
code
,
0
,
(
LPSTR
)
msg
,
sizeof
(
msg
),
NULL
);
if
(
nbytes
==
0
)
strcpy
(
msg
,
"Unknown error"
);
}
#else
explicit
SocketErrorMessage
(
socket_error_t
code
=
GetSocketError
())
:
msg
(
g_strerror
(
code
))
{}
#endif
operator
const
char
*
()
const
{
return
msg
;
}
};
static
inline
void
SetSocketError
(
GError
**
error_r
,
socket_error_t
code
)
{
#ifdef WIN32
if
(
error_r
==
NULL
)
return
;
#endif
char
buffer
[
256
];
DWORD
nbytes
=
FormatMessage
(
FORMAT_MESSAGE_FROM_SYSTEM
|
FORMAT_MESSAGE_IGNORE_INSERTS
|
FORMAT_MESSAGE_MAX_WIDTH_MASK
,
NULL
,
code
,
0
,
(
LPSTR
)
buffer
,
sizeof
(
buffer
),
NULL
);
const
char
*
msg
=
nbytes
>
0
?
buffer
:
"Unknown error"
;
const
SocketErrorMessage
msg
(
code
);
g_set_error_literal
(
error_r
,
SocketErrorQuark
(),
code
,
msg
);
#else
g_set_error_literal
(
error_r
,
SocketErrorQuark
(),
code
,
g_strerror
(
code
));
#endif
}
static
inline
void
...
...
@@ -120,20 +142,8 @@ gcc_malloc
static
inline
GError
*
NewSocketError
(
socket_error_t
code
)
{
#ifdef WIN32
char
buffer
[
256
];
DWORD
nbytes
=
FormatMessage
(
FORMAT_MESSAGE_FROM_SYSTEM
|
FORMAT_MESSAGE_IGNORE_INSERTS
|
FORMAT_MESSAGE_MAX_WIDTH_MASK
,
NULL
,
code
,
0
,
(
LPSTR
)
buffer
,
sizeof
(
buffer
),
NULL
);
const
char
*
msg
=
nbytes
>
0
?
buffer
:
"Unknown error"
;
const
SocketErrorMessage
msg
(
code
);
return
g_error_new_literal
(
SocketErrorQuark
(),
code
,
msg
);
#else
return
g_error_new_literal
(
SocketErrorQuark
(),
code
,
g_strerror
(
code
));
#endif
}
gcc_malloc
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment