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
e6e9b204
Commit
e6e9b204
authored
May 17, 2017
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
net/AllocatedSocketAddress: add method SetPort()
parent
77c747a8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
2 deletions
+66
-2
AllocatedSocketAddress.cxx
src/net/AllocatedSocketAddress.cxx
+38
-1
AllocatedSocketAddress.hxx
src/net/AllocatedSocketAddress.hxx
+28
-1
No files found.
src/net/AllocatedSocketAddress.cxx
View file @
e6e9b204
/*
/*
* Copyright (C) 2012-201
5
Max Kellermann <max.kellermann@gmail.com>
* Copyright (C) 2012-201
7
Max Kellermann <max.kellermann@gmail.com>
*
*
* Redistribution and use in source and binary forms, with or without
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* modification, are permitted provided that the following conditions
...
@@ -36,6 +36,14 @@
...
@@ -36,6 +36,14 @@
#include <sys/un.h>
#include <sys/un.h>
#endif
#endif
#ifdef HAVE_TCP
#ifdef WIN32
#include <ws2tcpip.h>
#else
#include <netinet/in.h>
#endif
#endif
AllocatedSocketAddress
&
AllocatedSocketAddress
&
AllocatedSocketAddress
::
operator
=
(
SocketAddress
src
)
AllocatedSocketAddress
::
operator
=
(
SocketAddress
src
)
{
{
...
@@ -82,3 +90,32 @@ AllocatedSocketAddress::SetLocal(const char *path) noexcept
...
@@ -82,3 +90,32 @@ AllocatedSocketAddress::SetLocal(const char *path) noexcept
}
}
#endif
#endif
#ifdef HAVE_TCP
bool
AllocatedSocketAddress
::
SetPort
(
unsigned
port
)
noexcept
{
if
(
IsNull
())
return
false
;
switch
(
GetFamily
())
{
case
AF_INET
:
{
auto
*
a
=
(
struct
sockaddr_in
*
)(
void
*
)
address
;
a
->
sin_port
=
htons
(
port
);
return
true
;
}
case
AF_INET6
:
{
auto
*
a
=
(
struct
sockaddr_in6
*
)(
void
*
)
address
;
a
->
sin6_port
=
htons
(
port
);
return
true
;
}
}
return
false
;
}
#endif
src/net/AllocatedSocketAddress.hxx
View file @
e6e9b204
/*
/*
* Copyright (C) 2012-201
5
Max Kellermann <max.kellermann@gmail.com>
* Copyright (C) 2012-201
7
Max Kellermann <max.kellermann@gmail.com>
*
*
* Redistribution and use in source and binary forms, with or without
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* modification, are permitted provided that the following conditions
...
@@ -143,6 +143,33 @@ public:
...
@@ -143,6 +143,33 @@ public:
void
SetLocal
(
const
char
*
path
)
noexcept
;
void
SetLocal
(
const
char
*
path
)
noexcept
;
#endif
#endif
#ifdef HAVE_TCP
/**
* Extract the port number. Returns 0 if not applicable.
*/
gcc_pure
unsigned
GetPort
()
const
noexcept
{
return
((
SocketAddress
)
*
this
).
GetPort
();
}
/**
* @return true on success, false if this address cannot have
* a port number
*/
bool
SetPort
(
unsigned
port
)
noexcept
;
static
AllocatedSocketAddress
WithPort
(
SocketAddress
src
,
unsigned
port
)
noexcept
{
AllocatedSocketAddress
result
(
src
);
result
.
SetPort
(
port
);
return
result
;
}
AllocatedSocketAddress
WithPort
(
unsigned
port
)
const
noexcept
{
return
WithPort
(
*
this
,
port
);
}
#endif
private
:
private
:
void
SetSize
(
size_type
new_size
)
noexcept
;
void
SetSize
(
size_type
new_size
)
noexcept
;
};
};
...
...
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