the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#pragma once
2#include <xrnm.h>
3#include <queue>
4#include <qnet.h>
5#include "InputStream.h"
6#include "OutputStream.h"
7
8#define SOCKET_CLIENT_END 0
9#define SOCKET_SERVER_END 1
10
11class SocketAddress;
12class ServerConnection;
13
14class Socket
15{
16public:
17 // 4J Added so we can add a priority write function
18 class SocketOutputStream : public OutputStream
19 {
20 public:
21 // The flags are those that can be used for the QNet SendData function
22 virtual void writeWithFlags(byteArray b, unsigned int offset, unsigned int length, int flags) { write(b, offset, length); }
23 };
24
25private:
26 class SocketInputStreamLocal : public InputStream
27 {
28 public:
29 bool m_streamOpen;
30 private:
31 int m_queueIdx;
32 public:
33 SocketInputStreamLocal(int queueIdx);
34
35 virtual int read();
36 virtual int read(byteArray b);
37 virtual int read(byteArray b, unsigned int offset, unsigned int length);
38 virtual void close();
39 virtual __int64 skip(__int64 n) { return n; } // 4J Stu - Not implemented
40 virtual void flush() {}
41 };
42
43 class SocketOutputStreamLocal : public SocketOutputStream
44 {
45 public:
46 bool m_streamOpen;
47 private:
48 int m_queueIdx;
49 public:
50 SocketOutputStreamLocal(int queueIdx);
51
52 virtual void write(unsigned int b);
53 virtual void write(byteArray b);
54 virtual void write(byteArray b, unsigned int offset, unsigned int length);
55 virtual void close();
56 virtual void flush() {}
57 };
58
59 class SocketInputStreamNetwork : public InputStream
60 {
61 bool m_streamOpen;
62 int m_queueIdx;
63 Socket *m_socket;
64 public:
65 SocketInputStreamNetwork(Socket *socket, int queueIdx);
66
67 virtual int read();
68 virtual int read(byteArray b);
69 virtual int read(byteArray b, unsigned int offset, unsigned int length);
70 virtual void close();
71 virtual __int64 skip(__int64 n) { return n; } // 4J Stu - Not implemented
72 virtual void flush() {}
73 };
74 class SocketOutputStreamNetwork : public SocketOutputStream
75 {
76 bool m_streamOpen;
77 int m_queueIdx;
78 Socket *m_socket;
79 public:
80 SocketOutputStreamNetwork(Socket *socket, int queueIdx);
81
82 virtual void write(unsigned int b);
83 virtual void write(byteArray b);
84 virtual void write(byteArray b, unsigned int offset, unsigned int length);
85 virtual void writeWithFlags(byteArray b, unsigned int offset, unsigned int length, int flags);
86 virtual void close();
87 virtual void flush() {}
88 };
89
90 bool m_hostServerConnection; // true if this is the connection between the host player and server
91 bool m_hostLocal; // true if this player on the same machine as the host
92 int m_end; // 0 for client side or 1 for host side
93
94 // For local connections between the host player and the server
95 static CRITICAL_SECTION s_hostQueueLock[2];
96 static std::queue<byte> s_hostQueue[2];
97 static SocketOutputStreamLocal *s_hostOutStream[2];
98 static SocketInputStreamLocal *s_hostInStream[2];
99
100 // For network connections
101 std::queue<byte> m_queueNetwork[2]; // For input data
102 CRITICAL_SECTION m_queueLockNetwork[2]; // For input data
103 SocketInputStreamNetwork *m_inputStream[2];
104 SocketOutputStreamNetwork *m_outputStream[2];
105 bool m_endClosed[2];
106
107 // Host only connection class
108 static ServerConnection *s_serverConnection;
109
110 BYTE networkPlayerSmallId;
111public:
112 C4JThread::Event* m_socketClosedEvent;
113
114 INetworkPlayer *getPlayer();
115 void setPlayer(INetworkPlayer *player);
116
117public:
118 static void Initialise(ServerConnection *serverConnection);
119 Socket(bool response = false); // 4J - Create a local socket, for end 0 or 1 of a connection
120 Socket(INetworkPlayer *player, bool response = false, bool hostLocal = false); // 4J - Create a socket for an INetworkPlayer
121 SocketAddress *getRemoteSocketAddress();
122 void pushDataToQueue(const BYTE * pbData, DWORD dwDataSize, bool fromHost = true);
123 static void addIncomingSocket(Socket *socket);
124 InputStream *getInputStream(bool isServerConnection);
125 void setSoTimeout(int a );
126 void setTrafficClass( int a );
127 SocketOutputStream *getOutputStream(bool isServerConnection);
128 bool close(bool isServerConnection);
129 bool createdOk;
130 bool isLocal() { return m_hostLocal; }
131
132 bool isClosing() { return m_endClosed[SOCKET_CLIENT_END] || m_endClosed[SOCKET_SERVER_END]; }
133 BYTE getSmallId() { return networkPlayerSmallId; }
134};