mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-15 10:35:11 -04:00
Move Invalid IP detection out of Program.c
This commit is contained in:
parent
629b44b038
commit
ed43827a4e
@ -923,10 +923,11 @@ cc_result Socket_GetError(cc_socket s, cc_result* result) {
|
||||
cc_result Socket_Connect(cc_socket s, const cc_string* ip, int port) {
|
||||
struct sockaddr addr;
|
||||
cc_result res;
|
||||
|
||||
addr.sa_family = AF_INET;
|
||||
|
||||
Stream_SetU16_BE( (cc_uint8*)&addr.sa_data[0], port);
|
||||
Utils_ParseIP(ip, (cc_uint8*)&addr.sa_data[2]);
|
||||
if (!Utils_ParseIP(ip, (cc_uint8*)&addr.sa_data[2]))
|
||||
return ERR_INVALID_ARGUMENT;
|
||||
|
||||
res = connect(s, &addr, sizeof(addr));
|
||||
return res == -1 ? Socket__Error() : 0;
|
||||
|
@ -124,10 +124,6 @@ static int Program_Run(int argc, char** argv) {
|
||||
String_Copy(&Game_Mppass, &args[1]);
|
||||
String_Copy(&Server.IP, &args[2]);
|
||||
|
||||
if (!Utils_ParseIP(&args[2], ip)) {
|
||||
WarnInvalidArg("Invalid IP", &args[2]);
|
||||
return 1;
|
||||
}
|
||||
if (!Convert_ParseUInt16(&args[3], &port)) {
|
||||
WarnInvalidArg("Invalid port", &args[3]);
|
||||
return 1;
|
||||
|
26
src/Server.c
26
src/Server.c
@ -23,6 +23,7 @@
|
||||
#include "Inventory.h"
|
||||
#include "Platform.h"
|
||||
#include "Input.h"
|
||||
#include "Errors.h"
|
||||
|
||||
static char nameBuffer[STRING_SIZE];
|
||||
static char motdBuffer[STRING_SIZE];
|
||||
@ -235,22 +236,26 @@ static void MPConnection_FinishConnect(void) {
|
||||
lastPacket = Game.Time;
|
||||
}
|
||||
|
||||
static void MPConnection_Fail(const cc_string* reason) {
|
||||
cc_string msg; char msgBuffer[STRING_SIZE * 2];
|
||||
String_InitArray(msg, msgBuffer);
|
||||
net_connecting = false;
|
||||
|
||||
String_Format2(&msg, "Failed to connect to %s:%i", &Server.IP, &Server.Port);
|
||||
Game_Disconnect(&msg, reason);
|
||||
OnClose();
|
||||
}
|
||||
|
||||
static void MPConnection_FailConnect(cc_result result) {
|
||||
static const cc_string reason = String_FromConst("You failed to connect to the server. It's probably down!");
|
||||
cc_string msg; char msgBuffer[STRING_SIZE * 2];
|
||||
|
||||
net_connecting = false;
|
||||
String_InitArray(msg, msgBuffer);
|
||||
|
||||
if (result) {
|
||||
String_Format3(&msg, "Error connecting to %s:%i: %i" _NL, &Server.IP, &Server.Port, &result);
|
||||
Logger_Log(&msg);
|
||||
msg.length = 0;
|
||||
}
|
||||
|
||||
String_Format2(&msg, "Failed to connect to %s:%i", &Server.IP, &Server.Port);
|
||||
Game_Disconnect(&msg, &reason);
|
||||
OnClose();
|
||||
MPConnection_Fail(&reason);
|
||||
}
|
||||
|
||||
static void MPConnection_TickConnect(void) {
|
||||
@ -294,9 +299,12 @@ static void MPConnection_BeginConnect(void) {
|
||||
Socket_SetBlocking(net_socket, false);
|
||||
net_connecting = true;
|
||||
net_connectTimeout = Game.Time + NET_TIMEOUT_SECS;
|
||||
|
||||
res = Socket_Connect(net_socket, &Server.IP, Server.Port);
|
||||
if (res && res != ReturnCode_SocketInProgess && res != ReturnCode_SocketWouldBlock) {
|
||||
|
||||
if (res == ERR_INVALID_ARGUMENT) {
|
||||
static const cc_string reason = String_FromConst("Invalid IP address");
|
||||
MPConnection_Fail(&reason);
|
||||
} else if (res && res != ReturnCode_SocketInProgess && res != ReturnCode_SocketWouldBlock) {
|
||||
MPConnection_FailConnect(res);
|
||||
} else {
|
||||
String_Format2(&title, "Connecting to %s:%i..", &Server.IP, &Server.Port);
|
||||
|
@ -318,7 +318,7 @@ void interop_OpenKeyboard(const char* text, int type, const char* placeholder) {
|
||||
}, text, type, placeholder);
|
||||
}
|
||||
|
||||
/* NOTE: When pressing 'Go' on the on-screen keyboard, web browser adds \n to value */
|
||||
/* NOTE: When pressing 'Go' on the on-screen keyboard, some web browsers add \n to value */
|
||||
void interop_SetKeyboardText(const char* text) {
|
||||
EM_ASM_({
|
||||
if (!window.cc_inputElem) return;
|
||||
@ -326,7 +326,7 @@ void interop_SetKeyboardText(const char* text) {
|
||||
var cur = window.cc_inputElem.value;
|
||||
|
||||
if (cur.length && cur[cur.length - 1] == '\n') { cur = cur.substring(0, cur.length - 1); }
|
||||
if (str != cur) window.cc_inputElem.value = str;
|
||||
if (str != cur) window.cc_inputElem.value = str;
|
||||
}, text);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user