Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="randomPort"></param>
        public Server(bool randomPort)
        {
            this.SocketContainer = new SocketContainer();
            setHostName(this.SocketContainer);

            if (randomPort)
            {
                bool foundOpenPort = false;
                this.SocketContainer.ListeningPort = PortNumberGenerator.getRandomPort();
                while (!foundOpenPort)
                {
                    try
                    {
                        Client c = new Client();
                        c.connect(this.SocketContainer.HostName, this.SocketContainer.ListeningPort);
                        c.disconnect();
                        this.SocketContainer.ListeningPort = PortNumberGenerator.getRandomPort();
                    }
                    catch (Exception)
                    {
                        foundOpenPort = true;
                    }
                }
            }
            else
            {
                this.SocketContainer.ListeningPort = 2702;                                                                         // use as default
                Int32 listeningPort = 2702;                                                                                        // default
                Int32.TryParse(ConfigurationManager.AppSettings[config.AppConfigSettingsConstants.ServerPort], out listeningPort); // if this fails, default is fine
                this.SocketContainer.ListeningPort = listeningPort;
            }
        }
Exemple #2
0
        // public Dictionary<String, Object> SharedData { get; set; }

        public Server()
        {
            this.SocketContainer = new SocketContainer();
            setHostName(this.SocketContainer);
            Int32 listeningPort = 2702;                                                                                        // default

            Int32.TryParse(ConfigurationManager.AppSettings[config.AppConfigSettingsConstants.ServerPort], out listeningPort); // if this fails, default is fine
            this.SocketContainer.ListeningPort = listeningPort;
        }
Exemple #3
0
        void setHostName(SocketContainer srv)
        {
            String configHostName = ConfigurationManager.AppSettings[config.AppConfigSettingsConstants.ServerHostName];

            if (!String.IsNullOrEmpty(configHostName))
            {
                srv.HostName = configHostName;
            }
            else
            {
                srv.HostName = IPv4Helper.getIPv4Address().ToString(); // want IP address for hostname
            }
        }
Exemple #4
0
        void acceptCallback(IAsyncResult iar)
        {
            SocketContainer srv = (SocketContainer)iar.AsyncState;

            if (!srv.Listening)
            {
                return;
            }
            Socket worker = null;

            try
            {
                // free up the main socket to accept requests immediately and assign socket to worker
                worker = srv.MainSocket.EndAccept(iar);
                // place main socket back in listening state for more connections
                this.SocketContainer.MainSocket.BeginAccept(new AsyncCallback(acceptCallback), this.SocketContainer);
            }
            catch
            {
                try
                {
                    // wait and try again
                    Thread.Sleep(100);
                    // free up the main socket to accept requests immediately and assign socket to worker
                    worker = this.SocketContainer.MainSocket.EndAccept(iar);
                    // place main socket back in listening state for more connections
                    this.SocketContainer.MainSocket.BeginAccept(new AsyncCallback(acceptCallback), this.SocketContainer);
                }
                catch { return; } // TODO: add thread safe logging to the reqhandle and log the beginaccept disposal
            }
            // copy reference to service state on our threadsafestateobject so shutdown can still be signaled
            ThreadSafeStateObject state = new ThreadSafeStateObject(worker, new byte[ThreadSafeStateObject.BufferSize])
            {
                ServiceState = srv.ServiceState
            };

            srv.AllDone.Set();
            worker.BeginReceive(state.Buffer, 0, ThreadSafeStateObject.BufferSize, SocketFlags.None, new AsyncCallback(dataReceivedCallback), state);
        }
Exemple #5
0
 public Server(String hostName, Int32 listeningPort)
 {
     this.SocketContainer               = new SocketContainer();
     this.SocketContainer.HostName      = hostName;
     this.SocketContainer.ListeningPort = listeningPort;
 }