Skip to content

Commit

Permalink
Fix time bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nagalun committed Jun 22, 2018
1 parent fa3c1ba commit 8eee2e1
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
database
build
out
out.exe
59 changes: 45 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,56 @@
OBJS = $(wildcard *.cpp)
SRC_FILES = $(wildcard src/*.cpp)
OBJ_FILES = $(SRC_FILES:src/%.cpp=build/%.o)
DEP_FILES = $(OBJ_FILES:.o=.d)

ALLOBJS = $(OBJS)
TARGET = out

COMPILER_FLAGS = -Wall -std=c++17 -O2
OPT_REL = -O2
LD_REL = -s
OPT_DBG = -Og -g

LIBS = -I ./ -I ./lib/json/include/ -I ./lib/uWebSockets/src/ -L ./lib/uWebSockets/ -s -static -luWS -lssl -lz -lcrypto
CPPFLAGS += -std=c++11 -Wall -Wextra -Wno-unknown-pragmas -Wno-unused-parameter
CPPFLAGS += -MMD -MP

UWS = ./lib/uWebSockets
JSON = ./lib/json
LIB_FILES = $(UWS)/libuWS.a

CPPFLAGS += -I ./src/
CPPFLAGS += -I $(UWS)/src/
CPPFLAGS += -I $(JSON)/include/
LDFLAGS += -L $(UWS)/

LDLIBS += -lssl -lz -lcrypto

ifeq ($(OS),Windows_NT)
LIBS += -luv -lstdc++ -Wl,--whole-archive -lpthread -Wl,-Bdynamic,--no-whole-archive -lWs2_32 -lwsock32 -lGdi32 -lpsapi -liphlpapi -luserenv
LDLIBS += -luv -lWs2_32 -lpsapi -liphlpapi -luserenv
endif

TARGET = out
.PHONY: all g clean clean-all

all: CPPFLAGS += $(OPT_REL)
all: LDFLAGS += $(LD_REL)
all: $(TARGET)

g: CPPFLAGS += $(OPT_DBG)
g: LDFLAGS += $(LD_DBG)
g: $(TARGET)

$(TARGET): $(OBJ_FILES) $(LIB_FILES)
$(CXX) $(LDFLAGS) -o $@ $^ $(LDLIBS)

build/%.o: src/%.cpp
$(CXX) $(CPPFLAGS) -c -o $@ $<


$(UWS)/libuWS.a:
$(MAKE) -C $(UWS) -f ../uWebSockets.mk

all: uWS $(ALLOBJS)
$(CXX) $(ALLOBJS) $(COMPILER_FLAGS) $(LIBS) -o $(TARGET)

vanilla: uWS $(ALLOBJS)
$(CXX) $(ALLOBJS) $(COMPILER_FLAGS) -DVANILLA_SERVER $(LIBS) -o $(TARGET)
clean:
- $(RM) $(TARGET) $(OBJ_FILES) $(DEP_FILES)

g: uWS $(ALLOBJS)
$(CXX) $(ALLOBJS) -Wall -std=gnu++0x -Og -g $(LIBS) -o $(TARGET)
clean-all: clean
$(MAKE) -C $(UWS) -f ../uWebSockets.mk clean

uWS:
$(MAKE) -C lib/uWebSockets -f ../uWebSockets.mk
-include $(DEP_FILES)
6 changes: 3 additions & 3 deletions lib/uWebSockets-fix-mingw64-compile.patch
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
diff --git a/src/Networking.h b/src/Networking.h
index e26888c..0b87e8e 100644
index e26888c..1b35977 100644
--- a/src/Networking.h
+++ b/src/Networking.h
@@ -29,6 +29,10 @@
#pragma comment(lib, "ws2_32.lib")
#define SHUT_WR SD_SEND
#ifdef __MINGW32__
+extern "C" {
+ WINSOCK_API_LINKAGE INT WSAAPI inet_pton( INT Family, PCSTR pszAddrString, PVOID pAddrBuf);
+ WINSOCK_API_LINKAGE PCSTR WSAAPI inet_ntop(INT Family, PVOID pAddr, PSTR pStringBuf, size_t StringBufSize);
+ WINSOCK_API_LINKAGE INT WSAAPI inet_pton(INT, PCSTR, PVOID);
+ WINSOCK_API_LINKAGE PCSTR WSAAPI inet_ntop(INT, PVOID, PSTR, size_t);
+}
// Windows has always been tied to LE
#define htobe64(x) __builtin_bswap64(x)
Expand Down
15 changes: 15 additions & 0 deletions lib/uWebSockets-fix-mingw64-redefined-macro.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
diff --git a/src/Networking.h b/src/Networking.h
index e26888c..7f55618 100644
--- a/src/Networking.h
+++ b/src/Networking.h
@@ -23,7 +23,9 @@
#endif

#ifdef _WIN32
-#define NOMINMAX
+#ifndef NOMINMAX
+ #define NOMINMAX
+#endif
#include <WinSock2.h>
#include <Ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
44 changes: 24 additions & 20 deletions lib/uWebSockets.mk
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
CPPFILES = $(wildcard src/*.cpp)
OBJFILES = $(CPPFILES:src/%.cpp=%.o)
INCLUDES = -I src -lssl -lz -lcrypto

ifeq ($(OS),Windows_NT)
INCLUDES += -luv -lWs2_32 -lwsock32 -lGdi32 -lpsapi -liphlpapi -luserenv
endif

LIB = libuWS.a

CPP_STATIC := -std=c++11 -O3 $(INCLUDES) -c $(CPPFILES)

$(LIB):
$(CXX) $(CPPFLAGS) $(CFLAGS) $(CPP_STATIC) -s
ar rcs $(LIB) $(OBJFILES)
rm -f $(OBJFILES)

clean:
rm -f $(LIB)
rm -f $(OBJFILES)
SRC_FILES = $(wildcard src/*.cpp)
OBJ_FILES = $(SRC_FILES:.cpp=.o)
DEP_FILES = $(OBJ_FILES:.o=.d)

CPPFLAGS += -std=c++11 -O3
CPPFLAGS += -I ./src/
CPPFLAGS += -MMD -MP

TARGET = libuWS.a

.PHONY: all clean

all: $(TARGET)

$(TARGET): $(OBJ_FILES)
$(AR) rcs $@ $^

src/%.o: src/%.cpp
$(CXX) $(CPPFLAGS) -c -o $@ $<

clean:
- $(RM) $(TARGET) $(OBJ_FILES) $(DEP_FILES)

-include $(DEP_FILES)
17 changes: 17 additions & 0 deletions src/crossfuncs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "crossfuncs.hpp"

#ifndef __WIN32
#include <sys/types.h>
#include <sys/stat.h>
#else
#include <windows.h>
#endif

bool makedir(const std::string & dir) {
#ifndef __WIN32
return mkdir(dir.c_str(), 0755) == 0;
#else
/* conversion from windows bool to c++ bool necessary? */
return CreateDirectory(dir.c_str(), nullptr) == TRUE;
#endif
}
4 changes: 4 additions & 0 deletions src/crossfuncs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once
#include <string>

