/// <summary> /// Start the server. Listen to client connections. /// </summary> public void Start() { IPAddress ipAddress = IPAddress.Parse(localHostname); IPEndPoint localEP = new IPEndPoint(ipAddress, localPort); // Create a TCP/IP socket. listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listeningSocket.Bind(localEP); listeningSocket.Listen(100); Thread acceptThread = new Thread(ServerAcceptThread); if (serverMode == ServerMode.SINGLE_REDUNDANCY_GROUP) { if (redGroups.Count > 0) { RedundancyGroup singleGroup = redGroups[0]; redGroups.Clear(); redGroups.Add(singleGroup); } else { RedundancyGroup singleGroup = new RedundancyGroup(); redGroups.Add(singleGroup); } } if (serverMode == ServerMode.SINGLE_REDUNDANCY_GROUP || serverMode == ServerMode.MULTIPLE_REDUNDANCY_GROUPS) { foreach (RedundancyGroup redGroup in redGroups) { redGroup.asduQueue = new ASDUQueue(maxQueueSize, enqueueMode, alParameters, DebugLog); redGroup.server = this; } } acceptThread.Start(); }
private void ServerAcceptThread() { running = true; DebugLog("Waiting for connections..."); while (running) { try { Socket newSocket = listeningSocket.Accept(); if (newSocket != null) { newSocket.NoDelay = true; DebugLog("New connection"); IPEndPoint ipEndPoint = (IPEndPoint)newSocket.RemoteEndPoint; DebugLog(" from IP: " + ipEndPoint.Address.ToString()); bool acceptConnection = true; if (OpenConnections >= maxOpenConnections) { acceptConnection = false; } if (acceptConnection && (connectionRequestHandler != null)) { acceptConnection = connectionRequestHandler(connectionRequestHandlerParameter, ipEndPoint.Address); } if (acceptConnection) { ClientConnection connection = null; if ((serverMode == ServerMode.SINGLE_REDUNDANCY_GROUP) || (serverMode == ServerMode.MULTIPLE_REDUNDANCY_GROUPS)) { RedundancyGroup catchAllGroup = null; RedundancyGroup matchingGroup = null; /* get matching redundancy group */ foreach (RedundancyGroup redGroup in redGroups) { if (redGroup.Matches(ipEndPoint.Address)) { matchingGroup = redGroup; break; } if (redGroup.IsCatchAll) { catchAllGroup = redGroup; } } if (matchingGroup == null) { matchingGroup = catchAllGroup; } if (matchingGroup != null) { connection = new ClientConnection(newSocket, securityInfo, apciParameters, alParameters, this, matchingGroup.asduQueue, debugOutput); matchingGroup.AddConnection(connection); DebugLog("Add connection to group " + matchingGroup.Name); } else { DebugLog("Found no matching redundancy group -> close connection"); newSocket.Close(); } } else { connection = new ClientConnection(newSocket, securityInfo, apciParameters, alParameters, this, new ASDUQueue(maxQueueSize, enqueueMode, alParameters, DebugLog), debugOutput); } if (connection != null) { allOpenConnections.Add(connection); CallConnectionEventHandler(connection, ClientConnectionEvent.OPENED); } } else { newSocket.Close(); } } } catch (Exception) { running = false; } } }
/// <summary> /// Adds a redundancy group to the server. Each redundancy group has its own event queue. /// </summary> /// <param name="redundancyGroup">Redundancy group.</param> public void AddRedundancyGroup(RedundancyGroup redundancyGroup) { redGroups.Add(redundancyGroup); }