summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--context.h80
-rw-r--r--include/pso/TSocket.h86
-rw-r--r--include/pso/forward.h5
-rw-r--r--obj_files.mk1
-rw-r--r--src/pso/TSocket.cpp59
5 files changed, 226 insertions, 5 deletions
diff --git a/context.h b/context.h
index f42ba4a..007aa61 100644
--- a/context.h
+++ b/context.h
@@ -485,6 +485,13 @@ struct packet_header {
void bswap();
} __packed__;
+// pso/TSocket.h
+// Union defs.
+union ipv4_addr {
+ u32 addr;
+ TArray<u8, 4> addr_bytes;
+};
+
// pso/TObject.h
// Enum defs.
enum object_flags {
@@ -516,8 +523,79 @@ static inline void operator^=(object_flags &a, object_flags b) { a = a ^ b; };
static inline void operator&=(object_flags &a, object_flags b) { a = a & b; };
static inline void operator|=(object_flags &a, object_flags b) { a = a | b; };
-// pso/TPlyGuildCardTag.h
+// pso/TSocket.h
// Class defs.
+class TSocket : public TObject {
+private:
+ void set_flags(u8 flags) {
+ m_sock_flags |= flags;
+ };
+
+ void clear_flags(u8 flags) {
+ m_sock_flags &= ~flags;
+ };
+
+ void toggle_flags(u8 flags) {
+ m_sock_flags ^= flags;
+ };
+
+ u8 get_flags(u8 flags) {
+ return m_sock_flags & flags;
+ };
+
+ ipv4_addr m_dst_addr;
+ u16 m_dst_port;
+ u16 m_src_port;
+ ipv4_addr m_src_addr;
+ s16 m_sock_fd;
+ u8 m_sock_flags;
+ bool m_buffer_cleared;
+ s16 m_size;
+ s16 m_buffer_offset;
+ u32 m_unused;
+ TArray<u8, 64> m_unused2;
+ TArray<u8, 2048> m_packet_buffer;
+ s16 m_stat;
+ u16 m_unused3;
+ u32 m_send_window;
+ u32 m_recv_window;
+ void (*m_callback)(TSocket *socket);
+public:
+ TSocket(TObject *parent);
+ virtual ~TSocket();
+
+ virtual int open() = 0;
+ virtual int close() = 0;
+ virtual void recv() = 0;
+ virtual int send(u8 *data) = 0;
+ virtual int send(u8 *data, size_t size) = 0;
+
+ int resolve_domain(char *domain);
+ void set_ip_address(u32 addr);
+ void set_port(u32 port);
+ const u8 next();
+ int is_empty();
+
+ PRIVATE_MEMBER_ACCESSORS(ipv4_addr, dst_addr);
+ PRIVATE_MEMBER_ACCESSORS(u16, dst_port);
+ PRIVATE_MEMBER_ACCESSORS(u16, src_port);
+ PRIVATE_MEMBER_ACCESSORS(ipv4_addr, src_addr);
+ PRIVATE_MEMBER_ACCESSORS(s16, sock_fd);
+ PRIVATE_MEMBER_ACCESSORS(u8, sock_flags);
+ PRIVATE_MEMBER_ACCESSORS(bool, buffer_cleared);
+ PRIVATE_MEMBER_ACCESSORS(s16, size);
+ PRIVATE_MEMBER_ACCESSORS(s16, buffer_offset);
+ PRIVATE_MEMBER_ACCESSORS(u32, unused);
+ PRIVATE_MEMBER_ACCESSORS_ARRAY(u8, unused2, 64);
+ PRIVATE_MEMBER_ACCESSORS_ARRAY(u8, packet_buffer, 2048);
+ PRIVATE_MEMBER_ACCESSORS(s16, stat);
+ PRIVATE_MEMBER_ACCESSORS(u16, unused3);
+ PRIVATE_MEMBER_ACCESSORS(u32, send_window);
+ PRIVATE_MEMBER_ACCESSORS(u32, recv_window);
+ PRIVATE_MEMBER_ACCESSORS_FUNC(void, callback, TSocket *socket);
+};
+
+// pso/TPlyGuildCardTag.h
class TPlyGuildCardTag {
public:
u8 tag0;
diff --git a/include/pso/TSocket.h b/include/pso/TSocket.h
new file mode 100644
index 0000000..1b571d9
--- /dev/null
+++ b/include/pso/TSocket.h
@@ -0,0 +1,86 @@
+#ifndef TSOCKET_H
+#define TSOCKET_H
+
+#include <global_types.h>
+#include <string.h>
+#include <pso/macros.h>
+#include <pso/forward.h>
+#include <pso/TArray.h>
+#include <pso/TObject.h>
+
+union ipv4_addr {
+ u32 addr;
+ TArray<u8, 4> addr_bytes;
+};
+
+class TSocket : public TObject {
+private:
+ void set_flags(u8 flags) {
+ m_sock_flags |= flags;
+ };
+
+ void clear_flags(u8 flags) {
+ m_sock_flags &= ~flags;
+ };
+
+ void toggle_flags(u8 flags) {
+ m_sock_flags ^= flags;
+ };
+
+ u8 get_flags(u8 flags) {
+ return m_sock_flags & flags;
+ };
+
+ ipv4_addr m_dst_addr;
+ u16 m_dst_port;
+ u16 m_src_port;
+ ipv4_addr m_src_addr;
+ s16 m_sock_fd;
+ u8 m_sock_flags;
+ bool m_buffer_cleared;
+ s16 m_size;
+ s16 m_buffer_offset;
+ u32 m_unused;
+ TArray<u8, 64> m_unused2;
+ TArray<u8, 2048> m_packet_buffer;
+ s16 m_stat;
+ u16 m_unused3;
+ u32 m_send_window;
+ u32 m_recv_window;
+ void (*m_callback)(TSocket *socket);
+public:
+ TSocket(TObject *parent);
+ virtual ~TSocket();
+
+ virtual int open() = 0;
+ virtual int close() = 0;
+ virtual void recv() = 0;
+ virtual int send(u8 *data) = 0;
+ virtual int send(u8 *data, size_t size) = 0;
+
+ int resolve_domain(char *domain);
+ void set_ip_address(u32 addr);
+ void set_port(u32 port);
+ const u8 next();
+ int is_empty();
+
+ PRIVATE_MEMBER_ACCESSORS(ipv4_addr, dst_addr);
+ PRIVATE_MEMBER_ACCESSORS(u16, dst_port);
+ PRIVATE_MEMBER_ACCESSORS(u16, src_port);
+ PRIVATE_MEMBER_ACCESSORS(ipv4_addr, src_addr);
+ PRIVATE_MEMBER_ACCESSORS(s16, sock_fd);
+ PRIVATE_MEMBER_ACCESSORS(u8, sock_flags);
+ PRIVATE_MEMBER_ACCESSORS(bool, buffer_cleared);
+ PRIVATE_MEMBER_ACCESSORS(s16, size);
+ PRIVATE_MEMBER_ACCESSORS(s16, buffer_offset);
+ PRIVATE_MEMBER_ACCESSORS(u32, unused);
+ PRIVATE_MEMBER_ACCESSORS_ARRAY(u8, unused2, 64);
+ PRIVATE_MEMBER_ACCESSORS_ARRAY(u8, packet_buffer, 2048);
+ PRIVATE_MEMBER_ACCESSORS(s16, stat);
+ PRIVATE_MEMBER_ACCESSORS(u16, unused3);
+ PRIVATE_MEMBER_ACCESSORS(u32, send_window);
+ PRIVATE_MEMBER_ACCESSORS(u32, recv_window);
+ PRIVATE_MEMBER_ACCESSORS_FUNC(void, callback, TSocket *socket);
+};
+
+#endif
diff --git a/include/pso/forward.h b/include/pso/forward.h
index ec4d77a..165b9ad 100644
--- a/include/pso/forward.h
+++ b/include/pso/forward.h
@@ -1,10 +1,7 @@
#ifndef FORWARD_H
#define FORWARD_H
-/*template <typename T, int num_entries, int num_pad_entries>
-class TMenuList;
-template <typename T>
-class TMenuListEntry;*/
+class TSocket;
class THeap;
class TObject;
class TMainTask;
diff --git a/obj_files.mk b/obj_files.mk
index d76a0e5..2098c26 100644
--- a/obj_files.mk
+++ b/obj_files.mk
@@ -1,5 +1,6 @@
O_FILES := $(BUILD_DIR)/src/main.o \
$(BUILD_DIR)/src/pso/TPlyGuildCardTag.o \
+ $(BUILD_DIR)/src/pso/TSocket.o \
$(BUILD_DIR)/src/pso/THeap.o \
$(BUILD_DIR)/src/pso/protocol.o \
$(BUILD_DIR)/src/pso/TMainTask.o \
diff --git a/src/pso/TSocket.cpp b/src/pso/TSocket.cpp
new file mode 100644
index 0000000..f9e954d
--- /dev/null
+++ b/src/pso/TSocket.cpp
@@ -0,0 +1,59 @@
+#include <global_types.h>
+#include <string.h>
+#include "pso/macros.h"
+#include "pso/TArray.h"
+#include "pso/TObject.h"
+#include "pso/TSocket.h"
+
+u16 bswap_word(u16 val);
+
+int TSocket::is_empty() {
+ if (all_parents_unqueued_for_destruction()) {
+ return !m_size;
+ } else {
+ return true;
+ }
+}
+
+const u8 TSocket::next() {
+ const u8 val = m_packet_buffer[m_buffer_offset++];
+ if (m_buffer_offset >= m_size) {
+ m_buffer_offset = 0;
+ m_size = 0;
+ }
+ return val;
+}
+
+void TSocket::set_port(u32 port) {
+ m_dst_port = bswap_word(port);
+}
+
+void TSocket::set_ip_address(u32 addr) {
+ m_dst_addr.addr_bytes.fast_copy_reverse<u32>(addr);
+}
+
+int TSocket::resolve_domain(char *domain) {
+ /* TODO: Implement, and match this. */
+ return 0;
+}
+
+TSocket::TSocket(TObject *parent) : TObject(parent) {
+ m_dst_addr.addr_bytes.fast_fill_with(0);
+ m_dst_port = 0;
+ m_src_port = 0;
+ m_sock_fd = -1;
+ m_sock_flags = 0;
+ m_size = 0;
+ m_buffer_offset = 0;
+ m_buffer_cleared = true;
+ m_callback = nullptr;
+}
+
+u16 bswap_word(u16 val) {
+ u8 *ptr = reinterpret_cast<u8 *>(&val);
+ return ptr[1] + (ptr[0] << 8);
+}
+
+TSocket::~TSocket() {
+
+}