summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2019-01-27 13:01:35 +0100
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2019-01-27 15:06:36 +0100
commita2345d1f45870eb00a2a3d5d6fd21a386efcade3 (patch)
tree895dbf8f78d5b0117fb0daaab43423775ac4c304 /src
parentMerge pull request #31 from uis246/pluginSystem (diff)
downloadAltCraft-a2345d1f45870eb00a2a3d5d6fd21a386efcade3.tar
AltCraft-a2345d1f45870eb00a2a3d5d6fd21a386efcade3.tar.gz
AltCraft-a2345d1f45870eb00a2a3d5d6fd21a386efcade3.tar.bz2
AltCraft-a2345d1f45870eb00a2a3d5d6fd21a386efcade3.tar.lz
AltCraft-a2345d1f45870eb00a2a3d5d6fd21a386efcade3.tar.xz
AltCraft-a2345d1f45870eb00a2a3d5d6fd21a386efcade3.tar.zst
AltCraft-a2345d1f45870eb00a2a3d5d6fd21a386efcade3.zip
Diffstat (limited to 'src')
-rw-r--r--src/Network.cpp20
-rw-r--r--src/Network.hpp5
-rw-r--r--src/Stream.cpp44
-rw-r--r--src/Stream.hpp4
4 files changed, 26 insertions, 47 deletions
diff --git a/src/Network.cpp b/src/Network.cpp
index c8be740..431a5b1 100644
--- a/src/Network.cpp
+++ b/src/Network.cpp
@@ -7,24 +7,16 @@
Network::Network(std::string address, unsigned short port) {
try {
- socket = new Socket(address, port);
+ socket = std::make_unique<Socket>(address, port);
+ stream = std::make_unique<StreamSocket>(socket.get());
} catch (std::exception &e) {
LOG(WARNING) << "Connection failed: " << e.what();
throw;
- }
-
- try {
- stream = new StreamSocket(socket);
- } catch (std::exception &e) {
- LOG(WARNING) << "Stream creation failed: " << e.what();
- }
-
-
+ }
}
Network::~Network() {
- delete stream;
- delete socket;
+
}
std::shared_ptr<Packet> Network::ReceivePacket(ConnectionState state, bool useCompression) {
@@ -94,7 +86,7 @@ void Network::SendPacket(Packet &packet, int compressionThreshold) {
stream->WriteVarInt(packetSize.GetCountedSize());
stream->WriteVarInt(0);
stream->WriteVarInt(packet.GetPacketId());
- packet.ToStream(stream);
+ packet.ToStream(stream.get());
} else {
throw std::runtime_error("Compressing send data not supported");
}
@@ -105,7 +97,7 @@ void Network::SendPacket(Packet &packet, int compressionThreshold) {
packet.ToStream(&packetSize);
stream->WriteVarInt(packetSize.GetCountedSize());
stream->WriteVarInt(packet.GetPacketId());
- packet.ToStream(stream);
+ packet.ToStream(stream.get());
}
}
diff --git a/src/Network.hpp b/src/Network.hpp
index 5d7fc38..29a090f 100644
--- a/src/Network.hpp
+++ b/src/Network.hpp
@@ -11,12 +11,13 @@ enum ConnectionState : unsigned char {
};
class Network {
- Socket *socket = nullptr;
- StreamSocket *stream = nullptr;
+ std::unique_ptr<Socket> socket;
+ std::unique_ptr<StreamSocket> stream;
std::shared_ptr<Packet> ReceivePacketByPacketId(int packetId, ConnectionState state, StreamInput &stream);
public:
Network(std::string address, unsigned short port);
+
~Network();
std::shared_ptr<Packet> ReceivePacket(ConnectionState state = Play, bool useCompression = false);
diff --git a/src/Stream.cpp b/src/Stream.cpp
index d66335e..efe37d2 100644
--- a/src/Stream.cpp
+++ b/src/Stream.cpp
@@ -71,11 +71,10 @@ double StreamInput::ReadDouble() {
std::string StreamInput::ReadString() {
int strLength = ReadVarInt();
- unsigned char *buff = new unsigned char[strLength + 1];
- ReadData(buff, strLength);
+ std::vector<unsigned char> buff(strLength + 1);
+ ReadData(buff.data(), strLength);
buff[strLength] = 0;
- std::string str((char *) buff);
- delete[] buff;
+ std::string str((char *) buff.data());
return str;
}
@@ -178,11 +177,9 @@ Uuid StreamInput::ReadUuid() {
}
std::vector<unsigned char> StreamInput::ReadByteArray(size_t arrLength) {
- unsigned char *buffer = new unsigned char[arrLength];
- ReadData(buffer, arrLength);
- std::vector<unsigned char> ret(buffer, buffer + arrLength);
- delete[] buffer;
- return ret;
+ std::vector<unsigned char> buffer(arrLength);
+ ReadData(buffer.data(), arrLength);
+ return buffer;
}
@@ -307,7 +304,7 @@ void StreamOutput::WriteByteArray(const std::vector<unsigned char> &value) {
}
void StreamBuffer::ReadData(unsigned char *buffPtr, size_t buffLen) {
- size_t bufferLengthLeft = buffer + bufferLength - bufferPtr;
+ size_t bufferLengthLeft = buffer.data() + buffer.size() - bufferPtr;
if (bufferLengthLeft < buffLen)
throw std::runtime_error("Internal error: StreamBuffer reader out of data");
@@ -316,38 +313,29 @@ void StreamBuffer::ReadData(unsigned char *buffPtr, size_t buffLen) {
}
void StreamBuffer::WriteData(unsigned char *buffPtr, size_t buffLen) {
- size_t bufferLengthLeft = buffer + bufferLength - bufferPtr;
+ size_t bufferLengthLeft = buffer.data() + buffer.size() - bufferPtr;
if (bufferLengthLeft < buffLen)
throw std::runtime_error("Internal error: StreamBuffer writer out of data");
std::memcpy(bufferPtr, buffPtr, buffLen);
bufferPtr += buffLen;
}
-StreamBuffer::StreamBuffer(unsigned char *data, size_t dataLen) {
- buffer = new unsigned char[dataLen];
- bufferPtr = buffer;
- bufferLength = dataLen;
- std::memcpy(buffer, data, dataLen);
+StreamBuffer::StreamBuffer(unsigned char *data, size_t dataLen) : buffer(data,data+dataLen) {
+ bufferPtr = buffer.data();
}
-StreamBuffer::StreamBuffer(size_t bufferLen) {
- buffer = new unsigned char[bufferLen];
- bufferPtr = buffer;
- bufferLength = bufferLen;
- for (unsigned char *p = buffer; p != buffer + bufferLength; ++p)
- *p = 0;
-}
-
-StreamBuffer::~StreamBuffer() {
- delete[] buffer;
+StreamBuffer::StreamBuffer(size_t bufferLen) : buffer(bufferLen) {
+ bufferPtr = buffer.data();
+ for (auto &it : buffer)
+ it = 0;
}
std::vector<unsigned char> StreamBuffer::GetBuffer() {
- return std::vector<unsigned char>(buffer, buffer + bufferLength);
+ return buffer;
}
size_t StreamBuffer::GetReadedLength() {
- return bufferPtr - buffer;
+ return bufferPtr - buffer.data();
}
void StreamCounter::WriteData(unsigned char *buffPtr, size_t buffLen) {
diff --git a/src/Stream.hpp b/src/Stream.hpp
index 8269300..a7c161b 100644
--- a/src/Stream.hpp
+++ b/src/Stream.hpp
@@ -73,9 +73,8 @@ public:
};
class StreamBuffer : public StreamInput, public StreamOutput {
- unsigned char *buffer;
+ std::vector<unsigned char> buffer;
unsigned char *bufferPtr;
- size_t bufferLength;
void ReadData(unsigned char *buffPtr, size_t buffLen) override;
void WriteData(unsigned char *buffPtr, size_t buffLen) override;
@@ -83,7 +82,6 @@ class StreamBuffer : public StreamInput, public StreamOutput {
public:
StreamBuffer(unsigned char *data, size_t dataLen);
StreamBuffer(size_t bufferLen);
- ~StreamBuffer();
std::vector<unsigned char> GetBuffer();
size_t GetReadedLength();