My Project
NPLMiniRuntime.hpp
1 #pragma once
2 //-----------------------------------------------------------------------------
3 // Class: NPLMiniRuntime
4 // Authors: LiXizhi
5 // Company: ParaEngine Co.
6 // Date: 2010.4.8
7 // Desc: NPLMiniRuntime is used by any standalone exe/dll to act as a NPL runtime
8 // without the need to link with the full-featured ParaEngine/NPL library.
9 // The mini runtime provides basic implementation to send/receive NPL pure data messages.
10 // Therefore, it is easy to build loosely coupled systems using NPL for messaging with minimum code overhead and dependency.
11 // To distinguish it from the real NPL runtime, we have put it in the NPLInterface namespace
12 //
13 // See comment of CNPLMiniRuntimeT for example usage
14 //-----------------------------------------------------------------------------
15 
21 // #define USE_BOOST_SIGNAL_FILE_HANDLER
22 
23 #include <boost/intrusive_ptr.hpp>
24 #include <boost/shared_ptr.hpp>
25 #include "PEtypes.h"
26 #include "NPLTypes.h"
27 #include "INPL.h"
28 #include "INPLRuntime.h"
29 #include "NPLInterface.hpp"
30 
31 #include "util/Mutex.hpp"
32 #include "util/Semaphore.hpp"
33 
34 #include <vector>
35 #include <map>
36 #include <set>
37 #include <queue>
38 
39 #ifdef USE_BOOST_SIGNAL_FILE_HANDLER
40 #include <boost/signals2.hpp>
41 #else
42 #include <boost/function.hpp>
43 #endif
44 
45 namespace NPLInterface
46 {
47 #pragma region Headers
48 
50  {
51  NPLMiniMessage(){};
52  NPLMiniMessage(const char * sNPLFilename, const char* sCode, int nCodeLength=0)
53  : m_type(0)
54  {
55  if(sNPLFilename)
56  m_sFilename = sNPLFilename;
57  if(sCode)
58  {
59  if(nCodeLength <= 0)
60  m_sCode = sCode;
61  else
62  {
63  m_sCode.reserve(nCodeLength);
64  m_sCode = sCode;
65  }
66  }
67  }
69  std::string m_sFilename;
70 
72  std::string m_sCode;
73 
75  int m_type;
76  };
77 
83  template <class StringType = std::string>
84  struct NPLFileNameT
85  {
90  StringType sRuntimeStateName;
96  StringType sNID;
100  StringType sRelativePath;
105  StringType sDNSServerName;
106 
107  public:
108  NPLFileNameT(){}
109 
121  NPLFileNameT(const char * filename){
122  FromString(filename);
123  }
124 
129  void SetRelativePath(const char* sPath, int nCount=-1){
130  if(nCount <= 0)
131  {
132  sRelativePath = sPath;
133  nCount = (int)sRelativePath.size();
134  }
135  else
136  {
137  sRelativePath.assign(sPath, nCount);
138  }
139  for(int i=0; i<nCount ;i++)
140  {
141  if(sRelativePath[i] == '\\')
142  sRelativePath[i] = '/';
143  }
144  }
145 
146  public:
150  std::string ToString(){
151  string output;
152  ToString(output);
153  return output;
154  }
155  void ToString(std::string & output){
156  output.clear();
157  if(!sRuntimeStateName.empty())
158  {
159  output.append("(");
160  output.append(sRuntimeStateName);
161  output.append(")");
162  }
163 
164  // the activation type
165  if( ! sNID.empty() )
166  output.append(sNID);
167  if( ! sRelativePath.empty() )
168  output.append(sRelativePath);
169  if( ! sDNSServerName.empty() )
170  {
171  output.append("@");
172  output.append(sDNSServerName);
173  }
174  }
175 
179  void FromString(const char* sFilePath){
180  // for empty string, default to local Glia file.
181  if(sFilePath[0] == '\0')
182  {
183  // FromString("(gl)script/empty.lua");
184  sRuntimeStateName.clear();
185  sNID.clear();
186  sDNSServerName.clear();
187  sRelativePath.clear();
188  return;
189  }
190 
191  int i = 0;
192  int nNIDIndex = 0;
193  int nRelativePathIndex;
194  int nDNSServerIndex;
195 
196  bool bExplicitActivationType = false;
198  if(sFilePath[i] == '(')
199  {
200  bExplicitActivationType = true;
201  i++;
202  while( (sFilePath[i]!=')') && (sFilePath[i]!='\0'))
203  {
204  i++;
205  }
206  i++;
207  if( !(i==4 && (sFilePath[1]=='g') && (sFilePath[2]=='l')) )
208  sRuntimeStateName.assign(sFilePath+1, i-2);
209  else
210  sRuntimeStateName.clear();
211  nNIDIndex = i;
212  }
213 
215  while( (sFilePath[i]!=':') && (sFilePath[i]!='\0'))
216  i++;
217 
218  if(sFilePath[i]=='\0')
219  {
220  sNID.clear();
221  sRelativePath.assign(sFilePath+nNIDIndex, i-nNIDIndex);
222  sDNSServerName.clear();
223  return;
224  }
225  else
226  {
227  if(i>nNIDIndex)
228  sNID.assign(sFilePath+nNIDIndex, i-nNIDIndex);
229  else
230  sNID.clear();
231 
233  nRelativePathIndex = i+1;
234  i++;
235  }
236 
238  while( (sFilePath[i]!='@') && (sFilePath[i]!='\0') )
239  i++;
240  SetRelativePath(sFilePath+nRelativePathIndex, i - nRelativePathIndex);
241 
242  if(sFilePath[i]=='\0')
243  {
244  sDNSServerName.clear();
245  return;
246  }
247  else
248  i++;
249 
251  nDNSServerIndex = i;
252 
253  while( sFilePath[i]!='\0')
254  i++;
255  sDNSServerName.assign(sFilePath+nDNSServerIndex, i - nDNSServerIndex);
256  }
257  };
258  typedef NPLFileNameT<> NPLFileName;
259 
260 #pragma endregion Headers
261 
262 #pragma region CNPLMiniState
263 
274  public boost::enable_shared_from_this<CNPLMiniState>,
275  private boost::noncopyable
276  {
277  public:
278 #ifdef USE_BOOST_SIGNAL_FILE_HANDLER
279  typedef boost::signals2::signal< void(int, void*) > NPLFileActivateHandler_t;
280  typedef NPLFileActivateHandler_t::slot_type NPLFileActivateHandlerCallback_t;
281  //typedef boost::function< void(int, void*) > NPLFileActivateHandlerCallback_t;
282  typedef std::map<std::string, boost::shared_ptr<typename NPLFileActivateHandler_t> > NPLFileHandlerMap_t;
283 #else
284  typedef boost::function< void(int, void*) > NPLFileActivateHandler_t;
285  typedef boost::function< void(int, void*) > NPLFileActivateHandlerCallback_t;
286  typedef std::map<std::string, typename NPLFileActivateHandler_t> NPLFileHandlerMap_t;
287 #endif
288  typedef std::queue<NPLMiniMessage> NPLMiniMessageQueue_t;
289 
290 
291  CNPLMiniState(const char* name=NULL): m_current_msg_length(0), m_current_msg(NULL),m_pNPLRuntime(NULL), m_processed_msg_count(0) {
292  if(name != NULL){
293  m_name = name;
294  }
295  };
296  virtual ~CNPLMiniState() {};
297 
298  void Init(){}
299 
302  {
303  public:
304  CCurrentMessage(CNPLMiniState* pState, const char* msg, int nLength):m_pState(pState){
305  if(m_pState)
306  m_pState->SetCurrentMessage(msg,nLength);
307  }
308  ~CCurrentMessage(){
309  if(m_pState)
310  m_pState->SetCurrentMessage(NULL, 0);
311  }
312  CNPLMiniState* m_pState;
313  };
314 
315  public:
317  virtual const std::string& GetName() const {return m_name;};
318 
322  virtual int activate(const char * sNPLFilename, const char* sCode, int nCodeLength=0, int priority=2, int reliability=4){
323  // Note: if one wants to process in the calling thread, just inherit and do following.
324  // NPLInterface::NPLObjectProxy tabMsg = NPLInterface::NPLHelper::MsgStringToNPLTable(sCode, nCodeLength);
325  // OnMessageCallback(tabMsg);
326 
327  ParaEngine::Mutex::ScopedLock lock_(m_mutex);
328  m_input_queue.push(NPLMiniMessage(sNPLFilename, sCode, nCodeLength));
329  return NPL::NPL_OK;
330  }
331 
340  virtual NPL::NPLReturnCode Activate_async(const std::string & filepath, const char * code = NULL,int nLength=0, int priority=0) {
341  return (NPL::NPLReturnCode)activate(filepath.c_str(), code, nLength);
342  }
343 
345  virtual NPL::NPLReturnCode ActivateLocal(const char* filepath, const char * code = NULL,int nLength=0, int priority=0) {
346  return (NPL::NPLReturnCode)activate(filepath, code, nLength);
347  }
348 
355  virtual NPL::NPLReturnCode Activate_async(NPL::NPLMessage_ptr& msg, int priority=0) {return NPL::NPL_OK;}
356 
365  virtual NPL::NPLReturnCode SendMessage(NPL::NPLMessage_ptr& msg, int priority=0) {return NPL::NPL_OK;}
366 
368  virtual const char* GetCurrentMsg(){return m_current_msg;}
369 
371  virtual int GetCurrentMsgLength(){return m_current_msg_length;}
372 
374  virtual NPL::INPLRuntime* GetNPLRuntime() {return m_pNPLRuntime;}
375 
381  virtual void WriteLog(const char* text, int nTextLen=0, int nLogType = 0) {}
382 
384  //
385  // Timer functions
386  //
388 
400  virtual bool SetTimer(int nIDEvent, float fElapse, const char* sNeuronFile) {return false;};
401 
409  virtual bool KillTimer(int nIDEvent) { return false;};
410 
421  virtual bool ChangeTimer(int nIDEvent, int dueTime, int period) { return false;};
422 
427  virtual void RegisterFile(const char* sFilename, NPL::INPLActivationFile* pFileHandler = NULL) {};
428 
430  virtual void call(const char * sNPLFilename, const char* sCode, int nCodeLength = 0){};
431  public:
433  virtual int Process()
434  {
435  int nCount = 0;
436  // process as many as possible.
437  ParaEngine::Mutex::ScopedLock lock_(m_mutex);
438  while(!m_input_queue.empty())
439  {
440  ProcessMsg(m_input_queue.front());
441  m_input_queue.pop();
442  ++nCount;
443  }
444  return nCount;
445  }
446 
452  bool RegisterFileHandler(const char* filename, const NPLFileActivateHandlerCallback_t& fileCallback)
453  {
454  if(filename == 0)
455  return false;
456  std::string sFileName = filename;
457 
458 
459 #ifdef USE_BOOST_SIGNAL_FILE_HANDLER
460  NPLFileHandlerMap_t::iterator iter = m_file_handlers_map.find(sFileName);
461  if( iter == m_file_handlers_map.end() )
462  {
463  m_file_handlers_map[sFileName] = boost::shared_ptr<typename NPLFileActivateHandler_t>(new NPLFileActivateHandler_t());
464  }
465  m_file_handlers_map[sFileName]->connect(fileCallback);
466 #else
467  m_file_handlers_map[sFileName] = fileCallback;
468 #endif
469  return true;
470  }
471 
472  protected:
473  void SetCurrentMessage(const char* msg, int nLength)
474  {
475  m_current_msg = msg;
476  m_current_msg_length = nLength;
477  }
478 
480  int ProcessMsg( const NPLMiniMessage& msg )
481  {
482  ++ m_processed_msg_count;
483 
484  if(msg.m_type == 0)
485  {
486  NPLFileHandlerMap_t::iterator iter = m_file_handlers_map.find(msg.m_sFilename);
487  if( iter!= m_file_handlers_map.end() )
488  {
489  CCurrentMessage push_msg(this, msg.m_sCode.c_str(), (int)(msg.m_sCode.size()));
490 #ifdef USE_BOOST_SIGNAL_FILE_HANDLER
491  iter->second->operator ()(ParaEngine::PluginActType_STATE, this);
492 #else
493  iter->second(ParaEngine::PluginActType_STATE, this);
494 #endif
495  }
496  }
497  return 0;
498  }
499 
500  protected:
501 
503  const char* m_current_msg;
504 
507 
509  std::string m_name;
510 
511  ParaEngine::Mutex m_mutex;
512 
513  NPL::INPLRuntime * m_pNPLRuntime;
514 
516  NPLMiniMessageQueue_t m_input_queue;
517 
519  NPLFileHandlerMap_t m_file_handlers_map;
520 
523  };
524 
525 #pragma endregion CNPLMiniState
526 
527 #pragma region CNPLMiniRuntime
528 
548  template <class NPL_STATE = CNPLMiniState>
550  {
551  public:
553  typedef std::vector<NPLRuntimeState_ptr> NPLRuntime_Temp_Pool_Type;
554 
557  {
558  bool operator()( const NPLRuntimeState_ptr & a, const NPLRuntimeState_ptr & b ) const
559  { return a.get() < b.get(); }
560  };
561  typedef std::set <NPLRuntimeState_ptr, NPLRuntimeState_PtrOps> NPLRuntime_Pool_Type;
562 
564  Init();
565  }
566  ~CNPLMiniRuntimeT(){
567  Cleanup();
568  }
569  public:
570 
572  virtual void Init(){
573  if(m_runtime_state_main.get() == 0)
574  {
575  // SetCompressionKey(NULL, 0, 1);
576  // the default "main" runtime state
577  m_runtime_state_main = CreateRuntimeState("main", NPL::NPLRuntimeStateType_NPL);
578  }
579  };
580 
586  virtual void Run(bool bToEnd = true)
587  {
588  // the main runtime state is processed in the main game thread.
589  {
590  // in case the structure is modified by other threads or during processing, we will first dump to a temp queue and then process from the queue.
591  ParaEngine::Mutex::ScopedLock lock_(m_mutex);
592  NPLRuntime_Pool_Type::const_iterator iter, iter_end = m_runtime_states.end();
593  for(iter = m_runtime_states.begin(); iter!=iter_end; ++iter)
594  {
595  m_temp_rts_pool.push_back(*iter);
596  }
597  }
598 
599  NPLRuntime_Temp_Pool_Type::iterator itCur, itEnd = m_temp_rts_pool.end();
600  for (itCur = m_temp_rts_pool.begin(); itCur != itEnd; ++itCur)
601  {
602  (*itCur)->Process();
603  }
604  m_temp_rts_pool.clear();
605  };
606 
608  virtual void Cleanup(){
609  m_runtime_state_main.reset();
610  m_runtime_states.clear();
611  m_active_state_map.clear();
612  };
613 
621  virtual void SetHostMainStatesInFrameMove(bool bHostMainStatesInFrameMove){};
622 
630  return CreateRuntimeState(name, type_).get();
631  };
632 
637  virtual NPL::INPLRuntimeState* GetState(const char* name = NULL){
638  if( name == NULL)
639  return m_runtime_state_main.get();
640 
641  return GetRuntimeState(name).get();
642  };
643 
646  return CreateGetRuntimeState(name, type_).get();
647  };
648 
651  virtual bool DeleteState(NPL::INPLRuntimeState* pRuntime_state){
652  if(pRuntime_state)
653  {
654  return DeleteRuntimeState(((NPL_STATE*)pRuntime_state)->shared_from_this());
655  }
656  return false;
657  };
658 
661  return m_runtime_state_main.get();
662  };
663 
667  virtual bool AddToMainThread(NPL::INPLRuntimeState* runtime_state){return false;};
668 
673  virtual void SetUseCompression(bool bCompressIncoming, bool bCompressOutgoing){};
674 
687  virtual void SetCompressionKey(const byte* sKey=0, int nSize=0, int nUsePlainTextEncoding = 0){};
688 
696  virtual void SetCompressionLevel(int nLevel){};
697  virtual int GetCompressionLevel(){return 0;};
698 
703  virtual void SetCompressionThreshold(int nThreshold){};
704  virtual int GetCompressionThreshold(){return 0;};
705 
720  virtual void SetTCPKeepAlive(bool bEnable){};
721 
725  virtual bool IsTCPKeepAliveEnabled(){return false;};
726 
731  virtual void SetKeepAlive(bool bEnable){};
732  virtual bool IsKeepAliveEnabled(){return false;};
733 
740  virtual void EnableIdleTimeout(bool bEnable){};
741  virtual bool IsIdleTimeoutEnabled(){return false;};
742 
744  virtual void SetIdleTimeoutPeriod(int nMilliseconds){};
745  virtual int GetIdleTimeoutPeriod(){return false;};
746 
753  virtual void StartNetServer(const char* server=NULL, const char* port=NULL){};
754 
756  virtual void StopNetServer(){};
757 
766  virtual void AddPublicFile(const std::string& filename, int nID){};
767 
771  virtual void ClearPublicFiles(){};
772 
779  virtual void GetIP(const char* nid, char* pOutput){};
780 
781 
787  virtual void accept(const char* tid, const char* nid = NULL){};
788 
793  virtual void reject(const char* nid, int nReason = 0){};
794 
795 
797  //
798  // jabber client functions
799  //
801 
807  virtual ParaEngine::INPLJabberClient* GetJabberClient(const char* sJID){return NULL;};
812  virtual ParaEngine::INPLJabberClient* CreateJabberClient(const char* sJID){return NULL;};
813 
819  virtual bool CloseJabberClient(const char* sJID){return false;};
820 
822  //
823  // new libcUrl interface.
824  //
826 
831  virtual bool AppendURLRequest(ParaEngine::CURLRequestTask* pUrlTask, const char* sPoolName = NULL){return false;};
832 
837  virtual bool ChangeRequestPoolSize(const char* sPoolName, int nCount){return false;};
838 
839 
841  //
842  // Downloader functions
843  //
845 
851  virtual void AsyncDownload(const char* url, const char* destFolder, const char* callbackScript, const char* DownloaderName){};
852 
853 
858  virtual void CancelDownload(const char* DownloaderName){};
859 
865  virtual int Download(const char* url, const char* destFolder, const char* callbackScript, const char* DownloaderName){return 0;};
866 
876  virtual void NPL_AddDNSRecord(const char * sDNSName, const char* sAddress){};
877 
882  virtual void NPL_SetDefaultChannel(int channel_ID){};
887  virtual int NPL_GetDefaultChannel(){return 0;};
888 
907  virtual void NPL_SetChannelProperty(int channel_ID, int priority, int reliability){};
921  virtual void NPL_ResetChannelProperties(){};
922 
929  virtual void NPL_GetChannelProperty(int channel_ID, int* priority, int* reliability){};
930 
932  //
933  // Global activation functions
934  //
936 
944  virtual int Activate(NPL::INPLRuntimeState* pRuntimeState, const char * sNeuronFile, const char * code = NULL,int nLength=0, int channel=0, int priority=2, int reliability=3){
945  if(sNeuronFile == NULL)
946  {
947  return (int)NPL::NPL_FailedToLoadFile;
948  }
949 
950  NPLFileName FullName(sNeuronFile);
951 
952  // use Dispatcher to dispatch to a proper local runtime state or a remote one.
953  if(pRuntimeState == 0)
954  {
955  // default to main state.
956  return m_runtime_state_main->Activate_async(FullName.sRelativePath, code, nLength);
957  }
958  else
959  {
960  if(FullName.sNID.empty())
961  {
962  // local activation between local npl runtime state.
963  if(!FullName.sRuntimeStateName.empty())
964  {
965  NPLRuntimeState_ptr rts = GetRuntimeState(FullName.sRuntimeStateName);
966  if(rts.get() != 0)
967  {
968  return rts->Activate_async(FullName.sRelativePath, code, nLength);
969  }
970  else
971  {
972  return -1;
973  }
974  }
975  else
976  {
977  return pRuntimeState->Activate_async(FullName.sRelativePath, code, nLength);
978  }
979  }
980  else
981  {
982  return (int)NPL::NPL_Error;
983  }
984  }
985  }
986  public:
993  NPLRuntimeState_ptr CreateRuntimeState(const std::string& name, NPL::NPLRuntimeStateType type_=NPL::NPLRuntimeStateType_NPL)
994  {
995  NPLRuntimeState_ptr runtimestate = GetRuntimeState(name);
996  if(runtimestate.get() == 0)
997  {
998  runtimestate.reset(new NPL_STATE(name.c_str()));
999  runtimestate->Init();
1000  ParaEngine::Mutex::ScopedLock lock_(m_mutex);
1001  m_runtime_states.insert(runtimestate);
1002  // assert(m_runtime_states.find(runtimestate)!= m_runtime_states.end());
1003  if(!name.empty())
1004  m_active_state_map[name] = runtimestate;
1005  }
1006  return runtimestate;
1007  }
1008 
1013  NPLRuntimeState_ptr GetRuntimeState(const std::string& name)
1014  {
1015  if( name.empty() )
1016  return m_runtime_state_main;
1017 
1018  ParaEngine::Mutex::ScopedLock lock_(m_mutex);
1019  std::map<string, NPLRuntimeState_ptr>::iterator iter = m_active_state_map.find(name);
1020 
1021  if( iter != m_active_state_map.end())
1022  {
1023  return iter->second;
1024  }
1025  return NPLRuntimeState_ptr();
1026  }
1027 
1029  NPLRuntimeState_ptr CreateGetRuntimeState(const std::string& name, NPL::NPLRuntimeStateType type_=NPL::NPLRuntimeStateType_NPL)
1030  {
1031  NPLRuntimeState_ptr runtimestate = GetRuntimeState(name);
1032  if(runtimestate.get() == 0)
1033  {
1034  // create the state and run it in the main thread.
1035  runtimestate = CreateRuntimeState(name, type_);
1036  }
1037  return runtimestate;
1038  }
1039 
1042  bool DeleteRuntimeState(NPLRuntimeState_ptr runtime_state)
1043  {
1044  if(runtime_state.get() == 0)
1045  return true;
1046  ParaEngine::Mutex::ScopedLock lock_(m_mutex);
1047  NPLRuntime_Pool_Type::iterator iter = m_runtime_states.find(runtime_state);
1048  if(iter != m_runtime_states.end())
1049  {
1050  m_runtime_states.erase(iter);
1051  return true;
1052  }
1053  if( ! runtime_state->GetName().empty() )
1054  {
1055  m_active_state_map.erase(runtime_state->GetName());
1056  }
1057  return false;
1058  }
1059 
1061  NPLRuntimeState_ptr GetMainRuntimeState(){
1062  return m_runtime_state_main;
1063  }
1064 
1065  protected:
1067  NPLRuntimeState_ptr m_runtime_state_main;
1068 
1070  NPLRuntime_Pool_Type m_runtime_states;
1071 
1073  NPLRuntime_Temp_Pool_Type m_temp_rts_pool;
1074 
1076  std::map<std::string, NPLRuntimeState_ptr> m_active_state_map;
1077 
1078  ParaEngine::Mutex m_mutex;
1079  ParaEngine::Semaphore m_semaphore;
1080  };
1081 #pragma endregion CNPLMiniRuntime
1082 }
virtual void NPL_GetChannelProperty(int channel_ID, int *priority, int *reliability)
see also NPL_SetChannelProperty
Definition: NPLMiniRuntime.hpp:929
virtual const std::string & GetName() const
return the name of this runtime state.
Definition: NPLMiniRuntime.hpp:317
virtual void AddPublicFile(const std::string &filename, int nID)
add a nID, filename pair to the public file list.
Definition: NPLMiniRuntime.hpp:766
NPLRuntimeState_ptr GetMainRuntimeState()
get the default (main) runtime state.
Definition: NPLMiniRuntime.hpp:1061
virtual void WriteLog(const char *text, int nTextLen=0, int nLogType=0)
write a log message
Definition: NPLMiniRuntime.hpp:381
virtual bool SetTimer(int nIDEvent, float fElapse, const char *sNeuronFile)
creates a timer with the specified time-out value [thread safe]
Definition: NPLMiniRuntime.hpp:400
int m_type
message type
Definition: NPLMiniRuntime.hpp:75
virtual void NPL_ResetChannelProperties()
reset all 16 predefined channel properties.
Definition: NPLMiniRuntime.hpp:921
virtual void StartNetServer(const char *server=NULL, const char *port=NULL)
start the NPL net server&#39;s io_service loop.
Definition: NPLMiniRuntime.hpp:753
virtual void accept(const char *tid, const char *nid=NULL)
accept a given connection.
Definition: NPLMiniRuntime.hpp:787
virtual NPL::INPLRuntimeState * CreateGetState(const char *name, NPL::NPLRuntimeStateType type_=NPL::NPLRuntimeStateType_NPL)
it get runtime state first, if none exist, it will create one and add it to the main threaded state ...
Definition: NPLMiniRuntime.hpp:645
virtual void StopNetServer()
stop the net server
Definition: NPLMiniRuntime.hpp:756
virtual NPL::INPLRuntimeState * CreateState(const char *name, NPL::NPLRuntimeStateType type_=NPL::NPLRuntimeStateType_NPL)
create a new runtime state.
Definition: NPLMiniRuntime.hpp:629
INPLRuntimeState interface for DLL interface.
Definition: INPLRuntimeState.h:27
virtual NPL::INPLRuntimeState * GetState(const char *name=NULL)
get a runtime state with an explicit name.
Definition: NPLMiniRuntime.hpp:637
bool DeleteRuntimeState(NPLRuntimeState_ptr runtime_state)
create a given runtime state.
Definition: NPLMiniRuntime.hpp:1042
NPLFileNameT(const char *filename)
create the NPL file name object from a string.
Definition: NPLMiniRuntime.hpp:121
virtual bool IsTCPKeepAliveEnabled()
whether SO_KEEPALIVE is enabled.
Definition: NPLMiniRuntime.hpp:725
StringType sRuntimeStateName
the runtime state name that the file belongs to.
Definition: NPLMiniRuntime.hpp:90
NPLRuntimeState_ptr GetRuntimeState(const std::string &name)
get a runtime state with an explicit name.
Definition: NPLMiniRuntime.hpp:1013
virtual void SetKeepAlive(bool bEnable)
enable application level keep alive.
Definition: NPLMiniRuntime.hpp:731
simple scoped lock function
Definition: Mutex.hpp:14
Definition: NPLInterface.hpp:43
URL request can be a HTTP get/post request etc.
Definition: NPLNetClient.h:45
std::string m_sCode
the pure data code.
Definition: NPLMiniRuntime.hpp:72
construct this to ensure matching calls to SetCurrentMessage().
Definition: NPLMiniRuntime.hpp:301
virtual void Run(bool bToEnd=true)
call this function regularly in the main game thread to process packages.
Definition: NPLMiniRuntime.hpp:586
StringType sRelativePath
the relative file path of the NPL file in the Runtime&#39;s file system.
Definition: NPLMiniRuntime.hpp:100
virtual void SetIdleTimeoutPeriod(int nMilliseconds)
how many milliseconds of inactivity to assume this connection should be timed out.
Definition: NPLMiniRuntime.hpp:744
NPLRuntime_Pool_Type m_runtime_states
all NPL runtime states in the NPL runtime
Definition: NPLMiniRuntime.hpp:1070
virtual bool DeleteState(NPL::INPLRuntimeState *pRuntime_state)
create a given runtime state.
Definition: NPLMiniRuntime.hpp:651
virtual int Activate(NPL::INPLRuntimeState *pRuntimeState, const char *sNeuronFile, const char *code=NULL, int nLength=0, int channel=0, int priority=2, int reliability=3)
activate the specified file.
Definition: NPLMiniRuntime.hpp:944
Definition: class.hpp:124
virtual NPL::NPLReturnCode SendMessage(NPL::NPLMessage_ptr &msg, int priority=0)
send a message to the current message queue.
Definition: NPLMiniRuntime.hpp:365
virtual void EnableIdleTimeout(bool bEnable)
Enable idle timeout.
Definition: NPLMiniRuntime.hpp:740
virtual int NPL_GetDefaultChannel()
Get the default channel ID, default value is 0.
Definition: NPLMiniRuntime.hpp:887
NPLFileHandlerMap_t m_file_handlers_map
file handlers map.
Definition: NPLMiniRuntime.hpp:519
virtual int GetCurrentMsgLength()
get length of the current message
Definition: NPLMiniRuntime.hpp:371
NPLRuntimeState_ptr CreateRuntimeState(const std::string &name, NPL::NPLRuntimeStateType type_=NPL::NPLRuntimeStateType_NPL)
create a new runtime state.
Definition: NPLMiniRuntime.hpp:993
virtual void ClearPublicFiles()
clear all public files, so that the NPL server will be completely private.
Definition: NPLMiniRuntime.hpp:771
virtual void RegisterFile(const char *sFilename, NPL::INPLActivationFile *pFileHandler=NULL)
function to register the a file handler in the current NPL state, so that it is callable from NPL scr...
Definition: NPLMiniRuntime.hpp:427
the default NPL runtime state, with all NPL and ParaEngine functions loaded. it will consume about 1M...
Definition: INPLRuntimeState.h:15
NPL interface of a Jabber-XMPP client proxy.
Definition: IParaWebService.h:108
virtual void GetIP(const char *nid, char *pOutput)
get the ip address of given NPL connection.
Definition: NPLMiniRuntime.hpp:779
virtual bool AddToMainThread(NPL::INPLRuntimeState *runtime_state)
add a given runtime state to the main game thread.
Definition: NPLMiniRuntime.hpp:667
virtual void NPL_SetDefaultChannel(int channel_ID)
Set the default channel ID, default value is 0.
Definition: NPLMiniRuntime.hpp:882
virtual void NPL_AddDNSRecord(const char *sDNSName, const char *sAddress)
add a DNS server record to the current NPL runtime.
Definition: NPLMiniRuntime.hpp:876
a globally unique name of a NPL file name instance.
Definition: NPLMiniRuntime.hpp:84
cross platform condition variable.
Definition: Semaphore.hpp:21
virtual void Cleanup()
clean up the NPL runtime environment
Definition: NPLMiniRuntime.hpp:608
virtual void Init()
initialize NPL runtime environment
Definition: NPLMiniRuntime.hpp:572
virtual bool ChangeTimer(int nIDEvent, int dueTime, int period)
Changes the start time and the interval between method invocations for a timer, using 32-bit signed i...
Definition: NPLMiniRuntime.hpp:421
virtual int Process()
process all queued message.
Definition: NPLMiniRuntime.hpp:433
virtual bool ChangeRequestPoolSize(const char *sPoolName, int nCount)
There is generally no limit to the number of requests sent.
Definition: NPLMiniRuntime.hpp:837
from the NPL activate function call. the second paramter to LibActivate() will be pointer of INPLRunt...
Definition: PEtypes.h:230
StringType sDNSServerName
the DNS file name.
Definition: NPLMiniRuntime.hpp:105
cross platform mutex
Definition: Mutex.hpp:88
virtual void AsyncDownload(const char *url, const char *destFolder, const char *callbackScript, const char *DownloaderName)
Asynchronously download a file from the url.
Definition: NPLMiniRuntime.hpp:851
virtual ParaEngine::INPLJabberClient * CreateJabberClient(const char *sJID)
Create a new jabber client instance with the given jabber client ID.
Definition: NPLMiniRuntime.hpp:812
void FromString(const char *sFilePath)
reset from string
Definition: NPLMiniRuntime.hpp:179
compare functions for runtime state ptr
Definition: NPLMiniRuntime.hpp:556
virtual void SetCompressionLevel(int nLevel)
Set the zlib compression level to use in case compression is enabled.
Definition: NPLMiniRuntime.hpp:696
virtual void reject(const char *nid, int nReason=0)
reject and close a given connection.
Definition: NPLMiniRuntime.hpp:793
virtual NPL::INPLRuntime * GetNPLRuntime()
get the NPL runtime environment
Definition: NPLMiniRuntime.hpp:374
virtual ParaEngine::INPLJabberClient * GetJabberClient(const char *sJID)
get an existing jabber client instance interface by its JID.
Definition: NPLMiniRuntime.hpp:807
virtual void CancelDownload(const char *DownloaderName)
cancel all asynchronous downloads that matches a certain downloader name pattern
Definition: NPLMiniRuntime.hpp:858
virtual int Download(const char *url, const char *destFolder, const char *callbackScript, const char *DownloaderName)
Synchronous call of the function AsyncDownload().
Definition: NPLMiniRuntime.hpp:865
Definition: inftrees.h:24
virtual void SetTCPKeepAlive(bool bEnable)
System level Enable/disable SO_KEEPALIVE.
Definition: NPLMiniRuntime.hpp:720
void SetRelativePath(const char *sPath, int nCount=-1)
set the relaive_Path
Definition: NPLMiniRuntime.hpp:129
virtual NPLReturnCode Activate_async(const std::string &filepath, const char *code=NULL, int nLength=0, int priority=0)=0
activate the specified file in this runtime state.
const char * m_current_msg
pointer to the current message.
Definition: NPLMiniRuntime.hpp:503
StringType sNID
the NPL runtime server ID that the file belongs to.
Definition: NPLMiniRuntime.hpp:96
virtual bool AppendURLRequest(ParaEngine::CURLRequestTask *pUrlTask, const char *sPoolName=NULL)
Append URL request to a pool.
Definition: NPLMiniRuntime.hpp:831
virtual NPL::NPLReturnCode ActivateLocal(const char *filepath, const char *code=NULL, int nLength=0, int priority=0)
same as Activate_async, except that it is a short cut name.
Definition: NPLMiniRuntime.hpp:345
std::string m_sFilename
the target filename.
Definition: NPLMiniRuntime.hpp:69
Definition: INPLAcitvationFile.h:18
virtual void NPL_SetChannelProperty(int channel_ID, int priority, int reliability)
Messages can be sent via predefined channels.
Definition: NPLMiniRuntime.hpp:907
NPLRuntimeState_ptr CreateGetRuntimeState(const std::string &name, NPL::NPLRuntimeStateType type_=NPL::NPLRuntimeStateType_NPL)
it get runtime state first, if none exist, it will create one and add it to the main threaded state ...
Definition: NPLMiniRuntime.hpp:1029
virtual const char * GetCurrentMsg()
get a pointer to the current message
Definition: NPLMiniRuntime.hpp:368
virtual bool KillTimer(int nIDEvent)
Destroys the specified timer [thread safe].
Definition: NPLMiniRuntime.hpp:409
int ProcessMsg(const NPLMiniMessage &msg)
process a single message.
Definition: NPLMiniRuntime.hpp:480
NPLRuntime_Temp_Pool_Type m_temp_rts_pool
temporary run time states queue
Definition: NPLMiniRuntime.hpp:1073
NPLMiniMessageQueue_t m_input_queue
the input message queue
Definition: NPLMiniRuntime.hpp:516
std::string ToString()
convert to string of the following format [(sRuntimeStateName|gl)][sNID:]sRelativePath[] ...
Definition: NPLMiniRuntime.hpp:150
virtual void SetHostMainStatesInFrameMove(bool bHostMainStatesInFrameMove)
whether we will process messages in the main threads in the frame move function.
Definition: NPLMiniRuntime.hpp:621
NPLRuntimeState_ptr m_runtime_state_main
the default (main) NPL runtime state.
Definition: NPLMiniRuntime.hpp:1067
virtual NPL::NPLReturnCode Activate_async(const std::string &filepath, const char *code=NULL, int nLength=0, int priority=0)
activate the specified file in this runtime state.
Definition: NPLMiniRuntime.hpp:340
virtual void SetCompressionKey(const byte *sKey=0, int nSize=0, int nUsePlainTextEncoding=0)
set the compression method of incoming the outgoing messages.
Definition: NPLMiniRuntime.hpp:687
virtual void call(const char *sNPLFilename, const char *sCode, int nCodeLength=0)
synchronous function call
Definition: NPLMiniRuntime.hpp:430
virtual void SetUseCompression(bool bCompressIncoming, bool bCompressOutgoing)
whether to use compression on transport layer for incoming and outgoing connections ...
Definition: NPLMiniRuntime.hpp:673
NPL Runtime Environment interface.
Definition: INPLRuntime.h:30
bool RegisterFileHandler(const char *filename, const NPLFileActivateHandlerCallback_t &fileCallback)
if USE_BOOST_SIGNAL_FILE_HANDLER is defined, we will allow multiple file handlers to subscribe to the...
Definition: NPLMiniRuntime.hpp:452
int m_current_msg_length
length of the current message.
Definition: NPLMiniRuntime.hpp:506
std::string m_name
the name of this runtime state.
Definition: NPLMiniRuntime.hpp:509
virtual int activate(const char *sNPLFilename, const char *sCode, int nCodeLength=0, int priority=2, int reliability=4)
activate the specified file.
Definition: NPLMiniRuntime.hpp:322
virtual NPL::INPLRuntimeState * GetMainState()
get the default (main) runtime state.
Definition: NPLMiniRuntime.hpp:660
int m_processed_msg_count
for stats
Definition: NPLMiniRuntime.hpp:522
virtual bool CloseJabberClient(const char *sJID)
close a given jabber client instance.
Definition: NPLMiniRuntime.hpp:819
For full featured NPL runtime, one need to use NPL::CNPLRuntime.
Definition: NPLMiniRuntime.hpp:549
One can usually implement one or more methods to make it a standalone runtime state.
Definition: NPLMiniRuntime.hpp:273
NPL mini message is used by mini runtime internally.
Definition: NPLMiniRuntime.hpp:49
NPLRuntimeStateType
The types of CNPLRuntimeState.
Definition: INPLRuntimeState.h:12
virtual void SetCompressionThreshold(int nThreshold)
set the default compression threshold for all connections on this machine.
Definition: NPLMiniRuntime.hpp:703
virtual NPL::NPLReturnCode Activate_async(NPL::NPLMessage_ptr &msg, int priority=0)
same as Activate_async.
Definition: NPLMiniRuntime.hpp:355
std::map< std::string, NPLRuntimeState_ptr > m_active_state_map
a mapping from the runtime state name to runtime state instance pointer
Definition: NPLMiniRuntime.hpp:1076