sync.tin 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #local uuid {@sync.UUID{}};
  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 {@sync.UUID{}};
  71. #line sub var sync.Wait {$callback} {$uuid};
  72. #return {$uuid};
  73. };
  74. ///=== {
  75. // ## sync.ClassWait <回调代码> [<同步符号>]
  76. // 类似于 sync.Wait,但会在 #class 消亡时自动忽略,不需要手动 sync.Ignore,提高易用性。
  77. // };
  78. #alias {sync.ClassWait} {
  79. #local callback {%21};
  80. #local uuid {%2};
  81. #if { {$callback} == {} } {
  82. xtt.Usage sync.ClassWait 回调代码不能为空;
  83. #return;
  84. };
  85. #if { {$uuid} == {%*{[^a-zA-Z0-9_./-]}%*} } {
  86. xtt.Usage sync.ClassWait 同步符号不能这么写;
  87. #return;
  88. };
  89. #if { "$uuid" == "" } {
  90. #local uuid {@sync.UUID{}};
  91. };
  92. #info session save;
  93. #local class {$info[SESSION][CLASS]};
  94. #unvar info[SESSION];
  95. #if { "$class" != "" } {
  96. #line sub var ttevent.HandleOnce {CLASS DESTROYED $class} {event} {event} {sync.Ignore $uuid};
  97. };
  98. sync.handle {$uuid} {$callback};
  99. sync.send {$uuid};
  100. };
  101. ///=== {
  102. // #@ sync.ClassWait <回调代码>
  103. // 函数版的 sync.ClassWait 相比别名版的 sync.ClassWait,省略了同步符号参数要求,
  104. // 改由 sync 模块自行生成,并作为返回值返回给用户。
  105. // };
  106. #func {sync.ClassWait} {
  107. #local callback {%1};
  108. #if { {$callback} == {} } {
  109. xtt.Usage sync.ClassWait 回调代码不能为空;
  110. #return;
  111. };
  112. #local uuid {@sync.UUID{}};
  113. #line sub var sync.ClassWait {$callback} {$uuid};
  114. #return {$uuid};
  115. };
  116. VAR {同步符号对应的回调代码} gSyncHandlers {};
  117. #alias {sync.handle} {
  118. #local uuid {%1};
  119. #local callback {%2};
  120. #var {gSyncHandlers[$uuid]} {$callback};
  121. };
  122. ///=== {
  123. // ## sync.Ignore <同步符号>
  124. // 忽略指定的同步信息,将不会再触发回调代码。
  125. // 根据设计,大部分情况下你不需要手动 sync.Ignore,参见 sync.ClassWait。
  126. // };
  127. #alias {sync.Ignore} {
  128. #local uuid {%1};
  129. #unvar {gSyncHandlers[$uuid]};
  130. };
  131. #alias {sync.send} {
  132. errLog BUG: 待实现。;
  133. #return;
  134. };