Example #1
0
        void GlobalServerManagerThread()
        {
            while (true)
            {
                try
                {
                    if (_gc == null || !_gc.Connected)
                    {
                        _gc = new GlobalClient();
                        _gc.OnAccountInfoResponse += OnAccountInfoResponse;
                        _gc.OnCurrencyUpdate      += OnCurrencyUpdate;
                        _gc.OnGlobalProductInfo   += OnGlobalProductInfo;

                        _gc.Connect(_address, _port);
                        if (_gc.Connected)
                        {
                            LogThread.Log("Connected to global server ", LogThread.LogMessageType.Normal, true);
                        }
                    }
                    else
                    {
                        _gc.Update();
                    }
                }
                catch (Exception ex)
                {
                    LogThread.Log(ex.ToString(), LogThread.LogMessageType.Error, true);
                }

                Thread.Sleep(1000);
            }
        }
Example #2
0
        public void Process()
        {
            _processing = true;
            while (_processing)
            {
                try
                {
                    if (_tasks.Count > 0)
                    {
                        // Grab the first task
                        _tasksLock.WaitOne();
                        Task t = _tasks[0];
                        _tasks.RemoveAt(0);
                        _tasksLock.ReleaseMutex();

                        // Execute the task
                        ProcessTask(t);
                    }
                }
                catch (Exception ex)
                {
                    LogThread.Log(ex.ToString(), LogThread.LogMessageType.Error, false);
                }

                Thread.Sleep(10);
            }
        }
Example #3
0
        void DatabaseThreadFunc()
        {
            while (true)
            {
                ValidateConnection();

                if (_sql != null && _sql.State == ConnectionState.Open && _queries.Count > 0)
                {
                    _queriesLock.WaitOne();
                    DBQuery query = _queries[0];
                    _queries.RemoveAt(0);
                    _queriesLock.ReleaseMutex();

                    try
                    {
                        ExecuteQuery(query);
                    }
                    catch (Exception ex)
                    {
                        LogThread.Log(ex.ToString(), LogThread.LogMessageType.Error, true);

                        // When the query fails, its likely due to database disconnection. Reschedule this query to be done again.
                        AddQuery(query);
                    }
                }

                Thread.Sleep(100);
            }
        }
Example #4
0
        void ExecuteQuery(DBQuery q)
        {
            LogThread.Log(string.Format("ExecuteQuery - {0}", q.SqlString), LogThread.LogMessageType.Debug);
            MySqlCommand cmd = new MySqlCommand(q.SqlString, _sql);

            if (q.Read)
            {
                MySqlDataReader reader = cmd.ExecuteReader();
                List <object[]> rows   = new List <object[]>();
                while (reader.Read())
                {
                    object[] row = new object[reader.FieldCount];
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        row[i] = reader[i];
                    }
                    rows.Add(row);
                }
                reader.Close();
                q.Rows = rows;
            }
            else
            {
                cmd.ExecuteNonQuery();
            }

            OnQueryComplete(q, null);
        }
Example #5
0
        void ValidateConnection()
        {
            if (_sql == null)
            {
                try
                {
                    _sql = new MySqlConnection();
                    _sql.ConnectionString = _dbConnectString;
                    _sql.Open();

                    Thread.Sleep(10);

                    while (_sql.State == ConnectionState.Connecting)
                    {
                        Thread.Sleep(100);
                    }

                    LogThread.Log("Connected to database: " + _dbConnectString);
                }
                catch (Exception ex)
                {
                    LogThread.Log(ex.ToString(), LogThread.LogMessageType.Error, true);
                    _sql.Close();
                    _sql = null;
                }
            }
            else
            {
                if (_sql.State == ConnectionState.Closed || _sql.State == ConnectionState.Broken)
                {
                    _sql.Close();
                    _sql = null;
                }
            }
        }
Example #6
0
        public ServerBase(int listenPort, string dbConnectString)
        {
            // Start log thread
            LogThread log = new LogThread();

            // Start database thread
            if( dbConnectString != null )
                _db = new DatabaseThread(dbConnectString);

            // Start listen thread
            _lt = new ListenThread(listenPort);

            // Start input thread
            _inputThread = new InputThread();
        }
Example #7
0
        public AuthAccountInfo FindAccount(string authString)
        {
            string ascii = AuthStringToAscii(authString);

            LogThread.Log("Checking auth cache for: " + ascii, NetworkCore.LogInterface.LogMessageType.Normal);
            if (_accounts.ContainsKey(authString))
            {
                AuthAccountInfo aai = _accounts[authString];
                aai.Timestamp = DateTime.Now.Ticks;
                LogThread.Log("Authstring found for account: " + aai.AccountID, NetworkCore.LogInterface.LogMessageType.Normal);
                return(aai);
            }

            LogThread.Log(string.Format("Authstring {0} not found in the local cache", ascii), NetworkCore.LogInterface.LogMessageType.System);
            return(null);
        }
