Esempio n. 1
0
 internal void _Join(Peer peer, IBootable bootable)
 {
     lock(_Peers)
         _Peers.Add(peer);
     peer.DisconnectEvent += () => { _Leave(peer, peer); };
     bootable.Launch();
 }
Esempio n. 2
0
 internal void _Leave(Peer peer, IBootable bootable)
 {
     lock(_Peers)
     {
         if (_Peers.Remove(peer))
         {
             bootable.Shutdown();
         }
         else
         {
             throw new Exception("no peer shutdown.");
         }
     }
 }
Esempio n. 3
0
        public void DoWork(object obj)
        {
            Singleton<Log>.Instance.WriteInfo("server socket launch");
            var are = (AutoResetEvent)obj;
            _Run = true;

            _Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _Socket.NoDelay = true;

            // _Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.ReuseAddress, true);
            _Socket.Bind(new IPEndPoint(IPAddress.Any, _Port));
            _Socket.Listen(5);
            _Socket.BeginAccept(_Accept, null);

            while(_Run)
            {
                if(_Sockets.Count > 0)
                {
                    lock(_Sockets)
                    {
                        while(_Sockets.Count > 0)
                        {
                            var socket = _Sockets.Dequeue();

                            Singleton<Log>.Instance.WriteInfo(
                                string.Format("socket accept Remote {0} Local {1} .", socket.RemoteEndPoint, socket.LocalEndPoint));
                            var peer = new Peer(socket);

                            _Peers.Join(peer);

                            _CoreHandler.Push(peer.Binder, peer.Handler);
                        }
                    }
                }

                _AutoPowerRegulator.Operate();
            }

            _Peers.Release();

            if(_Socket.Connected)
            {
                _Socket.Shutdown(SocketShutdown.Both);
            }

            _Socket.Close();

            are.Set();
            Singleton<Log>.Instance.WriteInfo("server socket shutdown");
        }
Esempio n. 4
0
 internal void Join(Peer peer)
 {
     _Join(peer, peer);
 }
Esempio n. 5
0
        public void DoWork()
        {
            _Run = true;

            Regulus.Utility.TimeCounter coreTimeCounter = new Utility.TimeCounter();
            Regulus.Utility.TimeCounter peerTimeCounter = new Utility.TimeCounter();

            _Socket.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Any, _Port));
            _Socket.Listen((int)System.Net.Sockets.SocketOptionName.MaxConnections);
            //_Socket.Listen(5);
            _Socket.BeginAccept(_Accept, null);
            _Core.Launch();

            while (_Run)
            {
                while (_Sockets.Count > 0)
                {
                    lock (_Sockets)
                    {
                        var socket = _Sockets.Dequeue();
                        var peer = new Peer(socket);
                        _Peers.Add(peer);
                        _Core.ObtainController(peer.Binder);
                    }
                }
                peerTimeCounter.Reset();
                _Peers.Update();
                PeerTimeCounter = peerTimeCounter.Second;

                coreTimeCounter.Reset();
                _Core.Update();
                CoreTimeCounter = coreTimeCounter.Second;

                _FPS.Update();
                System.Threading.Thread.Sleep(0);
            }

            _Core.Shutdown();
        }