void IModule.OnUpdate() { if (_client == null) { return; } // 更新网络客户端 _client.Update(); // 拉取网络包 // 注意:如果服务器意外断开,未拉取的网络包将会丢失 while (true) { INetworkPackage package = (INetworkPackage)_client.PickPackage(); if (package == null) { break; } NetworkPackageCallback?.Invoke(package); } // 侦测服务器主动断开连接 if (States == ENetworkStates.Connected) { if (_client.IsConnected() == false) { States = ENetworkStates.Disconnect; NetworkEventDispatcher.SendDisconnectMsg(); CloseClient(); MotionLog.Warning("Server disconnect."); } } }
/// <summary> /// 断开连接 /// </summary> public void DisconnectServer() { if (States == ENetworkStates.Connected) { States = ENetworkStates.Disconnect; NetworkEventDispatcher.SendDisconnectMsg(); CloseClient(); } }
private void OnConnectServer(SocketError error) { MotionLog.Log($"Server connect result : {error}"); if (error == SocketError.Success) { States = ENetworkStates.Connected; NetworkEventDispatcher.SendConnectSuccessMsg(); } else { States = ENetworkStates.Disconnect; NetworkEventDispatcher.SendConnectFailMsg(error.ToString()); } }
/// <summary> /// 连接服务器 /// </summary> /// <param name="host">地址</param> /// <param name="port">端口</param> public void ConnectServer(string host, int port) { if (States == ENetworkStates.Disconnect) { States = ENetworkStates.Connecting; NetworkEventDispatcher.SendBeginConnectMsg(); IPEndPoint remote = new IPEndPoint(IPAddress.Parse(host), port); _client.ConnectAsync(remote, OnConnectServer); // 记录数据 _host = host; _port = port; _family = remote.AddressFamily; } }
void IModule.OnUpdate() { if (_client == null) { return; } // 更新网络客户端 _client.Update(); // 拉取网络包 // 注意:如果服务器意外断开,未拉取的网络包将会丢失 INetworkPackage package = (INetworkPackage)_client.PickPackage(); if (package != null) { if (package.IsHotfixPackage) { HotfixPackageCallback.Invoke(package); } else { MonoPackageCallback.Invoke(package); } } // 侦测服务器主动断开连接 if (States == ENetworkStates.Connected) { if (_client.IsConnected() == false) { States = ENetworkStates.Disconnect; NetworkEventDispatcher.SendDisconnectMsg(); CloseClient(); MotionLog.Log(ELogLevel.Warning, "Server disconnect."); } } }