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 25 of file C4InteractiveThread.cpp.

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

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

Here is the call graph for this function:

C4InteractiveThread::~C4InteractiveThread ( )

Definition at line 38 of file C4InteractiveThread.cpp.

References Application, and StdScheduler::Remove().

39 {
40  CStdLock PushLock(&EventPushCSec), PopLock(&EventPopCSec);
41  // Remove all items. This may leak data, if pData was allocated on the heap.
42  while (PopEvent(nullptr, nullptr)) {}
43  // Delete head-item
44  delete pFirstEvent;
45  pFirstEvent = pLastEvent = nullptr;
46  // Unregister notify
47  Application.Remove(&NotifyProc);
48 }
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 50 of file C4InteractiveThread.cpp.

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

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

51 {
52  bool fFirst = !Scheduler.getProcCnt();
53  // Add the proc
54  Scheduler.Add(pProc);
55  // Not started yet?
56  if (fFirst)
57  if (!Scheduler.Start())
58  return false;
59  return true;
60 }
int getProcCnt() const
Definition: StdScheduler.h:249
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 123 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().

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

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 74 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().

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

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

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

63 {
64  // Process not in list?
65  if (!Scheduler.hasProc(pProc))
66  return;
67  // Last proc to be removed?
68  if (Scheduler.getProcCnt() == 1)
69  Scheduler.Stop();
70  // Remove
71  Scheduler.Remove(pProc);
72 }
bool hasProc(StdSchedulerProc *pProc)
Definition: StdScheduler.h:250
int getProcCnt() const
Definition: StdScheduler.h:249
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 167 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().

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

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 194 of file C4InteractiveThread.cpp.

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

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

Here is the call graph for this function:

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

Definition at line 176 of file C4InteractiveThread.cpp.

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

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

Here is the call graph for this function:

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

Definition at line 185 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().

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

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: