Ejemplo n.º 1
0
 public static ServerInfo Create()
 {
     var s = new ServerInfo();
     s.Name = "<name>";
     s.Ip = "<ip>";
     s.IsNew = true;
     return s;
 }
Ejemplo n.º 2
0
 public static ServerInfo Local(int port, string password)
 {
     var s = new ServerInfo();
     s.Name = "Local";
     s.Ip = "127.0.0.1";
     s.Port = port;
     s.Password = password;
     return s;
 }
Ejemplo n.º 3
0
        public void Initialize(int port, string password, int adminId, long ssid)
        {
            this.Stop();

            // Clear the message handling queue
            _messageQueue.Clear();
            this.OnMessageQueueUpdated();

            // Find existing state
            var existing = LoadState(ssid);
            
            _state = existing ?? new SyncState(ssid);

            _password = password;
            _adminId = adminId;
            _subSessionId = ssid;

            // Start new server
            _server = new WebSocketServer(port, IPAddress.Any)
            {
                OnReceive = OnReceive,
                OnConnected = OnConnected,
                OnDisconnect = OnDisconnect,
                TimeOut = new TimeSpan(0, 5, 0)
            };

            _server.Start();
            this.Status = ServerStatus.Active;

            _localServerInfo = ServerInfo.Local(port, password);

            // Start message queue handling thread
            _messageThread = new Thread(HandleMessageQueue);
            _messageThread.IsBackground = false;
            _messageThread.Start();

            // Start ping loop
            _pingThread = new Thread(PingLoop);
            _pingThread.IsBackground = true;
            _pingThread.Start();

            // Start web-update thread
            _webThread = new Thread(WebUpdateLoop);
            _webThread.IsBackground = true;
            _webThread.Start();
        }
Ejemplo n.º 4
0
        public void Stop()
        {
            // Stop server
            if (_server != null)
            {
                // Save state
                this.SaveState();

                // TODO: Disconnect everyone
                this.BroadcastDisconnect();
                foreach (var conn in _connections)
                {
                    if (conn.IsRegistered)
                    {
                        conn.User.IsConnected = false;
                        conn.User.IsRegistered = false;
                    }
                }
                foreach (var conn in _connections)
                {
                    conn.User = null;
                }
                _connections.Clear();

                _server.Stop();
                _server.Dispose();
            }
            _server = null;
            this.Status = ServerStatus.Stopped;

            _localServerInfo = null;

            _messageQueue.Clear();
            this.OnMessageQueueUpdated();

            // Stop threads
            if (_messageThread != null)
            {
                _messageThread.Abort();
                _messageThread = null;
            }

            if (_pingThread != null)
            {
                _pingThread.Abort();
                _pingThread = null;
            }

            if (_webThread != null)
            {
                _webThread.Abort();
                _webThread = null;
            }
        }
Ejemplo n.º 5
0
        public void Stop()
        {
            // Stop server
            if (_server != null)
            {
                // Save state
                this.SaveState();

                // TODO: Disconnect everyone
                foreach (var conn in _connections)
                {
                    if (conn.IsRegistered)
                    {
                        conn.User.IsConnected = false;
                        conn.User.IsRegistered = false;
                    }
                }
                this.BroadcastUserlist();
                foreach (var conn in _connections)
                {
                    conn.User = null;
                }
                _connections.Clear();

                _server.Stop();
                _server.Dispose();
            }
            _server = null;
            this.Status = ServerStatus.Stopped;

            _localServerInfo = null;

            // Stop ping loop
            if (_pingThread != null)
            {
                _pingThread.Abort();
                _pingThread = null;
            }
        }