Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
batajelo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
14
Issues
14
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
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
louiz’
batajelo
Commits
49106383
Commit
49106383
authored
Apr 16, 2012
by
louiz’
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement timed events.
parent
082831e2
Changes
13
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
162 additions
and
12 deletions
+162
-12
CMakeLists.txt
CMakeLists.txt
+4
-0
src/game/game.cpp
src/game/game.cpp
+0
-1
src/network/client.cpp
src/network/client.cpp
+5
-0
src/network/client.hpp
src/network/client.hpp
+5
-1
src/network/command_handler.cpp
src/network/command_handler.cpp
+1
-6
src/network/command_handler.hpp
src/network/command_handler.hpp
+0
-1
src/network/remote_client.cpp
src/network/remote_client.cpp
+5
-1
src/network/remote_client.hpp
src/network/remote_client.hpp
+4
-1
src/network/server.hpp
src/network/server.hpp
+2
-1
src/network/timed_event.cpp
src/network/timed_event.cpp
+29
-0
src/network/timed_event.hpp
src/network/timed_event.hpp
+44
-0
src/network/timed_event_handler.cpp
src/network/timed_event_handler.cpp
+25
-0
src/network/timed_event_handler.hpp
src/network/timed_event_handler.hpp
+38
-0
No files found.
CMakeLists.txt
View file @
49106383
...
...
@@ -118,6 +118,8 @@ file(
src/network/server.*
src/network/transfer_sender.*
src/network/command.*
src/network/timed_event.*
src/network/timed_event_handler.*
)
add_library
(
server STATIC
${
source_server
}
)
...
...
@@ -135,6 +137,8 @@ file(
src/network/command_handler.*
src/network/transfer_receiver.*
src/network/command.*
src/network/timed_event.*
src/network/timed_event_handler.*
)
add_library
(
client STATIC
${
source_client
}
)
...
...
src/game/game.cpp
View file @
49106383
#include <game/game.hpp>
#include <logging/logging.hpp>
#include <unistd.h>
static
const
char
*
auth_messages
[]
=
{
"Success"
,
...
...
src/network/client.cpp
View file @
49106383
...
...
@@ -71,6 +71,11 @@ void Client::transfer_init_callback(Command* received_command)
this
->
receivers
.
push_back
(
receiver
);
}
boost
::
asio
::
io_service
&
Client
::
get_io_service
()
{
return
this
->
io_service
;
}
void
Client
::
on_transfer_ended
(
const
TransferReceiver
*
receiver
)
{
log_debug
(
"on_transfer_ended"
);
...
...
src/network/client.hpp
View file @
49106383
...
...
@@ -20,12 +20,14 @@
# define __CLIENT_HPP__
#include <network/command_handler.hpp>
#include <network/timed_event_handler.hpp>
#include <network/command.hpp>
#include <network/timed_event.hpp>
#include <network/transfer_receiver.hpp>
using
boost
::
asio
::
ip
::
tcp
;
class
Client
:
public
CommandHandler
class
Client
:
public
CommandHandler
,
public
TimedEventHandler
{
public:
Client
();
...
...
@@ -51,6 +53,8 @@ public:
void
poll
(
void
);
virtual
void
on_connection_closed
()
{}
virtual
boost
::
asio
::
io_service
&
get_io_service
();
/**
* Called when a file transfer is finished.
*/
...
...
src/network/command_handler.cpp
View file @
49106383
...
...
@@ -125,7 +125,6 @@ void CommandHandler::binary_read_handler(const boost::system::error_code& error,
this
->
install_read_handler
();
}
// Set a callback to be called whenever there’s something to read on the socket.
void
CommandHandler
::
install_read_handler
(
void
)
{
boost
::
asio
::
async_read_until
(
*
this
->
socket
,
...
...
@@ -155,12 +154,8 @@ void CommandHandler::send(Command* command, boost::function< void(void) > on_sen
bool
CommandHandler
::
check_commands_to_send
()
{
log_debug
(
"Length of the queue: "
<<
this
->
commands_to_send
.
size
());
// log_debug("check_commands_to_send");
if
(
this
->
writing
||
this
->
commands_to_send
.
empty
())
{
// log_debug("not sending: this->writing=" << this->writing << " queue empty" << this->commands_to_send.empty());
return
false
;
}
return
false
;
this
->
actually_send
(
this
->
commands_to_send
.
back
());
this
->
commands_to_send
.
pop_back
();
return
true
;
...
...
src/network/command_handler.hpp
View file @
49106383
...
...
@@ -118,7 +118,6 @@ private:
* from it and we send it.
*/
command_queue
commands_to_send
;
/**
* Tells us if we are waiting for an async_write to finish or not.
* This must be set to true when calling async_write(), to false
...
...
src/network/remote_client.cpp
View file @
49106383
...
...
@@ -101,7 +101,6 @@ void RemoteClient::on_auth_success()
void
RemoteClient
::
start
()
{
log_debug
(
"Starting RemoteClient "
<<
this
->
number
);
CommandHandler
::
install_read_handler
();
this
->
install_callbacks
();
}
...
...
@@ -124,6 +123,11 @@ void RemoteClient::on_connection_closed()
this
->
server
->
remove_client
(
this
);
}
boost
::
asio
::
io_service
&
RemoteClient
::
get_io_service
()
{
return
this
->
server
->
io_service
;
}
void
RemoteClient
::
on_transfer_ended
(
const
TransferSender
*
transfer
)
{
log_debug
(
"on_transfer_ended"
);
...
...
src/network/remote_client.hpp
View file @
49106383
...
...
@@ -20,13 +20,15 @@
#include <database/user.hpp>
#include <network/command_handler.hpp>
#include <network/command.hpp>
#include <network/timed_event_handler.hpp>
#include <network/timed_event.hpp>
#include <network/transfer_sender.hpp>
class
Server
;
using
boost
::
asio
::
ip
::
tcp
;
class
RemoteClient
:
public
CommandHandler
class
RemoteClient
:
public
CommandHandler
,
public
TimedEventHandler
{
public:
RemoteClient
(
boost
::
asio
::
io_service
&
,
Server
*
);
...
...
@@ -47,6 +49,7 @@ public:
virtual
void
on_connection_closed
();
virtual
boost
::
asio
::
io_service
&
get_io_service
();
/**
* Sends a file to the remote client.
* @param filename The file to send.
...
...
src/network/server.hpp
View file @
49106383
...
...
@@ -43,13 +43,14 @@ public:
*/
RemoteClient
*
find_client_by_login
(
const
std
::
string
&
);
boost
::
asio
::
io_service
io_service
;
private:
void
install_accept_handler
(
void
);
void
handle_accept
(
RemoteClient
*
,
const
boost
::
system
::
error_code
&
);
void
accept
(
void
);
std
::
vector
<
RemoteClient
*>
clients
;
boost
::
asio
::
io_service
io_service
;
tcp
::
acceptor
*
acceptor
;
short
port
;
};
...
...
src/network/timed_event.cpp
0 → 100644
View file @
49106383
#include <network/timed_event.hpp>
#include <network/timed_event_handler.hpp>
TimedEvent
::
TimedEvent
(
const
TimedEventHandler
*
handler
,
boost
::
asio
::
deadline_timer
*
timer
,
const
t_timed_callback
callback
)
:
handler
(
handler
),
timer
(
timer
),
callback
(
callback
)
{
this
->
timer
->
async_wait
(
boost
::
bind
(
&
TimedEvent
::
on_expires
,
this
,
_1
));
}
void
TimedEvent
::
on_expires
(
const
boost
::
system
::
error_code
&
e
)
{
log_debug
(
"on_timeout"
);
this
->
callback
();
}
TimedEvent
::~
TimedEvent
()
{
log_debug
(
"Deleting timed event"
);
delete
this
->
timer
;
}
void
TimedEvent
::
cancel
()
{
this
->
timer
->
cancel
();
}
src/network/timed_event.hpp
0 → 100644
View file @
49106383
/** @addtogroup Network
* @{
*/
/**
* An event to be executed (using a callback) in the future, after the
* specified time has passed.
* @class TimedEvent
*/
#include <boost/asio.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#ifndef __TIMED_EVENT_HPP__
# define __TIMED_EVENT_HPP__
class
TimedEventHandler
;
typedef
boost
::
function
<
void
(
void
)
>
t_timed_callback
;
class
TimedEventHandler
;
class
TimedEvent
{
public:
TimedEvent
(
const
TimedEventHandler
*
,
boost
::
asio
::
deadline_timer
*
,
const
t_timed_callback
);
~
TimedEvent
();
void
cancel
();
void
on_expires
(
const
boost
::
system
::
error_code
&
);
private:
TimedEvent
(
const
TimedEvent
&
);
TimedEvent
&
operator
=
(
const
TimedEvent
&
);
const
TimedEventHandler
*
handler
;
boost
::
asio
::
deadline_timer
*
timer
;
const
t_timed_callback
callback
;
};
#endif // __TIMED_EVENT_HPP__
src/network/timed_event_handler.cpp
0 → 100644
View file @
49106383
#include <network/timed_event_handler.hpp>
TimedEventHandler
::
TimedEventHandler
()
{
}
TimedEventHandler
::~
TimedEventHandler
()
{
log_debug
(
"Deleting TimedEventHandler"
);
std
::
vector
<
TimedEvent
*>::
iterator
it
;
for
(
it
=
this
->
events
.
begin
();
it
<
this
->
events
.
end
();
++
it
)
{
delete
(
*
it
);
}
}
void
TimedEventHandler
::
install_timed_event
(
const
t_timed_callback
callback
,
const
int
delay
)
{
log_debug
(
"installing timed_event"
);
boost
::
asio
::
deadline_timer
*
timer
=
new
boost
::
asio
::
deadline_timer
(
this
->
get_io_service
(),
boost
::
posix_time
::
seconds
(
delay
));
TimedEvent
*
event
=
new
TimedEvent
(
this
,
timer
,
callback
);
this
->
events
.
push_back
(
event
);
}
src/network/timed_event_handler.hpp
0 → 100644
View file @
49106383
/** @addtogroup Network
* @{
*/
/**
*
* @class TimedEventHandler
*/
#include <map>
#ifndef __TIMED_EVENT_HANDLER_HPP__
# define __TIMED_EVENT_HANDLER_HPP__
#include <network/timed_event.hpp>
#include <logging/logging.hpp>
class
TimedEventHandler
{
public:
TimedEventHandler
();
~
TimedEventHandler
();
/**
* Install a timed event to be executed in the futur at the specified date.
*/
void
install_timed_event
(
const
t_timed_callback
,
const
int
);
virtual
boost
::
asio
::
io_service
&
get_io_service
()
=
0
;
private:
TimedEventHandler
(
const
TimedEventHandler
&
);
TimedEventHandler
&
operator
=
(
const
TimedEventHandler
&
);
std
::
vector
<
TimedEvent
*>
events
;
};
#endif // __TIMED_EVENT_HANDLER_HPP__
/**@}*/
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