summaryrefslogtreecommitdiff
path: root/include/pso/TSocket.h
blob: b11d8efa9279e8fb55dbc2b48d7961e187fa4280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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:
	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_val;
	u16 m_unused3;
	u32 m_send_window;
	u32 m_recv_window;
	void (*m_callback)(TSocket *socket);
public:
	TSocket(TObject *parent);
	virtual ~TSocket();

	virtual short open() = 0;
	virtual short close() = 0;
	virtual void recv() = 0;
	virtual short 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();

	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;
	};

	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_val);
	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