Exemplo n.º 1
0
            public int GetAvailablePort()
            {
                int portsCount = 0;

                lock (this._availablePortsList)
                {
                    portsCount = this._availablePortsList.Count;
                }

                if (portsCount > 0)
                {
                    int portToReturn = 0;
                    portToReturn = this.DequeueNextAvailablePort();
                    if (portsCount <= AVAILABLE_PORTS_QUEUE_MIN_SIZE)
                    {
                        this.SchedulePortsSearchingTask();
                    }
                    return(portToReturn);
                }
                else
                {
                    this._LastAssignedPortNumber = CommunicationsUtilities.GetFreeTCPPortOnPortsRange(this._portsRangeLowerLimit, this._portsRangeUpperLimit);
                    return(this._LastAssignedPortNumber);
                }
            }
Exemplo n.º 2
0
            private int DequeueNextAvailablePort()
            {
                bool gotPort = false;
                int  currentPortFromqueue = 0;

                while (!gotPort)
                {
                    if (this._availablePortsList.Count > 0)
                    {
                        currentPortFromqueue = System.Convert.ToInt32(this._availablePortsList.GetByIndex(0));

                        lock (this._availablePortsList)
                        {
                            this._availablePortsList.RemoveAt(0);
                        }

                        System.Net.Sockets.TcpListener listener = null;
                        string host = System.Net.Dns.GetHostName();
                        System.Net.IPAddress local_IPAddress = CommunicationsLibrary.Utilities.CommunicationsUtilities.GetActiveIPAddress();
                        listener = new System.Net.Sockets.TcpListener(local_IPAddress, currentPortFromqueue);
                        try
                        {
                            listener.Start();
                            listener.Stop();
                            this._LastAssignedPortNumber = currentPortFromqueue;
                            listener = null;
                            gotPort  = true;
                        }
                        catch (System.Net.Sockets.SocketException ex)
                        {
                            switch (ex.ErrorCode)
                            {
                            case 10048:                                     //the port is opened by another process
                                try
                                {
                                    listener.Stop();
                                }
                                catch (Exception)
                                {
                                }
                                break;

                            case 10055:                                     //there is no more space to allocate another port in the system, no more resources available
                                string msg = "Error finding an open port : " + ex.Message;
                                throw (new Exception(msg));
                            }
                        }
                        catch (Exception)
                        {
                            try
                            {
                                listener.Stop();
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                    else
                    {
                        this._LastAssignedPortNumber = CommunicationsUtilities.GetFreeTCPPortOnPortsRange(this._portsRangeLowerLimit, this._portsRangeUpperLimit);
                    }
                }

                return(this._LastAssignedPortNumber);
            }