Example #8
0
        protected void SendPacket()
        {
            if (_outgoingPacket == null)
            {
                throw new InvalidOperationException("Can not send packet that has not been started!");
            }

            byte[] data = _outgoingPacket.ToArray();
            _socket.Send(data);

            _lastSent = DateTime.Now;
            LogThread.Log(string.Format("SendPacket {0} bytes", data.Length), LogThread.LogMessageType.Debug);

            _outgoingBW.Close();
            _outgoingBW     = null;
            _outgoingPacket = null;
        }
Example #9
0
        void ProcessPacketData(byte[] data)
        {
            // Find the packet marker
            int packetStart = 0;

            while (packetStart < data.Length - 4)
            {
                if (data[packetStart] == 'U' && data[packetStart + 1] == 'G' && data[packetStart + 2] == 'G' && data[packetStart + 3] == '$')
                {
                    break;
                }
            }
            if (packetStart > 0)
            {
                LogThread.Log(string.Format("Connection threw away {0} bytes before packet marker", packetStart), LogThread.LogMessageType.Debug);
            }

            MemoryStream ms = new MemoryStream(data);

            ms.Seek(packetStart, SeekOrigin.Begin);
            BinaryReader br = new BinaryReader(ms);

            int    marker     = br.ReadInt32();
            ushort packetType = br.ReadUInt16();

            LogThread.Log(string.Format("Processing packet type {0}", packetType), LogThread.LogMessageType.Debug);

            if (_packetHandlers.ContainsKey(packetType))
            {
                _packetHandlers[packetType](br);

                int bytesProcessed = (int)ms.Position;
                if (bytesProcessed < data.Length)
                {
                    int    remaining     = data.Length - bytesProcessed;
                    byte[] remainingData = new byte[remaining];
                    Buffer.BlockCopy(data, bytesProcessed, remainingData, 0, remaining);
                    _pendingData = new List <byte>();
                    _pendingData.AddRange(remainingData);
                }
            }
            else
            {
                LogThread.Log(string.Format("Unhandled packet type {0}", packetType), LogThread.LogMessageType.Error, true);
            }
        }
Example #10
0
        public ServerBase(int listenPort, string dbConnectString)
        {
            // Start log thread
            LogThread log = new LogThread();

            // Start database thread
            if (dbConnectString != null)
            {
                _db = new DatabaseThread(dbConnectString);
            }

            // Start listen thread
            _lt = new ListenThread(listenPort);

            // Start input thread
            _inputThread = new InputThread();
        }
Example #11
0
        public void Connect(string address, int port)
        {
            if (_socket != null)
            {
                throw new InvalidOperationException("Cant connect when a socket already exists");
            }

            try
            {
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _socket.Connect(address, port);
            }
            catch (Exception ex)
            {
                LogThread.Log(string.Format("Failed to connect to {0}:{1}\n{2}", address, port, ex.ToString()), LogThread.LogMessageType.Error, true);
                _socket = null;
            }
        }
Example #12
0
        void InputThreadFunc()
        {
            while (true)
            {
                try
                {
                    _connectionsLock.WaitOne();
                    Connection[] connections = _connections.ToArray();
                    _connectionsLock.ReleaseMutex();

                    //if( connections.Length > 1 )
                    //    Console.WriteLine("{0} clients on the server", connections.Length - 1);

                    List <Connection> removeList = new List <Connection>();
                    foreach (Connection c in connections)
                    {
                        c.Update();
                        if (c.Status == Connection.ConnStatus.Closed || c.Status == Connection.ConnStatus.Disconnected)
                        {
                            removeList.Add(c);
                        }
                    }

                    _connectionsLock.WaitOne();
                    foreach (Connection c in removeList)
                    {
                        _connections.Remove(c);
                    }
                    _connectionsLock.ReleaseMutex();
                }
                catch (Exception ex)
                {
                    LogThread.Log(ex.ToString(), LogThread.LogMessageType.Error, true);
                }

                Thread.Sleep(10);
            }
        }
Example #13
0
 protected void BeginPacket(PacketType type)
 {
     LogThread.Log(string.Format("BeginPacket({0})", type), LogThread.LogMessageType.Debug);
     BeginPacket((ushort)type);
 }
Example #14
0
 void ProcessTask(Task t)
 {
     // Call the task handler, this will throw an exception if the handler isnt registered.
     LogThread.Log(string.Format("ProcessTask({0}) -> {1}", t.Type, _taskHandlers[t.Type].Method.Name), LogThread.LogMessageType.Debug);
     _taskHandlers[t.Type](t);
 }
Example #15
0
 void BeginPacket(GPacketType gt)
 {
     LogThread.Log(string.Format("BeginPacket({0})", gt), LogThread.LogMessageType.Debug);
     BeginPacket((ushort)gt);
 }