/
WiretapProfiler.cpp
93 lines (76 loc) · 2.23 KB
/
WiretapProfiler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <SFML/Network.hpp>
#include "WiretapProfiler.h"
#include "WiretapUtils.h"
using namespace Wiretap;
/*
** Profile point used to profile scopes
*/
ProfilePoint::ProfilePoint(char* name)
: m_Name(name)
, m_TimeStart(GetHiResTime())
{
Profiler::GetInstance().AddProfileEvent(m_Name, m_TimeStart, ProfileEvent_Start);
}
ProfilePoint::~ProfilePoint()
{
Profiler::GetInstance().AddProfileEvent(m_Name, GetHiResTime(), ProfileEvent_Stop);
}
/*
** Profiler recording events coming from profile points and dumping results
*/
Profiler* Profiler::ms_Instance = NULL;
Profiler::Profiler()
{
}
Profiler::~Profiler()
{
}
Profiler& Profiler::GetInstance()
{
if (!ms_Instance)
ms_Instance = new Profiler();
return *ms_Instance;
}
void Profiler::AddProfileEvent(const std::string& name, double time, EProfileEventType eventType)
{
m_Events.push_back(ProfileEvent(name, time, eventType));
}
// Sends this frame's events and clear them
void Profiler::SendData(const char* viewerIPAddress /* = "127.0.0.1" */, unsigned short viewerPort /* = 13001 */)
{
GetInstance()._SendData(viewerIPAddress, viewerPort);
}
void Profiler::_SendData(const char* viewerIPAddress /* = "127.0.0.1" */, unsigned short viewerPort /* = 13001 */)
{
static sf::TcpSocket socket;
static sf::Socket::Status socketStatus = sf::Socket::Error;
if (socketStatus == sf::Socket::Disconnected || socketStatus == sf::Socket::Error)
{
socket.disconnect();
socket.setBlocking(false);
socketStatus = socket.connect(viewerIPAddress, viewerPort);
socket.setBlocking(true);
}
if (socketStatus == sf::Socket::Done || socketStatus == sf::Socket::NotReady)
{
for (unsigned int i = 0; i < m_Events.size() && (socketStatus == sf::Socket::Done || socketStatus == sf::Socket::NotReady); i++)
{
const ProfileEvent& profileEvent = m_Events[i];
sf::Packet packet;
packet << profileEvent.GetTime() << profileEvent.GetName() << (unsigned int)profileEvent.GetType();
socketStatus = socket.send(packet);
}
m_Events.clear();
}
}
// Parses and prints the events (make sure to call this before SendData which clears them all)
void Profiler::DumpEvents()
{
GetInstance()._DumpEvents();
}
void Profiler::_DumpEvents()
{
::ParseEvents(m_Events);
::DumpEvents(m_Events);
}