All pastes #2128444 Raw Edit

Anonymous

public text v1 · immutable
#2128444 ·published 2012-03-15 18:14 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;

using InfServer.Network;
using InfServer.Protocol;
using InfServer.Data;
using DirectoryServer;
using InfServer;

namespace DirectoryServer
{
	// Database Class
	/// Used to maintain a connection with the database server
	///////////////////////////////////////////////////////u
	public class Database : IClient
	{	// Member variables
		///////////////////////////////////////////////////
        private LogClient _logger;				//Our log client for database related activities
		private ClientConn<Database> _conn;		//Our UDP connection client

		public ManualResetEvent _syncStart;		//Used for blocking connect attempts
		public bool _bLoginSuccess;				//Were we able to successfully login?

		public bool bActive
		{
			get
			{
				return _bLoginSuccess;
			}
		}


		///////////////////////////////////////////////////
		// Member Functions
		///////////////////////////////////////////////////
		/// <summary>
		/// Generic constructor
		/// </summary>
        public Database(DirectoryServer server)
		{
			_conn = new ClientConn<Database>(new S2CPacketFactory<Database>(), this);
			_syncStart = new ManualResetEvent(false);
            _logger = Log.createClient("Database");
            _conn._logger = _logger;
            Client.udpMaxSize = 496;
            Client.crcLength = 1;
		}

		#region Connection
		/// <summary>
		/// Attempts to create and initialize a connection to the DB server
		/// </summary>
        public bool connect(IPEndPoint dbPoint, bool bBlock)
        {	//Assume the worst
            _syncStart.Reset();
            _bLoginSuccess = false;
            int attemptsLeft = 1;

            using (LogAssume.Assume(_logger))
            {
                //Are we connecting at all?
                if (attemptsLeft == 0)
                {
                    Log.write(TLog.Warning, "Skipping database server connection..");
                    return false;
                }

                do
                {	//Let's go
                    Log.write("Connecting to Database");
                    //Keep trying to connect until we get a reaction.
                    do
                    {
                        if (attemptsLeft-- == 0)
                        {
                            Log.write(TLog.Warning, "Attempt to connect to the database server timed out.");
                            return false;
                        }

                        //Start our connection
                        _conn.begin(dbPoint);

                        //Send our initial packet
                        CS_Initial init = new CS_Initial();

                        _conn._client._connectionID = init.connectionID = new Random().Next();
                        init.CRCLength = Client.crcLength;
                        init.udpMaxPacket = Client.udpMaxSize;

                        _conn._client.send(init);


                        Log.write(init.Dump.ToString());
                    }
                    while (!_syncStart.WaitOne(3000));

                    //Reset our event
                    _syncStart.Reset();

                    //Wait for our login result
                } while (!_syncStart.WaitOne(10000));

                //Reset our event
                _syncStart.Reset();

                //Were we successful?
                return _bLoginSuccess;
            }
        }