the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 145 lines 3.4 kB view raw
1#pragma once 2using namespace std; 3 4#include "stdafx.h" 5#include <queue> 6#include "System.h" 7#include "DataInputStream.h" 8#include "DataOutputStream.h" 9#include "net.minecraft.network.packet.h" 10#include "C4JThread.h" 11 12#include "Socket.h" 13 14// 4J JEV, size of the threads (bytes). 15#define READ_STACK_SIZE 0 16#define WRITE_STACK_SIZE 0 17 18class ByteArrayOutputStream; 19 20class Connection 21{ 22 friend DWORD WINAPI runRead(LPVOID lpParam); 23 friend DWORD WINAPI runWrite(LPVOID lpParam); 24 friend DWORD WINAPI runSendAndQuit(LPVOID lpParam); 25 friend DWORD WINAPI runClose(LPVOID lpParam); 26 27private: 28 static const int SEND_BUFFER_SIZE = 1024 * 5; 29 30public: 31 static int readThreads, writeThreads; 32 33private: 34 static const int MAX_TICKS_WITHOUT_INPUT = 20 * 60; 35 36public: 37 static const int IPTOS_LOWCOST = 0x02; 38 static const int IPTOS_RELIABILITY = 0x04; 39 static const int IPTOS_THROUGHPUT = 0x08; 40 static const int IPTOS_LOWDELAY = 0x10; 41 42private: 43 Socket *socket; 44 const SocketAddress *address; 45 DataInputStream *dis; 46 DataOutputStream *bufferedDos; // 4J This is the same type of dos the java game has 47 48 // 4J Added 49 DataOutputStream *byteArrayDos; // 4J This dos allows us to write individual packets to the socket 50 ByteArrayOutputStream *baos; 51 Socket::SocketOutputStream *sos; 52 53 bool running; 54 55 queue<shared_ptr<Packet> > incoming; // 4J - was using synchronizedList... 56 CRITICAL_SECTION incoming_cs; // ... now has this critical section 57 queue<shared_ptr<Packet> > outgoing; // 4J - was using synchronizedList - but don't think it is required as usage is wrapped in writeLock critical section 58 queue<shared_ptr<Packet> > outgoing_slow; // 4J - was using synchronizedList - but don't think it is required as usage is wrapped in writeLock critical section 59 60 61 PacketListener *packetListener; 62 bool quitting; 63 64 65 C4JThread* readThread; 66 C4JThread* writeThread; 67 68 C4JThread::Event* m_hWakeReadThread; 69 C4JThread::Event* m_hWakeWriteThread; 70 71 DWORD saqThreadID, closeThreadID; 72 73 bool disconnected; 74 DisconnectPacket::eDisconnectReason disconnectReason; 75 void **disconnectReasonObjects; // 4J a pointer to an array. 76 77 int noInputTicks; 78 int estimatedRemaining; 79 80 int tickCount; // 4J Added 81 82public: 83 static int readSizes[256]; 84 static int writeSizes[256]; 85 86 int fakeLag; 87 88private: 89 void _init(); 90 91 // 4J Jev, these might be better of as private 92 CRITICAL_SECTION threadCounterLock; 93 CRITICAL_SECTION writeLock; 94 95public: 96 // 4J Jev, need to delete the critical section. 97 ~Connection(); 98 Connection(Socket *socket, const wstring& id, PacketListener *packetListener); // throws IOException 99 100 void setListener(PacketListener *packetListener); 101 void send(shared_ptr<Packet> packet); 102 103public: 104 void queueSend(shared_ptr<Packet> packet); 105 106private: 107 int slowWriteDelay; 108 109 bool writeTick(); 110 111public: 112 void flush(); 113 114private: 115 bool readTick(); 116 117private: 118 119 /* 4J JEV, removed try/catch 120 void handleException(Exception e) 121 { 122 e.printStackTrace(); 123 close("disconnect.genericReason", "Internal exception: " + e.toString()); 124 }*/ 125 126public: 127 void close(DisconnectPacket::eDisconnectReason reason, ...); 128 129 void tick(); 130 131 SocketAddress *getRemoteAddress(); 132 133 void sendAndQuit(); 134 135 int countDelayedPackets(); 136 137 Socket *getSocket() { return socket; } 138 139private: 140 static int runRead(void* lpParam); 141 static int runWrite(void* lpParam); 142 static int runClose(void* lpParam); 143 static int runSendAndQuit(void* lpParam); 144 145};