Fix crash on server connection

This commit is contained in:
yvt 2017-12-30 04:28:50 +09:00
parent c2b00aa464
commit f702eca1d8
2 changed files with 41 additions and 4 deletions

View File

@ -43,6 +43,7 @@
#include <Core/MemoryStream.h>
#include <Core/Settings.h>
#include <Core/Strings.h>
#include <Core/TMPUtils.h>
DEFINE_SPADES_SETTING(cg_unicode, "1");
@ -497,8 +498,12 @@ namespace spades {
SPRaise("Disconnected: %s", DisconnectReasonString(event.data).c_str());
}
stmp::optional<NetPacketReader> readerOrNone;
if (event.type == ENET_EVENT_TYPE_RECEIVE) {
NetPacketReader reader(event.packet);
readerOrNone.reset(event.packet);
auto &reader = readerOrNone.value();
try {
if (HandleHandshakePacket(reader)) {
continue;
@ -515,7 +520,7 @@ namespace spades {
if (event.type == ENET_EVENT_TYPE_CONNECT) {
statusString = _Tr("NetClient", "Awaiting for state");
} else if (event.type == ENET_EVENT_TYPE_RECEIVE) {
NetPacketReader reader(event.packet);
auto &reader = readerOrNone.value();
reader.DumpDebug();
if (reader.GetType() != PacketTypeMapStart) {
SPRaise("Unexpected packet: %d", (int)reader.GetType());
@ -529,7 +534,7 @@ namespace spades {
}
} else if (status == NetClientStatusReceivingMap) {
if (event.type == ENET_EVENT_TYPE_RECEIVE) {
NetPacketReader reader(event.packet);
auto &reader = readerOrNone.value();
if (reader.GetType() == PacketTypeMapChunk) {
std::vector<char> dt = reader.GetData();
@ -642,7 +647,7 @@ namespace spades {
}
} else if (status == NetClientStatusConnected) {
if (event.type == ENET_EVENT_TYPE_RECEIVE) {
NetPacketReader reader(event.packet);
auto &reader = readerOrNone.value();
// reader.DumpDebug();
try {
HandleGamePacket(reader);

View File

@ -25,9 +25,13 @@
#include <iostream>
#include <iterator>
#include <memory>
#include <stdexcept>
#include <type_traits>
namespace stmp {
struct bad_optional_access : public std::logic_error {
bad_optional_access() : std::logic_error{"bad optional access"} {};
};
// creating our own version because boost is overweighted
// (preproecssing optional.hpp emits 50000 lines of C++ code!)
@ -82,6 +86,7 @@ namespace stmp {
const T *get_pointer() const {
return has_some ? reinterpret_cast<const T *>(&storage) : nullptr;
}
T &get() & {
assert(has_some);
return *get_pointer();
@ -98,12 +103,39 @@ namespace stmp {
assert(has_some);
return *get_pointer();
}
T &value() & {
if (!has_some) {
throw bad_optional_access{};
}
return *get_pointer();
}
const T &value() const & {
if (!has_some) {
throw bad_optional_access{};
}
return *get_pointer();
}
T &&value() && {
if (!has_some) {
throw bad_optional_access{};
}
return *get_pointer();
}
const T &&value() const && {
if (!has_some) {
throw bad_optional_access{};
}
return *get_pointer();
}
template <class U> T value_or(U &&default_value) const & {
return *this ? get() : static_cast<T>(std::forward<U>(default_value));
}
template <class U> T value_or(U &&default_value) && {
return *this ? std::move(get()) : static_cast<T>(std::forward<U>(default_value));
}
T &operator->() {
assert(has_some);
return get();