コード例 #1
0
        public bool connectServer(ServerInfo server, EventHandler<MessageEvent> messageLog)
        {
            if (messageLog != null)
                messageLog(this, new MessageEvent("Attempting to connect server " + server.ToString()));

            bool gotLock = Monitor.TryEnter(lockObj, 1000);
            try
            {
                if (!gotLock)
                {
                    if (messageLog != null)
                        messageLog(this, new MessageEvent("Unable to obtain lock on servermanager. Aborting attempt."));
                    return false;
                }

                // Cannot connect to a server that isn't is the servers list.
                if (!servers.Contains(server))
                {
                    if (messageLog != null)
                        messageLog(this, new MessageEvent(server.ToString() + " is not on list of known servers. Aborting."));
                    return false;
                }

                // ensure that this server has a server connection status item
                if (!connections.ContainsKey(server))
                {
                    connections.Add(server, ConnectionStatus.Disconnected_Normal);
                }

                // if this server is disabled, then don't connect to it.
                if (!server.ServerEnabled)
                {
                    if (messageLog != null)
                        messageLog(this, new MessageEvent(server.ToString() + " is not enabled. Aborting."));
                    return false;
                }

                // set server status to connecting. If the server already was connecting, then return.
                if (connections[server] == ConnectionStatus.Connecting)
                {
                    if (messageLog != null)
                        messageLog(this, new MessageEvent(server.ToString() + " is already connecting. Aborting."));
                    return false;
                }

                connections[server] = ConnectionStatus.Connecting;

                // create an empty spot for the server communicator
                if (!communicators.ContainsKey(server))
                {
                    communicators.Add(server, null);
                }

                communicators[server] = null;

                // try to create the communicator
                try
                {

                    Thread connectThread = new Thread(new ParameterizedThreadStart(server_connect_proc));
                    connectThread.Start(server);

                    connectThread.Join(1000);
                    if (connectThread.ThreadState == ThreadState.Running)
                    {
                        connectThread.Abort();
                        if (messageLog != null)
                            messageLog(this, new MessageEvent("Connection took longer than 1000 ms, aborting."));

                        connectThread.Abort();
                        return false;
                    }

                }
                catch (Exception e)
                {
                    if (messageLog != null)
                        messageLog(this, new MessageEvent("Caught exception when attempting to connect to server " + server.ToString() + ":" + e.Message + e.StackTrace));
                    connections[server] = ConnectionStatus.Unable_To_Connect;
                    return false;
                }

                try
                {

                    if (pingServer(server, messageLog))
                    {
                        server.setServerName(communicators[server].getServerName());

                        if (connectedServerNames.Contains(server.ServerName))
                        {
                            connections[server] = ConnectionStatus.Error_Name_Not_Unique;
                            communicators.Remove(server);
                            return false;
                        }

                        connectedServerNames.Add(server.ServerName);
                        connections[server] = ConnectionStatus.Connected;
                        messageLog(this, new MessageEvent("Server connected successfully."));
                        return true;
                    }
                    else
                    {
                        if (messageLog != null)
                            messageLog(this, new MessageEvent("Server ping failed. Aborting."));
                        connections[server] = ConnectionStatus.Unable_To_Connect;
                        return false;
                    }
                }
                catch (Exception e)
                {
                    messageLog(this, new MessageEvent("Exception when attempting to ping server: " + e.Message + e.StackTrace));
                    connections[server] = ConnectionStatus.Unable_To_Connect;
                    return false;
                }
            }
            finally
            {
                if (gotLock)
                    Monitor.Exit(lockObj);
            }
            return true;
        }
コード例 #2
0
        public bool connectServer(ServerInfo server, EventHandler <MessageEvent> messageLog)
        {
            if (messageLog != null)
            {
                messageLog(this, new MessageEvent("Attempting to connect server " + server.ToString()));
            }

            bool gotLock = Monitor.TryEnter(lockObj, 1000);

            try
            {
                if (!gotLock)
                {
                    if (messageLog != null)
                    {
                        messageLog(this, new MessageEvent("Unable to obtain lock on servermanager. Aborting attempt."));
                    }
                    return(false);
                }

                // Cannot connect to a server that isn't is the servers list.
                if (!servers.Contains(server))
                {
                    if (messageLog != null)
                    {
                        messageLog(this, new MessageEvent(server.ToString() + " is not on list of known servers. Aborting."));
                    }
                    return(false);
                }

                // ensure that this server has a server connection status item
                if (!connections.ContainsKey(server))
                {
                    connections.Add(server, ConnectionStatus.Disconnected_Normal);
                }

                // if this server is disabled, then don't connect to it.
                if (!server.ServerEnabled)
                {
                    if (messageLog != null)
                    {
                        messageLog(this, new MessageEvent(server.ToString() + " is not enabled. Aborting."));
                    }
                    return(false);
                }

                // set server status to connecting. If the server already was connecting, then return.
                if (connections[server] == ConnectionStatus.Connecting)
                {
                    if (messageLog != null)
                    {
                        messageLog(this, new MessageEvent(server.ToString() + " is already connecting. Aborting."));
                    }
                    return(false);
                }

                connections[server] = ConnectionStatus.Connecting;


                // create an empty spot for the server communicator
                if (!communicators.ContainsKey(server))
                {
                    communicators.Add(server, null);
                }

                communicators[server] = null;


                // try to create the communicator
                try
                {
                    Thread connectThread = new Thread(new ParameterizedThreadStart(server_connect_proc));
                    connectThread.Start(server);

                    connectThread.Join(1000);
                    if (connectThread.ThreadState == ThreadState.Running)
                    {
                        connectThread.Abort();
                        if (messageLog != null)
                        {
                            messageLog(this, new MessageEvent("Connection took longer than 1000 ms, aborting."));
                        }

                        connectThread.Abort();
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    if (messageLog != null)
                    {
                        messageLog(this, new MessageEvent("Caught exception when attempting to connect to server " + server.ToString() + ":" + e.Message + e.StackTrace));
                    }
                    connections[server] = ConnectionStatus.Unable_To_Connect;
                    return(false);
                }

                try
                {
                    if (pingServer(server, messageLog))
                    {
                        server.setServerName(communicators[server].getServerName());

                        if (connectedServerNames.Contains(server.ServerName))
                        {
                            connections[server] = ConnectionStatus.Error_Name_Not_Unique;
                            communicators.Remove(server);
                            return(false);
                        }

                        connectedServerNames.Add(server.ServerName);
                        connections[server] = ConnectionStatus.Connected;
                        messageLog(this, new MessageEvent("Server connected successfully."));
                        return(true);
                    }
                    else
                    {
                        if (messageLog != null)
                        {
                            messageLog(this, new MessageEvent("Server ping failed. Aborting."));
                        }
                        connections[server] = ConnectionStatus.Unable_To_Connect;
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    messageLog(this, new MessageEvent("Exception when attempting to ping server: " + e.Message + e.StackTrace));
                    connections[server] = ConnectionStatus.Unable_To_Connect;
                    return(false);
                }
            }
            finally
            {
                if (gotLock)
                {
                    Monitor.Exit(lockObj);
                }
            }
            return(true);
        }