PageLinkIoi.xaml.vb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. Imports System.Net.Sockets
  2. Public Class PageLinkIoi
  3. Public Const RequestVersion As Integer = 4
  4. Public Const IoiVersion As Integer = 10 '由于已关闭更新渠道,在提升 IoiVersion 时必须提升 RequestVersion
  5. Public Shared PathIoi As String = PathAppdata & "联机模块\IOI 联机模块.exe"
  6. #Region "进程管理"
  7. Private Shared IoiId As String, IoiPassword As String
  8. Private Shared IoiProcess As Process = Nothing
  9. Private Shared IoiState As LoadState = LoadState.Waiting
  10. ''' <summary>
  11. ''' 若 Ioi 正在运行,则结束 Ioi 进程,同时初始化状态数据。返回是否关闭了对应进程。
  12. ''' </summary>
  13. Public Shared Function IoiStop(SleepWhenKilled As Boolean) As Boolean
  14. Return False
  15. End Function
  16. ''' <summary>
  17. ''' 启动 Ioi,并等待初始化完成后退出运行,同时更新 IoiId 与 IoiPassword。
  18. ''' 正常初始化返回 True,需要更新返回 False,其余情况抛出异常。
  19. ''' 若 Ioi 正在运行,则会先停止其运行。
  20. ''' </summary>
  21. Public Shared Function IoiStart() As Boolean
  22. Return False
  23. End Function
  24. 'Ioi 日志
  25. Private Shared Sub IoiLogLine(Content As String)
  26. End Sub
  27. Private Shared LogLinesCount As Integer = 0
  28. Private Shared LastPortsId As String = "" '上一个收到 portssub 的 ID,用于记录端口
  29. #End Region
  30. #Region "时钟"
  31. 'UI 线程刷新
  32. Private Shared UserListIdentifyCache As String = ""
  33. Private Shared RoomListIdentifyCache As String = ""
  34. Public Sub RefreshUi()
  35. End Sub
  36. '工作线程刷新
  37. Public Sub RefreshWorker()
  38. End Sub
  39. #End Region
  40. #Region "发送请求"
  41. ''' <summary>
  42. ''' 发送 Portsub 请求并等待获取控制台端口。进度将从 0 变化至 80%。
  43. ''' </summary>
  44. Private Shared Sub SendPortsubRequest(User As LinkUserIoi)
  45. End Sub
  46. ''' <summary>
  47. ''' 向控制台发送 Connect 请求。
  48. ''' </summary>
  49. Private Shared Sub SendConnectRequest(User As LinkUserIoi)
  50. Dim RawJson As New JObject()
  51. RawJson("version") = RequestVersion
  52. RawJson("name") = GetPlayerName()
  53. RawJson("id") = IoiId
  54. RawJson("type") = "connect"
  55. User.Send(RawJson)
  56. End Sub
  57. ''' <summary>
  58. ''' 向控制台发送 Update 请求。
  59. ''' </summary>
  60. Private Shared Sub SendUpdateRequest(User As LinkUserIoi, Stage As Integer, Optional Unique As Long = -1)
  61. If Unique = -1 Then Unique = GetTimeTick()
  62. Dim RawJson As New JObject
  63. RawJson("name") = GetPlayerName()
  64. RawJson("id") = IoiId
  65. RawJson("type") = "update"
  66. RawJson("stage") = Stage
  67. RawJson("unique") = Unique
  68. If Stage < 3 Then
  69. Dim Rooms As New JArray
  70. For Each Room In RoomListForMe
  71. Dim RoomObject As New JObject
  72. RoomObject("name") = Room.DisplayName
  73. RoomObject("port") = Room.Port
  74. Rooms.Add(RoomObject)
  75. Next
  76. RawJson("rooms") = Rooms
  77. User.PingPending(Unique) = Date.Now
  78. End If
  79. User.Send(RawJson)
  80. End Sub
  81. ''' <summary>
  82. ''' 尝试发送断开请求,并将其从用户列表中移除。
  83. ''' </summary>
  84. Private Shared Sub SendDisconnectRequest(User As LinkUserIoi, Optional Message As String = Nothing, Optional IsError As Boolean = False)
  85. End Sub
  86. #End Region
  87. #Region "左边栏操作"
  88. '刷新连接
  89. Private Shared Sub BtnListRefresh_Click(sender As MyIconButton, e As EventArgs)
  90. End Sub
  91. '断开连接
  92. Private Shared Sub BtnListDisconnect_Click(sender As MyIconButton, e As EventArgs)
  93. End Sub
  94. '复制联机码
  95. Public Shared Sub BtnLeftCopy_Click() Handles BtnLeftCopy.Click
  96. End Sub
  97. #End Region
  98. #Region "玩家名"
  99. ''' <summary>
  100. ''' 获取当前的玩家名。
  101. ''' </summary>
  102. Public Shared Function GetPlayerName() As String
  103. Return ""
  104. End Function
  105. Private Shared AutogenPlayerName As String = Nothing '并非由玩家自定义,而是自动生成的玩家名
  106. ''' <summary>
  107. ''' 检查某个玩家名是否合法。
  108. ''' </summary>
  109. Private Shared Function IsPlayerNameValid(Name As String) As Boolean
  110. Return True
  111. End Function
  112. #End Region
  113. #Region "请求核心"
  114. ''' <summary>
  115. ''' 启动 Socket 监听核心。
  116. ''' </summary>
  117. Public Shared Sub StartSocketListener()
  118. End Sub
  119. #End Region
  120. #Region "用户核心"
  121. '用户基类
  122. Public MustInherit Class LinkUserBase
  123. Implements IDisposable
  124. '基础数据
  125. Public Uuid As Integer = GetUuid()
  126. Public Id As String
  127. Public DisplayName As String
  128. '请求管理
  129. Public Socket As Socket = Nothing
  130. Public Sub Send(Request As JObject)
  131. End Sub
  132. Public ListenerThread As Thread = Nothing
  133. Public Sub StartListener()
  134. End Sub
  135. Public Sub BindSocket(Socket As Socket)
  136. If Me.Socket IsNot Nothing Then Throw New Exception("该用户已经绑定了 Socket")
  137. Me.Socket = Socket
  138. StartListener()
  139. End Sub
  140. 'Ping
  141. '0:与 Ping 计算无关,不回应
  142. '1:A to B,2:B to A,3:A to B
  143. Public PingPending As New Dictionary(Of Long, Date)
  144. Public PingRecord As New Queue(Of Integer)
  145. '心跳包
  146. Public LastSend As Date = Date.Now
  147. Public LastReceive As Date = Date.Now
  148. '类型转换
  149. Public Sub New(Id As String, DisplayName As String)
  150. Me.Id = Id
  151. Me.DisplayName = DisplayName
  152. Log("[IOI] 无通信包的新用户对象:" & ToString())
  153. End Sub
  154. Public Sub New(Id As String, DisplayName As String, Socket As Socket)
  155. Me.Id = Id
  156. Me.DisplayName = DisplayName
  157. Me.Socket = Socket
  158. Log("[IOI] 新用户对象:" & ToString())
  159. StartListener()
  160. End Sub
  161. Public Overrides Function ToString() As String
  162. Return DisplayName & " @ " & Id & " #" & Uuid
  163. End Function
  164. Public Shared Widening Operator CType(User As LinkUserBase) As String
  165. Return User.ToString
  166. End Operator
  167. '释放资源
  168. Public IsDisposed As Boolean = False
  169. Protected Overridable Sub Dispose(IsDisposing As Boolean)
  170. If Socket IsNot Nothing Then Socket.Dispose()
  171. If ListenerThread IsNot Nothing AndAlso ListenerThread.IsAlive Then ListenerThread.Interrupt()
  172. End Sub
  173. Public Sub Dispose() Implements IDisposable.Dispose
  174. If Not IsDisposed Then
  175. IsDisposed = True
  176. Dispose(True)
  177. End If
  178. GC.SuppressFinalize(Me)
  179. End Sub
  180. End Class
  181. '用户对象
  182. Public Shared UserList As New Dictionary(Of String, LinkUserIoi)
  183. Public Class LinkUserIoi
  184. Inherits LinkUserBase
  185. Public Sub New(Id As String, DisplayName As String, Socket As Socket)
  186. MyBase.New(Id, DisplayName, Socket)
  187. End Sub
  188. Public Sub New(Id As String, DisplayName As String)
  189. MyBase.New(Id, DisplayName)
  190. End Sub
  191. '基础数据
  192. Public Ports As New Dictionary(Of Integer, String)
  193. Public Rooms As New List(Of RoomEntry)
  194. '进度与 UI
  195. Public Progress As Double = 0
  196. Public RelativeThread As Thread = Nothing
  197. Public Function GetDescription() As String
  198. Return If(Progress < 1,
  199. "正在连接," & Math.Round(Progress * 100) & "%",
  200. "已连接," & If(Not PingRecord.Any(), "检查延迟中", Math.Round(PingRecord.Average) & "ms"))
  201. End Function
  202. Public Function ToListItem() As MyListItem
  203. Dim Item As New MyListItem With {
  204. .Title = DisplayName, .Height = 42, .Tag = Me, .Type = MyListItem.CheckType.None,
  205. .Logo = "pack://application:,,,/images/Blocks/Grass.png"}
  206. '绑定图标按钮
  207. Dim BtnRefresh As New MyIconButton With {.Logo = Logo.IconButtonRefresh, .LogoScale = 0.85, .ToolTip = "刷新", .Tag = Me}
  208. AddHandler BtnRefresh.Click, AddressOf BtnListRefresh_Click
  209. ToolTipService.SetPlacement(BtnRefresh, Primitives.PlacementMode.Bottom)
  210. ToolTipService.SetHorizontalOffset(BtnRefresh, -10)
  211. ToolTipService.SetVerticalOffset(BtnRefresh, 5)
  212. ToolTipService.SetInitialShowDelay(BtnRefresh, 200)
  213. Dim BtnClose As New MyIconButton With {.Logo = Logo.IconButtonCross, .LogoScale = 0.85, .ToolTip = "断开", .Tag = Me}
  214. AddHandler BtnClose.Click, AddressOf BtnListDisconnect_Click
  215. ToolTipService.SetPlacement(BtnClose, Primitives.PlacementMode.Bottom)
  216. ToolTipService.SetHorizontalOffset(BtnClose, -10)
  217. ToolTipService.SetVerticalOffset(BtnClose, 5)
  218. ToolTipService.SetInitialShowDelay(BtnClose, 200)
  219. Item.Buttons = {BtnRefresh, BtnClose}
  220. '刷新并返回
  221. RefreshUi(Item)
  222. Return Item
  223. End Function
  224. Public Sub RefreshUi(RelatedListItem As MyListItem)
  225. RelatedListItem.Title = DisplayName
  226. RelatedListItem.Info = GetDescription()
  227. RelatedListItem.Buttons(0).Visibility = If(Progress = 1, Visibility.Visible, Visibility.Collapsed)
  228. End Sub
  229. '释放
  230. Protected Overrides Sub Dispose(IsDisposing As Boolean)
  231. Log("[IOI] 用户资源释放(IOI, " & DisplayName & ")")
  232. If RelativeThread IsNot Nothing AndAlso RelativeThread.IsAlive Then RelativeThread.Interrupt()
  233. UserList.Remove(Id)
  234. MyBase.Dispose(IsDisposing)
  235. End Sub
  236. End Class
  237. '房间对象
  238. Private Shared RoomListForMe As New List(Of RoomEntry)
  239. Private Function GetRoomList() As List(Of RoomEntry)
  240. Dim RoomList As New List(Of RoomEntry)(RoomListForMe)
  241. For i = 0 To UserList.Count - 1
  242. If i > UserList.Count - 1 Then Exit For
  243. RoomList.AddRange(UserList.Values(i).Rooms)
  244. Next
  245. Return RoomList
  246. End Function
  247. Public Class RoomEntry
  248. '基础数据
  249. Public Port As Integer
  250. Public DisplayName As String
  251. Public User As LinkUserIoi = Nothing '若 IsOwner = True,则此项为 Nothing
  252. Public IsOwner As Boolean
  253. Public ReadOnly Property Ip As String
  254. Get
  255. If IsOwner Then
  256. Return "localhost:" & Port
  257. Else
  258. Return User.Ports(Port) & ":" & Port
  259. End If
  260. End Get
  261. End Property
  262. '类型转换
  263. Public Sub New(Port As Integer, DisplayName As String, Optional User As LinkUserIoi = Nothing)
  264. Me.IsOwner = User Is Nothing
  265. Me.User = User
  266. Me.DisplayName = DisplayName
  267. Me.Port = Port
  268. End Sub
  269. Public Overrides Function ToString() As String
  270. Return DisplayName & " - " & Port & " - " & IsOwner
  271. End Function
  272. Public Shared Widening Operator CType(Room As RoomEntry) As String
  273. Return Room.ToString
  274. End Operator
  275. Public Shared Function SelectPort(Room As RoomEntry) As Integer
  276. Return Room.Port
  277. End Function
  278. 'UI
  279. Public Function GetDescription() As String
  280. If IsOwner Then
  281. Return "由我创建,端口 " & Port
  282. Else
  283. Return "由 " & User.DisplayName & " 创建,端口 " & Port
  284. End If
  285. End Function
  286. Public Function ToListItem() As MyListItem
  287. Dim Item As New MyListItem With {
  288. .Title = DisplayName, .Height = 42, .Info = GetDescription(), .Tag = Me,
  289. .Type = If(IsOwner, MyListItem.CheckType.None, MyListItem.CheckType.Clickable),
  290. .Logo = "pack://application:,,,/images/Blocks/" & If(IsOwner, "GrassPath", "Grass") & ".png"}
  291. If IsOwner Then
  292. '绑定图标按钮
  293. Dim BtnEdit As New MyIconButton With {.Logo = Logo.IconButtonEdit, .LogoScale = 1, .ToolTip = "修改名称", .Tag = Me}
  294. AddHandler BtnEdit.Click, AddressOf BtnRoomEdit_Click
  295. ToolTipService.SetPlacement(BtnEdit, Primitives.PlacementMode.Bottom)
  296. ToolTipService.SetHorizontalOffset(BtnEdit, -22)
  297. ToolTipService.SetVerticalOffset(BtnEdit, 5)
  298. ToolTipService.SetInitialShowDelay(BtnEdit, 200)
  299. Dim BtnClose As New MyIconButton With {.Logo = Logo.IconButtonCross, .LogoScale = 0.85, .ToolTip = "关闭", .Tag = Me}
  300. AddHandler BtnClose.Click, AddressOf BtnRoomClose_Click
  301. ToolTipService.SetPlacement(BtnClose, Primitives.PlacementMode.Bottom)
  302. ToolTipService.SetHorizontalOffset(BtnClose, -10)
  303. ToolTipService.SetVerticalOffset(BtnClose, 5)
  304. ToolTipService.SetInitialShowDelay(BtnClose, 200)
  305. Item.Buttons = {BtnEdit, BtnClose}
  306. Else
  307. '绑定点击事件
  308. AddHandler Item.Click, AddressOf BtnRoom_Click
  309. End If
  310. Return Item
  311. End Function
  312. Public Sub RefreshUi(RelatedListItem As MyListItem)
  313. RelatedListItem.Title = DisplayName
  314. RelatedListItem.Info = GetDescription()
  315. End Sub
  316. End Class
  317. #End Region
  318. '正向与反向连接
  319. Public Shared Sub BtnLeftCreate_Click() Handles BtnLeftCreate.Click
  320. End Sub
  321. Private Shared Sub SendPortsubBack(User As LinkUserIoi, TargetVersion As Integer)
  322. End Sub
  323. '创建房间
  324. Private Sub LinkCreate() Handles BtnCreate.Click
  325. End Sub
  326. Private Shared Sub SendUpdateRequestToAllUsers()
  327. For i = 0 To UserList.Count - 1
  328. If i > UserList.Count - 1 Then Exit For
  329. Dim User = UserList.Values(i)
  330. If User.Progress < 1 Then Continue For
  331. Try
  332. SendUpdateRequest(User, 1) '不需要使用多线程,发送实际会瞬间完成
  333. Catch ex As Exception
  334. Log(ex, "发送全局刷新请求失败(" & User.DisplayName & ")")
  335. End Try
  336. Next
  337. End Sub
  338. '修改房间名称
  339. Private Shared Sub BtnRoomEdit_Click(sender As MyIconButton, e As EventArgs)
  340. End Sub
  341. '加入房间
  342. Private Shared Sub BtnRoom_Click(sender As MyListItem, e As EventArgs)
  343. Dim Room As RoomEntry = sender.Tag
  344. If MyMsgBox("请在多人游戏页面点击直接连接,输入 " & Room.Ip & " 以进入服务器!", "加入房间", "复制地址", "确定") = 1 Then
  345. ClipboardSet(Room.Ip)
  346. End If
  347. End Sub
  348. '关闭房间
  349. Private Shared Sub BtnRoomClose_Click(sender As MyIconButton, e As EventArgs)
  350. End Sub
  351. '获取数据包
  352. Public Shared Sub ReceiveJson(JsonData As JObject, Optional NewSocket As Socket = Nothing)
  353. End Sub
  354. ''' <summary>
  355. ''' 从用户列表中移除一位用户。提示信息视作该用户主动离开。
  356. ''' </summary>
  357. Public Shared Sub UserRemove(User As LinkUserIoi, ShowLeaveMessage As Boolean)
  358. End Sub
  359. Public Shared Sub ModuleStopManually()
  360. End Sub
  361. End Class