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
11e79cb5
Commit
11e79cb5
authored
Aug 26, 2015
by
Mathieu Stefani
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Peer data is now stored inside an unordered_map and indexed by string
parent
001c561b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
46 additions
and
14 deletions
+46
-14
src/http.cc
src/http.cc
+5
-4
src/http.h
src/http.h
+1
-1
src/peer.cc
src/peer.cc
+22
-6
src/peer.h
src/peer.h
+18
-3
No files found.
src/http.cc
View file @
11e79cb5
...
...
@@ -22,6 +22,8 @@ static constexpr char CR = 0xD;
static
constexpr
char
LF
=
0xA
;
static
constexpr
char
CRLF
[]
=
{
CR
,
LF
};
static
constexpr
const
char
*
ParserData
=
"__Parser"
;
namespace
Private
{
Parser
::
Buffer
::
Buffer
()
...
...
@@ -476,7 +478,7 @@ Handler::onOutput() {
void
Handler
::
onConnection
(
Tcp
::
Peer
&
peer
)
{
peer
.
setData
(
std
::
make_shared
<
Private
::
Parser
>
());
peer
.
putData
(
ParserData
,
std
::
make_shared
<
Private
::
Parser
>
());
}
void
...
...
@@ -484,9 +486,8 @@ Handler::onDisconnection(Tcp::Peer& peer) {
}
Private
::
Parser
&
Handler
::
getParser
(
Tcp
::
Peer
&
peer
)
{
auto
data
=
peer
.
data
();
return
*
std
::
static_pointer_cast
<
Private
::
Parser
>
(
data
);
Handler
::
getParser
(
Tcp
::
Peer
&
peer
)
const
{
return
*
peer
.
getData
<
Private
::
Parser
>
(
ParserData
);
}
Endpoint
::
Endpoint
()
...
...
src/http.h
View file @
11e79cb5
...
...
@@ -347,7 +347,7 @@ public:
virtual
void
onRequest
(
const
Request
&
request
,
Tcp
::
Peer
&
peer
)
=
0
;
private:
Private
::
Parser
&
getParser
(
Tcp
::
Peer
&
peer
);
Private
::
Parser
&
getParser
(
Tcp
::
Peer
&
peer
)
const
;
};
class
Endpoint
{
...
...
src/peer.cc
View file @
11e79cb5
...
...
@@ -15,13 +15,11 @@ using namespace std;
Peer
::
Peer
()
:
fd_
(
-
1
)
,
data_
(
nullptr
)
{
}
Peer
::
Peer
(
const
Address
&
addr
)
:
addr
(
addr
)
,
fd_
(
-
1
)
,
data_
(
nullptr
)
{
}
Address
...
...
@@ -49,13 +47,31 @@ Peer::fd() const {
}
void
Peer
::
setData
(
std
::
shared_ptr
<
void
>
data
)
{
data_
=
std
::
move
(
data
);
Peer
::
putData
(
std
::
string
name
,
std
::
shared_ptr
<
void
>
data
)
{
auto
it
=
data_
.
find
(
name
);
if
(
it
!=
std
::
end
(
data_
))
{
throw
std
::
runtime_error
(
"The data already exists"
);
}
data_
.
insert
(
std
::
make_pair
(
std
::
move
(
name
),
std
::
move
(
data
)));
}
std
::
shared_ptr
<
void
>
Peer
::
getData
(
std
::
string
name
)
const
{
auto
data
=
tryGetData
(
std
::
move
(
name
));
if
(
data
==
nullptr
)
{
throw
std
::
runtime_error
(
"The data does not exist"
);
}
return
data
;
}
std
::
shared_ptr
<
void
>
Peer
::
data
()
const
{
return
data_
;
Peer
::
tryGetData
(
std
::
string
(
name
))
const
{
auto
it
=
data_
.
find
(
name
);
if
(
it
==
std
::
end
(
data_
))
return
nullptr
;
return
it
->
second
;
}
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
Peer
&
peer
)
{
...
...
src/peer.h
View file @
11e79cb5
...
...
@@ -11,6 +11,7 @@
#include <string>
#include <iostream>
#include <memory>
#include <unordered_map>
namespace
Net
{
...
...
@@ -27,15 +28,29 @@ public:
void
associateFd
(
Fd
fd
);
Fd
fd
()
const
;
std
::
shared_ptr
<
void
>
data
()
const
;
void
setData
(
std
::
shared_ptr
<
void
>
data
);
void
putData
(
std
::
string
name
,
std
::
shared_ptr
<
void
>
data
);
std
::
shared_ptr
<
void
>
getData
(
std
::
string
name
)
const
;
template
<
typename
T
>
std
::
shared_ptr
<
T
>
getData
(
std
::
string
name
)
const
{
return
std
::
static_pointer_cast
<
T
>
(
getData
(
std
::
move
(
name
)));
}
std
::
shared_ptr
<
void
>
tryGetData
(
std
::
string
name
)
const
;
template
<
typename
T
>
std
::
shared_ptr
<
T
>
tryGetData
(
std
::
string
name
)
const
{
auto
data
=
tryGetData
(
std
::
move
(
name
));
if
(
data
==
nullptr
)
return
nullptr
;
return
std
::
static_pointer_cast
<
T
>
(
data
);
}
private:
Address
addr
;
std
::
string
hostname_
;
Fd
fd_
;
std
::
shared_ptr
<
void
>
data_
;
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
void
>
>
data_
;
};
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
Peer
&
peer
);
...
...
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