sources/interface.cpp

changeset 186
9330b93d9946
parent 185
e83ec58cc458
child 187
53f9c7b2c068
equal deleted inserted replaced
185:e83ec58cc458 186:9330b93d9946
130 130
131 switch (newstate) 131 switch (newstate)
132 { 132 {
133 case INPUTSTATE_ADDRESS: 133 case INPUTSTATE_ADDRESS:
134 if (m_remoteAddress.host != 0) 134 if (m_remoteAddress.host != 0)
135 getEditableInput() = m_remoteAddress.to_string(IPAddress::WITH_PORT); 135 getEditableInput() = net::ip_address_to_string(m_remoteAddress);
136 break; 136 break;
137 137
138 default: 138 default:
139 break; 139 break;
140 } 140 }
486 text = "Disconnected."; 486 text = "Disconnected.";
487 break; 487 break;
488 488
489 case RCON_CONNECTING: 489 case RCON_CONNECTING:
490 case RCON_AUTHENTICATING: 490 case RCON_AUTHENTICATING:
491 text = "Connecting to " + m_session.address().to_string(IPAddress::WITH_PORT) + "..."; 491 text = "Connecting to " + net::ip_address_to_string(m_session.address()) + "...";
492 break; 492 break;
493 493
494 case RCON_CONNECTED: 494 case RCON_CONNECTED:
495 { 495 {
496 std::string adminText; 496 std::string adminText;
504 adminText = zfc::sprintf("%d other admin%s", m_session.getAdminCount(), 504 adminText = zfc::sprintf("%d other admin%s", m_session.getAdminCount(),
505 m_session.getAdminCount() != 1 ? "s" : ""); 505 m_session.getAdminCount() != 1 ? "s" : "");
506 } 506 }
507 507
508 text = zfc::sprintf("%s | %s | %s", 508 text = zfc::sprintf("%s | %s | %s",
509 m_session.address().to_string(IPAddress::WITH_PORT).data(), 509 net::ip_address_to_string(m_session.address()).data(),
510 m_session.getLevel().data(), 510 m_session.getLevel().data(),
511 adminText.data()); 511 adminText.data());
512 } 512 }
513 break; 513 break;
514 } 514 }
602 602
603 std::string& input = getEditableInput(); 603 std::string& input = getEditableInput();
604 m_pasteBuffer = mid(input, a, b); 604 m_pasteBuffer = mid(input, a, b);
605 input = remove_range(input, a, b - a); 605 input = remove_range(input, a, b - a);
606 m_needInputRender = true; 606 m_needInputRender = true;
607 }
608
609 bool Interface::tryResolveAddress(const std::string &address_string, net::ip_address* target)
610 {
611 std::stringstream errors;
612 const std::optional<net::ip_address> address_opt = net::ip_resolve(address_string, errors);
613 if (address_opt.has_value())
614 {
615 *target = address_opt.value();
616 if (target->port == 0)
617 {
618 target->port = 10666;
619 }
620 return true;
621 }
622 else
623 {
624 this->printError("%s\n", errors.str().data());
625 return false;
626 }
607 } 627 }
608 628
609 // ------------------------------------------------------------------------------------------------- 629 // -------------------------------------------------------------------------------------------------
610 // 630 //
611 void Interface::handleInput() 631 void Interface::handleInput()
792 { 812 {
793 case INPUTSTATE_CONFIRM_DISCONNECTION: 813 case INPUTSTATE_CONFIRM_DISCONNECTION:
794 break; // handled above 814 break; // handled above
795 815
796 case INPUTSTATE_ADDRESS: 816 case INPUTSTATE_ADDRESS:
797 try 817 if (this->tryResolveAddress(this->getCurrentInput(), &this->m_remoteAddress))
798 { 818 {
799 m_remoteAddress = IPAddress::from_string(getCurrentInput()); 819 setInputState(INPUTSTATE_PASSWORD);
800 } 820 }
801 catch (std::exception& e)
802 {
803 print("%s\n", e.what());
804 return;
805 }
806
807 if (m_remoteAddress.port == 0)
808 m_remoteAddress.port = 10666;
809
810 setInputState(INPUTSTATE_PASSWORD);
811 break; 821 break;
812 822
813 case INPUTSTATE_PASSWORD: 823 case INPUTSTATE_PASSWORD:
814 if (m_inputState == INPUTSTATE_PASSWORD and not getCurrentInput().empty()) 824 if (m_inputState == INPUTSTATE_PASSWORD and not getCurrentInput().empty())
815 { 825 {
994 m_needOutputRender = true; 1004 m_needOutputRender = true;
995 } 1005 }
996 1006
997 // ------------------------------------------------------------------------------------------------- 1007 // -------------------------------------------------------------------------------------------------
998 // 1008 //
999 void Interface::connect(std::string address, std::string password) 1009 void Interface::connect(std::string address_string, std::string password)
1000 { 1010 {
1001 try 1011 if (this->tryResolveAddress(address_string, &this->m_remoteAddress))
1002 { 1012 {
1003 m_remoteAddress = IPAddress::from_string(address); 1013 m_session.disconnect();
1004 } 1014 m_session.setPassword(password);
1005 catch (std::exception& e) 1015 m_session.connect(m_remoteAddress);
1006 { 1016 }
1007 print("%s\n", e.what());
1008 return;
1009 }
1010
1011 if (m_remoteAddress.port == 0)
1012 m_remoteAddress.port = 10666;
1013
1014 m_session.disconnect();
1015 m_session.setPassword(password);
1016 m_session.connect(m_remoteAddress);
1017 } 1017 }
1018 1018
1019 // ------------------------------------------------------------------------------------------------- 1019 // -------------------------------------------------------------------------------------------------
1020 // 1020 //
1021 void Interface::setPlayerNames(const std::vector<std::string>& names) 1021 void Interface::setPlayerNames(const std::vector<std::string>& names)
1067 { 1067 {
1068 printError("Usage: /connect <address> <password>\n"); 1068 printError("Usage: /connect <address> <password>\n");
1069 } 1069 }
1070 else 1070 else
1071 { 1071 {
1072 IPAddress address; 1072 this->connect(args[0], args[1]);
1073
1074 try
1075 {
1076 address = IPAddress::from_string(args[0]);
1077 }
1078 catch (std::exception& e)
1079 {
1080 printError("%s\n", e.what());
1081 return;
1082 }
1083
1084 if (address.port == 0)
1085 address.port = 10666;
1086
1087 m_session.setPassword(args[1]);
1088 m_session.disconnect();
1089 m_session.connect(m_remoteAddress = address);
1090 } 1073 }
1091 } 1074 }
1092 else if (command == "disconnect") 1075 else if (command == "disconnect")
1093 { 1076 {
1094 m_session.disconnect(); 1077 m_session.disconnect();
1105 1088
1106 // ------------------------------------------------------------------------------------------------- 1089 // -------------------------------------------------------------------------------------------------
1107 // 1090 //
1108 void Interface::disconnected() 1091 void Interface::disconnected()
1109 { 1092 {
1110 print("Disconnected from %s\n", m_session.address().to_string(IPAddress::WITH_PORT).data()); 1093 print("Disconnected from %s\n", net::ip_address_to_string(m_session.address()).data());
1111 resetTitle(); 1094 resetTitle();
1112 renderFull(); 1095 renderFull();
1113 } 1096 }
1114 1097
1115 // ------------------------------------------------------------------------------------------------- 1098 // -------------------------------------------------------------------------------------------------

mercurial