sync.tin 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #nop vim: set filetype=tt:;
  2. /*
  3. 本文件属于 PaoTin++ 的一部分。
  4. PaoTin++ © 2020~2023 的所有版权均由担子炮(dzp <danzipao@gmail.com>) 享有并保留一切法律权利
  5. 你可以在遵照 GPLv3 协议的基础之上使用、修改及重新分发本程序。
  6. */
  7. ///=== {
  8. ///// sync 是一个服务器同步机制,可以确保服务器已经处理完了之前发送的命令队列。
  9. ///// 假设服务器是按照顺序处理客户端发送的命令,那么因为存在网络延迟,因此客户端
  10. ///// 可能已经发送了多个命令在网络上,但还没有到达服务器,或者服务器因为繁忙所以
  11. ///// 将其放入了命令缓冲队列尚未响应。
  12. /////
  13. ///// 但有些时候,客户端需要明确服务器是否已经处理了之前发送的命令。此时可以使用
  14. ///// 本模块。其基本原理是,通过发送别致同步符号,并请求服务器回显该符号,以等待
  15. ///// 服务器处理完之前发送的命令。如果收到了服务器的回显,则认为命令缓冲已空,并
  16. ///// 可以进行后续操作。
  17. ///// };
  18. #var lib_sync[META] {
  19. {NAME} {服务器同步}
  20. {DESC} {和服务器进行同步,本模块为北侠版}
  21. {AUTHOR} {担子炮}
  22. {NOTE} {}
  23. {CONFIG} {}
  24. };
  25. ///=== {
  26. // #@ sync.UUID
  27. // 用来生成一个唯一的同步符号。
  28. // };
  29. #func {sync.UUID} {
  30. #return {@uuid{}};
  31. };
  32. ///=== {
  33. // ## sync.Wait <回调代码> [<同步符号>]
  34. // 用来和服务器进行同步,参数说明如下:
  35. // 1. 回调代码
  36. // 回调代码会在与服务器同步之后执行。
  37. // 2. 同步符号(可选)
  38. // 同步符号参数用来对本次同步进行唯一标识,只能由大小写字母、数字、连字符、
  39. // 下划线、小数点、斜线构成。
  40. // 本参数为可选值,如果省略,则无法通过 sync.Ignore 取消回调
  41. // };
  42. #alias {sync.Wait} {
  43. #local callback {%21};
  44. #local uuid {%2};
  45. #if { {$callback} == {} } {
  46. xtt.Usage sync.Wait 回调代码不能为空;
  47. #return;
  48. };
  49. #if { {$uuid} == {%*{[^a-zA-Z0-9_./-]}%*} } {
  50. xtt.Usage sync.Wait 同步符号不能这么写;
  51. #return;
  52. };
  53. #if { "$uuid" == "" } {
  54. #format uuid {%U};
  55. };
  56. sync.handle {$uuid} {$callback};
  57. sync.send {$uuid};
  58. };
  59. ///=== {
  60. // #@ sync.Wait <回调代码>
  61. // 函数版的 sync.Wait 相比别名版的 sync.Wait,省略了同步符号参数要求,
  62. // 改由 sync 模块自行生成,并作为返回值返回给用户。
  63. // };
  64. #func {sync.Wait} {
  65. #local callback {%1};
  66. #if { {$callback} == {} } {
  67. xtt.Usage sync.Wait 回调代码不能为空;
  68. #return;
  69. };
  70. #local uuid {};
  71. #format uuid {%U};
  72. #line sub var sync.Wait {$callback} {$uuid};
  73. #return {$uuid};
  74. };
  75. ///=== {
  76. // ## sync.ClassWait <回调代码> [<同步符号>]
  77. // 类似于 sync.Wait,但会在 #class 消亡时自动忽略,不需要手动 sync.Ignore,提高易用性。
  78. // };
  79. #alias {sync.ClassWait} {
  80. #local callback {%21};
  81. #local uuid {%2};
  82. #if { {$callback} == {} } {
  83. xtt.Usage sync.ClassWait 回调代码不能为空;
  84. #return;
  85. };
  86. #if { {$uuid} == {%*{[^a-zA-Z0-9_./-]}%*} } {
  87. xtt.Usage sync.ClassWait 同步符号不能这么写;
  88. #return;
  89. };
  90. #if { "$uuid" == "" } {
  91. #format uuid {%U};
  92. };
  93. #info session save;
  94. #local class {$info[SESSION][CLASS]};
  95. #unvar info[SESSION];
  96. #if { "$class" != "" } {
  97. #line sub var ttevent.HandleOnce {CLASS DESTROYED $class} {event} {event} {sync.Ignore $uuid};
  98. };
  99. sync.handle {$uuid} {$callback};
  100. sync.send {$uuid};
  101. };
  102. ///=== {
  103. // #@ sync.ClassWait <回调代码>
  104. // 函数版的 sync.ClassWait 相比别名版的 sync.ClassWait,省略了同步符号参数要求,
  105. // 改由 sync 模块自行生成,并作为返回值返回给用户。
  106. // };
  107. #func {sync.ClassWait} {
  108. #local callback {%1};
  109. #if { {$callback} == {} } {
  110. xtt.Usage sync.ClassWait 回调代码不能为空;
  111. #return;
  112. };
  113. #local uuid {};
  114. #format uuid {%U};
  115. #line sub var sync.ClassWait {$callback} {$uuid};
  116. #return {$uuid};
  117. };
  118. VAR {同步符号对应的回调代码} gSyncHandlers {};
  119. #alias {sync.handle} {
  120. #local uuid {%1};
  121. #local callback {%2};
  122. #var {gSyncHandlers[$uuid]} {$callback};
  123. };
  124. ///=== {
  125. // ## sync.Ignore <同步符号>
  126. // 忽略指定的同步信息,将不会再触发回调代码。
  127. // 根据设计,大部分情况下你不需要手动 sync.Ignore,参见 sync.ClassWait。
  128. // };
  129. #alias {sync.Ignore} {
  130. #local uuid {%1};
  131. #unvar {gSyncHandlers[$uuid]};
  132. };
  133. #alias {sync.send} {
  134. errLog BUG: 待实现。;
  135. #return;
  136. };