Skip to content

Commit

Permalink
Added stat counter for desktop session.
Browse files Browse the repository at this point in the history
  • Loading branch information
dchapyshev committed Nov 15, 2023
1 parent 467f607 commit 16d3dc1
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 2 deletions.
2 changes: 2 additions & 0 deletions source/host/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ list(APPEND SOURCE_HOST_CORE
service_constants.h
service_main.cc
service_main.h
stat_counter.cc
stat_counter.h
system_settings.cc
system_settings.h
unconfirmed_client_session.cc
Expand Down
22 changes: 20 additions & 2 deletions source/host/client_session_desktop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ ClientSessionDesktop::ClientSessionDesktop(proto::SessionType session_type,
std::unique_ptr<base::TcpChannel> channel,
std::shared_ptr<base::TaskRunner> task_runner)
: ClientSession(session_type, std::move(channel)),
overflow_detection_timer_(base::WaitableTimer::Type::REPEATED, std::move(task_runner)),
overflow_detection_timer_(base::WaitableTimer::Type::REPEATED, task_runner),
incoming_message_(std::make_unique<proto::ClientToHost>()),
outgoing_message_(std::make_unique<proto::HostToClient>())
outgoing_message_(std::make_unique<proto::HostToClient>()),
stat_counter_(id(), task_runner)
{
LOG(LS_INFO) << "Ctor";
}
Expand Down Expand Up @@ -194,21 +195,31 @@ void ClientSessionDesktop::onReceived(uint8_t /* channel_id */, const base::Byte
out_mouse_event.set_y(pos_y);

desktop_session_proxy_->injectMouseEvent(out_mouse_event);
stat_counter_.addMouseEvent();
}
else if (incoming_message_->has_key_event())
{
if (sessionType() == proto::SESSION_TYPE_DESKTOP_MANAGE)
{
desktop_session_proxy_->injectKeyEvent(incoming_message_->key_event());
stat_counter_.addKeyboardEvent();
}
}
else if (incoming_message_->has_text_event())
{
if (sessionType() == proto::SESSION_TYPE_DESKTOP_MANAGE)
{
desktop_session_proxy_->injectTextEvent(incoming_message_->text_event());
stat_counter_.addTextEvent();
}
}
else if (incoming_message_->has_clipboard_event())
{
if (sessionType() == proto::SESSION_TYPE_DESKTOP_MANAGE)
{
desktop_session_proxy_->injectClipboardEvent(incoming_message_->clipboard_event());
stat_counter_.addIncomingClipboardEvent();
}
}
else if (incoming_message_->has_extension())
{
Expand Down Expand Up @@ -332,7 +343,10 @@ void ClientSessionDesktop::encodeScreen(const base::Frame* frame, const base::Mo
}

if (outgoing_message_->has_video_packet() || outgoing_message_->has_cursor_shape())
{
sendMessage(proto::HOST_CHANNEL_ID_SESSION, base::serialize(*outgoing_message_));
stat_counter_.addVideoPacket();
}
}

//--------------------------------------------------------------------------------------------------
Expand All @@ -350,6 +364,7 @@ void ClientSessionDesktop::encodeAudio(const proto::AudioPacket& audio_packet)
return;

sendMessage(proto::HOST_CHANNEL_ID_SESSION, base::serialize(*outgoing_message_));
stat_counter_.addAudioPacket();
}

//--------------------------------------------------------------------------------------------------
Expand All @@ -366,6 +381,7 @@ void ClientSessionDesktop::setVideoErrorCode(proto::VideoErrorCode error_code)
outgoing_message_->Clear();
outgoing_message_->mutable_video_packet()->set_error_code(error_code);
sendMessage(proto::HOST_CHANNEL_ID_SESSION, base::serialize(*outgoing_message_));
stat_counter_.addVideoError();
}

//--------------------------------------------------------------------------------------------------
Expand All @@ -386,6 +402,7 @@ void ClientSessionDesktop::setCursorPosition(const proto::CursorPosition& cursor
position->set_y(pos_y);

sendMessage(proto::HOST_CHANNEL_ID_SESSION, base::serialize(*outgoing_message_));
stat_counter_.addCursorPosition();
}

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -418,6 +435,7 @@ void ClientSessionDesktop::injectClipboardEvent(const proto::ClipboardEvent& eve
outgoing_message_->Clear();
outgoing_message_->mutable_clipboard_event()->CopyFrom(event);
sendMessage(proto::HOST_CHANNEL_ID_SESSION, base::serialize(*outgoing_message_));
stat_counter_.addOutgoingClipboardEvent();
}
else
{
Expand Down
3 changes: 3 additions & 0 deletions source/host/client_session_desktop.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "base/waitable_timer.h"
#include "host/client_session.h"
#include "host/desktop_session.h"
#include "host/stat_counter.h"

#if defined(OS_WIN)
#include "host/task_manager.h"
Expand Down Expand Up @@ -121,6 +122,8 @@ class ClientSessionDesktop
std::unique_ptr<proto::ClientToHost> incoming_message_;
std::unique_ptr<proto::HostToClient> outgoing_message_;

StatCounter stat_counter_;

DISALLOW_COPY_AND_ASSIGN(ClientSessionDesktop);
};

