public MainWindow() { InitializeComponent(); Server = new ControlHub.ControlHubServer(); BroadcastServer = new BroadcastServer(); }
private void Window_Loaded(object sender, RoutedEventArgs e) { // Get list of networks try { var networks = Network.GetLocalAddresses(); comboNetworks.ItemsSource = networks; comboNetworks.SelectedIndex = 0; string selectedHost = comboNetworks.SelectedValue.ToString(); new Thread(() => { Server.Host = selectedHost; Server.Start(); BroadcastServer.StartBroadcast(IPAddress.Parse(selectedHost)); }).Start(); CheckForUpdates(); WindowRendered = true; } catch (Exception ex) { System.Windows.MessageBox.Show("We couldn't detect your primary network! Ensure you have a network connection and try again. (" + ex.Message + ")", "Network error", MessageBoxButton.OK, MessageBoxImage.Error); this.Close(); // Close the window } }
protected override void OnExit(ExitEventArgs e) { //关闭所有网络相关服务 BroadcastClient.Stop(); BroadcastServer.Stop(); NetworkClient.Stop(); NetworkServer.Stop(); }
public void SendInterrupted(string message) { if (lockStatus != PrinterConnection.LockStatus.Locked) { return; } BroadcastServer.SendMessageToClient(clientID, message); }
public virtual void Shutdown() { SavePersistantData(); if (!broadcast_shutdown || BroadcastServer == null) { return; } BroadcastServer.BroadcastMessage(new SpoolerMessage(MessageType.PrinterShutdown, MySerialNumber, "null").Serialize()); }
public static void Main(string[] args) { var data = Observable.Interval (TimeSpan.FromSeconds (1)) .Publish() .RefCount(); var server = new BroadcastServer<long> ("127.0.0.1", 9800, data); server.Start (); Console.WriteLine ("Server started. Press any key to stop."); Console.ReadKey (); server.Stop (); }
private void RestartServer(string ip) { StopServer(); new Thread(() => { BroadcastServer.StartBroadcast(IPAddress.Parse(ip)); Server.Host = ip; Server.Start(); }).Start(); }
private void UpdateFirmwareStep() { var flag = false; foreach (KeyValuePair <char, FirmwareDetails> firmware in MyPrinterProfile.ProductConstants.FirmwareList) { FirmwareDetails firmwareDetails = firmware.Value; var key = firmware.Key; if (!mCheckedFirmwareStatus.ContainsKey(key) || mCheckedFirmwareStatus[key] != BootloaderController.FirmwareStatus.Good && mCheckedFirmwareStatus[key] != BootloaderController.FirmwareStatus.GoodQuit) { if (mInterfaceVersion == BootloaderController.BootloaderInterfaceVersion.V2) { SetBootloader(key); } try { var stopwatch = new Stopwatch(); stopwatch.Reset(); stopwatch.Start(); flag = BootloaderUpdateFirmware(firmwareDetails); stopwatch.Stop(); WriteLog("<< time: " + stopwatch.Elapsed, Logger.TextType.Write); } catch (Exception ex) { ErrorLogger.LogException("Exception in BootloaderConnection.DoBootLoop " + ex.Message, ex); flag = true; } if (!flag) { mCheckedFirmwareStatus[key] = BootloaderController.FirmwareStatus.Good; } if (flag) { break; } } } if (flag) { BroadcastServer.BroadcastMessage(new SpoolerMessage(MessageType.FirmwareUpdateFailed, MySerialNumber, "null").Serialize()); CurrentBootloaderStep = BootloaderController.BootLoaderStep.Waiting; Status = PrinterStatus.Bootloader_FirmwareUpdateFailed; } else { BroadcastServer.BroadcastMessage(new SpoolerMessage(MessageType.FirmwareUpdateComplete, MySerialNumber, "null").Serialize()); CurrentBootloaderStep = BootloaderController.BootLoaderStep.GotoApp; } }
public void BroadcastPluginMessage(SpoolerMessage message) { if (message.Type != MessageType.PluginMessage) { return; } if (clientID != Guid.Empty) { BroadcastServer.SendMessageToClient(clientID, message.Serialize()); } else { BroadcastServer.BroadcastMessage(message.Serialize()); } }
/// <summary> /// 初始化网络服务,获取本机地址,开始广播服务器地址 /// </summary> /// <returns></returns> public bool initService() { //获取本机地址 #region IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName()); //获取本地可用IPv4地址 foreach (IPAddress ipa in ips) { if (ipa.AddressFamily == AddressFamily.InterNetwork) { localIP = ipa; break; } } #endregion //广播服务器地址 #region try { ClientList = new Dictionary <int, IPEndPoint>(); RequestList_receive = new Queue <String>(); //请求接收队列 CommandList_send = new Queue <String>(); //指令发送队列 broadcastServer = new BroadcastServer(ref Server_receive, ref Server_send); //广播服务器 GameServer_send = broadcastServer.LocalIEP_send; //广播服务器发送地址 GameServer_receive = broadcastServer.LocalIEP_receive; //广播服务器接收地址 ServerBroadcast_thread = new Thread(() => { while (true) { broadcastServer.sendStartBroadcast(); Thread.Sleep(1000); } }); //服务器发送广播 ServerBroadcast_thread.IsBackground = true; ServerBroadcast_thread.Start(); //服务器开始广播 state = State.Suspend; } catch (Exception ex) { Console.WriteLine(ex.Message); return(false); } #endregion return(localIP != null); }
public CommandResult RecoveryPrintFromPowerFailure(bool bHomingRequired) { if (m_oPowerRecoveryPlugin == null) { return(CommandResult.Failed_NotInFirmware); } var num = (int)m_oPowerRecoveryPlugin.RecoveryPrintFromPowerFailure(bHomingRequired); if (num != 0) { return((CommandResult)num); } Status = PrinterStatus.Firmware_PowerRecovery; BroadcastServer.BroadcastMessage(new SpoolerMessage(MessageType.PowerOutageRecovery, Info.serial_number, null).Serialize()); return((CommandResult)num); }
private void RoomCloseHandler() { MessageBox.Show("当前房间已关闭"); ChatUserInfos.Clear(); ChatRecords.Clear(); NetworkClient.Stop(); if (GlobalValue.IsRoomMaster) { GlobalValue.IsRoomMaster = false; BroadcastServer.Stop(); NetworkServer.Stop(); } GlobalValue.IsInRoom = false; MainWindow.VM.Status = "Not in room...."; }
private void CreateRoomExecute(object obj) { if (string.IsNullOrEmpty(CreateRoomName)) { MessageBox.Show("房间名字不能为空"); return; } if (CreateRoomName.Contains(',')) { MessageBox.Show("房间名字不能带逗号"); return; } NetworkServer.Run(); IPEndPoint iep = (IPEndPoint)NetworkServer.ServerSocket.LocalEndPoint; string tcpLocalIp = iep.Address.ToString(); string tcpLocalPort = iep.Port.ToString(); NetworkClient.Run(iep); BroadcastServer.Run(new string[] { CreateRoomName, tcpLocalIp, tcpLocalPort }); OnCreatedRoom?.Invoke(); MainWindow.VM.Status = "Now you are room chairman..."; GlobalValue.IsInRoom = true; GlobalValue.IsRoomMaster = true; NetworkClient.Send(MessageType.UserJoin, new UserJoin() { UserName = GlobalValue.UserName }); }
public string WriteSerialdata(byte[] data, int getbytes) { lock (m_oLockBootloaderWait) { if (CurrentBootloaderStep == BootloaderController.BootLoaderStep.Waiting) { CurrentBootloaderStep = BootloaderController.BootLoaderStep.ProcessingRequest; Thread.Sleep(500); m_oBootloaderConnection.FlushIncomingBytes(); foreach (var output in data) { m_oBootloaderConnection.SPout(output); } byte[] numArray1 = new byte[1]; byte[] inArray = new byte[getbytes]; for (var index = 0; index < getbytes; ++index) { byte[] numArray2 = m_oBootloaderConnection.ReadBytes(1); if (index != 0 || numArray2[0] != 13) { inArray[index] = numArray2[0]; } else { --index; } } if (m_oBootloaderConnection.ReadBytes(1)[0] != 13) { return("FAIL"); } BroadcastServer.BroadcastMessage(new SpoolerMessage(MessageType.RawData, MySerialNumber, Convert.ToBase64String(inArray)).Serialize()); } CurrentBootloaderStep = BootloaderController.BootLoaderStep.Waiting; } return("OK"); }
/// <summary> /// 创建游戏服务器 /// </summary> public void CreatGameServer() { GameClientsBroadcastList = new List <IPEndPoint>(); //客户端广播地址表 Clients = new List <UdpClient>(); //客户端Udp连接 RequestList_receive = new Queue <String>(); //游戏请求接收队列 CommandList_send = new Queue <String>(); //游戏指令发送队列 broadcastServer = new BroadcastServer(ref Server_receive, ref Server_send); //游戏广播服务器 GameServer_send = broadcastServer.LocalIEP_send; //游戏广播服务器发送地址 GameServer_receive = broadcastServer.LocalIEP_receive; //游戏广播服务器接收地址 CreatGame_thread = new Thread(() => { while (true) { broadcastServer.sendStartBroadcast(); Thread.Sleep(1000); Console.WriteLine("sendbroadcast~"); } });//服务器发送广播 Thread receiveIPList_thread = new Thread(() => { while (GameClientsBroadcastList.Count < PlayerCount) { IPEndPoint P_temp = broadcastServer.receiveResponse(); Console.WriteLine("a new broadcastclient"); if (!isIPExist(P_temp)) { GameClientsBroadcastList.Add(P_temp); } } CreatGame_thread.Abort(); });//服务器接收响应 CreatGame_thread.IsBackground = true; receiveIPList_thread.IsBackground = true; CreatGame_thread.Start(); //服务器开始游戏广播 receiveIPList_thread.Start(); //服务器开始接收加入游戏响应 }
private void StopServer() { // Stop will await shutdown, running threads should stop when all stops are called BroadcastServer.StopBroadcast(); Server.Stop(); }
private void BootLoaderStartup() { FirmwareDetails firmware = MyPrinterProfile.ProductConstants.FirmwareList['M']; if (mInterfaceVersion == BootloaderController.BootloaderInterfaceVersion.V2) { SetBootloader('M'); } MyPrinterInfo.hardware.machine_type = "The_Micro"; var eepromMapping = new EEPROMMapping(m_oBootloaderConnection.ReadAllReadableEEPROM(), MyPrinterProfile.EEPROMConstants); var flag = false; var str = ""; byte[] bytesFromLocation = eepromMapping.GetBytesFromLocation("SerialNumber", 16); for (var index = 0; index < 16; ++index) { if (bytesFromLocation[index] == byte.MaxValue) { flag = true; } str += ((char)bytesFromLocation[index]).ToString(); } if (flag) { BroadcastServer.BroadcastMessage(new SpoolerMessage(MessageType.UserDefined, MySerialNumber, "There was a major error in your printer. Please reset the printer. If the problem persist, contact M3D.").Serialize()); Status = PrinterStatus.Error_PrinterNotAlive; } else { MySerialNumber = new PrinterSerialNumber(str); foreach (IFirstRunUpdater updater in MyPrinterProfile.FirstRunConstants.updater_list) { updater.CheckForUpdate(str, eepromMapping.GetAllEEPROMData(), m_oBootloaderConnection, MyPrinterProfile); } logger.ResetWithSerialNumber(MySerialNumber.ToString()); LoadPersistantData(); PersistantDetails.hours_used = eepromMapping.GetFloat("HoursCounterSpooler"); HardwareDetails.firmware_version = eepromMapping.GetUInt32("FirmwareVersion"); PersistantData.RestartOptions restartOptions = PersistantDetails.PopRestartAction(); SavePersistantData(); CurrentBootloaderStep = BootloaderController.BootLoaderStep.CheckFirmware; if (restartOptions.RestartAction == PersistantData.RestartAction.SetExtruderCurrent) { SetExtruderCurrent((ushort)restartOptions.RestartActionParam); } else if (restartOptions.RestartAction == PersistantData.RestartAction.SetFan) { SetFanConstants((FanConstValues.FanType)restartOptions.RestartActionParam); } else if (restartOptions.RestartAction == PersistantData.RestartAction.ForceStayBootloader) { Status = PrinterStatus.Bootloader_Ready; CurrentBootloaderStep = BootloaderController.BootLoaderStep.Waiting; } else { if (restartOptions.RestartAction != PersistantData.RestartAction.ForceUpdateFirmware) { return; } CurrentBootloaderStep = BootloaderController.BootLoaderStep.ForceFirmwareUpdate; } } }
private bool ProcessConnectedPrinter() { try { if (Status == PrinterStatus.Error_PrinterNotAlive) { return(false); } if (!IsOpen) { Status = PrinterStatus.Error_PrinterNotAlive; } BaseController controllerSelf = ControllerSelf; if (controllerSelf != null) { controllerSelf.DoConnectionLogic(); if (!controllerSelf.IsWorking || Status == PrinterStatus.Firmware_PrintingPaused) { var num = CurrentRPC_id.Value; if (num != 0U && (int)num != (int)Info.synchronization.LastCompletedRPCID) { Info.synchronization.LastCompletedRPCID = num; } } else { lock (lockResetTimer) { if (lockResetTimer.IsRunning) { lockResetTimer.Restart(); } } } if (lockStatus == PrinterConnection.LockStatus.Pending && Status != PrinterStatus.Connecting && (!controllerSelf.IsWorking && !controllerSelf.IsPrinting || Status == PrinterStatus.Firmware_PrintingPaused)) { lockStatus = PrinterConnection.LockStatus.Locked; Info.synchronization.Locked = true; BroadcastServer.SendMessageToClient(clientID, new SpoolerMessage(MessageType.LockConfirmed, Info.serial_number, lockID.ToString()).Serialize()); } if (controllerSelf.Idle && lockStatus == PrinterConnection.LockStatus.Locked) { lock (lockResetTimer) { if (!lockResetTimer.IsRunning) { lockResetTimer.Restart(); } if (lockResetTimer.Elapsed.TotalSeconds > 30.0) { DoBreakLock(); } } } else { lock (lockResetTimer) { if (lockResetTimer.IsRunning) { lockResetTimer.Stop(); } } } } } catch (Exception ex) { if (ex.Message.ToString().ToLower() != "unable to write program memory. the serial port is not open.") { ErrorLogger.LogException("PrinterConnection Exception", ex); } Shutdown(); return(false); } return(true); }
/// <summary> /// 创建游戏服务器 /// </summary> public void CreatGameServer() { GameClientsBroadcastList = new List<IPEndPoint>();//客户端广播地址表 Clients = new List<UdpClient>();//客户端Udp连接 RequestList_receive = new Queue<String>();//游戏请求接收队列 CommandList_send = new Queue<String>();//游戏指令发送队列 broadcastServer = new BroadcastServer(ref Server_receive, ref Server_send);//游戏广播服务器 GameServer_send = broadcastServer.LocalIEP_send;//游戏广播服务器发送地址 GameServer_receive = broadcastServer.LocalIEP_receive;//游戏广播服务器接收地址 CreatGame_thread = new Thread(() => { while (true) { broadcastServer.sendStartBroadcast(); Thread.Sleep(1000); Console.WriteLine("sendbroadcast~"); } });//服务器发送广播 Thread receiveIPList_thread = new Thread(() => { while (GameClientsBroadcastList.Count < PlayerCount) { IPEndPoint P_temp = broadcastServer.receiveResponse(); Console.WriteLine("a new broadcastclient"); if (!isIPExist(P_temp)) { GameClientsBroadcastList.Add(P_temp); } } CreatGame_thread.Abort(); });//服务器接收响应 CreatGame_thread.IsBackground = true; receiveIPList_thread.IsBackground = true; CreatGame_thread.Start();//服务器开始游戏广播 receiveIPList_thread.Start();//服务器开始接收加入游戏响应 }
private void DoBreakLock() { BroadcastServer.SendMessageToClient(clientID, new SpoolerMessage(MessageType.LockResult, Info.serial_number, 0.ToString("D8") + CommandResult.LockLost_TimedOut.ToString()).Serialize()); var num = (int)ReleaseLock(lockID); }