bool makedir(const std::string &);
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions msg.cpp → src/msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using nlohmann::json;

long int js_date_now();
int64_t js_date_now();
std::string roundfloat(const float, int);
size_t getUTF8strlen(const std::string&);

Expand All @@ -22,7 +22,7 @@ void server::msg::bin_n(server* sv, const char* msg, size_t len, uWS::WebSocket<
|| ssearch->second->is_owner(search->second.user))){
clinfo_t* usr = ssearch->second->get_info(search->second.user);
if(usr){
unsigned long int id = std::stoul(usr->id);
uint64_t id = std::stoull(usr->id);
char* newmsg = (char*)malloc(len + sizeof(id) + 1);
if(!newmsg){
std::cout << "Failed to allocate memory for bin note msg" << std::endl;
Expand Down Expand Up @@ -123,7 +123,7 @@ void server::msg::t(server* sv, json& j, uWS::WebSocket<uWS::SERVER> * s){
res[0] = {
{"m", "t"},
{"t", js_date_now()},
{"e", j["e"].get<long int>()}
{"e", j["e"].get<int64_t>()}
};
s->send((char *)res.dump().c_str(), res.dump().size(), uWS::TEXT);
}
Expand Down
11 changes: 6 additions & 5 deletions server.cpp → src/server.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "server.hpp"
#include <sstream>
#include <iostream>
#include <chrono>

/* sha1 hashing */
#include <openssl/sha.h>
Expand Down Expand Up @@ -40,11 +41,11 @@ std::string n2hexstr(uint32_t w, bool alpha = false) {
return rc;
}

long int js_date_now(){
struct timeval tp;
gettimeofday(&tp, NULL);
long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
return ms;
int64_t js_date_now(){
namespace c = std::chrono;

auto time = c::system_clock::now().time_since_epoch();
return c::duration_cast<c::milliseconds>(time).count();
}

nlohmann::json server::Room::get_json(std::string _id, bool includeppl){
Expand Down
11 changes: 4 additions & 7 deletions server.hpp → src/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <fstream>
#include <sstream>
#include "limiter.hpp"
#include "crossfuncs.hpp"


/* Define VANILLA_SERVER if you don't want custom network messages, like non-JSON note data
Expand Down Expand Up @@ -45,11 +46,7 @@ class server {
std::string name;
};
Database(const std::string& dir) : dir(dir){
#ifndef __WIN32
mkdir(dir.c_str(), 0700);
#else
CreateDirectory(dir.c_str(), nullptr);
#endif
makedir(dir);
};
pinfo_t get_usrinfo(uint32_t);
void set_usrinfo(uint32_t, pinfo_t);
Expand Down Expand Up @@ -84,7 +81,7 @@ class server {
Client* oldowner;
std::array<float,2> startpos;
std::array<float,2> endpos;
long int time;
int64_t time;
};
bool lobby, visible, chat, crownsolo;
uint32_t color;
Expand All @@ -97,7 +94,7 @@ class server {
visible(true),
chat(true),
crownsolo(false),
color(0xFF7F00),
color(0x98FB98/*0xFF7F00*/),
crown({NULL, NULL, {50, 50}, {50, 50}, 0}){};
nlohmann::json get_json(std::string, bool);
nlohmann::json get_chatlog_json();
Expand Down

0 comments on commit 8eee2e1

Please sign in to comment.