Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
net7
libsms
Commits
15acbe6f
Commit
15acbe6f
authored
Oct 24, 2017
by
Thomas Bailleux
Browse files
Add comment, move private section below public section according to google C++ style guide
parent
87992789
Changes
5
Hide whitespace changes
Inline
Side-by-side
include/sms/http_parser.hpp
View file @
15acbe6f
...
...
@@ -32,13 +32,27 @@
namespace
sms
{
/* An HTTP Header */
class
Header
{
public:
std
::
string
m_name
;
std
::
string
m_value
;
};
/* Parser for HTTP Request.
* This parser extracts the method, the path and the header. It does not support the content and the query. */
class
HttpParser
{
public:
/* Construct a parser from the buffer to parse. */
HttpParser
(
char
*
buffer
,
size_t
buffer_length
)
:
m_buffer
(
buffer
),
m_buffer_length
(
buffer_length
)
{};
/* Parse the buffer. */
int
parse
();
/* Get the headers. */
std
::
vector
<
Header
>&
get_headers
();
private:
std
::
string
m_method
;
std
::
string
m_path
;
...
...
@@ -49,10 +63,6 @@ class HttpParser {
int
parse_first_line
(
std
::
string
&
line
);
int
parse_header
(
std
::
string
&
line
);
void
uri_decode
(
std
::
string
&
data
);
public:
HttpParser
(
char
*
buffer
,
size_t
buffer_length
)
:
m_buffer
(
buffer
),
m_buffer_length
(
buffer_length
)
{};
int
parse
();
std
::
vector
<
Header
>&
get_headers
();
};
}
/* namespace sms */
...
...
include/sms/server.hpp
View file @
15acbe6f
...
...
@@ -35,14 +35,19 @@
namespace
sms
{
/* Type of events
* For now, there is only MESSAGE. This means the event when the server receives a message. */
enum
Event
{
MESSAGE
};
/* Handle an event. */
class
Handler
{
public:
/* Type of event. */
Event
m_event
;
/* Function (or method) which will be called by the server whenever a event happens. */
std
::
function
<
void
(
std
::
unique_ptr
<
Sms
>
sms
)
>
m_callback
;
};
...
...
@@ -50,6 +55,24 @@ class Connection;
class
Server
{
friend
class
Connection
;
public:
/* Construct a server which is going to listen on host:port.
* NOTE: host is not still supported, host is 0.0.0.0 for now. */
Server
(
std
::
string
&
host
,
unsigned
short
port
);
/* Retrieve a reference of the asio service which handles the server. */
boost
::
asio
::
io_service
&
get_io_service
();
/* Set an handler for a specified event. */
bool
set_event_handler
(
std
::
unique_ptr
<
Handler
>
handler
);
/* Initiate acceptor for clients. */
void
start_accept
();
/* Run server (aka run asio service). */
void
run
();
private:
boost
::
asio
::
io_service
m_io_service
;
std
::
unique_ptr
<
boost
::
asio
::
ip
::
tcp
::
acceptor
>
m_acceptor
;
...
...
@@ -62,18 +85,19 @@ class Server {
void
remove_connection
(
Connection
*
connection
);
void
notify_on_sms_received
(
std
::
unique_ptr
<
Sms
>
sms
);
public:
Server
(
std
::
string
&
host
,
unsigned
short
port
);
boost
::
asio
::
io_service
&
get_io_service
();
bool
set_event_handler
(
std
::
unique_ptr
<
Handler
>
handler
);
void
start_accept
();
void
run
();
};
/* Class which describes an HTTP request. */
class
Connection
:
std
::
enable_shared_from_this
<
Connection
>
{
friend
class
Server
;
public:
/* Construct an object from the pending server, the asio service and a random uuid. */
explicit
Connection
(
Server
&
server
)
:
m_socket
(
server
.
get_io_service
()),
m_server
(
server
),
m_data
{
0
},
m_uuid
(
boost
::
uuids
::
random_generator
()())
{}
private:
boost
::
asio
::
ip
::
tcp
::
socket
m_socket
;
Server
&
m_server
;
...
...
@@ -84,12 +108,6 @@ class Connection: std::enable_shared_from_this<Connection> {
boost
::
asio
::
ip
::
tcp
::
socket
&
get_socket
();
void
receive_http_request
(
const
boost
::
system
::
error_code
&
error
,
size_t
bytes_transferred
);
void
parse_http_request
(
size_t
length
);
public:
explicit
Connection
(
Server
&
server
)
:
m_socket
(
server
.
get_io_service
()),
m_server
(
server
),
m_data
{
0
},
m_uuid
(
boost
::
uuids
::
random_generator
()())
{}
};
}
/* namespace sms */
...
...
include/sms/sms.hpp
View file @
15acbe6f
...
...
@@ -25,21 +25,32 @@
namespace
sms
{
/* Type of message.
* For now, only SMS are supported. */
enum
SmsType
{
SMS
,
MMS
};
/* A simple SMS. */
class
Sms
{
private:
std
::
string
m_number
;
std
::
string
m_content
;
SmsType
m_type
;
public:
/* Construct a SMS from the sender's number, the content and the type. */
Sms
(
std
::
string
&
number
,
std
::
string
&
content
,
SmsType
type
)
:
m_number
(
number
),
m_content
(
content
),
m_type
(
type
)
{};
/* Get the content. */
const
std
::
string
&
get_content
();
/* Get the number. */
const
std
::
string
&
get_number
();
/* Get the message's type (SMS or MMS). */
SmsType
get_type
();
private:
std
::
string
m_number
;
std
::
string
m_content
;
SmsType
m_type
;
};
}
/* namespace sms */
...
...
src/http_parser.cc
View file @
15acbe6f
...
...
@@ -26,12 +26,14 @@ namespace sms {
int
HttpParser
::
parse
()
{
std
::
string
buffer
(
m_buffer
);
/* Remove the \r for commodity. */
buffer
.
erase
(
std
::
remove
(
buffer
.
begin
(),
buffer
.
end
(),
'\r'
),
buffer
.
end
());
std
::
istringstream
stream
(
buffer
);
std
::
string
s
;
int
ret
=
NO_ERROR
;
if
(
std
::
getline
(
stream
,
s
,
'\n'
))
{
ret
=
parse_first_line
(
s
);
ret
=
parse_first_line
(
s
);
/* This actually parses the method and the path. */
}
else
{
ret
=
BAD_REQUEST
;
}
...
...
src/server.cc
View file @
15acbe6f
...
...
@@ -24,6 +24,7 @@
namespace
sms
{
Server
::
Server
(
std
::
string
&
host
,
unsigned
short
port
)
{
m_host
=
host
;
m_port
=
port
;
...
...
@@ -97,11 +98,10 @@ void Connection::run() {
void
Connection
::
receive_http_request
(
const
boost
::
system
::
error_code
&
error
,
size_t
bytes_transferred
)
{
if
(
!
error
)
{
parse_http_request
(
bytes_transferred
);
/* Answer a simple 200, in order to close the connection. */
std
::
string
response
(
"HTTP/1.1 200 OK
\n
Content-Length: 0
\n\n
"
);
m_socket
.
send
(
boost
::
asio
::
buffer
(
response
));
}
else
{
std
::
cout
<<
"Error: "
<<
error
<<
std
::
endl
;
}
m_socket
.
close
();
m_server
.
remove_connection
(
this
);
...
...
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