示例#1
0
        public void ConnectToLoginServer(string server = null)
        {
            if (!string.IsNullOrEmpty(server))
            {
                Server = server;
            }

            if (string.IsNullOrEmpty(Server))
            {
                throw new ArgumentException("OwnerServer");
            }

            if (Session == null)
            {
                Session = new Session();
            }


            var addr = Dns.GetHostAddresses(Server);

            if (addr.Length == 0)
            {
                _logger.Error("Failed to resolve '{0}' to an ipaddress", Server);
                return;
            }


            _socket       = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            _socketReader = new SocketReader(Session, _socket);
            _socketWriter = new SocketWriter(Session, _socket);

            var ip = addr.First();

            _logger.Trace("resolved {0} to '{1}'", ip, Server);

            var endpoint = new IPEndPoint(ip, 44453);

            _socketReader.MaxMessageSize = 496;
            Session.Command = SessionCommand.Connect;

            _socket.Connect(endpoint);
            _socketReader.Start().WaitOne(Timeout);
            _socketWriter.Start().WaitOne(Timeout);


            var timeout = DateTime.Now.AddMilliseconds(Timeout);

            while (Session.Status != SessionStatus.Connected && Session.Status != SessionStatus.Error)
            {
                if (timeout <= DateTime.Now)
                {
                    throw new TimeoutException("Timeout waiting for server conenction");
                }
                Thread.Sleep(Timeout / 10);
            }
        }
示例#2
0
        public void EstablishConnection(uint userId, byte[] sessionKey, IPAddress address, int port)
        {
            ConnectedAddress = address;
            ConnectedPort    = port;
            Session          = new Session();
            _Socket          = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            _SocketReader    = new SocketReader(Session, _Socket);
            _SocketWriter    = new SocketWriter(Session, _Socket);
            Session.Command  = SessionCommand.Connect;

            _logger.Debug("Connecting to zone server at {0}:{1}", address, port);

            _Socket.Connect(address, port);
            _SocketReader.Start().WaitOne(ConnectTimeout);
            _SocketWriter.Start().WaitOne(ConnectTimeout);

            var timeout = DateTime.Now.Add(ConnectTimeout);

            while (Session.Status != SessionStatus.Connected && Session.Status != SessionStatus.Error)
            {
                if (timeout <= DateTime.Now)
                {
                    _logger.Error("Timeout waiting for connecting to establish");
                    throw new TimeoutException("Timeout waiting for server conenction");
                }

                Thread.Sleep(TimeSpan.FromMilliseconds(ConnectTimeout.TotalMilliseconds / 10));
            }

            if (!Start().WaitOne(ConnectTimeout))
            {
                _logger.Error("ObjectGraph service failed to start in time");
                throw new TimeoutException("Timeout waiting for server conenction");
            }


            var clientIDMsg = new ClientIDMessage(userId, sessionKey);

            clientIDMsg.AddFieldsToData();

            Session.SendChannelA(clientIDMsg);


            var messages =
                Messages.WaitForMessages(Convert.ToInt32(ConnectTimeout.TotalMilliseconds),
                                         MessageOp.ErrorMessage, MessageOp.ClientPermissionsMessage);

            if (messages == null)
            {
                _logger.Error("Timeout waiting for ClientPermissionsMessage");
                throw new TimeoutException("Timeout waiting for server connection");
            }

            var errorMsg =
                Message.Create <ErrorMessage>(
                    messages.FirstOrDefault(cur => cur.MessageOpCodeEnum == MessageOp.ErrorMessage));

            if (errorMsg != null)
            {
                _logger.Error("Error getting client permission: [{0}] {1}", errorMsg.ErrorType, errorMsg.Message);
                throw new Exception(string.Format("[{0}] {1}", errorMsg.ErrorType, errorMsg.Message));
            }

            var permissionMessage =
                Message.Create <ClientPermissionsMessage>(
                    messages.FirstOrDefault(cur => cur.MessageOpCodeEnum == MessageOp.ClientPermissionsMessage));

            if (permissionMessage == null)
            {
                _logger.Error("Unknow packet when expecting ClientPermissionsMessage");
                throw new Exception("Unknown error connecting to server");
            }

            SelectedGalaxyOpen         = permissionMessage.GalaxyOpenFlag == 1;
            CanCreateCharacterOnGalaxy = permissionMessage.CharacterSlotOpenFlag == 1;
        }