private void btnStartUDPService_Click(object sender, RoutedEventArgs e) { int nPort = 0; try { nPort = int.Parse(txtUDPPort.Text); } catch (Exception) { MessageBox.Show("Wrong port number.", "UDP Service", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (btnStartUDPService.Content.ToString() == "Start") { sockUDPRef = NeuronDataReader.BRStartUDPServiceAt(nPort); if (sockUDPRef != IntPtr.Zero) { btnStartUDPService.Content = "Stop"; } else { btnStartUDPService.Content = "Start"; } } else { NeuronDataReader.BRCloseSocket(sockUDPRef); btnStartUDPService.Content = "Start"; } }
static void DestroyConnection(NeuronSource source) { if (source == null) { return; } // We have to make a lock before removing the source from the maps. lock (_sourcesLock) _sources.Remove(source); // Now we can delete the source safely! if (source.SocketType == SocketType.TCP) { NeuronDataReader.BRCloseSocket(source.Socket); Debug.Log("[Neuron] Disconnected " + source.Address + ":" + source.Port); } else { NeuronDataReader.BRCloseSocket(source.Socket); Debug.Log("[Neuron] Stopped listening " + source.Port); } // Unregister the reader callback if this was the last connection. if (_sources.Count == 0) { NeuronDataReader.BRRegisterFrameDataCallback(IntPtr.Zero, null); } }
void OnApplicationQuit() { if (_connection != IntPtr.Zero) { if (NeuronDataReader.BRGetSocketStatus(_connection) != SocketStatus.CS_OffWork) { // disconnect NeuronDataReader.BRCloseSocket(_connection); _connection = IntPtr.Zero; } } }
private void Window_Closed(object sender, EventArgs e) { if (sockTCPRef != IntPtr.Zero) { NeuronDataReader.BRCloseSocket(sockTCPRef); sockTCPRef = IntPtr.Zero; } if (sockUDPRef != IntPtr.Zero) { NeuronDataReader.BRCloseSocket(sockUDPRef); sockUDPRef = IntPtr.Zero; } }
private void btnExitWindows(object sender, RoutedEventArgs e) { //添加状态判断,当TCP服务未断开时直接点击exit,会先关闭TCP连接再退出,避免程序崩溃 if (NeuronDataReader.BRGetSocketStatus(sockTCPRef) == SocketStatus.CS_Running) { NeuronDataReader.BRCloseSocket(sockTCPRef); } //添加状态判断,当UDP服务未断开时直接点击exit,会先关闭UDP连接再退出,避免程序崩溃 if (sockUDPRef != IntPtr.Zero) { NeuronDataReader.BRCloseSocket(sockUDPRef); } Environment.Exit(0); //立即 }
static void DestroyConnection(NeuronSource source) { if (source != null) { if (source.commandSocketReference != IntPtr.Zero) { commandSocketReferenceIndex.Remove(source.commandSocketReference); } source.OnDestroy(); Guid guid = source.guid; string address = source.address; int port = source.port; int commandServerPort = source.commandServerPort; SocketType socketType = source.socketType; IntPtr socketReference = source.socketReference; IntPtr commandSocketReference = source.commandSocketReference; connections.Remove(guid); socketReferencesIndex.Remove(socketReference); if (commandSocketReference != IntPtr.Zero) { NeuronDataReader.BRCloseSocket(commandSocketReference); Debug.Log(string.Format("[NeuronConnection] Disconnected from command server {0}:{1}.", address, commandServerPort)); } if (socketType == SocketType.TCP) { NeuronDataReader.BRCloseSocket(socketReference); Debug.Log(string.Format("[NeuronConnection] Disconnected from {0}:{1}.", address, port)); } else { NeuronDataReader.BRCloseSocket(socketReference); Debug.Log(string.Format("[NeuronConnection] Stop listening at {0}. {1}", port, source.guid.ToString("N"))); } } if (connections.Count == 0) { UnregisterReaderCallbacks(); } }
private void btnConnect1_Click(object sender, RoutedEventArgs e) { if (NeuronDataReader.BRGetSocketStatus(sockTCPRef1) == SocketStatus.CS_Running) { NeuronDataReader.BRCloseSocket(sockTCPRef1); sockTCPRef1 = IntPtr.Zero; btnConnect1.Content = "Connect"; } else { sockTCPRef1 = NeuronDataReader.BRConnectTo(txtIP1.Text, int.Parse(txtPort1.Text)); if (sockTCPRef1 == IntPtr.Zero) { string msg = NeuronDataReader.strBRGetLastErrorMessage(); MessageBox.Show(msg, "Connection error", MessageBoxButton.OK, MessageBoxImage.Error); return; } btnConnect1.Content = "Disconnect"; _frameCount1 = 0; } }
private void ButtonConnect_Click(object sender, RoutedEventArgs e) { if (NeuronDataReader.BRGetSocketStatus(sockTCPRef) == SocketStatus.CS_Running) { NeuronDataReader.BRCloseSocket(sockTCPRef); sockTCPRef = IntPtr.Zero; btnConnect.Content = "Connect"; } else { sockTCPRef = NeuronDataReader.BRConnectTo(txtIP.Text, int.Parse(txtPort.Text)); if (sockTCPRef == IntPtr.Zero) { string msg = NeuronDataReader.strBRGetLastErrorMessage(); MessageBox.Show(msg, "Connection error", MessageBoxButton.OK, MessageBoxImage.Error); return; } NeuronDataReader.BRRegisterAutoSyncParmeter(sockTCPRef, CmdId.Cmd_AvatarCount); btnConnect.Content = "Disconnect"; _frameCount = 0; } }