From 2ffb9279a753df4818bcf3a235bcba18982c6725 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Mon, 6 Jan 2025 00:52:02 +0200 Subject: [PATCH] draft epoll work --- CMakeLists.txt | 2 + src/core/libraries/network/epoll.cpp | 20 +++++++ src/core/libraries/network/epoll.h | 81 ++++++++++++++++++++++++++++ src/core/libraries/network/net.cpp | 9 +++- 4 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 src/core/libraries/network/epoll.cpp create mode 100644 src/core/libraries/network/epoll.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a917aea7a8..d36c7f7f49e 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -266,6 +266,8 @@ set(NETWORK_LIBS src/core/libraries/network/http.cpp src/core/libraries/network/posix_sockets.cpp src/core/libraries/network/sockets.h src/core/libraries/network/net_error.h + src/core/libraries/network/epoll.cpp + src/core/libraries/network/epoll.h ) set(AVPLAYER_LIB src/core/libraries/avplayer/avplayer_common.cpp diff --git a/src/core/libraries/network/epoll.cpp b/src/core/libraries/network/epoll.cpp new file mode 100644 index 00000000000..061ba48aed1 --- /dev/null +++ b/src/core/libraries/network/epoll.cpp @@ -0,0 +1,20 @@ +#include "epoll.h" + +namespace Libraries::Net { +int NetEpoll::Add(int id, net_socket sock, OrbisNetEpollEvent* ev) { + return 0; +} + +int NetEpoll::Del(int id, net_socket sock, OrbisNetEpollEvent* ev) { + return 0; +} + +int NetEpoll::Mod(int id, net_socket sock, OrbisNetEpollEvent* ev) { + return 0; +} + +int NetEpoll::Wait(OrbisNetEpollEvent* events, int maxevents, int timeout) { + return 0; +} + +} // namespace Libraries::Net \ No newline at end of file diff --git a/src/core/libraries/network/epoll.h b/src/core/libraries/network/epoll.h new file mode 100644 index 00000000000..902df96bf28 --- /dev/null +++ b/src/core/libraries/network/epoll.h @@ -0,0 +1,81 @@ +// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "common/types.h" +#ifdef _WIN32 +#define _WINSOCK_DEPRECATED_NO_WARNINGS +#include +#include +#include +typedef SOCKET net_socket; +typedef int socklen_t; +#else +#include +#include +#include +#include +#include +#include +#include +#include +typedef int net_socket; +#endif +#include +#include +#include + +namespace Libraries::Net { + +union OrbisNetEpollData { + void* ptr; + u32 _u32; + int fd; + u64 _u64; +}; + +struct OrbisNetEpollEvent { + u32 events; + u32 reserved; + u32 ident; + OrbisNetEpollData data; +}; + +struct EpollSocket { + unsigned int events; + OrbisNetEpollData data; + net_socket sock; +}; + +struct NetEpoll { + std::map eventEntries; + + int Add(int id, net_socket sock, OrbisNetEpollEvent* ev); + int Del(int id, net_socket sock, OrbisNetEpollEvent* ev); + int Mod(int id, net_socket sock, OrbisNetEpollEvent* ev); + int Wait(OrbisNetEpollEvent* events, int maxevents, int timeout); +}; + +typedef std::shared_ptr EpollPtr; + +class NetEpollInternal { +public: + explicit NetEpollInternal() = default; + ~NetEpollInternal() = default; + EpollPtr FindSocket(int sockid) { + std::scoped_lock lock{m_mutex}; + const auto it = epolls.find(sockid); + if (it != epolls.end()) { + return it->second; + } + return 0; + } + +public: + std::mutex m_mutex; + typedef std::map NetEpolls; + NetEpolls epolls; + int next_epool_sock_id = 0; +}; +} // namespace Libraries::Net \ No newline at end of file diff --git a/src/core/libraries/network/net.cpp b/src/core/libraries/network/net.cpp index 8892252db51..76bf89b5e32 100644 --- a/src/core/libraries/network/net.cpp +++ b/src/core/libraries/network/net.cpp @@ -16,6 +16,7 @@ #include "core/libraries/error_codes.h" #include "core/libraries/libs.h" #include "core/libraries/network/net.h" +#include "epoll.h" #include "net_error.h" #include "sockets.h" @@ -560,8 +561,12 @@ int PS4_SYSV_ABI sceNetEpollControl() { } int PS4_SYSV_ABI sceNetEpollCreate(const char* name, int flags) { - LOG_ERROR(Lib_Net, "(STUBBED) called"); - return ORBIS_OK; + LOG_ERROR(Lib_Net, "name = {} flags= {}", std::string(name), flags); + auto* net_epoll = Common::Singleton::Instance(); + auto epoll = std::make_shared(); + auto id = ++net_epoll->next_epool_sock_id; + net_epoll->epolls.emplace(id, epoll); + return id; } int PS4_SYSV_ABI sceNetEpollDestroy(int eid) {