OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4InteractiveThread Class Reference

#include <C4InteractiveThread.h>

Inheritance diagram for C4InteractiveThread:
[legend]

Classes

class  Callback
 

Public Member Functions

 C4InteractiveThread ()
 
 ~C4InteractiveThread ()
 
bool AddProc (StdSchedulerProc *pProc)
 
void RemoveProc (StdSchedulerProc *pProc)
 
bool PushEvent (C4InteractiveEventType eEventType, void *pData=nullptr)
 
void ProcessEvents ()
 
bool ThreadLog (const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
 
bool ThreadLogFatal (const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
 
bool ThreadLogS (const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
 
bool ThreadLogDebug (const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
 
template<typename RType = void, typename Functor >
bool ThreadPostAsync (Functor function)
 
void SetCallback (C4InteractiveEventType eEvent, Callback *pnNetworkCallback)
 
void ClearCallback (C4InteractiveEventType eEvent, Callback *pnNetworkCallback)
 

Detailed Description

Definition at line 59 of file C4InteractiveThread.h.

Constructor & Destructor Documentation

C4InteractiveThread::C4InteractiveThread ( )

Definition at line 23 of file C4InteractiveThread.cpp.

References StdScheduler::Add(), Application, Ev_None, C4InteractiveThreadNotifyProc::SetNotify(), and ZeroMem().

24 {
25  // Add head-item
26  pFirstEvent = pLastEvent = new Event();
27  pFirstEvent->Type = Ev_None;
28  pFirstEvent->Next = nullptr;
29  // reset event handlers
30  ZeroMem(&pCallbacks, sizeof(pCallbacks));
31  // Set notify proc
32  NotifyProc.SetNotify(this);
33  Application.Add(&NotifyProc);
34 }
void Add(StdSchedulerProc *pProc)
std::enable_if< std::is_pod< T >::value >::type ZeroMem(T *lpMem, size_t dwSize)
Definition: Standard.h:60
void SetNotify(class C4InteractiveThread *pnNotify)
C4Application Application
Definition: C4Globals.cpp:44

Here is the call graph for this function:

C4InteractiveThread::~C4InteractiveThread ( )

Definition at line 36 of file C4InteractiveThread.cpp.

References Application, and StdScheduler::Remove().

37 {
38  CStdLock PushLock(&EventPushCSec), PopLock(&EventPopCSec);
39  // Remove all items. This may leak data, if pData was allocated on the heap.
40  while (PopEvent(nullptr, nullptr)) {}
41  // Delete head-item
42  delete pFirstEvent;
43  pFirstEvent = pLastEvent = nullptr;
44  // Unregister notify
45  Application.Remove(&NotifyProc);
46 }
void Remove(StdSchedulerProc *pProc)
C4Application Application
Definition: C4Globals.cpp:44

Here is the call graph for this function:

Member Function Documentation

bool C4InteractiveThread::AddProc ( StdSchedulerProc pProc)

Definition at line 48 of file C4InteractiveThread.cpp.

References StdSchedulerThread::Add(), StdScheduler::getProcCnt(), and StdSchedulerThread::Start().

Referenced by C4StartupNetDlg::CheckVersionUpdate(), C4Network2IO::Init(), C4ChatControl::OnConnectBtn(), and C4StartupNetListEntry::SetRefQuery().

49 {
50  bool fFirst = !Scheduler.getProcCnt();
51  // Add the proc
52  Scheduler.Add(pProc);
53  // Not started yet?
54  if (fFirst)
55  if (!Scheduler.Start())
56  return false;
57  return true;
58 }
int getProcCnt() const
Definition: StdScheduler.h:245
void Add(StdSchedulerProc *pProc)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4InteractiveThread::ClearCallback ( C4InteractiveEventType  eEvent,
Callback pnNetworkCallback 
)
inline

Definition at line 122 of file C4InteractiveThread.h.

Referenced by C4Network2IO::Clear(), C4Network2IO::Init(), C4ChatControl::~C4ChatControl(), and C4StartupNetDlg::~C4StartupNetDlg().

123  { if (pCallbacks[eEvent] == pnNetworkCallback) pCallbacks[eEvent] = nullptr; }

Here is the caller graph for this function:

void C4InteractiveThread::ProcessEvents ( )

Definition at line 121 of file C4InteractiveThread.cpp.

References DebugLog(), Ev_Function, Ev_Last, Ev_Log, Ev_LogDebug, Ev_LogFatal, Ev_LogSilent, Ev_None, StdStrBuf::getData(), Log(), LogFatal(), LogSilent(), C4InteractiveThread::Callback::OnThreadEvent(), and StdStrBuf::Take().

Referenced by C4Network2IO::Clear(), C4InteractiveThreadNotifyProc::Execute(), and C4Network2UPnPP::~C4Network2UPnPP().

122 {
123  C4InteractiveEventType eEventType; void *pEventData;
124  while (PopEvent(&eEventType, &pEventData))
125  switch (eEventType)
126  {
127  // Logging
128  case Ev_Log: case Ev_LogSilent: case Ev_LogFatal: case Ev_LogDebug:
129  {
130  // Reconstruct the StdStrBuf which allocated the data.
131  StdStrBuf pLog;
132  pLog.Take(reinterpret_cast<char *>(pEventData));
133  switch (eEventType)
134  {
135  case Ev_Log:
136  Log(pLog.getData()); break;
137  case Ev_LogSilent:
138  LogSilent(pLog.getData()); break;
139  case Ev_LogFatal:
140  LogFatal(pLog.getData()); break;
141  case Ev_LogDebug:
142  DebugLog(pLog.getData()); break;
143  default: assert(eEventType == Ev_Log || eEventType == Ev_LogSilent || eEventType == Ev_LogFatal || eEventType == Ev_LogDebug); // obviously will not happen, but someone tell gcc
144  }
145 
146  }
147  break;
148 
149  case Ev_Function:
150  {
151  std::unique_ptr<std::function<void ()> > func(static_cast<std::function<void()>*>(pEventData));
152  (*func)();
153  }
154 
155  // Other events: check for a registered handler
156  default:
157  if (eEventType >= Ev_None && eEventType <= Ev_Last)
158  if (pCallbacks[eEventType])
159  pCallbacks[eEventType]->OnThreadEvent(eEventType, pEventData);
160  // Note that memory might leak if the event wasn't processed....
161  }
162 }
const char * getData() const
Definition: StdBuf.h:442
bool LogSilent(const char *szMessage, bool fConsole)
Definition: C4Log.cpp:114
virtual void OnThreadEvent(C4InteractiveEventType eEvent, void *pEventData)=0
bool DebugLog(const char *strMessage)
Definition: C4Log.cpp:270
void Take(char *pnData)
Definition: StdBuf.h:457
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:227
C4InteractiveEventType
bool Log(const char *szMessage)
Definition: C4Log.cpp:192

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4InteractiveThread::PushEvent ( C4InteractiveEventType  eEventType,
void *  pData = nullptr 
)

Definition at line 72 of file C4InteractiveThread.cpp.

References CStdLock::Clear(), CStdNotifyProc::Notify(), and C4TimeMilliseconds::Now().

Referenced by C4Network2IO::HandlePacket(), C4Network2HTTPClient::OnDisconn(), C4Network2IO::OnDisconn(), C4Network2HTTPClient::OnPacket(), ThreadLog(), ThreadLogDebug(), ThreadLogFatal(), ThreadLogS(), and ThreadPostAsync().

73 {
74  CStdLock PushLock(&EventPushCSec);
75  if (!pLastEvent) return false;
76  // create event
77  Event *pEvent = new Event;
78  pEvent->Type = eEvent;
79  pEvent->Data = pData;
80 #ifdef _DEBUG
81  pEvent->Time = C4TimeMilliseconds::Now();
82 #endif
83  pEvent->Next = nullptr;
84  // add item (at end)
85  pLastEvent->Next = pEvent;
86  pLastEvent = pEvent;
87  PushLock.Clear();
88  // notify main thread
89  NotifyProc.Notify();
90  return true;
91 }
static C4TimeMilliseconds Now()

Here is the call graph for this function:

Here is the caller graph for this function:

void C4InteractiveThread::RemoveProc ( StdSchedulerProc pProc)

Definition at line 60 of file C4InteractiveThread.cpp.

References StdScheduler::getProcCnt(), StdScheduler::hasProc(), StdSchedulerThread::Remove(), and StdSchedulerThread::Stop().

Referenced by C4Network2IO::Clear(), C4StartupNetListEntry::ClearRef(), and C4StartupNetDlg::~C4StartupNetDlg().

61 {
62  // Process not in list?
63  if (!Scheduler.hasProc(pProc))
64  return;
65  // Last proc to be removed?
66  if (Scheduler.getProcCnt() == 1)
67  Scheduler.Stop();
68  // Remove
69  Scheduler.Remove(pProc);
70 }
bool hasProc(StdSchedulerProc *pProc)
Definition: StdScheduler.h:246
int getProcCnt() const
Definition: StdScheduler.h:245
void Remove(StdSchedulerProc *pProc)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4InteractiveThread::SetCallback ( C4InteractiveEventType  eEvent,
Callback pnNetworkCallback 
)
inline

Definition at line 120 of file C4InteractiveThread.h.

Referenced by C4ChatControl::C4ChatControl(), C4StartupNetDlg::C4StartupNetDlg(), and C4Network2IO::Init().

121  { pCallbacks[eEvent] = pnNetworkCallback; }

Here is the caller graph for this function:

bool C4InteractiveThread::ThreadLog ( const char *  szMessage,
  ... 
)

Definition at line 165 of file C4InteractiveThread.cpp.

References Ev_Log, FormatStringV(), StdStrBuf::GrabPointer(), and PushEvent().

Referenced by C4Network2ResList::AddLoad(), C4GameControlNetwork::DoInput(), C4Network2IO::HandlePacket(), C4Network2IO::OnError(), and C4Network2IO::OnPacket().

166 {
167  // format message
168  va_list lst; va_start(lst, szMessage);
169  StdStrBuf Msg = FormatStringV(szMessage, lst);
170  // send to main thread
171  return PushEvent(Ev_Log, Msg.GrabPointer());
172 }
char * GrabPointer()
Definition: StdBuf.h:459
bool PushEvent(C4InteractiveEventType eEventType, void *pData=nullptr)
StdStrBuf FormatStringV(const char *szFmt, va_list args)
Definition: StdBuf.cpp:276

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4InteractiveThread::ThreadLogDebug ( const char *  szMessage,
  ... 
)

Definition at line 192 of file C4InteractiveThread.cpp.

References Ev_LogDebug, FormatStringV(), StdStrBuf::GrabPointer(), and PushEvent().

193 {
194  // format message
195  va_list lst; va_start(lst, szMessage);
196  StdStrBuf Msg = FormatStringV(szMessage, lst);
197  // send to main thread
198  return PushEvent(Ev_LogDebug, Msg.GrabPointer());
199 }
char * GrabPointer()
Definition: StdBuf.h:459
bool PushEvent(C4InteractiveEventType eEventType, void *pData=nullptr)
StdStrBuf FormatStringV(const char *szFmt, va_list args)
Definition: StdBuf.cpp:276

Here is the call graph for this function:

bool C4InteractiveThread::ThreadLogFatal ( const char *  szMessage,
  ... 
)

Definition at line 174 of file C4InteractiveThread.cpp.

References Ev_LogFatal, FormatStringV(), StdStrBuf::GrabPointer(), and PushEvent().

175 {
176  // format message
177  va_list lst; va_start(lst, szMessage);
178  StdStrBuf Msg = FormatStringV(szMessage, lst);
179  // send to main thread
180  return PushEvent(Ev_LogFatal, Msg.GrabPointer());
181 }
char * GrabPointer()
Definition: StdBuf.h:459
bool PushEvent(C4InteractiveEventType eEventType, void *pData=nullptr)
StdStrBuf FormatStringV(const char *szFmt, va_list args)
Definition: StdBuf.cpp:276

Here is the call graph for this function:

bool C4InteractiveThread::ThreadLogS ( const char *  szMessage,
  ... 
)

Definition at line 183 of file C4InteractiveThread.cpp.

References Ev_LogSilent, FormatStringV(), StdStrBuf::GrabPointer(), and PushEvent().

Referenced by C4Network2ResList::AddByCore(), C4Network2ResList::AddLoad(), C4Network2ResChunk::AddTo(), C4GameControlNetwork::CheckCompleteCtrl(), C4Network2IO::CheckTimeout(), C4Network2Res::Derive(), C4Network2HTTPClient::Execute(), C4Network2IO::HandlePacket(), C4Network2Res::OnChunk(), C4Network2IO::OnConn(), C4Network2IO::OnDisconn(), C4Network2IO::OnPacket(), C4Network2::OnPuncherConnect(), C4Network2ResList::OnResComplete(), C4Network2Res::SetLoad(), and C4Network2Res::StartLoad().

184 {
185  // format message
186  va_list lst; va_start(lst, szMessage);
187  StdStrBuf Msg = FormatStringV(szMessage, lst);
188  // send to main thread
189  return PushEvent(Ev_LogSilent, Msg.GrabPointer());
190 }
char * GrabPointer()
Definition: StdBuf.h:459
bool PushEvent(C4InteractiveEventType eEventType, void *pData=nullptr)
StdStrBuf FormatStringV(const char *szFmt, va_list args)
Definition: StdBuf.cpp:276

Here is the call graph for this function:

Here is the caller graph for this function:

template<typename RType = void, typename Functor >
bool C4InteractiveThread::ThreadPostAsync ( Functor  function)
inline

Definition at line 114 of file C4InteractiveThread.h.

References Ev_Function, and PushEvent().

Referenced by C4Network2IRCClient::Join().

115  {
116  return PushEvent(Ev_Function, new std::function<RType()>(function));
117  }
bool PushEvent(C4InteractiveEventType eEventType, void *pData=nullptr)

Here is the call graph for this function:

Here is the caller graph for this function:


The documentation for this class was generated from the following files: