private void ClientConnectionFinishCDB(SocketsServer server, SocketsServerClientConnectionHandler ClientHandler) { this._STXDSS_PublicationClientConnectionsManager.UnlogPendingSocketClientMatchConnectionWithSTXDSSClientTable(ClientHandler); DPE_Client client = default(DPE_Client); client = this._STXDSS_PublicationClientConnectionsManager.GetSubscriptorClient(ClientHandler); if (!(client == null)) { try { this._STXDSS_PublicationClientConnectionsManager.RemoveClientSubscription(ClientHandler); } catch (Exception) { } try { if (SubscriptionDeattachmentEvent != null) { SubscriptionDeattachmentEvent(this, client); } } catch (Exception) { } } }
public DPE_Publication(string publicationsDataBAseConnectionString, string publicationsGroup, string publicationName, DPE_Client STXDSSClient) { //sets the publication name this._publicationsGroup = publicationsGroup.ToUpper(); this._publicationName = publicationName.ToUpper(); this._publicationCreation = DateTime.Now; this._variablesPublishedRegistry = new DPE_PublishedVariablesRegistry(publicationName); //saves reference to the datra socket server that creates the publication this._publisherSTXDSSClient = STXDSSClient; //creates a sockets server to support the link between the client and the publication this._publicationSocketsServer = new SocketsServer(DPE_ServerDefs.INITIAL_TCP_PORT_DPE_SERVICE, DPE_ServerDefs.FINAL_TCP_PORT_DPE_SERVICE); this._publicationSocketsServer.NewClientConnection += this.NewIncommingConnectionCDB; this._publicationSocketsServer.ClientConnectionFinished += this.ClientConnectionFinishCDB; this._STXDSS_PublicationClientConnectionsManager = new DPE_PublicationClientConnectionsManager(this); this._updatePublicationOnClientConnectionQueue = new Queue(); this._publicationsDataBaseConnectionString = publicationsDataBAseConnectionString; //************************************************************************************************** //creates the publication in the data base according with the connection string passed by attribute CreatePublicationDataTable(this._publicationsDataBaseConnectionString, this._publicationName); CustomEventLog.WriteEntry(EventLogEntryType.SuccessAudit, "Publication \'" + this._publicationName + "\' -> CREATED succesfully"); }
private bool disposedValue = false; // To detect redundant calls // IDisposable protected virtual void Dispose(bool disposing) { if (!this.disposedValue) { if (disposing) { // TODO: free other state (managed objects). lock (this._serversTable) { IEnumerator enumm = this._serversTable.GetEnumerator(); SocketsServer server = null; while (enumm.MoveNext()) { server = (SocketsServer)(((DictionaryEntry)enumm.Current).Value); server.Dispose(); } } } // TODO: free your own state (unmanaged objects). // TODO: set large fields to null. } this.disposedValue = true; }
private void DisposeServer(SocketsServer server) { if (!(server == null)) { if (this._serversTable.ContainsKey(server.ServerID)) { server.ClientDataReceived -= eventHandling_ClientDataReceived; server.NewClientConnection -= eventHandling_NewClientConnection; server.ClientConnectionFinished -= eventHandling_ClientConnectionFinished; lock (this._serversTable) { this._serversTable.Remove(server.ServerID); this._serversStack.RemoveServerFromStack(server); } try { if (ServerDisposedEvent != null) { ServerDisposedEvent(server); } } catch (Exception) { } server.Dispose(); } } }
internal void LogClientDisconnectionFromPublication(DPE_DataPublicationsClient stxdssClient, string PublicationName) { try { this.UnLogClientForPublicationDataReception(stxdssClient, PublicationName); int connectedClients = this.GetConnectedClientsToPublicationCount(PublicationName); if (connectedClients <= 0) { //removes all the handles to receive data for this publication because nobody will be use it //removes the multicast clientss if exists MultiCastDataReplicatorClient publicationMultiCastClient = default(MultiCastDataReplicatorClient); publicationMultiCastClient = (MultiCastDataReplicatorClient)this._MultiCastDataReplicatorClientsTable[PublicationName]; if (!(publicationMultiCastClient == null)) { publicationMultiCastClient.Dispose(); } this._MultiCastDataReplicatorKeyStringsTable.Remove(PublicationName); this._MultiCastDataReplicatorClientsTable.Remove(PublicationName); SocketsServer listener = default(SocketsServer); listener = (SocketsServer)this._SocketServersListenersTable[PublicationName]; if (!(listener == null)) { listener.Dispose(); } this._SocketServersListenersTable.Remove(PublicationName); } } catch (Exception) { } }
public void eventHandling_ClientConnectionFinished(SocketsServer server, SocketsServerClientConnectionHandler ClientHandler) { // a client is disconnected from one of the servers so then is pushed into the stack to make it available //for the next request for a server available wiht room for connections try { if (server.ClientConnectionsCount < this._maxNumberOfClientsPerServer) { if (server.ClientConnectionsCount > 0) { this._serversStack.Push(server); } else { if (this._discardServersWhenThereWerentClientsConnected) { this.DisposeServer(server); } } } } catch (Exception) { } }
public void Push(SocketsServer server) { //adds a server to the end of the stack lock (this._serversTable) { int index = this._serversTable.Count + 1; this._serversTable.Add(index, server); } }
public SocketsServer Peek() { SocketsServer server = default(SocketsServer); lock (this._serversTable) { int index = this._serversTable.Count; server = (SocketsServer)this._serversTable[index]; } return(server); }
private void eventHandling_ClientDataReceived(SocketsServer server, SocketsServerClientConnectionHandler ClientHandler, SocketData data) { try { if (DataReceivedFromClientEvent != null) { DataReceivedFromClientEvent(server, ClientHandler, data); } } catch (Exception) { } }
private void ClientConnectionFinished(SocketsServer server, SocketsServerClientConnectionHandler ClientHandler) { try { string msg = DateTime.Now.ToString() + " -> " + ClientHandler.IdentityString; this.SetListBoxItem(this.lstOutgoingConnections, msg); this.RemoveListBoxItem(this.lstConnectedClients, ClientHandler); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public void RemoveServerFromStack(SocketsServer server) { lock (this._serversTable) { //removes all the references of the server on the sorted list IEnumerator enumm = this._serversTable.GetEnumerator(); int index = 0; SocketsServer localServerOnTable = null; Collection colOfIndexToDeleteFromTable = new Collection(); while (enumm.MoveNext()) { index = System.Convert.ToInt32(((DictionaryEntry)enumm.Current).Key); localServerOnTable = (SocketsServer)(((DictionaryEntry)enumm.Current).Value); if (localServerOnTable.ServerID == server.ServerID) { colOfIndexToDeleteFromTable.Add(index, null, null, null); } } //remves the elements from the list enumm = colOfIndexToDeleteFromTable.GetEnumerator(); while (enumm.MoveNext()) { index = System.Convert.ToInt32(enumm.Current); this._serversTable.Remove(index); } //copies all the elements left into a new colllection SortedList copyOfServersList = new SortedList(); int newIndex = 1; enumm = this._serversTable.GetEnumerator(); while (enumm.MoveNext()) { localServerOnTable = (SocketsServer)(((DictionaryEntry)enumm.Current).Value); copyOfServersList.Add(newIndex, localServerOnTable); newIndex++; } this._serversTable.Clear(); enumm = copyOfServersList.GetEnumerator(); while (enumm.MoveNext()) { localServerOnTable = (SocketsServer)(((DictionaryEntry)enumm.Current).Value); this.Push(localServerOnTable); } } }
public SocketsServerMultiplexingManager(int initialServerPortsNumberRange, int finalServerPortsNumberRange, int maxNumberOfClientsPerServer, bool DiscardServersWhenThereWerentClientsConnected) { this._serversTable = new Hashtable(); this._serversStack = new SocketsServersStack(); this._initialServerPortsNumberRange = initialServerPortsNumberRange; this._finalServerPortsNumberRange = finalServerPortsNumberRange; this._maxNumberOfClientsPerServer = maxNumberOfClientsPerServer; //sets and initial server and push into the stack to be retrieved SocketsServer initialServer = this.CreateAndStackServer(); this._discardServersWhenThereWerentClientsConnected = DiscardServersWhenThereWerentClientsConnected; }
internal CommunicationsLibrary.Services.SocketsDataDistribution.SocketsServer GetPublicationSocketsServerListener(DPE_DataPublicationsClient client, string PublicationName) { if (this._SocketServersListenersTable.ContainsKey(PublicationName)) { SocketsServer listener = default(SocketsServer); listener = (SocketsServer)this._SocketServersListenersTable[PublicationName]; this.LogClientForPublicationDataReception(client, PublicationName); return(listener); } else { SocketsServer listener = new SocketsServer(DPE_ServerDefs.INITIAL_TCP_PORT_DPE_SERVICE, DPE_ServerDefs.FINAL_TCP_PORT_DPE_SERVICE); this._SocketServersListenersTable.Add(PublicationName, listener); this.LogClientForPublicationDataReception(client, PublicationName); return(listener); } }
public SocketsServer GetCurrentAvailableServer() { SocketsServer currentServerAvailable = null; bool serverFound = false; lock (this._serversStack) { while (this._serversStack.Count > 0) { //gets the server on the stack top without removing it currentServerAvailable = this._serversStack.Peek(); if (!(currentServerAvailable == null)) { if (currentServerAvailable.ClientConnectionsCount < this._maxNumberOfClientsPerServer) { //the current server on the stack has no more room available , so then is created a new //server to receive data serverFound = true; break; } else { //if the server in the stack has no more room for the maximun number of connections //allowed for the server then is removed from the stack this._serversStack.Pop(); serverFound = false; } } else { //if the referece is invalid this._serversStack.Pop(); serverFound = false; } } } //by the end of the lookin up if the flag 'server found' is false means there where no server with availabel space if (!serverFound) { currentServerAvailable = this.CreateAndStackServer(); } return(currentServerAvailable); }
private SocketsServer CreateAndStackServer() { SocketsServer newSServer = default(SocketsServer); lock (this._serversTable) { newSServer = new SocketsServer(this._initialServerPortsNumberRange, this._finalServerPortsNumberRange); this._serversTable.Add(newSServer.ServerID, newSServer); newSServer.ClientDataReceived += eventHandling_ClientDataReceived; newSServer.NewClientConnection += eventHandling_NewClientConnection; newSServer.ClientConnectionFinished += eventHandling_ClientConnectionFinished; } this._serversStack.Push(newSServer); return(newSServer); }
private void ClientDataReceived(SocketsServer server, SocketsServerClientConnectionHandler ClientHandler, SocketData data) { try { this._dataReceptionCount++; this.SetTextBoxText(this.txtDataReceivedCount, System.Convert.ToString(this._dataReceptionCount)); this.ListBox_Items_Clear(this.lstDataAttributes); if (this.chkPutIncommingDataIntoList.Checked) { string msg = "[FROM=" + ClientHandler.IdentityString + "]->[DATA LENGTH = " + System.Convert.ToString(data.DataLenght) + "][DATANAME= " + data.DataName + "][DATA = " + data.XMLDataString + "]"; this.SetListBoxItem(this.lstBoxDataReceived, data); } if (this.chkBroadCastReceivedData.Checked) { server.BroadCastData(data); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private ALDSDataServer() { this._DataSocketServer = new SocketsServer(ALDS_DATA_SERVER_PORT); }
static void Main(string[] args) { int port = 8000; SocketsServer server = new SocketsServer(port, new CalculatorService()); server.StartServing(); Console.WriteLine("Started the simple server"); CustomBinding binding = new CustomBinding(new SizedTcpTransportBindingElement()); EndpointAddress address = new EndpointAddress( SizedTcpTransportBindingElement.SizedTcpScheme + "://localhost:" + port); ChannelFactory <IUntypedTest> factory = new ChannelFactory <IUntypedTest>(binding, address); IUntypedTest proxy = factory.CreateChannel(); string[] allInputs = new string[] { "{\"method\":\"Add\",\"params\":[5, 8],\"id\":1}", "{\"method\":\"Multiply\",\"params\":[5, 8],\"id\":2}", "{\"method\":\"Divide\",\"params\":[5, 0],\"id\":3}", }; foreach (string input in allInputs) { byte[] inputBytes = Encoding.UTF8.GetBytes(input); Console.WriteLine("Input: {0}", input); Message inputMessage = Formatting.BytesToMessage(inputBytes); Message outputMessage = proxy.Process(inputMessage); Console.WriteLine("Received output: {0}", outputMessage); byte[] outputBytes = Formatting.MessageToBytes(outputMessage); Console.WriteLine("Output bytes:"); Debugging.PrintBytes(outputBytes); } ((IClientChannel)proxy).Close(); factory.Close(); Console.WriteLine(); Console.WriteLine("Now using the typed interface"); ChannelFactory <ITypedTest> typedFactory = new ChannelFactory <ITypedTest>(binding, address); typedFactory.Endpoint.Behaviors.Add(new JsonRpcEndpointBehavior()); ITypedTest typedProxy = typedFactory.CreateChannel(); Console.WriteLine("Calling Add"); int result = typedProxy.Add(5, 8); Console.WriteLine(" ==> Result: {0}", result); Console.WriteLine(); Console.WriteLine("Calling Multiply"); result = typedProxy.Multiply(5, 8); Console.WriteLine(" ==> Result: {0}", result); Console.WriteLine(); Console.WriteLine("Calling Divide (throws)"); try { result = typedProxy.Divide(5, 0); Console.WriteLine(" ==> Result: {0}", result); } catch (JsonRpcException e) { Console.WriteLine("Error: {0}", e.JsonException); } Console.WriteLine(); Console.WriteLine("Now using the typed asynchronous interface"); var asyncTypedFactory = new ChannelFactory <ITypedTestAsync>(binding, address); asyncTypedFactory.Endpoint.Behaviors.Add(new JsonRpcEndpointBehavior()); ITypedTestAsync asyncTypedProxy = asyncTypedFactory.CreateChannel(); AutoResetEvent evt = new AutoResetEvent(false); Console.WriteLine("Calling BeginAdd"); asyncTypedProxy.BeginAdd(5, 8, delegate(IAsyncResult ar) { result = asyncTypedProxy.EndAdd(ar); Console.WriteLine(" ==> Result: {0}", result); Console.WriteLine(); evt.Set(); }, null); evt.WaitOne(); Console.WriteLine("Calling BeginMultiply"); asyncTypedProxy.BeginMultiply(5, 8, delegate(IAsyncResult ar) { result = asyncTypedProxy.EndMultiply(ar); Console.WriteLine(" ==> Result: {0}", result); Console.WriteLine(); evt.Set(); }, null); evt.WaitOne(); Console.WriteLine("Calling BeginDivide (throws)"); asyncTypedProxy.BeginDivide(5, 0, delegate(IAsyncResult ar) { try { result = asyncTypedProxy.EndDivide(ar); Console.WriteLine(" ==> Result: {0}", result); } catch (JsonRpcException e) { Console.WriteLine("Error: {0}", e.JsonException); } Console.WriteLine(); evt.Set(); }, null); evt.WaitOne(); }