diff options
Diffstat (limited to 'drivers/unix')
-rw-r--r-- | drivers/unix/dir_access_unix.cpp | 144 | ||||
-rw-r--r-- | drivers/unix/dir_access_unix.h | 34 | ||||
-rw-r--r-- | drivers/unix/file_access_unix.cpp | 144 | ||||
-rw-r--r-- | drivers/unix/file_access_unix.h | 39 | ||||
-rw-r--r-- | drivers/unix/ip_unix.cpp | 113 | ||||
-rw-r--r-- | drivers/unix/ip_unix.h | 6 | ||||
-rw-r--r-- | drivers/unix/mutex_posix.cpp | 15 | ||||
-rw-r--r-- | drivers/unix/mutex_posix.h | 17 | ||||
-rw-r--r-- | drivers/unix/os_unix.cpp | 251 | ||||
-rw-r--r-- | drivers/unix/os_unix.h | 53 | ||||
-rw-r--r-- | drivers/unix/packet_peer_udp_posix.cpp | 118 | ||||
-rw-r--r-- | drivers/unix/packet_peer_udp_posix.h | 14 | ||||
-rw-r--r-- | drivers/unix/rw_lock_posix.cpp | 25 | ||||
-rw-r--r-- | drivers/unix/rw_lock_posix.h | 6 | ||||
-rw-r--r-- | drivers/unix/semaphore_posix.cpp | 22 | ||||
-rw-r--r-- | drivers/unix/semaphore_posix.h | 8 | ||||
-rw-r--r-- | drivers/unix/socket_helpers.h | 48 | ||||
-rw-r--r-- | drivers/unix/stream_peer_tcp_posix.cpp | 61 | ||||
-rw-r--r-- | drivers/unix/stream_peer_tcp_posix.h | 18 | ||||
-rw-r--r-- | drivers/unix/tcp_server_posix.cpp | 34 | ||||
-rw-r--r-- | drivers/unix/tcp_server_posix.h | 6 | ||||
-rw-r--r-- | drivers/unix/thread_posix.cpp | 67 | ||||
-rw-r--r-- | drivers/unix/thread_posix.h | 29 |
23 files changed, 570 insertions, 702 deletions
diff --git a/drivers/unix/dir_access_unix.cpp b/drivers/unix/dir_access_unix.cpp index a09cf80e6c..d3c7a42c70 100644 --- a/drivers/unix/dir_access_unix.cpp +++ b/drivers/unix/dir_access_unix.cpp @@ -34,20 +34,19 @@ #include <sys/statvfs.h> #endif -#include <stdio.h> #include "os/memory.h" #include "print_string.h" #include <errno.h> +#include <stdio.h> DirAccess *DirAccessUnix::create_fs() { - return memnew( DirAccessUnix ); + return memnew(DirAccessUnix); } Error DirAccessUnix::list_dir_begin() { - + list_dir_end(); //close any previous dir opening! - //char real_current_dir_name[2048]; //is this enough?! //getcwd(real_current_dir_name,2048); @@ -61,55 +60,51 @@ Error DirAccessUnix::list_dir_begin() { } bool DirAccessUnix::file_exists(String p_file) { - - GLOBAL_LOCK_FUNCTION + GLOBAL_LOCK_FUNCTION if (p_file.is_rel_path()) - p_file=current_dir.plus_file(p_file); + p_file = current_dir.plus_file(p_file); - p_file=fix_path(p_file); + p_file = fix_path(p_file); struct stat flags; - bool success = (stat(p_file.utf8().get_data(),&flags)==0); + bool success = (stat(p_file.utf8().get_data(), &flags) == 0); if (success && S_ISDIR(flags.st_mode)) { - success=false; + success = false; } return success; - } bool DirAccessUnix::dir_exists(String p_dir) { GLOBAL_LOCK_FUNCTION - if (p_dir.is_rel_path()) - p_dir=get_current_dir().plus_file(p_dir); + p_dir = get_current_dir().plus_file(p_dir); - p_dir=fix_path(p_dir); + p_dir = fix_path(p_dir); struct stat flags; - bool success = (stat(p_dir.utf8().get_data(),&flags)==0); + bool success = (stat(p_dir.utf8().get_data(), &flags) == 0); if (success && S_ISDIR(flags.st_mode)) return true; return false; - } uint64_t DirAccessUnix::get_modified_time(String p_file) { if (p_file.is_rel_path()) - p_file=current_dir.plus_file(p_file); + p_file = current_dir.plus_file(p_file); - p_file=fix_path(p_file); + p_file = fix_path(p_file); struct stat flags; - bool success = (stat(p_file.utf8().get_data(),&flags)==0); + bool success = (stat(p_file.utf8().get_data(), &flags) == 0); if (success) { return flags.st_mtime; @@ -120,16 +115,15 @@ uint64_t DirAccessUnix::get_modified_time(String p_file) { return 0; }; - -String DirAccessUnix::get_next() { +String DirAccessUnix::get_next() { if (!dir_stream) return ""; dirent *entry; - entry=readdir(dir_stream); + entry = readdir(dir_stream); - if (entry==NULL) { + if (entry == NULL) { list_dir_end(); return ""; @@ -140,31 +134,27 @@ String DirAccessUnix::get_next() { String fname = fix_unicode_name(entry->d_name); - String f=current_dir.plus_file(fname); + String f = current_dir.plus_file(fname); - if (stat(f.utf8().get_data(),&flags)==0) { + if (stat(f.utf8().get_data(), &flags) == 0) { if (S_ISDIR(flags.st_mode)) { - _cisdir=true; + _cisdir = true; } else { - _cisdir=false; + _cisdir = false; } } else { - _cisdir=false; - + _cisdir = false; } - _cishidden=(fname!="." && fname!=".." && fname.begins_with(".")); - - + _cishidden = (fname != "." && fname != ".." && fname.begins_with(".")); return fname; - } bool DirAccessUnix::current_is_dir() const { @@ -177,13 +167,12 @@ bool DirAccessUnix::current_is_hidden() const { return _cishidden; } - void DirAccessUnix::list_dir_end() { if (dir_stream) closedir(dir_stream); - dir_stream=0; - _cisdir=false; + dir_stream = 0; + _cisdir = false; } int DirAccessUnix::get_drive_count() { @@ -200,24 +189,21 @@ Error DirAccessUnix::make_dir(String p_dir) { GLOBAL_LOCK_FUNCTION if (p_dir.is_rel_path()) - p_dir=get_current_dir().plus_file(p_dir); - - - p_dir=fix_path(p_dir); + p_dir = get_current_dir().plus_file(p_dir); + p_dir = fix_path(p_dir); #if 1 - - bool success=(mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)==0); + bool success = (mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0); int err = errno; #else char real_current_dir_name[2048]; - getcwd(real_current_dir_name,2048); + getcwd(real_current_dir_name, 2048); chdir(current_dir.utf8().get_data()); //ascii since this may be unicode or wathever the host os wants - bool success=(mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)==0); + bool success = (mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0); int err = errno; chdir(real_current_dir_name); @@ -233,91 +219,86 @@ Error DirAccessUnix::make_dir(String p_dir) { return ERR_CANT_CREATE; } - Error DirAccessUnix::change_dir(String p_dir) { GLOBAL_LOCK_FUNCTION - p_dir=fix_path(p_dir); - + p_dir = fix_path(p_dir); char real_current_dir_name[2048]; - getcwd(real_current_dir_name,2048); + getcwd(real_current_dir_name, 2048); String prev_dir; if (prev_dir.parse_utf8(real_current_dir_name)) - prev_dir=real_current_dir_name; //no utf8, maybe latin? + prev_dir = real_current_dir_name; //no utf8, maybe latin? chdir(current_dir.utf8().get_data()); //ascii since this may be unicode or wathever the host os wants - bool worked=(chdir(p_dir.utf8().get_data())==0); // we can only give this utf8 + bool worked = (chdir(p_dir.utf8().get_data()) == 0); // we can only give this utf8 String base = _get_root_path(); - if (base!="") { + if (base != "") { - getcwd(real_current_dir_name,2048); + getcwd(real_current_dir_name, 2048); String new_dir; new_dir.parse_utf8(real_current_dir_name); if (!new_dir.begins_with(base)) - worked=false; + worked = false; } if (worked) { - getcwd(real_current_dir_name,2048); + getcwd(real_current_dir_name, 2048); if (current_dir.parse_utf8(real_current_dir_name)) - current_dir=real_current_dir_name; //no utf8, maybe latin? + current_dir = real_current_dir_name; //no utf8, maybe latin? } chdir(prev_dir.utf8().get_data()); - return worked?OK:ERR_INVALID_PARAMETER; - + return worked ? OK : ERR_INVALID_PARAMETER; } String DirAccessUnix::get_current_dir() { String base = _get_root_path(); - if (base!="") { + if (base != "") { - String bd = current_dir.replace_first(base,""); + String bd = current_dir.replace_first(base, ""); if (bd.begins_with("/")) - return _get_root_string()+bd.substr(1,bd.length()); + return _get_root_string() + bd.substr(1, bd.length()); else - return _get_root_string()+bd; - + return _get_root_string() + bd; } return current_dir; } -Error DirAccessUnix::rename(String p_path,String p_new_path) { +Error DirAccessUnix::rename(String p_path, String p_new_path) { if (p_path.is_rel_path()) - p_path=get_current_dir().plus_file(p_path); + p_path = get_current_dir().plus_file(p_path); - p_path=fix_path(p_path); + p_path = fix_path(p_path); if (p_new_path.is_rel_path()) - p_new_path=get_current_dir().plus_file(p_new_path); + p_new_path = get_current_dir().plus_file(p_new_path); - p_new_path=fix_path(p_new_path); + p_new_path = fix_path(p_new_path); - return ::rename(p_path.utf8().get_data(),p_new_path.utf8().get_data())==0?OK:FAILED; + return ::rename(p_path.utf8().get_data(), p_new_path.utf8().get_data()) == 0 ? OK : FAILED; } -Error DirAccessUnix::remove(String p_path) { +Error DirAccessUnix::remove(String p_path) { if (p_path.is_rel_path()) - p_path=get_current_dir().plus_file(p_path); + p_path = get_current_dir().plus_file(p_path); - p_path=fix_path(p_path); + p_path = fix_path(p_path); struct stat flags; - if ((stat(p_path.utf8().get_data(),&flags)!=0)) + if ((stat(p_path.utf8().get_data(), &flags) != 0)) return FAILED; if (S_ISDIR(flags.st_mode)) - return ::rmdir(p_path.utf8().get_data())==0?OK:FAILED; + return ::rmdir(p_path.utf8().get_data()) == 0 ? OK : FAILED; else - return ::unlink(p_path.utf8().get_data())==0?OK:FAILED; + return ::unlink(p_path.utf8().get_data()) == 0 ? OK : FAILED; } - size_t DirAccessUnix::get_space_left() { #ifndef NO_STATVFS @@ -331,28 +312,23 @@ size_t DirAccessUnix::get_space_left() { #else #warning THIS IS BROKEN return 0; -#endif +#endif }; - - DirAccessUnix::DirAccessUnix() { - dir_stream=0; - current_dir="."; - _cisdir=false; + dir_stream = 0; + current_dir = "."; + _cisdir = false; /* determine drive count */ change_dir(current_dir); - } - DirAccessUnix::~DirAccessUnix() { list_dir_end(); } - #endif //posix_enabled diff --git a/drivers/unix/dir_access_unix.h b/drivers/unix/dir_access_unix.h index f075c48268..0ec0e551d7 100644 --- a/drivers/unix/dir_access_unix.h +++ b/drivers/unix/dir_access_unix.h @@ -31,65 +31,57 @@ #if defined(UNIX_ENABLED) || defined(LIBC_FILEIO_ENABLED) -#include <sys/types.h> +#include <dirent.h> #include <sys/stat.h> +#include <sys/types.h> #include <unistd.h> -#include <dirent.h> #include "os/dir_access.h" - /** @author Juan Linietsky <reduzio@gmail.com> */ class DirAccessUnix : public DirAccess { - + DIR *dir_stream; - + static DirAccess *create_fs(); - + String current_dir; bool _cisdir; bool _cishidden; -protected: - virtual String fix_unicode_name(const char* p_name) const { return String::utf8(p_name); } +protected: + virtual String fix_unicode_name(const char *p_name) const { return String::utf8(p_name); } public: - virtual Error list_dir_begin(); ///< This starts dir listing virtual String get_next(); virtual bool current_is_dir() const; virtual bool current_is_hidden() const; - - virtual void list_dir_end(); ///< - + + virtual void list_dir_end(); ///< + virtual int get_drive_count(); virtual String get_drive(int p_drive); - + virtual Error change_dir(String p_dir); ///< can be relative or absolute, return false on success virtual String get_current_dir(); ///< return current dir location virtual Error make_dir(String p_dir); - + virtual bool file_exists(String p_file); virtual bool dir_exists(String p_dir); virtual uint64_t get_modified_time(String p_file); - - virtual Error rename(String p_from, String p_to); virtual Error remove(String p_name); virtual size_t get_space_left(); - - + DirAccessUnix(); ~DirAccessUnix(); - }; - - #endif //UNIX ENABLED #endif diff --git a/drivers/unix/file_access_unix.cpp b/drivers/unix/file_access_unix.cpp index ee51db6694..723bf3321a 100644 --- a/drivers/unix/file_access_unix.cpp +++ b/drivers/unix/file_access_unix.cpp @@ -30,20 +30,20 @@ #if defined(UNIX_ENABLED) || defined(LIBC_FILEIO_ENABLED) -#include <sys/types.h> -#include <sys/stat.h> -#include "print_string.h" #include "core/os/os.h" +#include "print_string.h" +#include <sys/stat.h> +#include <sys/types.h> #ifndef ANDROID_ENABLED #include <sys/statvfs.h> #endif #ifdef MSVC - #define S_ISREG(m) ((m)&_S_IFREG) +#define S_ISREG(m) ((m)&_S_IFREG) #endif #ifndef S_ISREG - #define S_ISREG(m) ((m) & S_IFREG) +#define S_ISREG(m) ((m)&S_IFREG) #endif void FileAccessUnix::check_errors() const { @@ -52,31 +52,30 @@ void FileAccessUnix::check_errors() const { if (feof(f)) { - last_error=ERR_FILE_EOF; + last_error = ERR_FILE_EOF; } - } -Error FileAccessUnix::_open(const String& p_path, int p_mode_flags) { +Error FileAccessUnix::_open(const String &p_path, int p_mode_flags) { if (f) fclose(f); - f=NULL; + f = NULL; - path=fix_path(p_path); + path = fix_path(p_path); //printf("opening %ls, %i\n", path.c_str(), Memory::get_static_mem_usage()); - ERR_FAIL_COND_V(f,ERR_ALREADY_IN_USE); - const char* mode_string; - - if (p_mode_flags==READ) - mode_string="rb"; - else if (p_mode_flags==WRITE) - mode_string="wb"; - else if (p_mode_flags==READ_WRITE) - mode_string="rb+"; - else if (p_mode_flags==WRITE_READ) - mode_string="wb+"; + ERR_FAIL_COND_V(f, ERR_ALREADY_IN_USE); + const char *mode_string; + + if (p_mode_flags == READ) + mode_string = "rb"; + else if (p_mode_flags == WRITE) + mode_string = "wb"; + else if (p_mode_flags == READ_WRITE) + mode_string = "rb+"; + else if (p_mode_flags == WRITE_READ) + mode_string = "wb+"; else return ERR_INVALID_PARAMETER; @@ -85,30 +84,28 @@ Error FileAccessUnix::_open(const String& p_path, int p_mode_flags) { //printf("opening %s as %s\n", p_path.utf8().get_data(), path.utf8().get_data()); struct stat st; - if (stat(path.utf8().get_data(),&st) == 0) { + if (stat(path.utf8().get_data(), &st) == 0) { if (!S_ISREG(st.st_mode)) return ERR_FILE_CANT_OPEN; - }; - if (is_backup_save_enabled() && p_mode_flags&WRITE && !(p_mode_flags&READ)) { - save_path=path; - path=path+".tmp"; + if (is_backup_save_enabled() && p_mode_flags & WRITE && !(p_mode_flags & READ)) { + save_path = path; + path = path + ".tmp"; //print_line("saving instead to "+path); } - f=fopen(path.utf8().get_data(),mode_string); + f = fopen(path.utf8().get_data(), mode_string); - if (f==NULL) { - last_error=ERR_FILE_CANT_OPEN; + if (f == NULL) { + last_error = ERR_FILE_CANT_OPEN; return ERR_FILE_CANT_OPEN; } else { - last_error=OK; - flags=p_mode_flags; + last_error = OK; + flags = p_mode_flags; return OK; } - } void FileAccessUnix::close() { @@ -117,57 +114,53 @@ void FileAccessUnix::close() { fclose(f); f = NULL; if (close_notification_func) { - close_notification_func(path,flags); + close_notification_func(path, flags); } - if (save_path!="") { + if (save_path != "") { //unlink(save_path.utf8().get_data()); //print_line("renaming.."); - int rename_error = rename((save_path+".tmp").utf8().get_data(),save_path.utf8().get_data()); + int rename_error = rename((save_path + ".tmp").utf8().get_data(), save_path.utf8().get_data()); if (rename_error && close_fail_notify) { close_fail_notify(save_path); } - save_path=""; - ERR_FAIL_COND( rename_error != 0); + save_path = ""; + ERR_FAIL_COND(rename_error != 0); } - - } -bool FileAccessUnix::is_open() const{ +bool FileAccessUnix::is_open() const { - return (f!=NULL); + return (f != NULL); } void FileAccessUnix::seek(size_t p_position) { ERR_FAIL_COND(!f); - last_error=OK; - if ( fseek(f,p_position,SEEK_SET) ) + last_error = OK; + if (fseek(f, p_position, SEEK_SET)) check_errors(); } -void FileAccessUnix::seek_end(int64_t p_position) { +void FileAccessUnix::seek_end(int64_t p_position) { ERR_FAIL_COND(!f); - if ( fseek(f,p_position,SEEK_END) ) + if (fseek(f, p_position, SEEK_END)) check_errors(); } -size_t FileAccessUnix::get_pos() const{ - +size_t FileAccessUnix::get_pos() const { - size_t aux_position=0; - if ( !(aux_position = ftell(f)) ) { + size_t aux_position = 0; + if (!(aux_position = ftell(f))) { check_errors(); }; return aux_position; } -size_t FileAccessUnix::get_len() const{ +size_t FileAccessUnix::get_len() const { + ERR_FAIL_COND_V(!f, 0); - ERR_FAIL_COND_V(!f,0); - - FileAccessUnix *fau = const_cast<FileAccessUnix*>(this); + FileAccessUnix *fau = const_cast<FileAccessUnix *>(this); int pos = fau->get_pos(); fau->seek_end(); int size = fau->get_pos(); @@ -176,16 +169,16 @@ size_t FileAccessUnix::get_len() const{ return size; } -bool FileAccessUnix::eof_reached() const{ +bool FileAccessUnix::eof_reached() const { - return last_error==ERR_FILE_EOF; + return last_error == ERR_FILE_EOF; } -uint8_t FileAccessUnix::get_8() const{ +uint8_t FileAccessUnix::get_8() const { - ERR_FAIL_COND_V(!f,0); + ERR_FAIL_COND_V(!f, 0); uint8_t b; - if (fread(&b,1,1,f) == 0) { + if (fread(&b, 1, 1, f) == 0) { check_errors(); }; @@ -194,13 +187,13 @@ uint8_t FileAccessUnix::get_8() const{ int FileAccessUnix::get_buffer(uint8_t *p_dst, int p_length) const { - ERR_FAIL_COND_V(!f,-1); + ERR_FAIL_COND_V(!f, -1); int read = fread(p_dst, 1, p_length, f); check_errors(); return read; }; -Error FileAccessUnix::get_error() const{ +Error FileAccessUnix::get_error() const { return last_error; } @@ -208,18 +201,16 @@ Error FileAccessUnix::get_error() const{ void FileAccessUnix::store_8(uint8_t p_dest) { ERR_FAIL_COND(!f); - fwrite(&p_dest,1,1,f); - + fwrite(&p_dest, 1, 1, f); } - bool FileAccessUnix::file_exists(const String &p_path) { FILE *g; //printf("opening file %s\n", p_fname.c_str()); - String filename=fix_path(p_path); - g=fopen(filename.utf8().get_data(),"rb"); - if (g==NULL) { + String filename = fix_path(p_path); + g = fopen(filename.utf8().get_data(), "rb"); + if (g == NULL) { return false; } else { @@ -231,38 +222,35 @@ bool FileAccessUnix::file_exists(const String &p_path) { uint64_t FileAccessUnix::_get_modified_time(const String &p_file) { - String file=fix_path(p_file); + String file = fix_path(p_file); struct stat flags; - bool success = (stat(file.utf8().get_data(),&flags)==0); + bool success = (stat(file.utf8().get_data(), &flags) == 0); if (success) { return flags.st_mtime; } else { - print_line("ERROR IN: "+p_file); + print_line("ERROR IN: " + p_file); ERR_FAIL_V(0); }; - } -FileAccess * FileAccessUnix::create_libc() { +FileAccess *FileAccessUnix::create_libc() { - return memnew( FileAccessUnix ); + return memnew(FileAccessUnix); } -CloseNotificationFunc FileAccessUnix::close_notification_func=NULL; +CloseNotificationFunc FileAccessUnix::close_notification_func = NULL; FileAccessUnix::FileAccessUnix() { - f=NULL; - flags=0; - last_error=OK; - + f = NULL; + flags = 0; + last_error = OK; } FileAccessUnix::~FileAccessUnix() { close(); - } #endif diff --git a/drivers/unix/file_access_unix.h b/drivers/unix/file_access_unix.h index 57b643dc26..4b5897e9a5 100644 --- a/drivers/unix/file_access_unix.h +++ b/drivers/unix/file_access_unix.h @@ -39,50 +39,47 @@ @author Juan Linietsky <reduzio@gmail.com> */ - -typedef void (*CloseNotificationFunc)(const String& p_file,int p_flags); +typedef void (*CloseNotificationFunc)(const String &p_file, int p_flags); class FileAccessUnix : public FileAccess { - + FILE *f; int flags; void check_errors() const; mutable Error last_error; String save_path; String path; - - static FileAccess* create_libc(); + + static FileAccess *create_libc(); + public: - static CloseNotificationFunc close_notification_func; - virtual Error _open(const String& p_path, int p_mode_flags); ///< open a file + virtual Error _open(const String &p_path, int p_mode_flags); ///< open a file virtual void close(); ///< close a file - virtual bool is_open() const; ///< true when file is open + virtual bool is_open() const; ///< true when file is open - virtual void seek(size_t p_position); ///< seek to a given position - virtual void seek_end(int64_t p_position=0); ///< seek from the end of file - virtual size_t get_pos() const; ///< get position in the file - virtual size_t get_len() const; ///< get size of the file + virtual void seek(size_t p_position); ///< seek to a given position + virtual void seek_end(int64_t p_position = 0); ///< seek from the end of file + virtual size_t get_pos() const; ///< get position in the file + virtual size_t get_len() const; ///< get size of the file - virtual bool eof_reached() const; ///< reading passed EOF + virtual bool eof_reached() const; ///< reading passed EOF - virtual uint8_t get_8() const; ///< get a byte + virtual uint8_t get_8() const; ///< get a byte virtual int get_buffer(uint8_t *p_dst, int p_length) const; - virtual Error get_error() const; ///< get last error + virtual Error get_error() const; ///< get last error - virtual void store_8(uint8_t p_dest); ///< store a byte - - virtual bool file_exists(const String& p_path); ///< return true if a file exists + virtual void store_8(uint8_t p_dest); ///< store a byte - virtual uint64_t _get_modified_time(const String& p_file); + virtual bool file_exists(const String &p_path); ///< return true if a file exists + + virtual uint64_t _get_modified_time(const String &p_file); FileAccessUnix(); virtual ~FileAccessUnix(); - }; - #endif #endif diff --git a/drivers/unix/ip_unix.cpp b/drivers/unix/ip_unix.cpp index fc0b3faccc..d9dfd3c736 100644 --- a/drivers/unix/ip_unix.cpp +++ b/drivers/unix/ip_unix.cpp @@ -33,60 +33,60 @@ #include <string.h> #ifdef WINDOWS_ENABLED - #include <ws2tcpip.h> - #include <winsock2.h> - #include <windows.h> - #include <stdio.h> - #ifndef UWP_ENABLED - #if defined(__MINGW32__ ) && (!defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 4) - // MinGW-w64 on Ubuntu 12.04 (our Travis build env) has bugs in this code where - // some includes are missing in dependencies of iphlpapi.h for WINVER >= 0x0600 (Vista). - // We don't use this Vista code for now, so working it around by disabling it. - // MinGW-w64 >= 4.0 seems to be better judging by its headers. - #undef _WIN32_WINNT - #define _WIN32_WINNT 0x0501 // Windows XP, disable Vista API - #include <iphlpapi.h> - #undef _WIN32_WINNT - #define _WIN32_WINNT 0x0600 // Reenable Vista API - #else - #include <iphlpapi.h> - #endif // MINGW hack - #endif +#include <stdio.h> +#include <windows.h> +#include <winsock2.h> +#include <ws2tcpip.h> +#ifndef UWP_ENABLED +#if defined(__MINGW32__) && (!defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 4) +// MinGW-w64 on Ubuntu 12.04 (our Travis build env) has bugs in this code where +// some includes are missing in dependencies of iphlpapi.h for WINVER >= 0x0600 (Vista). +// We don't use this Vista code for now, so working it around by disabling it. +// MinGW-w64 >= 4.0 seems to be better judging by its headers. +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x0501 // Windows XP, disable Vista API +#include <iphlpapi.h> +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x0600 // Reenable Vista API #else - #include <netdb.h> - #ifdef ANDROID_ENABLED - #include "platform/android/ifaddrs_android.h" - #else - #ifdef __FreeBSD__ - #include <sys/types.h> - #endif - #include <ifaddrs.h> - #endif - #include <arpa/inet.h> - #include <sys/socket.h> - #ifdef __FreeBSD__ - #include <netinet/in.h> - #endif +#include <iphlpapi.h> +#endif // MINGW hack +#endif +#else +#include <netdb.h> +#ifdef ANDROID_ENABLED +#include "platform/android/ifaddrs_android.h" +#else +#ifdef __FreeBSD__ +#include <sys/types.h> +#endif +#include <ifaddrs.h> +#endif +#include <arpa/inet.h> +#include <sys/socket.h> +#ifdef __FreeBSD__ +#include <netinet/in.h> +#endif #endif -static IP_Address _sockaddr2ip(struct sockaddr* p_addr) { +static IP_Address _sockaddr2ip(struct sockaddr *p_addr) { IP_Address ip; if (p_addr->sa_family == AF_INET) { - struct sockaddr_in* addr = (struct sockaddr_in*)p_addr; + struct sockaddr_in *addr = (struct sockaddr_in *)p_addr; ip.set_ipv4((uint8_t *)&(addr->sin_addr)); } else { - struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr; + struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr; ip.set_ipv6(addr6->sin6_addr.s6_addr); }; return ip; }; -IP_Address IP_Unix::_resolve_hostname(const String& p_hostname, Type p_type) { +IP_Address IP_Unix::_resolve_hostname(const String &p_hostname, Type p_type) { struct addrinfo hints; - struct addrinfo* result; + struct addrinfo *result; memset(&hints, 0, sizeof(struct addrinfo)); if (p_type == TYPE_IPV4) { @@ -115,7 +115,6 @@ IP_Address IP_Unix::_resolve_hostname(const String& p_hostname, Type p_type) { freeaddrinfo(result); return ip; - } #if defined(WINDOWS_ENABLED) @@ -136,23 +135,22 @@ void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const { r_addresses->push_back(IP_Address(String(hostnames->GetAt(i)->CanonicalName->Data()))); } } - }; #else void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const { ULONG buf_size = 1024; - IP_ADAPTER_ADDRESSES* addrs; + IP_ADAPTER_ADDRESSES *addrs; while (true) { - addrs = (IP_ADAPTER_ADDRESSES*)memalloc(buf_size); + addrs = (IP_ADAPTER_ADDRESSES *)memalloc(buf_size); int err = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST | - GAA_FLAG_SKIP_MULTICAST | - GAA_FLAG_SKIP_DNS_SERVER | - GAA_FLAG_SKIP_FRIENDLY_NAME, - NULL, addrs, &buf_size); + GAA_FLAG_SKIP_MULTICAST | + GAA_FLAG_SKIP_DNS_SERVER | + GAA_FLAG_SKIP_FRIENDLY_NAME, + NULL, addrs, &buf_size); if (err == NO_ERROR) { break; }; @@ -166,29 +164,27 @@ void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const { return; }; - - IP_ADAPTER_ADDRESSES* adapter = addrs; + IP_ADAPTER_ADDRESSES *adapter = addrs; while (adapter != NULL) { - IP_ADAPTER_UNICAST_ADDRESS* address = adapter->FirstUnicastAddress; + IP_ADAPTER_UNICAST_ADDRESS *address = adapter->FirstUnicastAddress; while (address != NULL) { IP_Address ip; if (address->Address.lpSockaddr->sa_family == AF_INET) { - SOCKADDR_IN* ipv4 = reinterpret_cast<SOCKADDR_IN*>(address->Address.lpSockaddr); + SOCKADDR_IN *ipv4 = reinterpret_cast<SOCKADDR_IN *>(address->Address.lpSockaddr); ip.set_ipv4((uint8_t *)&(ipv4->sin_addr)); } else { // ipv6 - SOCKADDR_IN6* ipv6 = reinterpret_cast<SOCKADDR_IN6*>(address->Address.lpSockaddr); + SOCKADDR_IN6 *ipv6 = reinterpret_cast<SOCKADDR_IN6 *>(address->Address.lpSockaddr); ip.set_ipv6(ipv6->sin6_addr.s6_addr); }; - r_addresses->push_back(ip); address = address->Next; @@ -205,8 +201,8 @@ void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const { void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const { - struct ifaddrs * ifAddrStruct=NULL; - struct ifaddrs * ifa=NULL; + struct ifaddrs *ifAddrStruct = NULL; + struct ifaddrs *ifa = NULL; getifaddrs(&ifAddrStruct); @@ -218,19 +214,18 @@ void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const { r_addresses->push_back(ip); } - if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct); - + if (ifAddrStruct != NULL) freeifaddrs(ifAddrStruct); } #endif void IP_Unix::make_default() { - _create=_create_unix; + _create = _create_unix; } -IP* IP_Unix::_create_unix() { +IP *IP_Unix::_create_unix() { - return memnew( IP_Unix ); + return memnew(IP_Unix); } IP_Unix::IP_Unix() { diff --git a/drivers/unix/ip_unix.h b/drivers/unix/ip_unix.h index eb7ebf8bb0..c22fedfe1c 100644 --- a/drivers/unix/ip_unix.h +++ b/drivers/unix/ip_unix.h @@ -36,11 +36,11 @@ class IP_Unix : public IP { GDCLASS(IP_Unix, IP); - virtual IP_Address _resolve_hostname(const String& p_hostname, IP::Type p_type); + virtual IP_Address _resolve_hostname(const String &p_hostname, IP::Type p_type); - static IP* _create_unix(); -public: + static IP *_create_unix(); +public: virtual void get_local_addresses(List<IP_Address> *r_addresses) const; static void make_default(); diff --git a/drivers/unix/mutex_posix.cpp b/drivers/unix/mutex_posix.cpp index c9b5bdce75..9009da2065 100644 --- a/drivers/unix/mutex_posix.cpp +++ b/drivers/unix/mutex_posix.cpp @@ -34,7 +34,6 @@ void MutexPosix::lock() { pthread_mutex_lock(&mutex); - } void MutexPosix::unlock() { @@ -42,32 +41,30 @@ void MutexPosix::unlock() { } Error MutexPosix::try_lock() { - return (pthread_mutex_trylock(&mutex)==0)?OK:ERR_BUSY; + return (pthread_mutex_trylock(&mutex) == 0) ? OK : ERR_BUSY; } Mutex *MutexPosix::create_func_posix(bool p_recursive) { - return memnew( MutexPosix(p_recursive) ); + return memnew(MutexPosix(p_recursive)); } void MutexPosix::make_default() { - create_func=create_func_posix; + create_func = create_func_posix; } MutexPosix::MutexPosix(bool p_recursive) { - + pthread_mutexattr_init(&attr); if (p_recursive) - pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(&mutex,&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&mutex, &attr); } - MutexPosix::~MutexPosix() { pthread_mutex_destroy(&mutex); } - #endif diff --git a/drivers/unix/mutex_posix.h b/drivers/unix/mutex_posix.h index a71400924a..84fb32b844 100644 --- a/drivers/unix/mutex_posix.h +++ b/drivers/unix/mutex_posix.h @@ -31,29 +31,26 @@ #if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED) -#include <pthread.h> #include "os/mutex.h" +#include <pthread.h> class MutexPosix : public Mutex { - pthread_mutexattr_t attr; + pthread_mutexattr_t attr; pthread_mutex_t mutex; - + static Mutex *create_func_posix(bool p_recursive); - -public: - virtual void lock(); +public: + virtual void lock(); virtual void unlock(); - virtual Error try_lock(); - + virtual Error try_lock(); static void make_default(); MutexPosix(bool p_recursive); - - ~MutexPosix(); + ~MutexPosix(); }; #endif diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp index fe49501328..e2a544b676 100644 --- a/drivers/unix/os_unix.cpp +++ b/drivers/unix/os_unix.cpp @@ -32,18 +32,18 @@ #include "servers/visual_server.h" -#include "thread_posix.h" -#include "semaphore_posix.h" +#include "core/os/thread_dummy.h" #include "mutex_posix.h" #include "rw_lock_posix.h" -#include "core/os/thread_dummy.h" +#include "semaphore_posix.h" +#include "thread_posix.h" //#include "core/io/file_access_buffered_fa.h" -#include "file_access_unix.h" #include "dir_access_unix.h" -#include "tcp_server_posix.h" -#include "stream_peer_tcp_posix.h" +#include "file_access_unix.h" #include "packet_peer_udp_posix.h" +#include "stream_peer_tcp_posix.h" +#include "tcp_server_posix.h" #ifdef __APPLE__ #include <mach-o/dyld.h> @@ -52,51 +52,50 @@ #ifdef __FreeBSD__ #include <sys/param.h> #endif +#include "global_config.h" +#include <assert.h> +#include <errno.h> +#include <poll.h> +#include <signal.h> #include <stdarg.h> -#include <sys/time.h> -#include <sys/wait.h> #include <stdlib.h> -#include <signal.h> #include <string.h> -#include <poll.h> -#include <errno.h> -#include <assert.h> -#include "global_config.h" +#include <sys/time.h> +#include <sys/wait.h> extern bool _print_error_enabled; -void OS_Unix::print_error(const char* p_function,const char* p_file,int p_line,const char *p_code,const char*p_rationale,ErrorType p_type) { +void OS_Unix::print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) { if (!_print_error_enabled) return; - const char* err_details; + const char *err_details; if (p_rationale && p_rationale[0]) - err_details=p_rationale; + err_details = p_rationale; else - err_details=p_code; + err_details = p_code; - switch(p_type) { + switch (p_type) { case ERR_ERROR: - print("\E[1;31mERROR: %s: \E[0m\E[1m%s\n",p_function,err_details); - print("\E[0;31m At: %s:%i.\E[0m\n",p_file,p_line); + print("\E[1;31mERROR: %s: \E[0m\E[1m%s\n", p_function, err_details); + print("\E[0;31m At: %s:%i.\E[0m\n", p_file, p_line); break; case ERR_WARNING: - print("\E[1;33mWARNING: %s: \E[0m\E[1m%s\n",p_function,err_details); - print("\E[0;33m At: %s:%i.\E[0m\n",p_file,p_line); + print("\E[1;33mWARNING: %s: \E[0m\E[1m%s\n", p_function, err_details); + print("\E[0;33m At: %s:%i.\E[0m\n", p_file, p_line); break; case ERR_SCRIPT: - print("\E[1;35mSCRIPT ERROR: %s: \E[0m\E[1m%s\n",p_function,err_details); - print("\E[0;35m At: %s:%i.\E[0m\n",p_file,p_line); + print("\E[1;35mSCRIPT ERROR: %s: \E[0m\E[1m%s\n", p_function, err_details); + print("\E[0;35m At: %s:%i.\E[0m\n", p_file, p_line); break; case ERR_SHADER: - print("\E[1;36mSHADER ERROR: %s: \E[0m\E[1m%s\n",p_function,err_details); - print("\E[0;36m At: %s:%i.\E[0m\n",p_file,p_line); + print("\E[1;36mSHADER ERROR: %s: \E[0m\E[1m%s\n", p_function, err_details); + print("\E[0;36m At: %s:%i.\E[0m\n", p_file, p_line); break; } } - void OS_Unix::debug_break() { assert(false); @@ -105,26 +104,26 @@ void OS_Unix::debug_break() { int OS_Unix::get_audio_driver_count() const { return 1; - } -const char * OS_Unix::get_audio_driver_name(int p_driver) const { +const char *OS_Unix::get_audio_driver_name(int p_driver) const { return "dummy"; } - + int OS_Unix::unix_initialize_audio(int p_audio_driver) { return 0; } - + // Very simple signal handler to reap processes where ::execute was called with // !p_blocking void handle_sigchld(int sig) { int saved_errno = errno; - while (waitpid((pid_t)(-1), 0, WNOHANG) > 0) {} + while (waitpid((pid_t)(-1), 0, WNOHANG) > 0) { + } errno = saved_errno; } - + void OS_Unix::initialize_core() { #ifdef NO_PTHREADS @@ -132,9 +131,9 @@ void OS_Unix::initialize_core() { SemaphoreDummy::make_default(); MutexDummy::make_default(); #else - ThreadPosix::make_default(); + ThreadPosix::make_default(); SemaphorePosix::make_default(); - MutexPosix::make_default(); + MutexPosix::make_default(); RWLockPosix::make_default(); #endif FileAccess::make_default<FileAccessUnix>(FileAccess::ACCESS_RESOURCES); @@ -152,8 +151,8 @@ void OS_Unix::initialize_core() { IP_Unix::make_default(); #endif - ticks_start=0; - ticks_start=get_ticks_usec(); + ticks_start = 0; + ticks_start = get_ticks_usec(); struct sigaction sa; sa.sa_handler = &handle_sigchld; @@ -165,39 +164,34 @@ void OS_Unix::initialize_core() { } void OS_Unix::finalize_core() { - - - } - -void OS_Unix::vprint(const char* p_format, va_list p_list,bool p_stder) { +void OS_Unix::vprint(const char *p_format, va_list p_list, bool p_stder) { if (p_stder) { - vfprintf(stderr,p_format,p_list); + vfprintf(stderr, p_format, p_list); fflush(stderr); } else { - vprintf(p_format,p_list); + vprintf(p_format, p_list); fflush(stdout); } } -void OS_Unix::print(const char *p_format, ... ) { +void OS_Unix::print(const char *p_format, ...) { va_list argp; va_start(argp, p_format); - vprintf(p_format, argp ); + vprintf(p_format, argp); va_end(argp); - } -void OS_Unix::alert(const String& p_alert,const String& p_title) { +void OS_Unix::alert(const String &p_alert, const String &p_title) { - fprintf(stderr,"ERROR: %s\n",p_alert.utf8().get_data()); + fprintf(stderr, "ERROR: %s\n", p_alert.utf8().get_data()); } -static int has_data(FILE* p_fd, int timeout_usec = 0) { +static int has_data(FILE *p_fd, int timeout_usec = 0) { fd_set readset; int fd = fileno(p_fd); @@ -206,17 +200,16 @@ static int has_data(FILE* p_fd, int timeout_usec = 0) { timeval time; time.tv_sec = 0; time.tv_usec = timeout_usec; - int res = 0;//select(fd + 1, &readset, NULL, NULL, &time); + int res = 0; //select(fd + 1, &readset, NULL, NULL, &time); return res > 0; }; - String OS_Unix::get_stdin_string(bool p_block) { String ret; if (p_block) { char buff[1024]; - ret = stdin_buf + fgets(buff,1024,stdin); + ret = stdin_buf + fgets(buff, 1024, stdin); stdin_buf = ""; return ret; }; @@ -243,7 +236,6 @@ String OS_Unix::get_name() { return "Unix"; } - uint64_t OS_Unix::get_unix_time() const { return time(NULL); @@ -257,39 +249,38 @@ uint64_t OS_Unix::get_system_time_secs() const { return uint64_t(tv_now.tv_sec); } - OS::Date OS_Unix::get_date(bool utc) const { - time_t t=time(NULL); + time_t t = time(NULL); struct tm *lt; if (utc) - lt=gmtime(&t); + lt = gmtime(&t); else - lt=localtime(&t); + lt = localtime(&t); Date ret; - ret.year=1900+lt->tm_year; + ret.year = 1900 + lt->tm_year; // Index starting at 1 to match OS_Unix::get_date - // and Windows SYSTEMTIME and tm_mon follows the typical structure + // and Windows SYSTEMTIME and tm_mon follows the typical structure // of 0-11, noted here: http://www.cplusplus.com/reference/ctime/tm/ - ret.month=(Month)(lt->tm_mon + 1); - ret.day=lt->tm_mday; - ret.weekday=(Weekday)lt->tm_wday; - ret.dst=lt->tm_isdst; - + ret.month = (Month)(lt->tm_mon + 1); + ret.day = lt->tm_mday; + ret.weekday = (Weekday)lt->tm_wday; + ret.dst = lt->tm_isdst; + return ret; } OS::Time OS_Unix::get_time(bool utc) const { - time_t t=time(NULL); + time_t t = time(NULL); struct tm *lt; if (utc) - lt=gmtime(&t); + lt = gmtime(&t); else - lt=localtime(&t); + lt = localtime(&t); Time ret; - ret.hour=lt->tm_hour; - ret.min=lt->tm_min; - ret.sec=lt->tm_sec; + ret.hour = lt->tm_hour; + ret.min = lt->tm_min; + ret.sec = lt->tm_sec; get_time_zone_info(); return ret; } @@ -327,105 +318,99 @@ void OS_Unix::delay_usec(uint32_t p_usec) const { uint64_t OS_Unix::get_ticks_usec() const { struct timeval tv_now; - gettimeofday(&tv_now,NULL); - - uint64_t longtime = (uint64_t)tv_now.tv_usec + (uint64_t)tv_now.tv_sec*1000000L; - longtime-=ticks_start; - + gettimeofday(&tv_now, NULL); + + uint64_t longtime = (uint64_t)tv_now.tv_usec + (uint64_t)tv_now.tv_sec * 1000000L; + longtime -= ticks_start; + return longtime; } -Error OS_Unix::execute(const String& p_path, const List<String>& p_arguments,bool p_blocking,ProcessID *r_child_id,String* r_pipe,int *r_exitcode) { - +Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id, String *r_pipe, int *r_exitcode) { if (p_blocking && r_pipe) { - String argss; - argss="\""+p_path+"\""; + argss = "\"" + p_path + "\""; - for(int i=0;i<p_arguments.size();i++) { + for (int i = 0; i < p_arguments.size(); i++) { - argss+=String(" \"")+p_arguments[i]+"\""; + argss += String(" \"") + p_arguments[i] + "\""; } - argss+=" 2>/dev/null"; //silence stderr - FILE* f=popen(argss.utf8().get_data(),"r"); + argss += " 2>/dev/null"; //silence stderr + FILE *f = popen(argss.utf8().get_data(), "r"); - ERR_FAIL_COND_V(!f,ERR_CANT_OPEN); + ERR_FAIL_COND_V(!f, ERR_CANT_OPEN); char buf[65535]; - while(fgets(buf,65535,f)) { + while (fgets(buf, 65535, f)) { - (*r_pipe)+=buf; + (*r_pipe) += buf; } int rv = pclose(f); if (r_exitcode) - *r_exitcode=rv; + *r_exitcode = rv; return OK; } - pid_t pid = fork(); - ERR_FAIL_COND_V(pid<0,ERR_CANT_FORK); - + ERR_FAIL_COND_V(pid < 0, ERR_CANT_FORK); - if (pid==0) { + if (pid == 0) { // is child Vector<CharString> cs; cs.push_back(p_path.utf8()); - for(int i=0;i<p_arguments.size();i++) + for (int i = 0; i < p_arguments.size(); i++) cs.push_back(p_arguments[i].utf8()); - Vector<char*> args; - for(int i=0;i<cs.size();i++) - args.push_back((char*)cs[i].get_data());// shitty C cast + Vector<char *> args; + for (int i = 0; i < cs.size(); i++) + args.push_back((char *)cs[i].get_data()); // shitty C cast args.push_back(0); #ifdef __FreeBSD__ - if(p_path.find("/")) { + if (p_path.find("/")) { // exec name contains path so use it - execv(p_path.utf8().get_data(),&args[0]); - }else{ + execv(p_path.utf8().get_data(), &args[0]); + } else { // use program name and search through PATH to find it - execvp(getprogname(),&args[0]); + execvp(getprogname(), &args[0]); } #else - execv(p_path.utf8().get_data(),&args[0]); + execv(p_path.utf8().get_data(), &args[0]); #endif // still alive? something failed.. - fprintf(stderr,"**ERROR** OS_Unix::execute - Could not create child process while executing: %s\n",p_path.utf8().get_data()); + fprintf(stderr, "**ERROR** OS_Unix::execute - Could not create child process while executing: %s\n", p_path.utf8().get_data()); abort(); } if (p_blocking) { int status; - waitpid(pid,&status,0); + waitpid(pid, &status, 0); if (r_exitcode) - *r_exitcode=WEXITSTATUS(status); + *r_exitcode = WEXITSTATUS(status); } else { if (r_child_id) - *r_child_id=pid; + *r_child_id = pid; } return OK; - } -Error OS_Unix::kill(const ProcessID& p_pid) { +Error OS_Unix::kill(const ProcessID &p_pid) { - int ret = ::kill(p_pid,SIGKILL); + int ret = ::kill(p_pid, SIGKILL); if (!ret) { //avoid zombie process int st; - ::waitpid(p_pid,&st,0); - + ::waitpid(p_pid, &st, 0); } - return ret?ERR_INVALID_PARAMETER:OK; + return ret ? ERR_INVALID_PARAMETER : OK; } int OS_Unix::get_process_ID() const { @@ -433,10 +418,9 @@ int OS_Unix::get_process_ID() const { return getpid(); }; +bool OS_Unix::has_environment(const String &p_var) const { -bool OS_Unix::has_environment(const String& p_var) const { - - return getenv(p_var.utf8().get_data())!=NULL; + return getenv(p_var.utf8().get_data()) != NULL; } String OS_Unix::get_locale() const { @@ -446,21 +430,20 @@ String OS_Unix::get_locale() const { String locale = get_environment("LANG"); int tp = locale.find("."); - if (tp!=-1) - locale=locale.substr(0,tp); + if (tp != -1) + locale = locale.substr(0, tp); return locale; } -Error OS_Unix::set_cwd(const String& p_cwd) { +Error OS_Unix::set_cwd(const String &p_cwd) { - if (chdir(p_cwd.utf8().get_data())!=0) + if (chdir(p_cwd.utf8().get_data()) != 0) return ERR_CANT_OPEN; return OK; } - -String OS_Unix::get_environment(const String& p_var) const { +String OS_Unix::get_environment(const String &p_var) const { if (getenv(p_var.utf8().get_data())) return getenv(p_var.utf8().get_data()); @@ -475,35 +458,30 @@ int OS_Unix::get_processor_count() const { String OS_Unix::get_data_dir() const { String an = get_safe_application_name(); - if (an!="") { - - + if (an != "") { if (has_environment("HOME")) { bool use_godot = GlobalConfig::get_singleton()->get("application/use_shared_user_dir"); if (use_godot) - return get_environment("HOME")+"/.godot/app_userdata/"+an; + return get_environment("HOME") + "/.godot/app_userdata/" + an; else - return get_environment("HOME")+"/."+an; + return get_environment("HOME") + "/." + an; } } return GlobalConfig::get_singleton()->get_resource_path(); - } -bool OS_Unix::check_feature_support(const String& p_feature) { +bool OS_Unix::check_feature_support(const String &p_feature) { return VisualServer::get_singleton()->has_os_feature(p_feature); - } - String OS_Unix::get_installed_templates_path() const { - String p=get_global_settings_path(); - if (p!="") - return p+"/templates/"; + String p = get_global_settings_path(); + if (p != "") + return p + "/templates/"; else return ""; } @@ -513,11 +491,11 @@ String OS_Unix::get_executable_path() const { #ifdef __linux__ //fix for running from a symlink char buf[256]; - memset(buf,0,256); + memset(buf, 0, 256); readlink("/proc/self/exe", buf, sizeof(buf)); String b; b.parse_utf8(buf); - if (b=="") { + if (b == "") { WARN_PRINT("Couldn't get executable path from /proc/self/exe, using argv[0]"); return OS::get_executable_path(); } @@ -530,10 +508,10 @@ String OS_Unix::get_executable_path() const { return String(resolved_path); #elif defined(__APPLE__) char temp_path[1]; - uint32_t buff_size=1; + uint32_t buff_size = 1; _NSGetExecutablePath(temp_path, &buff_size); - char* resolved_path = new char[buff_size + 1]; + char *resolved_path = new char[buff_size + 1]; if (_NSGetExecutablePath(resolved_path, &buff_size) == 1) WARN_PRINT("MAXPATHLEN is too small"); @@ -548,5 +526,4 @@ String OS_Unix::get_executable_path() const { #endif } - #endif diff --git a/drivers/unix/os_unix.h b/drivers/unix/os_unix.h index 220f818ff6..3ac4f46109 100644 --- a/drivers/unix/os_unix.h +++ b/drivers/unix/os_unix.h @@ -35,61 +35,57 @@ #ifdef UNIX_ENABLED - -#include "os/os.h" #include "drivers/unix/ip_unix.h" - +#include "os/os.h" class OS_Unix : public OS { uint64_t ticks_start; -protected: +protected: // UNIX only handles the core functions. // inheriting platforms under unix (eg. X11) should handle the rest - + //virtual int get_video_driver_count() const; - //virtual const char * get_video_driver_name(int p_driver) const; + //virtual const char * get_video_driver_name(int p_driver) const; //virtual VideoMode get_default_video_mode() const; - + virtual int get_audio_driver_count() const; - virtual const char * get_audio_driver_name(int p_driver) const; - + virtual const char *get_audio_driver_name(int p_driver) const; + virtual void initialize_core(); virtual int unix_initialize_audio(int p_audio_driver); //virtual void initialize(int p_video_driver,int p_audio_driver); - + //virtual void finalize(); virtual void finalize_core(); - + String stdin_buf; String get_global_settings_path() const; public: + virtual void print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR); - - virtual void print_error(const char* p_function,const char* p_file,int p_line,const char *p_code,const char*p_rationale,ErrorType p_type=ERR_ERROR); - - virtual void print(const char *p_format, ... ); - virtual void vprint(const char* p_format, va_list p_list,bool p_stderr=false); - virtual void alert(const String& p_alert,const String& p_title="ALERT!"); + virtual void print(const char *p_format, ...); + virtual void vprint(const char *p_format, va_list p_list, bool p_stderr = false); + virtual void alert(const String &p_alert, const String &p_title = "ALERT!"); virtual String get_stdin_string(bool p_block); //virtual void set_mouse_show(bool p_show); //virtual void set_mouse_grab(bool p_grab); //virtual bool is_mouse_grab_enabled() const = 0; - //virtual void get_mouse_pos(int &x, int &y) const; + //virtual void get_mouse_pos(int &x, int &y) const; //virtual void set_window_title(const String& p_title); - + //virtual void set_video_mode(const VideoMode& p_video_mode); //virtual VideoMode get_video_mode() const; //virtual void get_fullscreen_mode_list(List<VideoMode> *p_list) const; - virtual Error set_cwd(const String& p_cwd); + virtual Error set_cwd(const String &p_cwd); virtual String get_name(); - + virtual Date get_date(bool utc) const; virtual Time get_time(bool utc) const; virtual TimeZoneInfo get_time_zone_info() const; @@ -97,31 +93,28 @@ public: virtual uint64_t get_unix_time() const; virtual uint64_t get_system_time_secs() const; - virtual void delay_usec(uint32_t p_usec) const; + virtual void delay_usec(uint32_t p_usec) const; virtual uint64_t get_ticks_usec() const; - virtual Error execute(const String& p_path, const List<String>& p_arguments,bool p_blocking,ProcessID *r_child_id=NULL,String* r_pipe=NULL,int *r_exitcode=NULL); - virtual Error kill(const ProcessID& p_pid); + virtual Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id = NULL, String *r_pipe = NULL, int *r_exitcode = NULL); + virtual Error kill(const ProcessID &p_pid); virtual int get_process_ID() const; - virtual bool has_environment(const String& p_var) const; - virtual String get_environment(const String& p_var) const; + virtual bool has_environment(const String &p_var) const; + virtual String get_environment(const String &p_var) const; virtual String get_locale() const; virtual int get_processor_count() const; - virtual void debug_break(); virtual String get_installed_templates_path() const; virtual String get_executable_path() const; virtual String get_data_dir() const; - virtual bool check_feature_support(const String& p_feature); + virtual bool check_feature_support(const String &p_feature); //virtual void run( MainLoop * p_main_loop ); - - }; #endif diff --git a/drivers/unix/packet_peer_udp_posix.cpp b/drivers/unix/packet_peer_udp_posix.cpp index 7696a5fcb5..98883f3267 100644 --- a/drivers/unix/packet_peer_udp_posix.cpp +++ b/drivers/unix/packet_peer_udp_posix.cpp @@ -30,22 +30,21 @@ #ifdef UNIX_ENABLED - #include <errno.h> -#include <unistd.h> #include <netdb.h> -#include <sys/types.h> #include <sys/socket.h> +#include <sys/types.h> +#include <unistd.h> #include <netinet/in.h> #include <stdio.h> #ifndef NO_FCNTL - #ifdef __HAIKU__ - #include <fcntl.h> - #else - #include <sys/fcntl.h> - #endif +#ifdef __HAIKU__ +#include <fcntl.h> +#else +#include <sys/fcntl.h> +#endif #else #include <sys/ioctl.h> #endif @@ -58,19 +57,19 @@ int PacketPeerUDPPosix::get_available_packet_count() const { - Error err = const_cast<PacketPeerUDPPosix*>(this)->_poll(false); - if (err!=OK) + Error err = const_cast<PacketPeerUDPPosix *>(this)->_poll(false); + if (err != OK) return 0; return queue_count; } -Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer,int &r_buffer_size) const{ +Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer, int &r_buffer_size) const { - Error err = const_cast<PacketPeerUDPPosix*>(this)->_poll(false); - if (err!=OK) + Error err = const_cast<PacketPeerUDPPosix *>(this)->_poll(false); + if (err != OK) return err; - if (queue_count==0) + if (queue_count == 0) return ERR_UNAVAILABLE; uint32_t size; @@ -78,38 +77,37 @@ Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer,int &r_buffer_size rb.read(&type, 1, true); if (type == IP::TYPE_IPV4) { uint8_t ip[4]; - rb.read(ip,4,true); + rb.read(ip, 4, true); packet_ip.set_ipv4(ip); } else { uint8_t ipv6[16]; - rb.read(ipv6,16,true); + rb.read(ipv6, 16, true); packet_ip.set_ipv6(ipv6); }; - rb.read((uint8_t*)&packet_port,4,true); - rb.read((uint8_t*)&size,4,true); - rb.read(packet_buffer,size,true); + rb.read((uint8_t *)&packet_port, 4, true); + rb.read((uint8_t *)&size, 4, true); + rb.read(packet_buffer, size, true); --queue_count; - *r_buffer=packet_buffer; - r_buffer_size=size; + *r_buffer = packet_buffer; + r_buffer_size = size; return OK; - } -Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer,int p_buffer_size){ +Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer, int p_buffer_size) { ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED); - if (sock_type==IP::TYPE_NONE) + if (sock_type == IP::TYPE_NONE) sock_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; int sock = _get_socket(); - ERR_FAIL_COND_V( sock == -1, FAILED ); + ERR_FAIL_COND_V(sock == -1, FAILED); struct sockaddr_storage addr; size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, sock_type); errno = 0; int err; - while ( (err = sendto(sock, p_buffer, p_buffer_size, 0, (struct sockaddr*)&addr, addr_size)) != p_buffer_size) { + while ((err = sendto(sock, p_buffer, p_buffer_size, 0, (struct sockaddr *)&addr, addr_size)) != p_buffer_size) { if (errno != EAGAIN) { return FAILED; @@ -119,15 +117,15 @@ Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer,int p_buffer_size){ return OK; } -int PacketPeerUDPPosix::get_max_packet_size() const{ +int PacketPeerUDPPosix::get_max_packet_size() const { return 512; // uhm maybe not } Error PacketPeerUDPPosix::listen(int p_port, IP_Address p_bind_address, int p_recv_buffer_size) { - ERR_FAIL_COND_V(sockfd!=-1,ERR_ALREADY_IN_USE); - ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(),ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(sockfd != -1, ERR_ALREADY_IN_USE); + ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER); #ifdef __OpenBSD__ sock_type = IP::TYPE_IPV4; // OpenBSD does not support dual stacking, fallback to IPv4 only. @@ -135,18 +133,18 @@ Error PacketPeerUDPPosix::listen(int p_port, IP_Address p_bind_address, int p_re sock_type = IP::TYPE_ANY; #endif - if(p_bind_address.is_valid()) + if (p_bind_address.is_valid()) sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; int sock = _get_socket(); - if (sock == -1 ) + if (sock == -1) return ERR_CANT_CREATE; - sockaddr_storage addr = {0}; + sockaddr_storage addr = { 0 }; size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, IP_Address()); - if (bind(sock, (struct sockaddr*)&addr, addr_size) == -1 ) { + if (bind(sock, (struct sockaddr *)&addr, addr_size) == -1) { close(); return ERR_UNAVAILABLE; } @@ -154,17 +152,16 @@ Error PacketPeerUDPPosix::listen(int p_port, IP_Address p_bind_address, int p_re return OK; } -void PacketPeerUDPPosix::close(){ +void PacketPeerUDPPosix::close() { if (sockfd != -1) ::close(sockfd); - sockfd=-1; + sockfd = -1; sock_type = IP::TYPE_NONE; rb.resize(16); - queue_count=0; + queue_count = 0; } - Error PacketPeerUDPPosix::wait() { return _poll(true); @@ -172,22 +169,22 @@ Error PacketPeerUDPPosix::wait() { Error PacketPeerUDPPosix::_poll(bool p_wait) { - if (sockfd==-1) { + if (sockfd == -1) { return FAILED; } - struct sockaddr_storage from = {0}; + struct sockaddr_storage from = { 0 }; socklen_t len = sizeof(struct sockaddr_storage); int ret; - while ( (ret = recvfrom(sockfd, recv_buffer, MIN((int)sizeof(recv_buffer),MAX(rb.space_left()-24, 0)), p_wait?0:MSG_DONTWAIT, (struct sockaddr*)&from, &len)) > 0) { + while ((ret = recvfrom(sockfd, recv_buffer, MIN((int)sizeof(recv_buffer), MAX(rb.space_left() - 24, 0)), p_wait ? 0 : MSG_DONTWAIT, (struct sockaddr *)&from, &len)) > 0) { uint32_t port = 0; if (from.ss_family == AF_INET) { uint8_t type = (uint8_t)IP::TYPE_IPV4; rb.write(&type, 1); - struct sockaddr_in* sin_from = (struct sockaddr_in*)&from; - rb.write((uint8_t*)&sin_from->sin_addr, 4); + struct sockaddr_in *sin_from = (struct sockaddr_in *)&from; + rb.write((uint8_t *)&sin_from->sin_addr, 4); port = ntohs(sin_from->sin_port); } else if (from.ss_family == AF_INET6) { @@ -195,8 +192,8 @@ Error PacketPeerUDPPosix::_poll(bool p_wait) { uint8_t type = (uint8_t)IP::TYPE_IPV6; rb.write(&type, 1); - struct sockaddr_in6* s6_from = (struct sockaddr_in6*)&from; - rb.write((uint8_t*)&s6_from->sin6_addr, 16); + struct sockaddr_in6 *s6_from = (struct sockaddr_in6 *)&from; + rb.write((uint8_t *)&s6_from->sin6_addr, 16); port = ntohs(s6_from->sin6_port); @@ -206,26 +203,25 @@ Error PacketPeerUDPPosix::_poll(bool p_wait) { rb.write(&type, 1); }; - rb.write((uint8_t*)&port, 4); - rb.write((uint8_t*)&ret, 4); + rb.write((uint8_t *)&port, 4); + rb.write((uint8_t *)&ret, 4); rb.write(recv_buffer, ret); len = sizeof(struct sockaddr_storage); ++queue_count; }; - // TODO: Should ECONNRESET be handled here? - if (ret == 0 || (ret == -1 && errno != EAGAIN) ) { + if (ret == 0 || (ret == -1 && errno != EAGAIN)) { close(); return FAILED; }; return OK; } -bool PacketPeerUDPPosix::is_listening() const{ +bool PacketPeerUDPPosix::is_listening() const { - return sockfd!=-1; + return sockfd != -1; } IP_Address PacketPeerUDPPosix::get_packet_address() const { @@ -233,14 +229,14 @@ IP_Address PacketPeerUDPPosix::get_packet_address() const { return packet_ip; } -int PacketPeerUDPPosix::get_packet_port() const{ +int PacketPeerUDPPosix::get_packet_port() const { return packet_port; } int PacketPeerUDPPosix::_get_socket() { - ERR_FAIL_COND_V(sock_type==IP::TYPE_NONE, -1); + ERR_FAIL_COND_V(sock_type == IP::TYPE_NONE, -1); if (sockfd != -1) return sockfd; @@ -250,14 +246,13 @@ int PacketPeerUDPPosix::_get_socket() { return sockfd; } +void PacketPeerUDPPosix::set_dest_address(const IP_Address &p_address, int p_port) { -void PacketPeerUDPPosix::set_dest_address(const IP_Address& p_address,int p_port) { - - peer_addr=p_address; - peer_port=p_port; + peer_addr = p_address; + peer_port = p_port; } -PacketPeerUDP* PacketPeerUDPPosix::_create() { +PacketPeerUDP *PacketPeerUDPPosix::_create() { return memnew(PacketPeerUDPPosix); }; @@ -267,13 +262,12 @@ void PacketPeerUDPPosix::make_default() { PacketPeerUDP::_create = PacketPeerUDPPosix::_create; }; - PacketPeerUDPPosix::PacketPeerUDPPosix() { - sockfd=-1; - packet_port=0; - queue_count=0; - peer_port=0; + sockfd = -1; + packet_port = 0; + queue_count = 0; + peer_port = 0; sock_type = IP::TYPE_NONE; rb.resize(16); } diff --git a/drivers/unix/packet_peer_udp_posix.h b/drivers/unix/packet_peer_udp_posix.h index ac68344d78..b44ef49f2c 100644 --- a/drivers/unix/packet_peer_udp_posix.h +++ b/drivers/unix/packet_peer_udp_posix.h @@ -36,9 +36,8 @@ class PacketPeerUDPPosix : public PacketPeerUDP { - enum { - PACKET_BUFFER_SIZE=65536 + PACKET_BUFFER_SIZE = 65536 }; mutable RingBuffer<uint8_t> rb; @@ -55,18 +54,17 @@ class PacketPeerUDPPosix : public PacketPeerUDP { _FORCE_INLINE_ int _get_socket(); - static PacketPeerUDP* _create(); + static PacketPeerUDP *_create(); virtual Error _poll(bool p_block); public: - virtual int get_available_packet_count() const; - virtual Error get_packet(const uint8_t **r_buffer,int &r_buffer_size) const; - virtual Error put_packet(const uint8_t *p_buffer,int p_buffer_size); + virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) const; + virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size); virtual int get_max_packet_size() const; - virtual Error listen(int p_port, IP_Address p_bind_address=IP_Address("*"), int p_recv_buffer_size=65536); + virtual Error listen(int p_port, IP_Address p_bind_address = IP_Address("*"), int p_recv_buffer_size = 65536); virtual void close(); virtual Error wait(); virtual bool is_listening() const; @@ -74,7 +72,7 @@ public: virtual IP_Address get_packet_address() const; virtual int get_packet_port() const; - virtual void set_dest_address(const IP_Address& p_address,int p_port); + virtual void set_dest_address(const IP_Address &p_address, int p_port); static void make_default(); diff --git a/drivers/unix/rw_lock_posix.cpp b/drivers/unix/rw_lock_posix.cpp index 455dde73b1..9c9ad26165 100644 --- a/drivers/unix/rw_lock_posix.cpp +++ b/drivers/unix/rw_lock_posix.cpp @@ -30,17 +30,17 @@ #include "rw_lock_posix.h" -#include "os/memory.h" #include "error_macros.h" +#include "os/memory.h" #include <stdio.h> void RWLockPosix::read_lock() { - int err =pthread_rwlock_rdlock(&rwlock); - if (err!=0) { + int err = pthread_rwlock_rdlock(&rwlock); + if (err != 0) { perror("wtf: "); } - ERR_FAIL_COND(err!=0); + ERR_FAIL_COND(err != 0); } void RWLockPosix::read_unlock() { @@ -50,18 +50,17 @@ void RWLockPosix::read_unlock() { Error RWLockPosix::read_try_lock() { - if (pthread_rwlock_tryrdlock(&rwlock)!=0) { + if (pthread_rwlock_tryrdlock(&rwlock) != 0) { return ERR_BUSY; } else { return OK; } - } void RWLockPosix::write_lock() { int err = pthread_rwlock_wrlock(&rwlock); - ERR_FAIL_COND(err!=0); + ERR_FAIL_COND(err != 0); } void RWLockPosix::write_unlock() { @@ -70,36 +69,32 @@ void RWLockPosix::write_unlock() { } Error RWLockPosix::write_try_lock() { - if (pthread_rwlock_trywrlock(&rwlock)!=0) { + if (pthread_rwlock_trywrlock(&rwlock) != 0) { return ERR_BUSY; } else { return OK; } } - RWLock *RWLockPosix::create_func_posix() { - return memnew( RWLockPosix ); + return memnew(RWLockPosix); } void RWLockPosix::make_default() { - create_func=create_func_posix; + create_func = create_func_posix; } - RWLockPosix::RWLockPosix() { //rwlock=PTHREAD_RWLOCK_INITIALIZER; fails on OSX - pthread_rwlock_init(&rwlock,NULL); + pthread_rwlock_init(&rwlock, NULL); } - RWLockPosix::~RWLockPosix() { pthread_rwlock_destroy(&rwlock); - } #endif diff --git a/drivers/unix/rw_lock_posix.h b/drivers/unix/rw_lock_posix.h index 35a686b15c..429b5c22d7 100644 --- a/drivers/unix/rw_lock_posix.h +++ b/drivers/unix/rw_lock_posix.h @@ -31,18 +31,16 @@ #if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED) -#include <pthread.h> #include "os/rw_lock.h" +#include <pthread.h> class RWLockPosix : public RWLock { - pthread_rwlock_t rwlock; static RWLock *create_func_posix(); public: - virtual void read_lock(); virtual void read_unlock(); virtual Error read_try_lock(); @@ -56,10 +54,8 @@ public: RWLockPosix(); ~RWLockPosix(); - }; #endif - #endif // RWLOCKPOSIX_H diff --git a/drivers/unix/semaphore_posix.cpp b/drivers/unix/semaphore_posix.cpp index 83b34a42dd..69f499bb52 100644 --- a/drivers/unix/semaphore_posix.cpp +++ b/drivers/unix/semaphore_posix.cpp @@ -36,10 +36,9 @@ Error SemaphorePosix::wait() { - - while(sem_wait(&sem)) { - if (errno==EINTR) { - errno=0; + while (sem_wait(&sem)) { + if (errno == EINTR) { + errno = 0; continue; } else { perror("sem waiting"); @@ -51,39 +50,36 @@ Error SemaphorePosix::wait() { Error SemaphorePosix::post() { - return (sem_post(&sem)==0)?OK:ERR_BUSY; + return (sem_post(&sem) == 0) ? OK : ERR_BUSY; } int SemaphorePosix::get() const { int val; sem_getvalue(&sem, &val); - - return val; -} + return val; +} Semaphore *SemaphorePosix::create_semaphore_posix() { - return memnew( SemaphorePosix ); + return memnew(SemaphorePosix); } void SemaphorePosix::make_default() { - create_func=create_semaphore_posix; + create_func = create_semaphore_posix; } SemaphorePosix::SemaphorePosix() { - int r = sem_init(&sem,0,0); + int r = sem_init(&sem, 0, 0); if (r != 0) perror("sem creating"); } - SemaphorePosix::~SemaphorePosix() { sem_destroy(&sem); } - #endif diff --git a/drivers/unix/semaphore_posix.h b/drivers/unix/semaphore_posix.h index 96d1ff5c06..66e10db3c3 100644 --- a/drivers/unix/semaphore_posix.h +++ b/drivers/unix/semaphore_posix.h @@ -29,8 +29,6 @@ #ifndef SEMAPHORE_POSIX_H #define SEMAPHORE_POSIX_H - - #include "os/semaphore.h" #if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED) @@ -46,16 +44,14 @@ class SemaphorePosix : public Semaphore { static Semaphore *create_semaphore_posix(); public: - virtual Error wait(); - virtual Error post(); + virtual Error post(); virtual int get() const; static void make_default(); SemaphorePosix(); - - ~SemaphorePosix(); + ~SemaphorePosix(); }; #endif diff --git a/drivers/unix/socket_helpers.h b/drivers/unix/socket_helpers.h index fd5fa618ca..d27328a01e 100644 --- a/drivers/unix/socket_helpers.h +++ b/drivers/unix/socket_helpers.h @@ -31,28 +31,28 @@ #include <string.h> -#if defined(__MINGW32__ ) && (!defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 4) - // Workaround for mingw-w64 < 4.0 - #ifndef IPV6_V6ONLY - #define IPV6_V6ONLY 27 - #endif +#if defined(__MINGW32__) && (!defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 4) +// Workaround for mingw-w64 < 4.0 +#ifndef IPV6_V6ONLY +#define IPV6_V6ONLY 27 +#endif #endif // helpers for sockaddr -> IP_Address and back, should work for posix and winsock. All implementations should use this -static size_t _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p_ip, int p_port, IP::Type p_sock_type = IP::TYPE_ANY) { +static size_t _set_sockaddr(struct sockaddr_storage *p_addr, const IP_Address &p_ip, int p_port, IP::Type p_sock_type = IP::TYPE_ANY) { memset(p_addr, 0, sizeof(struct sockaddr_storage)); - ERR_FAIL_COND_V(!p_ip.is_valid(),0); + ERR_FAIL_COND_V(!p_ip.is_valid(), 0); // IPv6 socket if (p_sock_type == IP::TYPE_IPV6 || p_sock_type == IP::TYPE_ANY) { // IPv6 only socket with IPv4 address - ERR_FAIL_COND_V(p_sock_type == IP::TYPE_IPV6 && p_ip.is_ipv4(),0); + ERR_FAIL_COND_V(p_sock_type == IP::TYPE_IPV6 && p_ip.is_ipv4(), 0); - struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr; + struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr; addr6->sin6_family = AF_INET6; addr6->sin6_port = htons(p_port); copymem(&addr6->sin6_addr.s6_addr, p_ip.get_ipv6(), 16); @@ -61,36 +61,36 @@ static size_t _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p } else { // IPv4 socket // IPv4 socket with IPv6 address - ERR_FAIL_COND_V(!p_ip.is_ipv4(),0); + ERR_FAIL_COND_V(!p_ip.is_ipv4(), 0); uint32_t ipv4 = *((uint32_t *)p_ip.get_ipv4()); - struct sockaddr_in* addr4 = (struct sockaddr_in*)p_addr; + struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr; addr4->sin_family = AF_INET; - addr4->sin_port = htons(p_port); // short, network byte order + addr4->sin_port = htons(p_port); // short, network byte order copymem(&addr4->sin_addr.s_addr, p_ip.get_ipv4(), 16); return sizeof(sockaddr_in); }; }; -static size_t _set_listen_sockaddr(struct sockaddr_storage* p_addr, int p_port, IP::Type p_sock_type, const IP_Address p_bind_address) { +static size_t _set_listen_sockaddr(struct sockaddr_storage *p_addr, int p_port, IP::Type p_sock_type, const IP_Address p_bind_address) { memset(p_addr, 0, sizeof(struct sockaddr_storage)); if (p_sock_type == IP::TYPE_IPV4) { - struct sockaddr_in* addr4 = (struct sockaddr_in*)p_addr; + struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr; addr4->sin_family = AF_INET; addr4->sin_port = htons(p_port); - if(p_bind_address.is_valid()) { + if (p_bind_address.is_valid()) { copymem(&addr4->sin_addr.s_addr, p_bind_address.get_ipv4(), 4); } else { addr4->sin_addr.s_addr = INADDR_ANY; } return sizeof(sockaddr_in); } else { - struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr; + struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr; addr6->sin6_family = AF_INET6; addr6->sin6_port = htons(p_port); - if(p_bind_address.is_valid()) { + if (p_bind_address.is_valid()) { copymem(&addr6->sin6_addr.s6_addr, p_bind_address.get_ipv6(), 16); } else { addr6->sin6_addr = in6addr_any; @@ -106,12 +106,12 @@ static int _socket_create(IP::Type p_type, int type, int protocol) { int family = p_type == IP::TYPE_IPV4 ? AF_INET : AF_INET6; int sockfd = socket(family, type, protocol); - ERR_FAIL_COND_V( sockfd == -1, -1 ); + ERR_FAIL_COND_V(sockfd == -1, -1); - if(family == AF_INET6) { + if (family == AF_INET6) { // Select IPv4 over IPv6 mapping int opt = p_type != IP::TYPE_ANY; - if(setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&opt, sizeof(opt)) != 0) { + if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&opt, sizeof(opt)) != 0) { WARN_PRINT("Unable to set/unset IPv4 address mapping over IPv6"); } } @@ -119,24 +119,22 @@ static int _socket_create(IP::Type p_type, int type, int protocol) { return sockfd; } - -static void _set_ip_addr_port(IP_Address& r_ip, int& r_port, struct sockaddr_storage* p_addr) { +static void _set_ip_addr_port(IP_Address &r_ip, int &r_port, struct sockaddr_storage *p_addr) { if (p_addr->ss_family == AF_INET) { - struct sockaddr_in* addr4 = (struct sockaddr_in*)p_addr; + struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr; r_ip.set_ipv4((uint8_t *)&(addr4->sin_addr.s_addr)); r_port = ntohs(addr4->sin_port); } else if (p_addr->ss_family == AF_INET6) { - struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr; + struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr; r_ip.set_ipv6(addr6->sin6_addr.s6_addr); r_port = ntohs(addr6->sin6_port); }; }; - #endif diff --git a/drivers/unix/stream_peer_tcp_posix.cpp b/drivers/unix/stream_peer_tcp_posix.cpp index 08a2954617..fc4838f1e0 100644 --- a/drivers/unix/stream_peer_tcp_posix.cpp +++ b/drivers/unix/stream_peer_tcp_posix.cpp @@ -30,21 +30,21 @@ #include "stream_peer_tcp_posix.h" +#include <errno.h> +#include <netdb.h> #include <poll.h> #include <stdio.h> #include <stdlib.h> -#include <unistd.h> -#include <errno.h> #include <string.h> -#include <netdb.h> -#include <sys/types.h> #include <sys/ioctl.h> +#include <sys/types.h> +#include <unistd.h> #ifndef NO_FCNTL - #ifdef __HAIKU__ - #include <fcntl.h> - #else - #include <sys/fcntl.h> - #endif +#ifdef __HAIKU__ +#include <fcntl.h> +#else +#include <sys/fcntl.h> +#endif #else #include <sys/ioctl.h> #endif @@ -58,12 +58,12 @@ #include <netinet/tcp.h> #if defined(OSX_ENABLED) || defined(IPHONE_ENABLED) - #define MSG_NOSIGNAL SO_NOSIGPIPE +#define MSG_NOSIGNAL SO_NOSIGPIPE #endif #include "drivers/unix/socket_helpers.h" -StreamPeerTCP* StreamPeerTCPPosix::_create() { +StreamPeerTCP *StreamPeerTCPPosix::_create() { return memnew(StreamPeerTCPPosix); }; @@ -95,7 +95,7 @@ Error StreamPeerTCPPosix::_poll_connection() const { struct sockaddr_storage their_addr; size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port, sock_type); - if (::connect(sockfd, (struct sockaddr *)&their_addr,addr_size) == -1) { + if (::connect(sockfd, (struct sockaddr *)&their_addr, addr_size) == -1) { if (errno == EISCONN) { status = STATUS_CONNECTED; @@ -134,9 +134,9 @@ void StreamPeerTCPPosix::set_socket(int p_sockfd, IP_Address p_host, int p_port, peer_port = p_port; }; -Error StreamPeerTCPPosix::connect_to_host(const IP_Address& p_host, uint16_t p_port) { +Error StreamPeerTCPPosix::connect_to_host(const IP_Address &p_host, uint16_t p_port) { - ERR_FAIL_COND_V( !p_host.is_valid(), ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER); sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP); @@ -158,7 +158,7 @@ Error StreamPeerTCPPosix::connect_to_host(const IP_Address& p_host, uint16_t p_p size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port, sock_type); errno = 0; - if (::connect(sockfd, (struct sockaddr *)&their_addr,addr_size) == -1 && errno != EINPROGRESS) { + if (::connect(sockfd, (struct sockaddr *)&their_addr, addr_size) == -1 && errno != EINPROGRESS) { ERR_PRINT("Connection to remote host failed!"); disconnect_from_host(); @@ -177,7 +177,7 @@ Error StreamPeerTCPPosix::connect_to_host(const IP_Address& p_host, uint16_t p_p return OK; }; -Error StreamPeerTCPPosix::write(const uint8_t* p_data,int p_bytes, int &r_sent, bool p_block) { +Error StreamPeerTCPPosix::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block) { if (status == STATUS_NONE || status == STATUS_ERROR) { @@ -237,7 +237,7 @@ Error StreamPeerTCPPosix::write(const uint8_t* p_data,int p_bytes, int &r_sent, return OK; }; -Error StreamPeerTCPPosix::read(uint8_t* p_buffer, int p_bytes,int &r_received, bool p_block) { +Error StreamPeerTCPPosix::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block) { if (!is_connected_to_host()) { @@ -282,9 +282,9 @@ Error StreamPeerTCPPosix::read(uint8_t* p_buffer, int p_bytes,int &r_received, b }; _block(sockfd, true, false); - } else if (read==0) { + } else if (read == 0) { - sockfd=-1; + sockfd = -1; status = STATUS_NONE; peer_port = 0; peer_host = IP_Address(); @@ -305,8 +305,8 @@ Error StreamPeerTCPPosix::read(uint8_t* p_buffer, int p_bytes,int &r_received, b void StreamPeerTCPPosix::set_nodelay(bool p_enabled) { ERR_FAIL_COND(!is_connected_to_host()); - int flag=p_enabled?1:0; - setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int)); + int flag = p_enabled ? 1 : 0; + setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int)); } bool StreamPeerTCPPosix::is_connected_to_host() const { @@ -319,7 +319,7 @@ bool StreamPeerTCPPosix::is_connected_to_host() const { return true; }; - return (sockfd!=-1); + return (sockfd != -1); }; StreamPeerTCP::Status StreamPeerTCPPosix::get_status() const { @@ -331,39 +331,37 @@ StreamPeerTCP::Status StreamPeerTCPPosix::get_status() const { return status; }; - void StreamPeerTCPPosix::disconnect_from_host() { if (sockfd != -1) close(sockfd); sock_type = IP::TYPE_NONE; - sockfd=-1; + sockfd = -1; status = STATUS_NONE; peer_port = 0; peer_host = IP_Address(); }; - -Error StreamPeerTCPPosix::put_data(const uint8_t* p_data,int p_bytes) { +Error StreamPeerTCPPosix::put_data(const uint8_t *p_data, int p_bytes) { int total; return write(p_data, p_bytes, total, true); }; -Error StreamPeerTCPPosix::put_partial_data(const uint8_t* p_data,int p_bytes, int &r_sent) { +Error StreamPeerTCPPosix::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) { return write(p_data, p_bytes, r_sent, false); }; -Error StreamPeerTCPPosix::get_data(uint8_t* p_buffer, int p_bytes) { +Error StreamPeerTCPPosix::get_data(uint8_t *p_buffer, int p_bytes) { int total; return read(p_buffer, p_bytes, total, true); }; -Error StreamPeerTCPPosix::get_partial_data(uint8_t* p_buffer, int p_bytes,int &r_received) { +Error StreamPeerTCPPosix::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) { return read(p_buffer, p_bytes, r_received, false); }; @@ -371,10 +369,9 @@ Error StreamPeerTCPPosix::get_partial_data(uint8_t* p_buffer, int p_bytes,int &r int StreamPeerTCPPosix::get_available_bytes() const { unsigned long len; - int ret = ioctl(sockfd,FIONREAD,&len); - ERR_FAIL_COND_V(ret==-1,0) + int ret = ioctl(sockfd, FIONREAD, &len); + ERR_FAIL_COND_V(ret == -1, 0) return len; - } IP_Address StreamPeerTCPPosix::get_connected_host() const { diff --git a/drivers/unix/stream_peer_tcp_posix.h b/drivers/unix/stream_peer_tcp_posix.h index 7f8d90a448..ef98f2ab83 100644 --- a/drivers/unix/stream_peer_tcp_posix.h +++ b/drivers/unix/stream_peer_tcp_posix.h @@ -38,7 +38,6 @@ class StreamPeerTCPPosix : public StreamPeerTCP { protected: - mutable Status status; IP::Type sock_type; @@ -51,20 +50,19 @@ protected: IP_Address peer_host; int peer_port; - Error write(const uint8_t* p_data,int p_bytes, int &r_sent, bool p_block); - Error read(uint8_t* p_buffer, int p_bytes,int &r_received, bool p_block); + Error write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block); + Error read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block); - static StreamPeerTCP* _create(); + static StreamPeerTCP *_create(); public: + virtual Error connect_to_host(const IP_Address &p_host, uint16_t p_port); - virtual Error connect_to_host(const IP_Address& p_host, uint16_t p_port); - - virtual Error put_data(const uint8_t* p_data,int p_bytes); - virtual Error put_partial_data(const uint8_t* p_data,int p_bytes, int &r_sent); + virtual Error put_data(const uint8_t *p_data, int p_bytes); + virtual Error put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent); - virtual Error get_data(uint8_t* p_buffer, int p_bytes); - virtual Error get_partial_data(uint8_t* p_buffer, int p_bytes,int &r_received); + virtual Error get_data(uint8_t *p_buffer, int p_bytes); + virtual Error get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received); virtual int get_available_bytes() const; diff --git a/drivers/unix/tcp_server_posix.cpp b/drivers/unix/tcp_server_posix.cpp index 7e9970453f..9049faebb8 100644 --- a/drivers/unix/tcp_server_posix.cpp +++ b/drivers/unix/tcp_server_posix.cpp @@ -33,32 +33,32 @@ #include <poll.h> +#include <errno.h> +#include <netdb.h> #include <stdio.h> #include <stdlib.h> -#include <unistd.h> -#include <errno.h> #include <string.h> -#include <netdb.h> #include <sys/types.h> +#include <unistd.h> #ifndef NO_FCNTL - #ifdef __HAIKU__ - #include <fcntl.h> - #else - #include <sys/fcntl.h> - #endif +#ifdef __HAIKU__ +#include <fcntl.h> +#else +#include <sys/fcntl.h> +#endif #else #include <sys/ioctl.h> #endif #ifdef JAVASCRIPT_ENABLED #include <arpa/inet.h> #endif +#include <assert.h> #include <netinet/in.h> #include <sys/socket.h> -#include <assert.h> #include "drivers/unix/socket_helpers.h" -TCP_Server* TCPServerPosix::_create() { +TCP_Server *TCPServerPosix::_create() { return memnew(TCPServerPosix); }; @@ -68,9 +68,9 @@ void TCPServerPosix::make_default() { TCP_Server::_create = TCPServerPosix::_create; }; -Error TCPServerPosix::listen(uint16_t p_port,const IP_Address p_bind_address) { +Error TCPServerPosix::listen(uint16_t p_port, const IP_Address p_bind_address) { - ERR_FAIL_COND_V(listen_sockfd!=-1,ERR_ALREADY_IN_USE); + ERR_FAIL_COND_V(listen_sockfd != -1, ERR_ALREADY_IN_USE); ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER); int sockfd; @@ -95,8 +95,8 @@ Error TCPServerPosix::listen(uint16_t p_port,const IP_Address p_bind_address) { ioctl(sockfd, FIONBIO, &bval); #endif - int reuse=1; - if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) { + int reuse = 1; + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) { WARN_PRINT("REUSEADDR failed!") } @@ -110,8 +110,7 @@ Error TCPServerPosix::listen(uint16_t p_port,const IP_Address p_bind_address) { close(sockfd); ERR_FAIL_V(FAILED); }; - } - else { + } else { return ERR_ALREADY_IN_USE; }; @@ -177,14 +176,13 @@ void TCPServerPosix::stop() { if (listen_sockfd != -1) { int ret = close(listen_sockfd); - ERR_FAIL_COND(ret!=0); + ERR_FAIL_COND(ret != 0); }; listen_sockfd = -1; sock_type = IP::TYPE_NONE; }; - TCPServerPosix::TCPServerPosix() { listen_sockfd = -1; diff --git a/drivers/unix/tcp_server_posix.h b/drivers/unix/tcp_server_posix.h index ea42d0fc0c..408179c197 100644 --- a/drivers/unix/tcp_server_posix.h +++ b/drivers/unix/tcp_server_posix.h @@ -37,11 +37,10 @@ class TCPServerPosix : public TCP_Server { int listen_sockfd; IP::Type sock_type; - static TCP_Server* _create(); + static TCP_Server *_create(); public: - - virtual Error listen(uint16_t p_port, IP_Address p_bind_address=IP_Address("*")); + virtual Error listen(uint16_t p_port, IP_Address p_bind_address = IP_Address("*")); virtual bool is_connection_available() const; virtual Ref<StreamPeerTCP> take_connection(); @@ -53,6 +52,5 @@ public: ~TCPServerPosix(); }; - #endif // TCP_SERVER_POSIX_H #endif diff --git a/drivers/unix/thread_posix.cpp b/drivers/unix/thread_posix.cpp index ecea67c37b..c33cc8cc5d 100644 --- a/drivers/unix/thread_posix.cpp +++ b/drivers/unix/thread_posix.cpp @@ -39,18 +39,18 @@ Thread::ID ThreadPosix::get_ID() const { - return id; + return id; } -Thread* ThreadPosix::create_thread_posix() { +Thread *ThreadPosix::create_thread_posix() { - return memnew( ThreadPosix ); + return memnew(ThreadPosix); } void *ThreadPosix::thread_callback(void *userdata) { - ThreadPosix *t=reinterpret_cast<ThreadPosix*>(userdata); - t->id=(ID)pthread_self(); + ThreadPosix *t = reinterpret_cast<ThreadPosix *>(userdata); + t->id = (ID)pthread_self(); ScriptServer::thread_enter(); //scripts may need to attach a stack @@ -61,80 +61,77 @@ void *ThreadPosix::thread_callback(void *userdata) { return NULL; } -Thread* ThreadPosix::create_func_posix(ThreadCreateCallback p_callback,void *p_user,const Settings&) { +Thread *ThreadPosix::create_func_posix(ThreadCreateCallback p_callback, void *p_user, const Settings &) { - ThreadPosix *tr= memnew(ThreadPosix); - tr->callback=p_callback; - tr->user=p_user; + ThreadPosix *tr = memnew(ThreadPosix); + tr->callback = p_callback; + tr->user = p_user; pthread_attr_init(&tr->pthread_attr); pthread_attr_setdetachstate(&tr->pthread_attr, PTHREAD_CREATE_JOINABLE); pthread_attr_setstacksize(&tr->pthread_attr, 256 * 1024); - + pthread_create(&tr->pthread, &tr->pthread_attr, thread_callback, tr); - + return tr; } Thread::ID ThreadPosix::get_thread_ID_func_posix() { return (ID)pthread_self(); } -void ThreadPosix::wait_to_finish_func_posix(Thread* p_thread) { +void ThreadPosix::wait_to_finish_func_posix(Thread *p_thread) { - ThreadPosix *tp=static_cast<ThreadPosix*>(p_thread); + ThreadPosix *tp = static_cast<ThreadPosix *>(p_thread); ERR_FAIL_COND(!tp); - ERR_FAIL_COND(tp->pthread==0); - - pthread_join(tp->pthread,NULL); - tp->pthread=0; + ERR_FAIL_COND(tp->pthread == 0); + + pthread_join(tp->pthread, NULL); + tp->pthread = 0; } -Error ThreadPosix::set_name_func_posix(const String& p_name) { +Error ThreadPosix::set_name_func_posix(const String &p_name) { pthread_t running_thread = pthread_self(); - #ifdef PTHREAD_NO_RENAME +#ifdef PTHREAD_NO_RENAME return ERR_UNAVAILABLE; - #else +#else - #ifdef PTHREAD_RENAME_SELF +#ifdef PTHREAD_RENAME_SELF // check if thread is the same as caller int err = pthread_setname_np(p_name.utf8().get_data()); - - #else - #ifdef PTHREAD_BSD_SET_NAME +#else + +#ifdef PTHREAD_BSD_SET_NAME pthread_set_name_np(running_thread, p_name.utf8().get_data()); int err = 0; // Open/FreeBSD ignore errors in this function - #else +#else int err = pthread_setname_np(running_thread, p_name.utf8().get_data()); - #endif // PTHREAD_BSD_SET_NAME +#endif // PTHREAD_BSD_SET_NAME - #endif // PTHREAD_RENAME_SELF +#endif // PTHREAD_RENAME_SELF return err == 0 ? OK : ERR_INVALID_PARAMETER; - #endif // PTHREAD_NO_RENAME +#endif // PTHREAD_NO_RENAME }; void ThreadPosix::make_default() { - create_func=create_func_posix; - get_thread_ID_func=get_thread_ID_func_posix; - wait_to_finish_func=wait_to_finish_func_posix; + create_func = create_func_posix; + get_thread_ID_func = get_thread_ID_func_posix; + wait_to_finish_func = wait_to_finish_func_posix; set_name_func = set_name_func_posix; } ThreadPosix::ThreadPosix() { - pthread=0; + pthread = 0; } - ThreadPosix::~ThreadPosix() { - } - #endif diff --git a/drivers/unix/thread_posix.h b/drivers/unix/thread_posix.h index cf360e164a..a756ed972c 100644 --- a/drivers/unix/thread_posix.h +++ b/drivers/unix/thread_posix.h @@ -35,9 +35,9 @@ #if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED) -#include <sys/types.h> -#include <pthread.h> #include "os/thread.h" +#include <pthread.h> +#include <sys/types.h> class ThreadPosix : public Thread { @@ -47,31 +47,26 @@ class ThreadPosix : public Thread { void *user; ID id; - static Thread* create_thread_posix(); - - + static Thread *create_thread_posix(); + static void *thread_callback(void *userdata); - - static Thread* create_func_posix(ThreadCreateCallback p_callback,void *,const Settings&); + + static Thread *create_func_posix(ThreadCreateCallback p_callback, void *, const Settings &); static ID get_thread_ID_func_posix(); - static void wait_to_finish_func_posix(Thread* p_thread); + static void wait_to_finish_func_posix(Thread *p_thread); - static Error set_name_func_posix(const String& p_name); + static Error set_name_func_posix(const String &p_name); + + ThreadPosix(); - ThreadPosix(); public: - - virtual ID get_ID() const; - + static void make_default(); - - - ~ThreadPosix(); + ~ThreadPosix(); }; - #endif #endif |