// Construtores #region Constructors /// <summary> /// Nova busca de servidores /// </summary> /// <param name="port">Porta de escuta</param> public ServerFinder(ushort port) { // Valida porta if (port == 0) { throw new ArgumentOutOfRangeException(nameof(port)); } // Cria socket para escuta this._Socket = new BroadcastSocket(port); // Tenta inicializar escuta if (this._Socket.TryBind()) { // Linka recebimento de pacotes this._Socket.Received += this.Socket_Received; } else { // Cria thread para executar binding this._BindThread = new Thread(this.BindThread) { Priority = ThreadPriority.BelowNormal }; this._BindThread.Start(); } }
// Construtores #region Constructors /// <summary> /// Nova descoberta de serviço /// </summary> public ServerPublisher(ServerInfo server, ushort port) { // Valida entrada if (server == null) { throw new ArgumentNullException(nameof(server)); } if (port == 0) { throw new ArgumentOutOfRangeException(nameof(port)); } // Cria novo socket de envio BroadcastSocket socket = new BroadcastSocket(port); // Guarda referência ao servidor this._Server = server; // Atualiza dados this.Update(); // Executa o primeiro envio para teste socket.SendSync(this._Data); // Cria e inicializa thread de envio this._SendThread = new Thread(this.SendThread) { Priority = ThreadPriority.Lowest }; this._Active = true; this._SendThread.Start(new object[] { socket }); }
// Métodos privados #region Private /// <summary> /// Thread para binding /// </summary> private void BindThread() { try { // Loop while (true) { // Possui socket e conseguiu executar o bind? BroadcastSocket socket = this._Socket; if ((socket != null) && (socket.TryBind())) { // Linka recebimento de pacotes socket.Received += this.Socket_Received; // Sai do laço break; } // Aguarda até próxima tentativa Thread.Sleep(2000); } } // Ignora thread abortada catch (ThreadAbortException ex) { ex.Ignore(); } // Reporta exceções catch (Exception ex) { ex.Log(); } // Libera referência da thread this._BindThread = null; }
/// <summary> /// Libera recursos alocados /// </summary> public void Dispose() { // Finaliza thread de bind, caso houver this._BindThread = this._BindThread.SafeAbort(); // Possui socket? BroadcastSocket socket = this._Socket; this._Socket = null; if (socket != null) { // Libera eventos e socket socket.Received -= this.Socket_Received; socket.Dispose(); } }
public static void Broadcast(byte[] Msg, string UName, bool Flag) { foreach (DictionaryEntry Item in ClientsList) { TcpClient BroadcastSocket; BroadcastSocket = (TcpClient)Item.Value; NetworkStream BroadcastStream = BroadcastSocket.GetStream(); Byte[] BroadcastBytes = null; if (Flag) { BroadcastBytes = Msg; BroadcastStream.Write(BroadcastBytes, 0, BroadcastBytes.Length); BroadcastStream.Flush(); } } }
// Métodos privados #region Private /// <summary> /// Thread de notificação em rede /// </summary> private void SendThread(object args) { try { // Recupera argumentos de entrada object[] input = (object[])args; BroadcastSocket socket = (BroadcastSocket)input[0]; // Aguarda até próximo envio Thread.Sleep(1000); // Enquanto estiver com serviço ativo while (this._Active) { try { // Envia dados socket.SendSync(this._Data); // Aguarda até próximo envio Thread.Sleep(2000); } // Thread abortada, redispara catch (ThreadAbortException) { throw; } // Reporta exceções catch (Exception ex) { ex.Log(); } } } // Ignora thread abortada catch (ThreadAbortException ex) { ex.Ignore(); } // Reporta outras exceções catch (Exception ex) { ex.Log(); } }
/// <summary> /// Constructor for broadcast link object /// </summary> /// <param name="socket"></param> /// <param name="proxy"></param> /// <param name="remoteId"></param> /// <param name="localAddress"></param> /// <param name="peerAddress"></param> internal BroadcastLink(BroadcastSocket socket, INameRecord proxy, Reference remoteId, SocketAddress localAddress, SocketAddress peerAddress) : base(socket, proxy, remoteId, localAddress, peerAddress) { }
internal PluginManager() { //An aggregate catalog that combines multiple catalogs var catalog = new AggregateCatalog(); //Adds all the parts found in same directory where the application is running! var currentPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(PluginManager)).Location); catalog.Catalogs.Add(new DirectoryCatalog(currentPath)); //Create the CompositionContainer with the parts in the catalog var container = new CompositionContainer(catalog); //Fill the imports of this object try { container.ComposeParts(this); } catch (CompositionException compositionException) { Console.WriteLine(compositionException.ToString()); } if (NodeTaskPlugin != null) { Injector.Instance.Bind <INodeTask>(NodeTaskPlugin.GetType()); } if (Socket != null) { Injector.Instance.Bind <ISocket>(Socket.GetType()); } if (BroadcastSocket != null) { Injector.Instance.Bind <IBroadcastSocket>(BroadcastSocket.GetType()); } if (LogProvider != null) { Injector.Instance.Bind <ILogProvider>(LogProvider.GetType()); } if (ObservableQueue != null) { Injector.Instance.Bind <IObservableQueue <ClientProcess> >(ObservableQueue.GetType()); } if (SerialiserAutoDiscoveryMessage != null) { Injector.Instance.Bind <ISerialiser <AutoDiscoveryMessage, byte[]> >(SerialiserAutoDiscoveryMessage.GetType()); } if (SerialiserClientMessage != null) { Injector.Instance.Bind <ISerialiser <ClientMessage, byte[]> >(SerialiserClientMessage.GetType()); } if (SerialiserClientProcess != null) { Injector.Instance.Bind <ISerialiser <ClientProcess, byte[]> >(SerialiserClientProcess.GetType()); } if (SerialiserClientResultMessage != null) { Injector.Instance.Bind <ISerialiser <ClientResultMessage, byte[]> >(SerialiserClientResultMessage.GetType()); } if (SerialiserInterNodeCommunicationMessage != null) { Injector.Instance.Bind <ISerialiser <InterNodeCommunicationMessage, byte[]> >(SerialiserInterNodeCommunicationMessage.GetType()); } if (SerialiserObservableQueue != null) { Injector.Instance.Bind <ISerialiser <IObservableQueue <ClientProcess>, byte[]> >(SerialiserObservableQueue.GetType()); } }