Expand Down
106 changes: 106 additions & 0 deletions source/host/stat_counter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//
// Aspia Project
// Copyright (C) 2016-2023 Dmitry Chapyshev <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

#include "host/stat_counter.h"

#include "base/logging.h"

namespace host {

//--------------------------------------------------------------------------------------------------
StatCounter::StatCounter(uint32_t client_session_id, std::shared_ptr<base::TaskRunner> task_runner)
: client_session_id_(client_session_id),
timer_(base::WaitableTimer::Type::REPEATED, std::move(task_runner))
{
timer_.start(std::chrono::seconds(30), std::bind(&StatCounter::onTimeout, this));
}

//--------------------------------------------------------------------------------------------------
StatCounter::~StatCounter()
{
timer_.stop();
}

//--------------------------------------------------------------------------------------------------
void StatCounter::addVideoPacket()
{
++video_packets_;
}

//--------------------------------------------------------------------------------------------------
void StatCounter::addAudioPacket()
{
++audio_packets_;
}

//--------------------------------------------------------------------------------------------------
void StatCounter::addIncomingClipboardEvent()
{
++incoming_clipboard_events_;
}

//--------------------------------------------------------------------------------------------------
void StatCounter::addOutgoingClipboardEvent()
{
++outgoing_clipboard_events_;
}

//--------------------------------------------------------------------------------------------------
void StatCounter::addKeyboardEvent()
{
++keyboard_events_;
}

//--------------------------------------------------------------------------------------------------
void StatCounter::addTextEvent()
{
++text_events_;
}

//--------------------------------------------------------------------------------------------------
void StatCounter::addMouseEvent()
{
++mouse_events_;
}

//--------------------------------------------------------------------------------------------------
void StatCounter::addVideoError()
{
++video_error_count_;
}

//--------------------------------------------------------------------------------------------------
void StatCounter::addCursorPosition()
{
++cursor_positions_;
}

//--------------------------------------------------------------------------------------------------
void StatCounter::onTimeout()
{
LOG(LS_INFO) << "### Statistics (sid=" << client_session_id_ << "###";
LOG(LS_INFO) << "Packets: video=" << video_packets_ << " audio=" << audio_packets_
<< " video_error=" << video_error_count_;
LOG(LS_INFO) << "Clipboard: in=" << incoming_clipboard_events_
<< " out=" << outgoing_clipboard_events_;
LOG(LS_INFO) << "Input: keyboard=" << keyboard_events_ << " mouse=" << mouse_events_
<< " text=" << text_events_;
LOG(LS_INFO) << "Cursor positions: " << cursor_positions_;
}

} // namespace host
67 changes: 67 additions & 0 deletions source/host/stat_counter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// Aspia Project
// Copyright (C) 2016-2023 Dmitry Chapyshev <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

#ifndef HOST_STAT_COUNTER_H
#define HOST_STAT_COUNTER_H

#include "base/macros_magic.h"
#include "base/waitable_timer.h"

#include <cstdint>

namespace host {

class StatCounter
{
public:
StatCounter(uint32_t client_session_id, std::shared_ptr<base::TaskRunner> task_runner);
~StatCounter();

void addVideoPacket();
void addAudioPacket();
void addIncomingClipboardEvent();
void addOutgoingClipboardEvent();
void addKeyboardEvent();
void addTextEvent();
void addMouseEvent();
void addVideoError();
void addCursorPosition();

private:
void onTimeout();

const uint32_t client_session_id_;

base::WaitableTimer timer_;

uint64_t video_packets_ = 0;
uint64_t audio_packets_ = 0;
uint64_t incoming_clipboard_events_ = 0;
uint64_t outgoing_clipboard_events_ = 0;
uint64_t keyboard_events_ = 0;
uint64_t text_events_ = 0;
uint64_t mouse_events_ = 0;
uint64_t video_error_count_ = 0;
uint64_t cursor_positions_ = 0;

DISALLOW_COPY_AND_ASSIGN(StatCounter);
};

} // namespace host

#endif // HOST_STAT_COUNTER_H

0 comments on commit 16d3dc1

Please sign in to comment.