Skip to content

Commit

Permalink
WIP Connection::writeTick implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
GRAnimated committed Oct 17, 2024
1 parent 542efab commit 9de8d39
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 25 deletions.
12 changes: 6 additions & 6 deletions data/mcswitch_functions.csv
Original file line number Diff line number Diff line change
Expand Up @@ -6305,10 +6305,10 @@ Address,Quality,Size,Name
0x00000071000e9858,O,000228,_ZN10Connection4sendENSt3__110shared_ptrI6PacketEE
0x00000071000e993c,O,000120,_ZNSt3__15dequeINS_10shared_ptrI6PacketEENS_9allocatorIS3_EEE9push_backERKS3_
0x00000071000e99b4,O,000124,_ZN10Connection9queueSendENSt3__110shared_ptrI6PacketEE
0x00000071000e9a30,U,000984,_ZN10Connection9writeTickEv
0x00000071000e9a30,M,000984,_ZN10Connection9writeTickEv
0x00000071000e9e08,O,000116,_ZNSt3__15dequeINS_10shared_ptrI6PacketEENS_9allocatorIS3_EEE9pop_frontEv
0x00000071000e9e7c,U,000008,sub_71000E9E7C
0x00000071000e9e84,U,000008,sub_71000E9E84
0x00000071000e9e7c,U,000008,_ZN21ByteArrayOutputStream4sizeEv
0x00000071000e9e84,U,000008,_ZN21ByteArrayOutputStream5clearEv
0x00000071000e9e8c,O,000056,_ZN10Connection5flushEv
0x00000071000e9ec4,O,000244,_ZN10Connection8readTickEv
0x00000071000e9fb8,O,000284,_ZN10Connection5closeEN16DisconnectPacket17eDisconnectReasonE
Expand Down Expand Up @@ -23673,8 +23673,8 @@ Address,Quality,Size,Name
0x000000710034e8f0,U,000020,sub_710034E8F0
0x000000710034e904,U,000284,sub_710034E904
0x000000710034ea20,U,000060,sub_710034EA20
0x000000710034ea5c,U,000064,sub_710034EA5C
0x000000710034ea9c,U,000048,sub_710034EA9C
0x000000710034ea5c,U,000064,SocketOutputStreamLocal__SocketOutputStreamLocal
0x000000710034ea9c,U,000048,SocketOutputStream__SocketOutputStream
0x000000710034eacc,U,000116,sub_710034EACC
0x000000710034eb40,U,000020,sub_710034EB40
0x000000710034eb54,U,000188,sub_710034EB54
Expand All @@ -23684,7 +23684,7 @@ Address,Quality,Size,Name
0x000000710034ed74,U,000020,sub_710034ED74
0x000000710034ed88,U,000292,sub_710034ED88
0x000000710034eeac,U,000008,sub_710034EEAC
0x000000710034eeb4,U,000080,sub_710034EEB4
0x000000710034eeb4,U,000080,SocketOutputStreamNetwork__SocketOutputStreamNetwork
0x000000710034ef04,U,000112,sub_710034EF04
0x000000710034ef74,U,000020,sub_710034EF74
0x000000710034ef88,U,000016,sub_710034EF88
Expand Down
1 change: 1 addition & 0 deletions src/Minecraft.Core/io/BufferedOutputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class BufferedOutputStream : public OutputStream {
public:
BufferedOutputStream(OutputStream* outputStream, int bufferSize);

virtual ~BufferedOutputStream() override;
virtual void write(unsigned int) override;
virtual void write(arrayWithLength<unsigned char>) override;
Expand Down
3 changes: 3 additions & 0 deletions src/Minecraft.Core/io/ByteArrayOutputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class ByteArrayOutputStream : public OutputStream {
virtual void flush() override;
virtual void toByteArray();

unsigned int size();
void clear();

arrayWithLength<unsigned char> mBuffer;
unsigned int mSize;
};
64 changes: 53 additions & 11 deletions src/Minecraft.Network/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ Connection::Connection(Socket* socket, std::wstring const& type, PacketListener*

mDataInputStream = new DataInputStream(socket->getInputStream(packetListener->isServerPacketListener()));

mBufferedOutputStream = (BufferedOutputStream*)socket->getOutputStream(packetListener->isServerPacketListener());
mDataOutputStream = new DataOutputStream(new BufferedOutputStream(mBufferedOutputStream, 0x1400));
mSocketOutputStream = socket->getOutputStream(packetListener->isServerPacketListener());
mDataOutputStream = new DataOutputStream(new BufferedOutputStream(mSocketOutputStream, 0x1400));

mByteArrayOutputStream = new ByteArrayOutputStream(0x1400);
mDataOutputStream2 = new DataOutputStream(mByteArrayOutputStream);
Expand Down Expand Up @@ -236,14 +236,56 @@ void Connection::queueSend(std::shared_ptr<Packet> packet) {
}
}

// TODO: Implement Connection::writeTick
/*
// NON_MATCHING: Shared pointer stuff
bool Connection::writeTick() {
if (!mDataOutputStream || !mDataOutputStream2)
return false;
...

bool returnValue;
if (!mOutgoingQueue.empty() && (!mFakeLag || (System::processTimeInMilliSecs() - mOutgoingQueue.front()->mCreatedTime >= mFakeLag))) {
nn::os::LockMutex(&mMutexType4);
std::shared_ptr<Packet> packet = mOutgoingQueue.front();
mOutgoingQueue.pop_front();

mEstimatedSize += ~packet->getEstimatedSize();
nn::os::UnlockMutex(&mMutexType4);

Packet::writePacket(packet, mDataOutputStream, mPacketListener->isServerPacketListener(), field_164);

dword_71017869A0[packet->getPacketId()] += packet->getEstimatedSize() + 1;

returnValue = true;
} else {
returnValue = false;
}

if (mDelay-- > 0 || mSlowOutgoingQueue.empty() || (mFakeLag && System::processTimeInMilliSecs() - mSlowOutgoingQueue.front()->mCreatedTime < mFakeLag)) {
return returnValue;
}

nn::os::LockMutex(&mMutexType4);
std::shared_ptr<Packet> packet = mSlowOutgoingQueue.front();
mSlowOutgoingQueue.pop_front();

mEstimatedSize += ~packet->getEstimatedSize();
nn::os::UnlockMutex(&mMutexType4);

if (packet->mShouldDelay) {
Packet::writePacket(packet, mDataOutputStream2, mPacketListener->isServerPacketListener(), field_164);

if (mDataOutputStream)
mDataOutputStream->flush();
mSocketOutputStream->writeWithFlags(mByteArrayOutputStream->mBuffer, 0, mByteArrayOutputStream->size(), 1);
mByteArrayOutputStream->clear();
} else {
Packet::writePacket(packet, mDataOutputStream, mPacketListener->isServerPacketListener(), field_164);
}

dword_71017869A0[packet->getPacketId()] += packet->getEstimatedSize() + 1;

mDelay = 0;
return true;
}
*/

void Connection::flush() {
mC4JEventImpl1->Set();
Expand Down Expand Up @@ -305,8 +347,8 @@ void Connection::close(DisconnectPacket::eDisconnectReason reason) {
void Connection::tick() {
if (field_119)
close(DisconnectPacket::eDisconnectReason::_29);
if (mEstimatedSize > 0x100000)
close(DisconnectPacket::eDisconnectReason::_11);
if (mEstimatedSize > 0x100000) // 1MB
close(DisconnectPacket::eDisconnectReason::Overflow);
nn::os::LockMutex(&mMutexType2);
long test = (reinterpret_cast<long*>(&mIncomingQueue)[5]);
nn::os::UnlockMutex(&mMutexType2);
Expand All @@ -315,7 +357,7 @@ void Connection::tick() {
else {
field_158++;
if (field_158 == 1200)
close(DisconnectPacket::eDisconnectReason::_10);
close(DisconnectPacket::eDisconnectReason::Timeout);
}
if (System::processTimeInNanoSecs() - mTimeInMs > 1000) {
if (mPacketListener->isServerPacketListener()) {
Expand Down Expand Up @@ -367,7 +409,7 @@ void Connection::tick() {
flush();

if (mSocket && mSocket->sub_71000EA668())
close(DisconnectPacket::eDisconnectReason::_2);
close(DisconnectPacket::eDisconnectReason::Closed);
if (byte_148) {
nn::os::LockMutex(&mMutexType2);
long test = (reinterpret_cast<long*>(&mIncomingQueue)[5]);
Expand All @@ -382,6 +424,6 @@ void Connection::sendAndQuit() {
if (!mIsDisconnecting) {
flush();
mIsDisconnecting = true;
close(DisconnectPacket::eDisconnectReason::_2);
close(DisconnectPacket::eDisconnectReason::Closed);
}
}
8 changes: 5 additions & 3 deletions src/Minecraft.Network/Connection.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#pragma once

#include <deque>
#include "Minecraft.Network/Socket.h"
#include "Minecraft.Network/protocol/game/DisconnectPacket.h"
#include "nn/os/os_MutexTypes.h"

class Socket;
class DataInputStream;
class DataOutputStream;
class OutputStream;
class ByteArrayOutputStream;
class BufferedOutputStream;
class PacketListener;
class C4JThreadImpl;
class C4JEventImpl;
Expand All @@ -20,6 +21,7 @@ class Connection {
static int dword_710178659C; // runWrite

static int dword_71017865A0[256]; // read
static int dword_71017869A0[256]; // write

bool getAndSetRunning(bool);
void _init();
Expand All @@ -44,7 +46,7 @@ class Connection {
DataOutputStream* mDataOutputStream;
DataOutputStream* mDataOutputStream2;
ByteArrayOutputStream* mByteArrayOutputStream;
BufferedOutputStream* mBufferedOutputStream;
Socket::SocketOutputStream* mSocketOutputStream;
bool mIsRunning;
nn::os::MutexType mMutexType;
std::deque<std::shared_ptr<Packet>> mIncomingQueue;
Expand All @@ -65,7 +67,7 @@ class Connection {
int field_158;
int mEstimatedSize;
unsigned int field_160;
int field_164;
unsigned int field_164;
int mFakeLag;
int dword_16C;
nn::os::MutexType mMutexType3;
Expand Down
43 changes: 41 additions & 2 deletions src/Minecraft.Network/Socket.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,55 @@
#pragma once

#include "Minecraft.Core/io/OutputStream.h"
#include "Minecraft.World/ArrayWithLength.h"

class InputStream;
class OutputStream;

class Socket {
public:
class SocketOutputStream : public OutputStream {
public:
SocketOutputStream();
virtual void writeWithFlags(arrayWithLength<unsigned char>, unsigned int, unsigned int, int);
};

class SocketOutputStreamLocal : public OutputStream {
public:
SocketOutputStreamLocal(int);
virtual ~SocketOutputStreamLocal() override;
virtual void write(unsigned int) override;
virtual void write(arrayWithLength<unsigned char>) override;
virtual void write(arrayWithLength<unsigned char>, unsigned int, unsigned int) override;
virtual void close() override;
virtual void flush() override;
virtual void writeWithFlags(arrayWithLength<unsigned char>, unsigned int, unsigned int, int);

int field_8;
int field_C;
};

class SocketOutputStreamNetwork : public OutputStream {
public:
SocketOutputStreamNetwork(Socket*, int);
virtual ~SocketOutputStreamNetwork() override;
virtual void write(unsigned int) override;
virtual void write(arrayWithLength<unsigned char>) override;
virtual void write(arrayWithLength<unsigned char>, unsigned int, unsigned int) override;
virtual void close() override;
virtual void flush() override;
virtual void writeWithFlags(arrayWithLength<unsigned char>, unsigned int, unsigned int, int);

int field_8;
int field_C;
Socket* mSocket;
};

long getRemoteSocketAddress();

void setSoTimeout(int);
void setTrafficClass(int);
InputStream* getInputStream(bool);
OutputStream* getOutputStream(bool);
SocketOutputStream* getOutputStream(bool);

void close(bool);

Expand Down
1 change: 1 addition & 0 deletions src/Minecraft.Network/protocol/Packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class PacketListener;
class Packet {
public:
static std::shared_ptr<Packet> readPacket(DataInputStream*, bool, unsigned int&, bool&);
static void writePacket(std::shared_ptr<Packet>, DataOutputStream*, bool, unsigned int&);

Packet();
virtual ~Packet();
Expand Down
7 changes: 4 additions & 3 deletions src/Minecraft.Network/protocol/game/DisconnectPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ class DisconnectPacket : public Packet, public std::enable_shared_from_this<Disc

enum eDisconnectReason {
_0 = 0,
_2 = 2,
_10 = 10,
_11 = 11,
Closed = 2,
Kicked = 8,
Timeout = 10,
Overflow = 11,
_29 = 29,
};

Expand Down

0 comments on commit 9de8d39

Please sign in to comment.