Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pistache
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
pistache
Commits
857fd82b
Commit
857fd82b
authored
Mar 25, 2016
by
octal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implemented a listenAndServe function and fixed a bug in Address parsing
parent
d698fbe4
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
16 deletions
+52
-16
include/endpoint.h
include/endpoint.h
+16
-0
include/net.h
include/net.h
+7
-0
src/common/net.cc
src/common/net.cc
+29
-16
No files found.
include/endpoint.h
View file @
857fd82b
...
@@ -80,6 +80,22 @@ private:
...
@@ -80,6 +80,22 @@ private:
Net
::
Tcp
::
Listener
listener
;
Net
::
Tcp
::
Listener
listener
;
};
};
template
<
typename
Handler
>
void
listenAndServe
(
Address
addr
)
{
auto
options
=
Endpoint
::
options
().
threads
(
1
);
listenAndServe
<
Handler
>
(
addr
,
options
);
}
template
<
typename
Handler
>
void
listenAndServe
(
Address
addr
,
const
Endpoint
::
Options
&
options
)
{
Endpoint
endpoint
(
addr
);
endpoint
.
init
(
options
);
endpoint
.
setHandler
(
make_handler
<
Handler
>
());
endpoint
.
serve
();
}
}
// namespace Http
}
// namespace Http
...
...
include/net.h
View file @
857fd82b
...
@@ -9,6 +9,7 @@
...
@@ -9,6 +9,7 @@
#include <sys/socket.h>
#include <sys/socket.h>
#include <cstring>
#include <cstring>
#include <stdexcept>
#include <stdexcept>
#include <limits>
#ifndef _KERNEL_FASTOPEN
#ifndef _KERNEL_FASTOPEN
#define _KERNEL_FASTOPEN
#define _KERNEL_FASTOPEN
...
@@ -30,6 +31,10 @@ public:
...
@@ -30,6 +31,10 @@ public:
bool
isReserved
()
const
;
bool
isReserved
()
const
;
bool
isUsed
()
const
;
bool
isUsed
()
const
;
static
constexpr
uint16_t
max
()
{
return
std
::
numeric_limits
<
uint16_t
>::
max
();
}
private:
private:
uint16_t
port
;
uint16_t
port
;
};
};
...
@@ -53,6 +58,7 @@ public:
...
@@ -53,6 +58,7 @@ public:
Address
();
Address
();
Address
(
std
::
string
host
,
Port
port
);
Address
(
std
::
string
host
,
Port
port
);
Address
(
std
::
string
addr
);
Address
(
std
::
string
addr
);
Address
(
const
char
*
addr
);
Address
(
Ipv4
ip
,
Port
port
);
Address
(
Ipv4
ip
,
Port
port
);
Address
(
const
Address
&
other
)
=
default
;
Address
(
const
Address
&
other
)
=
default
;
...
@@ -67,6 +73,7 @@ public:
...
@@ -67,6 +73,7 @@ public:
Port
port
()
const
;
Port
port
()
const
;
private:
private:
void
init
(
std
::
string
addr
);
std
::
string
host_
;
std
::
string
host_
;
Port
port_
;
Port
port_
;
};
};
...
...
src/common/net.cc
View file @
857fd82b
...
@@ -11,6 +11,7 @@
...
@@ -11,6 +11,7 @@
#include <cstring>
#include <cstring>
#include <netinet/in.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/inet.h>
#include <iostream>
using
namespace
std
;
using
namespace
std
;
...
@@ -66,21 +67,12 @@ Address::Address(std::string host, Port port)
...
@@ -66,21 +67,12 @@ Address::Address(std::string host, Port port)
Address
::
Address
(
std
::
string
addr
)
Address
::
Address
(
std
::
string
addr
)
{
{
auto
pos
=
addr
.
find
(
':'
);
init
(
std
::
move
(
addr
));
}
if
(
pos
==
std
::
string
::
npos
)
throw
std
::
invalid_argument
(
"Invalid address"
);
std
::
string
host
=
addr
.
substr
(
0
,
pos
);
char
*
end
;
const
std
::
string
portPart
=
addr
.
substr
(
pos
+
1
);
long
port
=
strtol
(
portPart
.
c_str
(),
&
end
,
10
);
if
(
*
end
!=
0
||
port
>
std
::
numeric_limits
<
Port
>::
max
())
throw
std
::
invalid_argument
(
"Invalid port"
);
host_
=
std
::
move
(
host
);
Address
::
Address
(
const
char
*
addr
)
port_
=
port
;
{
init
(
std
::
string
(
addr
));
}
}
Address
::
Address
(
Ipv4
ip
,
Port
port
)
Address
::
Address
(
Ipv4
ip
,
Port
port
)
...
@@ -98,14 +90,35 @@ Address::fromUnix(struct sockaddr* addr) {
...
@@ -98,14 +90,35 @@ Address::fromUnix(struct sockaddr* addr) {
return
Address
(
std
::
move
(
host
),
port
);
return
Address
(
std
::
move
(
host
),
port
);
}
}
std
::
string
Address
::
host
()
const
{
std
::
string
Address
::
host
()
const
{
return
host_
;
return
host_
;
}
}
Port
Address
::
port
()
const
{
Port
Address
::
port
()
const
{
return
port_
;
return
port_
;
}
}
void
Address
::
init
(
std
::
string
addr
)
{
auto
pos
=
addr
.
find
(
':'
);
if
(
pos
==
std
::
string
::
npos
)
throw
std
::
invalid_argument
(
"Invalid address"
);
std
::
string
host
=
addr
.
substr
(
0
,
pos
);
char
*
end
;
const
std
::
string
portPart
=
addr
.
substr
(
pos
+
1
);
long
port
=
strtol
(
portPart
.
c_str
(),
&
end
,
10
);
if
(
*
end
!=
0
||
port
>
Port
::
max
())
throw
std
::
invalid_argument
(
"Invalid port"
);
host_
=
std
::
move
(
host
);
port_
=
port
;
}
Error
::
Error
(
const
char
*
message
)
Error
::
Error
(
const
char
*
message
)
:
std
::
runtime_error
(
message
)
:
std
::
runtime_error
(
message
)
{
}
{
}
...
...
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