Skip to content

Commit

Permalink
draft epoll work
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemoralis committed Jan 7, 2025
1 parent 8a1582b commit 6b62ba2
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/core/libraries/network/epoll.cpp
Original file line number Diff line number Diff line change
@@ -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
81 changes: 81 additions & 0 deletions src/core/libraries/network/epoll.h
Original file line number Diff line number Diff line change
@@ -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 <Ws2tcpip.h>
#include <iphlpapi.h>
#include <winsock2.h>
typedef SOCKET net_socket;
typedef int socklen_t;
#else
#include <cerrno>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
typedef int net_socket;
#endif
#include <map>
#include <memory>
#include <mutex>

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<int, EpollSocket> 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<NetEpoll> 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<int, EpollPtr> NetEpolls;
NetEpolls epolls;
int next_epool_sock_id = 0;
};
} // namespace Libraries::Net
9 changes: 7 additions & 2 deletions src/core/libraries/network/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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<NetEpollInternal>::Instance();
auto epoll = std::make_shared<NetEpoll>();
auto id = ++net_epoll->next_epool_sock_id;
net_epoll->epolls.emplace(id, epoll);
return id;
}

int PS4_SYSV_ABI sceNetEpollDestroy(int eid) {
Expand Down

0 comments on commit 6b62ba2

Please sign in to comment.