mirror of
https://onedev.site.tesses.net/tesses-framework
synced 2026-04-16 10:17:22 +00:00
Compare commits
19 Commits
8b6801e608
...
developmen
| Author | SHA1 | Date | |
|---|---|---|---|
| 65d87a1759 | |||
| 3889c746fe | |||
| 9dae77a0b9 | |||
| 61275c0f5f | |||
| adf11bd144 | |||
| f53bacb18b | |||
| 94eb56aad6 | |||
| 4efd654941 | |||
| 34b484f633 | |||
| 96ba20d65c | |||
| f825c2616a | |||
| 67874eba30 | |||
| 8eb7e050c0 | |||
| e96b359bb8 | |||
| 848fca7f36 | |||
| 2f5271f7c3 | |||
| 7dc4ad9b08 | |||
| f88cc21a85 | |||
| 1d5ba40ef0 |
44
.gitea/workflows/tag.yaml
Normal file
44
.gitea/workflows/tag.yaml
Normal file
@@ -0,0 +1,44 @@
|
||||
name: Build and Deploy on Tag
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*'
|
||||
|
||||
env:
|
||||
GITEA_AUTH: ${{ secrets.MY_GITEA_AUTH }}
|
||||
|
||||
jobs:
|
||||
build-arch:
|
||||
runs-on: arch-builder
|
||||
steps:
|
||||
- run: pacman --noconfirm -Sy nodejs npm
|
||||
- uses: actions/checkout@v4
|
||||
- run: pacman --noconfirm -Sy mbedtls curl
|
||||
- run: pacman --config /opt/cross/ppc/pacman.conf --noconfirm -Sy mbedtls
|
||||
- run: cp Packaging/Linux/PKGBUILD /home/build/PKGBUILD
|
||||
- run: cp Packaging/Linux/build-arch.sh /home/build/build-arch.sh
|
||||
- run: chmod 755 /home/build/build-arch.sh
|
||||
- run: chown build:build /home/build/PKGBUILD
|
||||
- run: chown build:build /home/build/build-arch.sh
|
||||
- run: su build -c /home/build/build-arch.sh
|
||||
build-jammy:
|
||||
runs-on: deb-builder-jammy
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Build for jammy, noble
|
||||
run: |
|
||||
bash build-ubuntu-jammy.sh
|
||||
bash push-ubuntu-jammy.sh
|
||||
working-directory: ./Packaging/Linux
|
||||
build-plucky:
|
||||
runs-on: deb-builder-plucky
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Build for plucky, resolute
|
||||
run: |
|
||||
bash build-ubuntu-plucky.sh
|
||||
bash push-ubuntu-plucky.sh
|
||||
working-directory: ./Packaging/Linux
|
||||
- uses: akkuman/gitea-release-action@v1
|
||||
env:
|
||||
NODE_OPTIONS: '--experimental-fetch' # if nodejs < 18
|
||||
@@ -1,18 +1,21 @@
|
||||
version: 39
|
||||
version: 43
|
||||
jobs:
|
||||
- name: Build for x86_64
|
||||
steps:
|
||||
- !CheckoutStep
|
||||
- type: CheckoutStep
|
||||
name: Checkout
|
||||
cloneCredential: !DefaultCredential {}
|
||||
cloneCredential:
|
||||
type: DefaultCredential
|
||||
withLfs: true
|
||||
withSubmodules: false
|
||||
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL
|
||||
- !CommandStep
|
||||
condition: SUCCESSFUL
|
||||
optional: false
|
||||
- type: CommandStep
|
||||
name: Execute build
|
||||
runInContainer: true
|
||||
image: onedev.site.tesses.net/dependencies/dependencies:latest
|
||||
interpreter: !DefaultInterpreter
|
||||
interpreter:
|
||||
type: DefaultInterpreter
|
||||
commands: |
|
||||
mkdir build
|
||||
cd build
|
||||
@@ -20,72 +23,28 @@ jobs:
|
||||
make -j12
|
||||
make install DESTDIR=out
|
||||
useTTY: true
|
||||
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL
|
||||
- !BuildImageStep
|
||||
condition: SUCCESSFUL
|
||||
optional: false
|
||||
- type: BuildImageStep
|
||||
name: Build Docker Image
|
||||
dockerfile: Dockerfile.run
|
||||
output: !RegistryOutput
|
||||
tags: onedev.site.tesses.net/tesses-framework/tesses-framework:latest onedev.site.tesses.net/tesses-framework/tesses-framework:@commit_hash@
|
||||
output:
|
||||
type: RegistryOutput
|
||||
tags: onedev.site.tesses.net/tesses-framework/tesses-framework:latest onedev.site.tesses.net/tesses-framework/tesses-framework:@commit_hash@ git.tesseslanguage.com/tesses50/tesses-framework:latest git.tesseslanguage.com/tesses50/tesses-framework:@commit_hash@
|
||||
registryLogins:
|
||||
- registryUrl: '@server_url@'
|
||||
userName: '@job_token@'
|
||||
passwordSecret: dockersecret
|
||||
- registryUrl: git.tesseslanguage.com
|
||||
userName: tesses50
|
||||
passwordSecret: GITEA_AUTH
|
||||
platforms: linux/amd64
|
||||
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL
|
||||
- !CommandStep
|
||||
name: Build archlinux
|
||||
runInContainer: true
|
||||
image: git.tesseslanguage.com/tesses50/arch-builds:2025-11-11
|
||||
interpreter: !DefaultInterpreter
|
||||
commands: |
|
||||
pacman --noconfirm -Sy mbedtls curl
|
||||
pacman --config /opt/cross/ppc/pacman.conf --noconfirm -Sy mbedtls
|
||||
cp Packaging/Linux/PKGBUILD /home/build/PKGBUILD
|
||||
cp Packaging/Linux/build-arch.sh /home/build/build-arch.sh
|
||||
chmod 755 /home/build/build-arch.sh
|
||||
chown build:build /home/build/PKGBUILD
|
||||
chown build:build /home/build/build-arch.sh
|
||||
su build -c /home/build/build-arch.sh
|
||||
envVars:
|
||||
- name: GITEA_AUTH
|
||||
value: '@secret:GITEA_AUTH@'
|
||||
useTTY: true
|
||||
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL
|
||||
- !CommandStep
|
||||
name: Build and Publish Deb Package
|
||||
runInContainer: true
|
||||
image: onedev.site.tesses.net/dependencies/debbuilder/jammy:latest
|
||||
interpreter: !DefaultInterpreter
|
||||
commands: |
|
||||
cd Packaging/Linux
|
||||
bash build-ubuntu-jammy.sh
|
||||
bash push-ubuntu-jammy.sh
|
||||
envVars:
|
||||
- name: GITEA_AUTH
|
||||
value: '@secret:GITEA_AUTH@'
|
||||
- name: BUILD_NO
|
||||
value: '@build_number@'
|
||||
useTTY: true
|
||||
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL
|
||||
- !CommandStep
|
||||
name: Build and Publish Deb Package (Plucky)
|
||||
runInContainer: true
|
||||
image: onedev.site.tesses.net/dependencies/debbuilder/plucky:latest
|
||||
interpreter: !DefaultInterpreter
|
||||
commands: |
|
||||
cd Packaging/Linux
|
||||
bash build-ubuntu-plucky.sh
|
||||
bash push-ubuntu-plucky.sh
|
||||
envVars:
|
||||
- name: GITEA_AUTH
|
||||
value: '@secret:GITEA_AUTH@'
|
||||
- name: BUILD_NO
|
||||
value: '@build_number@'
|
||||
useTTY: true
|
||||
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL
|
||||
condition: SUCCESSFUL
|
||||
optional: false
|
||||
triggers:
|
||||
- !BranchUpdateTrigger
|
||||
- type: BranchUpdateTrigger
|
||||
branches: master
|
||||
userMatch: anyone
|
||||
projects: tesses-framework
|
||||
retryCondition: never
|
||||
maxRetries: 3
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(TessesFramework VERSION 1.0.0)
|
||||
set(TESSESFRAMEWORK_MAJOR_VERSION 0)
|
||||
set(TESSESFRAMEWORK_MINOR_VERSION 0)
|
||||
set(TESSESFRAMEWORK_PATCH_VERSION 2)
|
||||
|
||||
project(TessesFramework VERSION ${TESSESFRAMEWORK_MAJOR_VERSION}.${TESSESFRAMEWORK_MINOR_VERSION}.${TESSESFRAMEWORK_PATCH_VERSION})
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
list(APPEND TESSESFRAMEWORK_SOURCE
|
||||
src/Random.cpp
|
||||
src/Date/Date.cpp
|
||||
src/Http/FileServer.cpp
|
||||
src/Http/MountableServer.cpp
|
||||
src/Http/RouteServer.cpp
|
||||
src/Http/CallbackServer.cpp
|
||||
src/Http/HttpServer.cpp
|
||||
src/Http/HttpUtils.cpp
|
||||
@@ -22,6 +28,7 @@ src/Mail/Smtp.cpp
|
||||
src/Serialization/Json.cpp
|
||||
src/Serialization/SQLite.cpp
|
||||
src/Serialization/BitConverter.cpp
|
||||
src/Serialization/Bencode.cpp
|
||||
src/Platform/Environment.cpp
|
||||
src/Platform/Process.cpp
|
||||
src/Streams/FileStream.cpp
|
||||
@@ -58,6 +65,9 @@ src/Crypto/MbedTLS/Crypto.cpp
|
||||
src/Args.cpp
|
||||
src/TF_Init.cpp
|
||||
src/HiddenField.cpp
|
||||
src/Uuid.cpp
|
||||
src/BitTorrent/TorrentFile.cpp
|
||||
src/BitTorrent/TorrentStream.cpp
|
||||
)
|
||||
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
|
||||
|
||||
@@ -384,6 +394,36 @@ install(TARGETS ttime DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
||||
add_executable(tshell apps/tshell.cpp)
|
||||
target_link_libraries(tshell PUBLIC tessesframework)
|
||||
install(TARGETS tshell DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
||||
|
||||
add_executable(tprinttorrent apps/printtorrent.cpp)
|
||||
|
||||
target_link_libraries(tprinttorrent PUBLIC tessesframework)
|
||||
install(TARGETS tprinttorrent DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
||||
|
||||
add_executable(ttorrentcreate apps/ttorrentcreate.cpp)
|
||||
|
||||
target_link_libraries(ttorrentcreate PUBLIC tessesframework)
|
||||
install(TARGETS ttorrentcreate DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
||||
|
||||
add_executable(tclearwebseed apps/tclearwebseed.cpp)
|
||||
|
||||
target_link_libraries(tclearwebseed PUBLIC tessesframework)
|
||||
install(TARGETS tclearwebseed DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
||||
|
||||
add_executable(trng apps/trng.cpp)
|
||||
|
||||
target_link_libraries(trng PUBLIC tessesframework)
|
||||
install(TARGETS trng DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
||||
|
||||
add_executable(twatch apps/twatch.cpp)
|
||||
|
||||
target_link_libraries(twatch PUBLIC tessesframework)
|
||||
install(TARGETS twatch DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
||||
|
||||
add_executable(tuuid apps/tuuid.cpp)
|
||||
|
||||
target_link_libraries(tuuid PUBLIC tessesframework)
|
||||
install(TARGETS tuuid DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
||||
endif()
|
||||
|
||||
include(InstallRequiredSystemLibraries)
|
||||
|
||||
@@ -11,22 +11,11 @@ find_package(Threads REQUIRED)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(TESSESFRAMEWORK_ENABLE_LIBWEBCAM @TESSESFRAMEWORK_ENABLE_LIBWEBCAM@)
|
||||
|
||||
if(${TESSESFRAMEWORK_ENABLE_LIBWEBCAM})
|
||||
find_package(libwebcam)
|
||||
endif()
|
||||
|
||||
set(TESSESFRAMEWORK_ENABLE_SQLITE @TESSESFRAMEWORK_ENABLE_SQLITE@)
|
||||
set(TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS @TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS@)
|
||||
set(TESSESFRAMEWORK_ENABLE_SDL2 @TESSESFRAMEWORK_ENABLE_SDL2@)
|
||||
|
||||
|
||||
if(${TESSESFRAMEWORK_ENABLE_SDL2})
|
||||
find_package(SDL2 REQUIRED)
|
||||
find_package(SDL2_ttf REQUIRED)
|
||||
find_package(SDL2_image REQUIRED)
|
||||
endif()
|
||||
|
||||
set(TESSESFRAMEWORK_ENABLE_NETWORKING @TESSESFRAMEWORK_ENABLE_NETWORKING@)
|
||||
set(TESSESFRAMEWORK_ENABLE_THREADING @TESSESFRAMEWORK_ENABLE_THREADING@)
|
||||
set(TESSESFRAMEWORK_ENABLE_PROCESS @TESSESFRAMEWORK_ENABLE_PROCESS@)
|
||||
set(TESSESFRAMEWORK_ENABLE_RPATH @TESSESFRAMEWORK_ENABLE_RPATH@)
|
||||
|
||||
7
Dockerfile.portable-json-creator
Normal file
7
Dockerfile.portable-json-creator
Normal file
@@ -0,0 +1,7 @@
|
||||
FROM onedev.site.tesses.net/tesses-framework/tesses-framework:latest
|
||||
|
||||
COPY ./portable-json-creator /wwwroot
|
||||
|
||||
EXPOSE 4999
|
||||
|
||||
ENTRYPOINT ["tfileserver","--port","4999","/wwwroot"]
|
||||
@@ -1,6 +1,6 @@
|
||||
# Maintainer: Mike Nolan <tesses@tesses.net>
|
||||
pkgname=tesses-framework # '-bzr', '-git', '-hg' or '-svn'
|
||||
pkgver=1.0.0
|
||||
pkgver=0.0.2
|
||||
pkgrel=1
|
||||
pkgdesc=""
|
||||
arch=('x86_64' 'powerpc')
|
||||
@@ -18,21 +18,7 @@ options=(!strip)
|
||||
else
|
||||
options=(!buildflags !strip)
|
||||
fi
|
||||
# Please refer to the 'USING VCS SOURCES' section of the PKGBUILD man page for
|
||||
# a description of each element in the source array.
|
||||
|
||||
pkgver() {
|
||||
cd "$srcdir/${pkgname}"
|
||||
|
||||
# The examples below are not absolute and need to be adapted to each repo. The
|
||||
# primary goal is to generate version numbers that will increase according to
|
||||
# pacman's version comparisons with later commits to the repo. The format
|
||||
# VERSION='VER_NUM.rREV_NUM.HASH', or a relevant subset in case VER_NUM or HASH
|
||||
# are not available, is recommended.
|
||||
|
||||
# Git, no tags available
|
||||
printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
|
||||
}
|
||||
|
||||
prepare() {
|
||||
cd "$srcdir/${pkgname}"
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
export BUILD=$(($BUILD_NO-163))
|
||||
export DEB_VERSION=1.0.0-$BUILD
|
||||
export DEB_VERSION=0.0.2
|
||||
@@ -1,6 +1,11 @@
|
||||
#pragma once
|
||||
#define TESSES_FRAMEWORK_FLAG_OFF 0
|
||||
#define TESSES_FRAMEWORK_FLAG_ON 1
|
||||
|
||||
#define TESSES_FRAMEWORK_MAJOR @TESSESFRAMEWORK_MAJOR_VERSION@
|
||||
#define TESSES_FRAMEWORK_MINOR @TESSESFRAMEWORK_MINOR_VERSION@
|
||||
#define TESSES_FRAMEWORK_PATCH @TESSESFRAMEWORK_PATCH_VERSION@
|
||||
|
||||
#if defined(TESSES_FRAMEWORK_FLAG_@TESSESFRAMEWORK_ENABLE_SQLITE@) && !defined(TESSESFRAMEWORK_ENABLE_SQLITE)
|
||||
#define TESSESFRAMEWORK_ENABLE_SQLITE
|
||||
#endif
|
||||
|
||||
26
apps/printtorrent.cpp
Normal file
26
apps/printtorrent.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "TessesFramework/TessesFramework.hpp"
|
||||
|
||||
using namespace Tesses::Framework;
|
||||
using namespace Tesses::Framework::Streams;
|
||||
using namespace Tesses::Framework::Filesystem;
|
||||
using namespace Tesses::Framework::Serialization::Bencode;
|
||||
using namespace Tesses::Framework::TextStreams;
|
||||
using namespace Tesses::Framework::BitTorrent;
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
TF_Init();
|
||||
if(argc < 2)
|
||||
{
|
||||
printf("USAGE: %s /path/to/torrent/file\n",argv[0]);
|
||||
return 1;
|
||||
}
|
||||
auto strm = LocalFS->OpenFile((std::string)argv[1],"rb");
|
||||
auto bencode = Bencode::Load(strm);
|
||||
if(std::holds_alternative<BeDictionary>(bencode))
|
||||
{
|
||||
|
||||
TorrentFile file(std::get<BeDictionary>(bencode));
|
||||
file.Print(std::make_shared<ConsoleWriter>());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
22
apps/tclearwebseed.cpp
Normal file
22
apps/tclearwebseed.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "TessesFramework/TessesFramework.hpp"
|
||||
using namespace Tesses::Framework;
|
||||
using namespace Tesses::Framework::BitTorrent;
|
||||
using namespace Tesses::Framework::Serialization::Bencode;
|
||||
using namespace Tesses::Framework::Filesystem;
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
TF_Init();
|
||||
if(argc < 2)
|
||||
{
|
||||
printf("USAGE: %s torrent_file\n",argv[0]);
|
||||
return 1;
|
||||
}
|
||||
auto strm = LocalFS->OpenFile((std::string)argv[1],"rb");
|
||||
auto data = Bencode::Load(strm);
|
||||
if(std::holds_alternative<BeDictionary>(data)){
|
||||
std::get<BeDictionary>(data).SetValue("url-list",BeUndefined());
|
||||
}
|
||||
strm = LocalFS->OpenFile((std::string)argv[1],"wb");
|
||||
Bencode::Save(strm,data);
|
||||
return 0;
|
||||
}
|
||||
36
apps/trng.cpp
Normal file
36
apps/trng.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include "TessesFramework/TessesFramework.hpp"
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if(argc < 2)
|
||||
{
|
||||
|
||||
std::cout << "USAGE: " << argv[0] << " times" << std::endl;
|
||||
std::cout << "USAGE: " << argv[0] << " times max" << std::endl;
|
||||
std::cout << "USAGE: " << argv[0] << " times min max" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Tesses::Framework::Random random;
|
||||
int times=std::atoi(argv[1]);
|
||||
if(argc > 2)
|
||||
{
|
||||
int32_t min = 0;
|
||||
int32_t max = std::stoi(argv[2]);
|
||||
if(argc > 3)
|
||||
{
|
||||
min = max;
|
||||
max = std::stoi(argv[3]);
|
||||
|
||||
}
|
||||
for(int i = 0; i < times; i++)
|
||||
{
|
||||
std::cout << random.Next(min,max) << std::endl;
|
||||
}
|
||||
}
|
||||
else {
|
||||
for(int i = 0; i < times; i++)
|
||||
{
|
||||
std::cout << random.Next() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
66
apps/ttorrentcreate.cpp
Normal file
66
apps/ttorrentcreate.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "TessesFramework/TessesFramework.hpp"
|
||||
|
||||
using namespace Tesses::Framework;
|
||||
using namespace Tesses::Framework::Streams;
|
||||
using namespace Tesses::Framework::Filesystem;
|
||||
using namespace Tesses::Framework::Serialization::Bencode;
|
||||
using namespace Tesses::Framework::TextStreams;
|
||||
using namespace Tesses::Framework::BitTorrent;
|
||||
void usage(Args& args)
|
||||
{
|
||||
std::cout << "USAGE: " << args.filename << " [options] torrent_contents torrent_file.torrent" << std::endl << std::endl;
|
||||
std::cout << "OPTIONS:" << std::endl;
|
||||
std::cout << "tracker: torrent tracker url (require at least one)" << std::endl;
|
||||
std::cout << "webseed: url for web seeding" << std::endl;
|
||||
std::cout << "comment: set the comment" << std::endl;
|
||||
std::cout << "created_by: set the created by field, defaults to TessesFrameworkTorrent" << std::endl;
|
||||
std::cout << "piece_length: set the piece length, defaults to " + std::to_string(DEFAULT_PIECE_LENGTH) << std::endl << std::endl;
|
||||
std::cout << "FLAGS:" << std::endl;
|
||||
std::cout << "help: bring this help message up" << std::endl;
|
||||
std::cout << "private: enable private tracker flag" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
TF_Init();
|
||||
std::vector<BeString> webseeds;
|
||||
std::vector<BeString> trackers;
|
||||
bool isPrivate=false;
|
||||
int64_t pieceLength = DEFAULT_PIECE_LENGTH;
|
||||
std::string comment="";
|
||||
std::string created_by = "TessesFrameworkTorrent";
|
||||
|
||||
Args args(argc, argv);
|
||||
if(args.positional.size() < 2)
|
||||
usage(args);
|
||||
|
||||
for(auto& opt : args.options)
|
||||
{
|
||||
if(opt.first == "tracker")
|
||||
trackers.push_back(opt.second);
|
||||
else if(opt.first == "webseed")
|
||||
webseeds.push_back(opt.second);
|
||||
else if(opt.first == "comment")
|
||||
comment = opt.second;
|
||||
else if(opt.first == "created_by")
|
||||
created_by = opt.second;
|
||||
else if(opt.first == "piece_length")
|
||||
pieceLength = std::stoll(opt.second);
|
||||
}
|
||||
|
||||
for(auto& flag : args.flags)
|
||||
{
|
||||
if(flag == "help")
|
||||
usage(args);
|
||||
if(flag == "private")
|
||||
isPrivate = true;
|
||||
}
|
||||
|
||||
if(trackers.empty())
|
||||
usage(args);
|
||||
{
|
||||
auto torrent_file_stream = LocalFS->OpenFile(args.positional[1],"wb");
|
||||
TorrentFile::CreateTorrent(torrent_file_stream,trackers,webseeds,LocalFS, args.positional[0], isPrivate, pieceLength, comment, created_by);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
24
apps/tuuid.cpp
Normal file
24
apps/tuuid.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "TessesFramework/Uuid.hpp"
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
//e794499c
|
||||
using namespace Tesses::Framework;
|
||||
|
||||
Uuid uuid = Uuid::Generate();
|
||||
/*uuid.time_low = 0xe794499c;
|
||||
uuid.time_mid = 0x823c;
|
||||
uuid.time_hi_and_version = 0x304c;
|
||||
uuid.clock_seq_hi_and_reserved = 0xa4;
|
||||
|
||||
uuid.clock_seq_low = 0x59;
|
||||
uuid.node[0] = 0x8f;
|
||||
uuid.node[1] = 0xdc;
|
||||
uuid.node[2] = 0xd9;
|
||||
uuid.node[3] = 0x60;
|
||||
uuid.node[4] = 0xb4;
|
||||
uuid.node[5] = 0xac;*/
|
||||
|
||||
std::cout << uuid.ToString(Tesses::Framework::UuidStringifyConfig::LowercaseNoCurly) << std::endl;
|
||||
}
|
||||
24
apps/twatch.cpp
Normal file
24
apps/twatch.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <TessesFramework/TessesFramework.hpp>
|
||||
|
||||
using namespace Tesses::Framework;
|
||||
using namespace Tesses::Framework::Filesystem;
|
||||
using namespace Tesses::Framework::Filesystem::Literals;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
TF_Init();
|
||||
if(argc<2)
|
||||
{
|
||||
std::cout << "USAGE " << argv[0] << " <file|dir>" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto watcher=FSWatcher::Create(LocalFS,VFSPath{argv[1]});
|
||||
watcher->events = FSWatcherEventType::All;
|
||||
watcher->event = [](FSWatcherEvent& evt)->void{
|
||||
std::cout << evt.ToString() << std::endl;
|
||||
};
|
||||
watcher->SetEnabled(true);
|
||||
TF_RunEventLoop();
|
||||
return 0;
|
||||
}
|
||||
8
changelog.md
Normal file
8
changelog.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# Changelog
|
||||
|
||||
## 0.0.2
|
||||
Add UUIDs
|
||||
|
||||
|
||||
## 0.0.1
|
||||
Start versioning
|
||||
5
docker-compose/portable-json-creator/docker-compose.yml
Normal file
5
docker-compose/portable-json-creator/docker-compose.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
services:
|
||||
portable-json:
|
||||
image: onedev.site.tesses.net/tesses-framework/portable-json-creator:latest
|
||||
ports:
|
||||
- "4999:4999"
|
||||
@@ -2,6 +2,7 @@
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
Tesses::Framework::TF_Init();
|
||||
std::cout << "Basic auth test" << std::endl;
|
||||
std::string theEncoded="dXNlcjpwYXNz";
|
||||
Tesses::Framework::Http::ServerContext ctx(nullptr);
|
||||
ctx.requestHeaders.SetValue("Authorization","Basic " + theEncoded);
|
||||
@@ -10,4 +11,5 @@ int main(int argc, char** argv)
|
||||
std::cout << Tesses::Framework::Http::BasicAuthServer::GetCreds(ctx,user,pass) << std::endl;
|
||||
std::cout << user << std::endl;
|
||||
std::cout << pass << std::endl;
|
||||
|
||||
}
|
||||
@@ -139,11 +139,35 @@ class MyOtherWebServer : public IHttpServer
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
TF_InitWithConsole();
|
||||
std::shared_ptr<RouteServer> routeSvr = std::make_shared<RouteServer>();
|
||||
routeSvr->Get("/name/{name}/greeting",[](ServerContext& ctx)->bool{
|
||||
std::string name;
|
||||
if(ctx.pathArguments.TryGetFirst("name",name))
|
||||
{
|
||||
ctx.WithMimeType("text/plain").SendText("Hello " + name);
|
||||
}
|
||||
else {
|
||||
ctx.WithMimeType("text/plain").SendText("Please provide a name");
|
||||
}
|
||||
return true;
|
||||
});
|
||||
routeSvr->Get("/name/{name}/length",[](ServerContext& ctx)->bool{
|
||||
std::string name;
|
||||
if(ctx.pathArguments.TryGetFirst("name",name))
|
||||
{
|
||||
ctx.WithMimeType("text/plain").SendText("The length of the name is " + std::to_string(name.size()));
|
||||
}
|
||||
else {
|
||||
ctx.WithMimeType("text/plain").SendText("Please provide a name");
|
||||
}
|
||||
return true;
|
||||
});
|
||||
std::shared_ptr<MyOtherWebServer> myo = std::make_shared<MyOtherWebServer>();
|
||||
std::shared_ptr<MyWebServer> mws = std::make_shared<MyWebServer>();
|
||||
|
||||
std::shared_ptr<MountableServer> mountable = std::make_shared<MountableServer>(myo);
|
||||
mountable->Mount("/mymount/",mws);
|
||||
mountable->Mount("/routeSvr/", routeSvr);
|
||||
HttpServer server(10001,mountable);
|
||||
server.StartAccepting();
|
||||
TF_RunEventLoop();
|
||||
|
||||
70
include/TessesFramework/BitTorrent/TorrentFile.hpp
Normal file
70
include/TessesFramework/BitTorrent/TorrentFile.hpp
Normal file
@@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
#include "../Serialization/Bencode.hpp"
|
||||
#include "TorrentStream.hpp"
|
||||
namespace Tesses::Framework::BitTorrent
|
||||
{
|
||||
constexpr int DEFAULT_PIECE_LENGTH = 524288;
|
||||
|
||||
class TorrentFileInfo {
|
||||
|
||||
Serialization::Bencode::BeDictionary info;
|
||||
public:
|
||||
TorrentFileInfo();
|
||||
TorrentFileInfo(const Serialization::Bencode::BeDictionary& tkn);
|
||||
|
||||
|
||||
int64_t piece_length=DEFAULT_PIECE_LENGTH;
|
||||
Serialization::Bencode::BeString pieces;
|
||||
int64_t isPrivate=0;
|
||||
Serialization::Bencode::BeString name;
|
||||
std::variant<int64_t, std::vector<TorrentFileEntry>> fileListOrLength;
|
||||
int64_t GetTorrentFileSize();
|
||||
/// @brief Generate the info
|
||||
/// @return the dictionary containing the info
|
||||
Serialization::Bencode::BeDictionary& GenerateInfoDictionary();
|
||||
/// @brief Get the info hash for info (if you are filling out the fields, you need to call GenerateInfoDictionary first)
|
||||
/// @return the 20 byte info hash
|
||||
Serialization::Bencode::BeString GetInfoHash();
|
||||
/// @brief Get the info (does not regenerate info from the fields)
|
||||
/// @return the dictionary containing the info
|
||||
Serialization::Bencode::BeDictionary& GetInfoDictionary();
|
||||
|
||||
/// @brief Used to load a dictionary in, will overwrite all the fields
|
||||
/// @param dict the dictionary to load
|
||||
void Load(const Serialization::Bencode::BeDictionary& dict);
|
||||
/// @brief Gets a read write based on this info object
|
||||
/// @param vfs the vfs object
|
||||
/// @param path file or directory inside vfs
|
||||
/// @return The read write at object
|
||||
std::shared_ptr<ReadWriteAt> GetStreamFromFilesystem(std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, const Tesses::Framework::Filesystem::VFSPath& path);
|
||||
|
||||
/// @brief Create info from file/directory
|
||||
/// @param vfs the vfs object
|
||||
/// @param path file or directory inside vfs
|
||||
/// @param isPrivate whether you use private trackers
|
||||
/// @param pieceLength length of pieces
|
||||
void CreateFromFilesystem(std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, const Tesses::Framework::Filesystem::VFSPath& path, bool isPrivate=false, int64_t pieceLength=DEFAULT_PIECE_LENGTH);
|
||||
};
|
||||
class TorrentFile {
|
||||
public:
|
||||
TorrentFile();
|
||||
TorrentFile(const Serialization::Bencode::BeDictionary& tkn);
|
||||
|
||||
|
||||
Serialization::Bencode::BeString announce;
|
||||
std::vector<Serialization::Bencode::BeString> announce_list;
|
||||
std::vector<Serialization::Bencode::BeString> url_list;
|
||||
Serialization::Bencode::BeString comment;
|
||||
Serialization::Bencode::BeString created_by;
|
||||
int64_t creation_date=0;
|
||||
TorrentFileInfo info;
|
||||
|
||||
|
||||
Serialization::Bencode::BeDictionary ToDictionary();
|
||||
|
||||
void Print(std::shared_ptr<TextStreams::TextWriter> writer);
|
||||
|
||||
static void CreateTorrent(std::shared_ptr<Tesses::Framework::Streams::Stream> torrent_file_stream,const std::vector<Serialization::Bencode::BeString>& trackers,const std::vector<Serialization::Bencode::BeString>& webseeds, std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, const Tesses::Framework::Filesystem::VFSPath& path, bool isPrivate=false, int64_t pieceLength=DEFAULT_PIECE_LENGTH, Serialization::Bencode::BeString comment="", Serialization::Bencode::BeString created_by = "TessesFrameworkTorrent");
|
||||
|
||||
};
|
||||
}
|
||||
46
include/TessesFramework/BitTorrent/TorrentStream.hpp
Normal file
46
include/TessesFramework/BitTorrent/TorrentStream.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include "TessesFramework/Http/HttpClient.hpp"
|
||||
#include "TessesFramework/Streams/NetworkStream.hpp"
|
||||
namespace Tesses::Framework::BitTorrent
|
||||
{
|
||||
class TorrentFileEntry {
|
||||
public:
|
||||
TorrentFileEntry();
|
||||
TorrentFileEntry(Tesses::Framework::Filesystem::VFSPath path, int64_t length);
|
||||
Tesses::Framework::Filesystem::VFSPath path;
|
||||
int64_t length=0;
|
||||
};
|
||||
class ReadWriteAt {
|
||||
public:
|
||||
virtual bool ReadBlockAt(uint64_t offset, uint8_t* data, size_t len)=0;
|
||||
virtual void WriteBlockAt(uint64_t offset, const uint8_t* data, size_t len)=0;
|
||||
virtual ~ReadWriteAt();
|
||||
|
||||
};
|
||||
|
||||
class TorrentFileStream : public ReadWriteAt
|
||||
{
|
||||
Tesses::Framework::Threading::Mutex mtx;
|
||||
public:
|
||||
std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs;
|
||||
Tesses::Framework::Filesystem::VFSPath path;
|
||||
uint64_t length;
|
||||
TorrentFileStream(std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, Tesses::Framework::Filesystem::VFSPath path, uint64_t length);
|
||||
bool ReadBlockAt(uint64_t offset, uint8_t* data, size_t len);
|
||||
void WriteBlockAt(uint64_t offset, const uint8_t* data, size_t len);
|
||||
|
||||
};
|
||||
class TorrentDirectoryStream : public ReadWriteAt
|
||||
{
|
||||
|
||||
std::vector<Tesses::Framework::Threading::Mutex> mtxs;
|
||||
public:
|
||||
std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs;
|
||||
Tesses::Framework::Filesystem::VFSPath path;
|
||||
std::vector<TorrentFileEntry> entries;
|
||||
TorrentDirectoryStream(std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, Tesses::Framework::Filesystem::VFSPath path,std::vector<TorrentFileEntry> entries);
|
||||
bool ReadBlockAt(uint64_t offset, uint8_t* data, size_t len);
|
||||
void WriteBlockAt(uint64_t offset, const uint8_t* data, size_t len);
|
||||
};
|
||||
}
|
||||
@@ -9,9 +9,12 @@
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include "Threading/Mutex.hpp"
|
||||
#include <optional>
|
||||
#include <atomic>
|
||||
|
||||
namespace Tesses::Framework
|
||||
{
|
||||
|
||||
template<typename...TArgs>
|
||||
class Event {
|
||||
public:
|
||||
@@ -88,8 +91,11 @@ namespace Tesses::Framework
|
||||
};
|
||||
|
||||
extern EventList<uint64_t> OnItteraton;
|
||||
std::optional<std::string> TF_GetCommandName();
|
||||
|
||||
void TF_Init();
|
||||
void TF_InitWithConsole();
|
||||
void TF_AllowPortable(std::string argv0);
|
||||
void TF_ConnectToSelf(uint16_t port);
|
||||
void TF_InitEventLoop();
|
||||
void TF_RunEventLoop();
|
||||
@@ -101,6 +107,7 @@ namespace Tesses::Framework
|
||||
void TF_SetConsoleEventsEnabled(bool flag);
|
||||
void TF_InitConsole();
|
||||
void TF_Invoke(std::function<void()> cb);
|
||||
std::string TF_FileSize(uint64_t bytes, bool usesBin=true);
|
||||
|
||||
#if defined(TESSESFRAMEWORK_LOGTOFILE)
|
||||
void TF_Log(std::string dataToLog);
|
||||
|
||||
@@ -39,6 +39,12 @@ namespace Tesses::Framework::Filesystem
|
||||
|
||||
void Chmod(VFSPath path, uint32_t mode);
|
||||
|
||||
void Lock(VFSPath path);
|
||||
void Unlock(VFSPath path);
|
||||
|
||||
protected:
|
||||
std::shared_ptr<FSWatcher> CreateWatcher(std::shared_ptr<VFS> vfs, VFSPath path);
|
||||
|
||||
};
|
||||
extern std::shared_ptr<LocalFilesystem> LocalFS;
|
||||
}
|
||||
@@ -57,5 +57,7 @@ namespace Tesses::Framework::Filesystem
|
||||
|
||||
void Chmod(VFSPath path, uint32_t mode);
|
||||
|
||||
void Lock(VFSPath path);
|
||||
void Unlock(VFSPath path);
|
||||
};
|
||||
}
|
||||
@@ -41,5 +41,7 @@ namespace Tesses::Framework::Filesystem
|
||||
|
||||
void Chmod(VFSPath path, uint32_t mode);
|
||||
|
||||
void Lock(VFSPath path);
|
||||
void Unlock(VFSPath path);
|
||||
};
|
||||
}
|
||||
@@ -46,6 +46,9 @@ namespace Tesses::Framework::Filesystem
|
||||
|
||||
void Chmod(VFSPath path, uint32_t mode);
|
||||
void Close();
|
||||
|
||||
void Lock(VFSPath path);
|
||||
void Unlock(VFSPath path);
|
||||
~TempFS();
|
||||
};
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
namespace Tesses::Framework::Filesystem
|
||||
{
|
||||
|
||||
class StatVFSData {
|
||||
public:
|
||||
uint64_t BlockSize;
|
||||
@@ -29,13 +30,15 @@ namespace Tesses::Framework::Filesystem
|
||||
static std::vector<std::string> SplitPath(std::string path);
|
||||
std::vector<std::string> path;
|
||||
VFSPath();
|
||||
|
||||
explicit VFSPath(const char* path) : VFSPath(std::string(path))
|
||||
{}
|
||||
VFSPath(std::vector<std::string> path);
|
||||
VFSPath(std::string path);
|
||||
VFSPath(VFSPath p, std::string subent);
|
||||
VFSPath(VFSPath p, VFSPath p2);
|
||||
|
||||
|
||||
//does not check for ?
|
||||
static VFSPath ParseUriPath(std::string path);
|
||||
|
||||
VFSPath GetParent() const;
|
||||
VFSPath CollapseRelativeParents() const;
|
||||
@@ -117,7 +120,74 @@ namespace Tesses::Framework::Filesystem
|
||||
|
||||
VFSPathEnumeratorItterator end();
|
||||
};
|
||||
enum class FSWatcherEventType {
|
||||
|
||||
None = 0,
|
||||
//IN_ACCESS
|
||||
Accessed=1,
|
||||
//IN_ATTRIB
|
||||
AttributeChanged =2,
|
||||
//IN_CLOSE_WRITE
|
||||
Writen = 4,
|
||||
//IN_CLOSE_NOWRITE
|
||||
Read = 8,
|
||||
//IN_CREATE
|
||||
Created = 16,
|
||||
//IN_DELETE
|
||||
Deleted = 32,
|
||||
//IN_DELETE_SELF
|
||||
WatchEntryDeleted = 64,
|
||||
//IN_MODIFY
|
||||
Modified = 128,
|
||||
//IN_MOVE_SELF
|
||||
WatchEntryMoved = 256,
|
||||
//IN_MOVED_FROM
|
||||
MoveOld = 512,
|
||||
//IN_MOVED_TO
|
||||
MoveNew = 1024,
|
||||
//IN_OPEN
|
||||
Opened = 2048,
|
||||
//IN_CLOSE
|
||||
Closed = Writen | Read,
|
||||
//IN_MOVE
|
||||
Moved = MoveOld | MoveNew,
|
||||
//IN_ALL_EVENTS
|
||||
All = Accessed | AttributeChanged | Created | Deleted | WatchEntryDeleted | Modified | WatchEntryMoved | Opened | Closed | Moved
|
||||
};
|
||||
struct FSWatcherEvent {
|
||||
//the file or source on move
|
||||
VFSPath src;
|
||||
//the dest when moving
|
||||
VFSPath dest;
|
||||
FSWatcherEventType type;
|
||||
bool isDir;
|
||||
|
||||
bool IsEvent(FSWatcherEventType e);
|
||||
|
||||
std::string ToString();
|
||||
};
|
||||
class VFS;
|
||||
|
||||
class FSWatcher {
|
||||
private:
|
||||
std::shared_ptr<VFS> vfs;
|
||||
VFSPath path;
|
||||
protected:
|
||||
|
||||
std::atomic<bool> enabled=false;
|
||||
virtual void SetEnabledImpl(bool enabled);
|
||||
public:
|
||||
FSWatcher(std::shared_ptr<VFS> vfs, VFSPath path);
|
||||
std::function<void(FSWatcherEvent&)> event;
|
||||
FSWatcherEventType events = FSWatcherEventType::All;
|
||||
bool GetEnabled();
|
||||
void SetEnabled(bool val);
|
||||
std::shared_ptr<VFS> GetFilesystem();
|
||||
const VFSPath& GetPath();
|
||||
virtual ~FSWatcher() = default;
|
||||
|
||||
static std::shared_ptr<FSWatcher> Create(std::shared_ptr<VFS> vfs, VFSPath path);
|
||||
};
|
||||
|
||||
class VFS {
|
||||
public:
|
||||
@@ -153,8 +223,27 @@ namespace Tesses::Framework::Filesystem
|
||||
|
||||
virtual void Chmod(VFSPath path, uint32_t mode);
|
||||
|
||||
virtual void Lock(VFSPath path);
|
||||
virtual void Unlock(VFSPath path);
|
||||
|
||||
|
||||
virtual ~VFS();
|
||||
|
||||
virtual void Close();
|
||||
|
||||
protected:
|
||||
virtual std::shared_ptr<FSWatcher> CreateWatcher(std::shared_ptr<VFS> vfs, VFSPath path);
|
||||
friend class FSWatcher;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
namespace Literals
|
||||
{
|
||||
inline VFSPath operator""_tpath(const char* path)
|
||||
{
|
||||
return VFSPath(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,4 +3,8 @@
|
||||
#undef DeleteFile
|
||||
#undef MoveFile
|
||||
#undef MoveDirectory
|
||||
|
||||
/*
|
||||
Just in case
|
||||
*/
|
||||
#undef Lock
|
||||
#undef Unlock
|
||||
@@ -23,6 +23,8 @@ namespace Tesses::Framework::Http
|
||||
HttpDictionary requestHeaders;
|
||||
HttpDictionary responseHeaders;
|
||||
HttpDictionary queryParams;
|
||||
//used by path
|
||||
HttpDictionary pathArguments;
|
||||
std::string path;
|
||||
std::string originalPath;
|
||||
std::string method;
|
||||
|
||||
@@ -25,6 +25,7 @@ namespace Tesses::Framework::Http
|
||||
int64_t GetPosition();
|
||||
size_t Read(uint8_t* buffer, size_t len);
|
||||
size_t Write(const uint8_t* buffer, size_t len);
|
||||
void Close();
|
||||
~HttpStream();
|
||||
};
|
||||
}
|
||||
@@ -143,8 +143,16 @@ struct CaseInsensitiveLess {
|
||||
class HttpUtils
|
||||
{
|
||||
public:
|
||||
static char NibbleToHex(uint8_t b, bool isUppercase);
|
||||
static char NibbleToHex(uint8_t nibble);
|
||||
static uint8_t HexToNibble(char c);
|
||||
static std::string BytesToHex(const std::vector<uint8_t>& data);
|
||||
static void BytesToHex(std::string& text,const std::vector<uint8_t>& data);
|
||||
|
||||
static std::string BytesToHex(const std::vector<uint8_t>& data,bool isUppercase);
|
||||
static void BytesToHex(std::string& text,const std::vector<uint8_t>& data, bool isUppercase);
|
||||
static std::vector<uint8_t> HexToBytes(const std::string& text);
|
||||
static void HexToBytes(std::vector<uint8_t>& data,const std::string& text);
|
||||
static std::string MimeType(std::filesystem::path p);
|
||||
static bool Invalid(char c);
|
||||
static std::string Sanitise(std::string text);
|
||||
|
||||
41
include/TessesFramework/Http/RouteServer.hpp
Normal file
41
include/TessesFramework/Http/RouteServer.hpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
#include "HttpServer.hpp"
|
||||
#include "../Filesystem/VFSFix.hpp"
|
||||
#include "../Filesystem/VFS.hpp"
|
||||
|
||||
namespace Tesses::Framework::Http
|
||||
{
|
||||
using ServerRequestHandler = std::function<bool(ServerContext&)>;
|
||||
|
||||
|
||||
class RouteServer : public IHttpServer
|
||||
{
|
||||
class RouteServerRoute {
|
||||
public:
|
||||
std::vector<std::pair<std::string,bool>> parts;
|
||||
std::string method;
|
||||
ServerRequestHandler handler;
|
||||
|
||||
RouteServerRoute() = default;
|
||||
RouteServerRoute(std::string route, std::string method, ServerRequestHandler handler);
|
||||
bool Equals(Tesses::Framework::Filesystem::VFSPath& path, HttpDictionary& args);
|
||||
};
|
||||
std::vector<RouteServerRoute> routes;
|
||||
std::shared_ptr<IHttpServer> root;
|
||||
public:
|
||||
RouteServer() = default;
|
||||
RouteServer(std::shared_ptr<IHttpServer> root);
|
||||
void Get(std::string pattern, ServerRequestHandler handler);
|
||||
void Post(std::string pattern, ServerRequestHandler handler);
|
||||
void Put(std::string pattern, ServerRequestHandler handler);
|
||||
void Patch(std::string pattern, ServerRequestHandler handler);
|
||||
|
||||
void Delete(std::string pattern, ServerRequestHandler handler);
|
||||
|
||||
void Trace(std::string pattern, ServerRequestHandler handler);
|
||||
void Options(std::string pattern, ServerRequestHandler handler);
|
||||
void Add(std::string method, std::string pattern, ServerRequestHandler handler);
|
||||
bool Handle(ServerContext& ctx);
|
||||
|
||||
};
|
||||
}
|
||||
@@ -6,7 +6,23 @@ namespace Tesses::Framework::Platform::Environment
|
||||
{
|
||||
extern const char EnvPathSeperator;
|
||||
|
||||
|
||||
struct PortableAppConfig {
|
||||
|
||||
std::optional<Tesses::Framework::Filesystem::VFSPath> desktop;
|
||||
std::optional<Tesses::Framework::Filesystem::VFSPath> documents;
|
||||
std::optional<Tesses::Framework::Filesystem::VFSPath> music;
|
||||
std::optional<Tesses::Framework::Filesystem::VFSPath> pictures;
|
||||
std::optional<Tesses::Framework::Filesystem::VFSPath> videos;
|
||||
std::optional<Tesses::Framework::Filesystem::VFSPath> downloads;
|
||||
std::optional<Tesses::Framework::Filesystem::VFSPath> user;
|
||||
std::optional<Tesses::Framework::Filesystem::VFSPath> config;
|
||||
std::optional<Tesses::Framework::Filesystem::VFSPath> state;
|
||||
std::optional<Tesses::Framework::Filesystem::VFSPath> data;
|
||||
std::optional<Tesses::Framework::Filesystem::VFSPath> cache;
|
||||
std::optional<Tesses::Framework::Filesystem::VFSPath> temp;
|
||||
};
|
||||
|
||||
extern PortableAppConfig portable_config;
|
||||
|
||||
namespace SpecialFolders {
|
||||
Tesses::Framework::Filesystem::VFSPath GetTemp();
|
||||
|
||||
15
include/TessesFramework/Random.hpp
Normal file
15
include/TessesFramework/Random.hpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
#include "Common.hpp"
|
||||
|
||||
namespace Tesses::Framework {
|
||||
class Random {
|
||||
uint64_t num;
|
||||
public:
|
||||
Random();
|
||||
Random(uint64_t seed);
|
||||
uint64_t Next();
|
||||
uint32_t Next(uint32_t max);
|
||||
int32_t Next(int32_t min,int32_t max);
|
||||
uint8_t NextByte();
|
||||
};
|
||||
}
|
||||
59
include/TessesFramework/Serialization/Bencode.hpp
Normal file
59
include/TessesFramework/Serialization/Bencode.hpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <variant>
|
||||
#include <string>
|
||||
#include "../Streams/Stream.hpp"
|
||||
#include "../TextStreams/TextWriter.hpp"
|
||||
#include "Json.hpp"
|
||||
namespace Tesses::Framework::Serialization::Bencode {
|
||||
class BeArray;
|
||||
class BeDictionary;
|
||||
class BeString;
|
||||
using BeUndefined = std::monostate;
|
||||
|
||||
using BeToken = std::variant<BeUndefined,BeArray,BeDictionary,BeString,int64_t>;
|
||||
|
||||
class BeArray {
|
||||
public:
|
||||
std::vector<BeToken> tokens;
|
||||
};
|
||||
|
||||
class BeDictionary {
|
||||
public:
|
||||
std::vector<std::pair<BeString,BeToken>> tokens;
|
||||
BeToken GetValue(BeString key) const;
|
||||
void SetValue(BeString key, BeToken value);
|
||||
};
|
||||
class BeString {
|
||||
public:
|
||||
BeString();
|
||||
BeString(const std::string& text);
|
||||
BeString(const char* text);
|
||||
BeString(const std::vector<uint8_t>& data);
|
||||
std::vector<uint8_t> data;
|
||||
|
||||
operator std::string() const;
|
||||
};
|
||||
bool operator==(const BeString& lStr, const BeString& rStr);
|
||||
bool operator==(const BeString& lStr, const std::string& rStr);
|
||||
bool operator==(const std::string& lStr, const BeString& rStr);
|
||||
bool operator==(const BeString& lStr, const char* rStr);
|
||||
bool operator==(const char* lStr, const BeString& rStr);
|
||||
|
||||
bool operator!=(const BeString& lStr, const BeString& rStr);
|
||||
bool operator!=(const BeString& lStr, const std::string& rStr);
|
||||
bool operator!=(const std::string& lStr, const BeString& rStr);
|
||||
bool operator!=(const BeString& lStr, const char* rStr);
|
||||
bool operator!=(const char* lStr, const BeString& rStr);
|
||||
|
||||
class Bencode {
|
||||
public:
|
||||
static void Save(std::shared_ptr<Tesses::Framework::Streams::Stream> strm,const BeToken& value);
|
||||
static BeToken Load(std::shared_ptr<Tesses::Framework::Streams::Stream> strm);
|
||||
//This cannot be converted back to torrent, this may (probably will) be out of order
|
||||
static Json::JToken ToJson(const BeToken& tkn);
|
||||
//This may (probably will) be out of order
|
||||
static void Print(std::shared_ptr<Tesses::Framework::TextStreams::TextWriter> writer, BeToken tkn);
|
||||
};
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include "../Common.hpp"
|
||||
#include "../Uuid.hpp"
|
||||
namespace Tesses::Framework::Serialization
|
||||
{
|
||||
|
||||
@@ -9,40 +10,73 @@ namespace Tesses::Framework::Serialization
|
||||
*/
|
||||
class BitConverter {
|
||||
public:
|
||||
/**
|
||||
* @brief Get the bits of a double from a int64_t
|
||||
*
|
||||
* @param v a int64_t with double's bits
|
||||
* @return double the double
|
||||
*/
|
||||
|
||||
static double ToDoubleBits(uint64_t v);
|
||||
/**
|
||||
* @brief Get the bits of a int64_t from a double
|
||||
*
|
||||
* @param v a double with int64_t's bits
|
||||
* @return uint64_t the int64_t
|
||||
*/
|
||||
|
||||
static uint64_t ToUintBits(double v);
|
||||
/**
|
||||
* @brief Get big endian double from uint8_t reference of first element of 8 byte array
|
||||
*
|
||||
* @param b a reference to the first byte of an array
|
||||
* @return double the double
|
||||
*/
|
||||
static float ToFloatBits(uint32_t v);
|
||||
|
||||
static uint32_t ToUint32Bits(float v);
|
||||
|
||||
static double ToDoubleBE(uint8_t& b);
|
||||
/**
|
||||
* @brief Get big endian uint64_t from uint8_t reference of first element of 8 byte array
|
||||
*
|
||||
* @param b a reference to the first byte of an array
|
||||
* @return uint64_t the uint64_t
|
||||
*/
|
||||
static float ToFloatBE(uint8_t& b);
|
||||
|
||||
static uint64_t ToUint64BE(uint8_t& b);
|
||||
static uint32_t ToUint32BE(uint8_t& b);
|
||||
static uint16_t ToUint16BE(uint8_t& b);
|
||||
|
||||
static double ToDoubleLE(uint8_t& b);
|
||||
static float ToFloatLE(uint8_t& b);
|
||||
static uint64_t ToUint64LE(uint8_t& b);
|
||||
static uint32_t ToUint32LE(uint8_t& b);
|
||||
static uint16_t ToUint16LE(uint8_t& b);
|
||||
|
||||
static int64_t ToSint64BE(uint8_t& b);
|
||||
static int32_t ToSint32BE(uint8_t& b);
|
||||
static int16_t ToSint16BE(uint8_t& b);
|
||||
static int64_t ToSint64LE(uint8_t& b);
|
||||
static int32_t ToSint32LE(uint8_t& b);
|
||||
static int16_t ToSint16LE(uint8_t& b);
|
||||
|
||||
static Uuid ToUuid(uint8_t& b);
|
||||
|
||||
|
||||
static void ToUuid(uint8_t& b, Uuid& uuid);
|
||||
|
||||
|
||||
static void FromDoubleBE(uint8_t& b, double v);
|
||||
static void FromFloatBE(uint8_t& b, float v);
|
||||
static void FromUint64BE(uint8_t& b, uint64_t v);
|
||||
static void FromUint32BE(uint8_t& b, uint32_t v);
|
||||
static void FromUint16BE(uint8_t& b, uint16_t v);
|
||||
|
||||
static void FromDoubleLE(uint8_t& b, double v);
|
||||
static void FromFloatLE(uint8_t& b, float v);
|
||||
static void FromUint64LE(uint8_t& b, uint64_t v);
|
||||
static void FromUint32LE(uint8_t& b, uint32_t v);
|
||||
static void FromUint16LE(uint8_t& b, uint16_t v);
|
||||
|
||||
static void FromSint64BE(uint8_t& b, int64_t v);
|
||||
static void FromSint32BE(uint8_t& b, int32_t v);
|
||||
static void FromSint16BE(uint8_t& b, int16_t v);
|
||||
|
||||
static void FromSint64LE(uint8_t& b, int64_t v);
|
||||
static void FromSint32LE(uint8_t& b, int32_t v);
|
||||
static void FromSint16LE(uint8_t& b, int16_t v);
|
||||
|
||||
static void FromUuid(uint8_t& b, const Uuid& uuid);
|
||||
|
||||
|
||||
|
||||
static inline bool IsLittleEndian()
|
||||
{
|
||||
uint8_t a[2];
|
||||
a[0] = 0x01;
|
||||
a[1] = 0xA4;
|
||||
uint16_t num=0;
|
||||
memcpy(&num,&a, 2);
|
||||
return num != 420;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -9,9 +9,7 @@ namespace Tesses::Framework::Serialization::Json
|
||||
{
|
||||
class JArray;
|
||||
class JObject;
|
||||
class JUndefined {
|
||||
public:
|
||||
};
|
||||
using JUndefined = std::monostate;
|
||||
|
||||
using JToken = std::variant<JUndefined,std::nullptr_t,bool,int64_t,double,std::string, JArray, JObject>;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include "Stream.hpp"
|
||||
#include "../Uuid.hpp"
|
||||
|
||||
namespace Tesses::Framework::Streams
|
||||
{
|
||||
@@ -26,5 +27,7 @@ namespace Tesses::Framework::Streams
|
||||
float ReadF32LE();
|
||||
double ReadF64BE();
|
||||
double ReadF64LE();
|
||||
Uuid ReadUuid();
|
||||
void ReadUuid(Uuid& uuid);
|
||||
};
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#include "Stream.hpp"
|
||||
|
||||
#include "../Uuid.hpp"
|
||||
namespace Tesses::Framework::Streams
|
||||
{
|
||||
class ByteWriter {
|
||||
@@ -27,5 +27,6 @@ namespace Tesses::Framework::Streams
|
||||
void WriteF32LE(float v);
|
||||
void WriteF64BE(double v);
|
||||
void WriteF64LE(double v);
|
||||
void WriteUuid(const Uuid& uuid);
|
||||
};
|
||||
}
|
||||
@@ -34,6 +34,7 @@ namespace Tesses::Framework::Streams
|
||||
bool success;
|
||||
bool endOfStream;
|
||||
public:
|
||||
bool DataAvailable(int timeout=0);
|
||||
bool EndOfStream();
|
||||
bool CanRead();
|
||||
bool CanWrite();
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "Http/ChangeableServer.hpp"
|
||||
#include "Http/CGIServer.hpp"
|
||||
#include "Http/BasicAuthServer.hpp"
|
||||
#include "Http/RouteServer.hpp"
|
||||
#include "Streams/FileStream.hpp"
|
||||
#include "Streams/MemoryStream.hpp"
|
||||
#include "Streams/NetworkStream.hpp"
|
||||
@@ -39,7 +40,10 @@
|
||||
#include "HiddenField.hpp"
|
||||
#include "Serialization/Json.hpp"
|
||||
#include "Serialization/SQLite.hpp"
|
||||
#include "Serialization/Bencode.hpp"
|
||||
#include "Platform/Environment.hpp"
|
||||
#include "Platform/Process.hpp"
|
||||
#include "Serialization/BitConverter.hpp"
|
||||
#include "Args.hpp"
|
||||
#include "Args.hpp"
|
||||
#include "BitTorrent/TorrentFile.hpp"
|
||||
#include "Random.hpp"
|
||||
@@ -5,12 +5,14 @@ namespace Tesses::Framework::TextStreams
|
||||
{
|
||||
class TextReader
|
||||
{
|
||||
bool eof=false;
|
||||
public:
|
||||
virtual bool Rewind();
|
||||
virtual bool ReadBlock(std::string& str,size_t sz)=0;
|
||||
int32_t ReadChar();
|
||||
std::string ReadLine();
|
||||
bool ReadLine(std::string& str);
|
||||
bool ReadLineHttp(std::string& str);
|
||||
void ReadAllLines(std::vector<std::string>& lines);
|
||||
std::string ReadToEnd();
|
||||
void ReadToEnd(std::string& str);
|
||||
|
||||
37
include/TessesFramework/Uuid.hpp
Normal file
37
include/TessesFramework/Uuid.hpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
#include "Common.hpp"
|
||||
|
||||
namespace Tesses::Framework {
|
||||
enum class UuidStringifyConfig {
|
||||
IsUppercase=0b001,
|
||||
HasCurly=0b010,
|
||||
HasDashes=0b100,
|
||||
UppercaseCompact = IsUppercase,
|
||||
LowercaseCompact = 0,
|
||||
UppercaseNoCurly = IsUppercase | HasDashes,
|
||||
LowercaseNoCurly = HasDashes,
|
||||
UppercaseCurly = IsUppercase | HasDashes | HasCurly,
|
||||
LowercaseCurly = HasDashes | HasCurly
|
||||
};
|
||||
struct Uuid {
|
||||
Uuid() = default;
|
||||
uint32_t time_low = 0;
|
||||
uint16_t time_mid = 0;
|
||||
uint16_t time_hi_and_version = 0;
|
||||
uint8_t clock_seq_hi_and_reserved = 0;
|
||||
uint8_t clock_seq_low = 0;
|
||||
uint8_t node[6] = {0,0,0,0,0,0};
|
||||
|
||||
static Uuid Generate();
|
||||
static void Generate(Uuid& uuid);
|
||||
|
||||
static bool TryParse(std::string text, Uuid& uuid);
|
||||
|
||||
std::string ToString(UuidStringifyConfig cfg = UuidStringifyConfig::UppercaseCurly) const;
|
||||
|
||||
bool IsEmpty() const;
|
||||
};
|
||||
|
||||
bool operator==(const Uuid& left, const Uuid& right);
|
||||
bool operator!=(const Uuid& left, const Uuid& right);
|
||||
}
|
||||
25
portable-json-creator/index.html
Normal file
25
portable-json-creator/index.html
Normal file
@@ -0,0 +1,25 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>TessesFramework portable.json creator</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>TessesFramework portable.json creator</h1>
|
||||
|
||||
Choose the way you want your app to be portable
|
||||
<ul>
|
||||
<li>
|
||||
<a href="./portableapps/">
|
||||
PortableApps.com
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="./relative/">
|
||||
Relative To Application
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
98
portable-json-creator/portableapps/index.html
Normal file
98
portable-json-creator/portableapps/index.html
Normal file
@@ -0,0 +1,98 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>TessesFramework portable.json creator</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>TessesFramework portable.json creator</h1>
|
||||
Don't want PortableApps.com click <a href="../relative/">here</a> for just having directories relative to application
|
||||
|
||||
<fieldset>
|
||||
<legend>Configuration</legend>
|
||||
|
||||
<label for="user">TF_User: </label>
|
||||
<select id="user">
|
||||
<option value="system">System's %USERPROFILE%</option>
|
||||
<option value="app">MyApp\Data\TF_User (tied to app)</option>
|
||||
<option value="documents" selected>PAF\Documents\TF_User (shared between all tessesframework portable apps)</option>
|
||||
</select>
|
||||
|
||||
<br>
|
||||
<label for="desktop">Desktop: </label>
|
||||
<select id="desktop">
|
||||
<option value="system">System's</option>
|
||||
<option value="tf_user">TF_User\Desktop</option>
|
||||
<option value="documents" selected>PAF\Documents\Desktop</option>
|
||||
</select>
|
||||
<br>
|
||||
<label for="downloads">Downloads: </label>
|
||||
<select id="downloads">
|
||||
<option value="system">System's</option>
|
||||
<option value="tf_user">TF_User\Downloads</option>
|
||||
<option value="documents" selected>PAF\Documents\Downloads</option>
|
||||
</select>
|
||||
<br>
|
||||
<input type="checkbox" id="system_documents">
|
||||
<label for="system_documents">Use System's Documents Folder</label>
|
||||
<br>
|
||||
<input type="checkbox" id="system_pictures">
|
||||
<label for="system_pictures">Use System's Pictures Folder</label>
|
||||
<br>
|
||||
<input type="checkbox" id="system_videos">
|
||||
<label for="system_videos">Use System's Videos Folder</label>
|
||||
<br>
|
||||
<input type="checkbox" id="system_music">
|
||||
<label for="system_music">Use System's Music Folder</label>
|
||||
<br>
|
||||
<input type="checkbox" id="system_config">
|
||||
<label for="system_config">Use System's Config Folder</label>
|
||||
<br>
|
||||
<input type="checkbox" id="system_cache">
|
||||
<label for="system_cache">Use System's Cache Folder</label>
|
||||
<br>
|
||||
<input type="checkbox" id="system_data">
|
||||
<label for="system_data">Use System's Data Folder</label>
|
||||
<br>
|
||||
<input type="checkbox" id="system_state">
|
||||
<label for="system_state">Use System's State Folder</label>
|
||||
|
||||
<br>
|
||||
<input type="checkbox" id="system_temp" checked>
|
||||
<label for="system_temp">Use System's Temp Folder</label>
|
||||
|
||||
</fieldset>
|
||||
<button id="saveBtn">Save</button>
|
||||
<script>
|
||||
(function(){
|
||||
saveBtn.onclick = ()=>{
|
||||
const dict = {
|
||||
portable_type: "PortableApps.com",
|
||||
portable_data: {
|
||||
user: user.value,
|
||||
desktop: desktop.value,
|
||||
downloads: downloads.value,
|
||||
|
||||
system_documents: system_documents.checked,
|
||||
system_pictures: system_pictures.checked,
|
||||
system_videos: system_videos.checked,
|
||||
system_music: system_music.checked,
|
||||
system_config: system_config.checked,
|
||||
system_cache: system_cache.checked,
|
||||
system_data: system_data.checked,
|
||||
system_state: system_state.checked,
|
||||
system_temp: system_temp.checked
|
||||
}
|
||||
};
|
||||
|
||||
const json = JSON.stringify(dict,null,'\t');
|
||||
const a = document.createElement('a');
|
||||
a.href = `data:text/json;charset=utf-8,${encodeURIComponent(json)}`;
|
||||
a.download="portable.json";
|
||||
a.click();
|
||||
};
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
157
portable-json-creator/relative/index.html
Normal file
157
portable-json-creator/relative/index.html
Normal file
@@ -0,0 +1,157 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>TessesFramework portable.json creator</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>TessesFramework portable.json creator</h1>
|
||||
Want PortableApps.com click <a href="../portableapps/">here</a>
|
||||
<br>system: don't make this folder portable, default: the default value (not portable if TF_User directory is "system", otherwise it's a subdirectory in the TF_User directory), anything else is relative to executable directory (for all of these)
|
||||
<br>Please use "/" and not "\" even if on Windows
|
||||
<fieldset>
|
||||
<legend>Configuration</legend>
|
||||
<label for="user">TF_User: </label>
|
||||
<datalist id="user_datalist">
|
||||
<option>system</option>
|
||||
<option>../TF_User</option>
|
||||
</datalist>
|
||||
<input type="text" id="user" value="../TF_User" list="user_datalist">
|
||||
|
||||
<br>
|
||||
<label for="documents">Documents: </label>
|
||||
<datalist id="documents_datalist">
|
||||
<option>system</option>
|
||||
<option>default</option>
|
||||
<option>../TF_User/Documents</option>
|
||||
</datalist>
|
||||
<input type="text" id="documents" value="default" list="documents_datalist">
|
||||
|
||||
<br>
|
||||
<label for="downloads">Downloads: </label>
|
||||
<datalist id="downloads_datalist">
|
||||
<option>system</option>
|
||||
<option>default</option>
|
||||
<option>../TF_User/Downloads</option>
|
||||
</datalist>
|
||||
<input type="text" id="downloads" value="default" list="downloads_datalist">
|
||||
|
||||
<br>
|
||||
<label for="desktop">Desktop: </label>
|
||||
<datalist id="desktop_datalist">
|
||||
<option>system</option>
|
||||
<option>default</option>
|
||||
<option>../TF_User/Desktop</option>
|
||||
</datalist>
|
||||
<input type="text" id="desktop" value="default" list="desktop_datalist">
|
||||
|
||||
|
||||
|
||||
<br>
|
||||
<label for="pictures">Pictures: </label>
|
||||
<datalist id="pictures_datalist">
|
||||
<option>system</option>
|
||||
<option>default</option>
|
||||
<option>../TF_User/Pictures</option>
|
||||
</datalist>
|
||||
<input type="text" id="pictures" value="default" list="pictures_datalist">
|
||||
|
||||
<br>
|
||||
<label for="videos">Videos: </label>
|
||||
<datalist id="videos_datalist">
|
||||
<option>system</option>
|
||||
<option>default</option>
|
||||
<option>../TF_User/Videos</option>
|
||||
</datalist>
|
||||
<input type="text" id="videos" value="default" list="videos_datalist">
|
||||
|
||||
<br>
|
||||
<label for="music">Music: </label>
|
||||
<datalist id="music_datalist">
|
||||
<option>system</option>
|
||||
<option>default</option>
|
||||
<option>../TF_User/Music</option>
|
||||
</datalist>
|
||||
<input type="text" id="music" value="default" list="music_datalist">
|
||||
|
||||
<br>
|
||||
<label for="config">Config: </label>
|
||||
<datalist id="config_datalist">
|
||||
<option>system</option>
|
||||
<option>default</option>
|
||||
<option>../TF_User/Config</option>
|
||||
</datalist>
|
||||
<input type="text" id="config" value="default" list="config_datalist">
|
||||
|
||||
<br>
|
||||
<label for="cache">Cache: </label>
|
||||
<datalist id="cache_datalist">
|
||||
<option>system</option>
|
||||
<option>default</option>
|
||||
<option>../TF_User/Cache</option>
|
||||
</datalist>
|
||||
<input type="text" id="cache" value="default" list="cache_datalist">
|
||||
|
||||
<br>
|
||||
<label for="data">Data: </label>
|
||||
<datalist id="data_datalist">
|
||||
<option>system</option>
|
||||
<option>default</option>
|
||||
<option>../TF_User/Data</option>
|
||||
</datalist>
|
||||
<input type="text" id="data" value="default" list="data_datalist">
|
||||
|
||||
<br>
|
||||
<label for="state">State: </label>
|
||||
<datalist id="state_datalist">
|
||||
<option>system</option>
|
||||
<option>default</option>
|
||||
<option>../TF_User/State</option>
|
||||
</datalist>
|
||||
<input type="text" id="state" value="default" list="state_datalist">
|
||||
|
||||
<br>
|
||||
<label for="temp">Temp: </label>
|
||||
<datalist id="temp_datalist">
|
||||
<option>system</option>
|
||||
<option>default</option>
|
||||
<option>../TF_User/Temp</option>
|
||||
</datalist>
|
||||
<input type="text" id="temp" value="system" list="temp_datalist">
|
||||
</fieldset>
|
||||
|
||||
|
||||
<button id="saveBtn">Save</button>
|
||||
<script>
|
||||
(function(){
|
||||
saveBtn.onclick = ()=>{
|
||||
const dict = {
|
||||
portable_type: "relative",
|
||||
portable_data: {
|
||||
user: user.value,
|
||||
documents: documents.value,
|
||||
downloads: downloads.value,
|
||||
desktop: desktop.value,
|
||||
pictures: pictures.value,
|
||||
videos: videos.value,
|
||||
music: music.value,
|
||||
config: config.value,
|
||||
cache: cache.value,
|
||||
data: data.value,
|
||||
state: state.value,
|
||||
temp: temp.value
|
||||
}
|
||||
};
|
||||
|
||||
const json = JSON.stringify(dict,null,'\t');
|
||||
const a = document.createElement('a');
|
||||
a.href = `data:text/json;charset=utf-8,${encodeURIComponent(json)}`;
|
||||
a.download="portable.json";
|
||||
a.click();
|
||||
};
|
||||
})();
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
379
src/BitTorrent/TorrentFile.cpp
Normal file
379
src/BitTorrent/TorrentFile.cpp
Normal file
@@ -0,0 +1,379 @@
|
||||
#include "TessesFramework/BitTorrent/TorrentFile.hpp"
|
||||
#include "TessesFramework/Streams/MemoryStream.hpp"
|
||||
#include "TessesFramework/Crypto/Crypto.hpp"
|
||||
#include "TessesFramework/Http/HttpUtils.hpp"
|
||||
namespace Tesses::Framework::BitTorrent
|
||||
{
|
||||
|
||||
TorrentFileInfo::TorrentFileInfo()
|
||||
{
|
||||
this->fileListOrLength = 0;
|
||||
}
|
||||
TorrentFileInfo::TorrentFileInfo(const Serialization::Bencode::BeDictionary& tkn)
|
||||
{
|
||||
Load(tkn);
|
||||
}
|
||||
int64_t TorrentFileInfo::GetTorrentFileSize()
|
||||
{
|
||||
if(std::holds_alternative<int64_t>(this->fileListOrLength))
|
||||
return std::get<int64_t>(fileListOrLength);
|
||||
else if(std::holds_alternative<std::vector<TorrentFileEntry>>(this->fileListOrLength)) {
|
||||
auto& files= std::get<std::vector<TorrentFileEntry>>(this->fileListOrLength);
|
||||
int64_t no=0;
|
||||
for(auto itm : files)
|
||||
{
|
||||
no += itm.length;
|
||||
}
|
||||
return no;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Serialization::Bencode::BeDictionary& TorrentFileInfo::GenerateInfoDictionary()
|
||||
{
|
||||
this->info = {};
|
||||
this->info.tokens.emplace_back("name",this->name);
|
||||
this->info.tokens.emplace_back("piece length",this->piece_length);
|
||||
this->info.tokens.emplace_back("pieces",this->pieces);
|
||||
if(this->isPrivate)
|
||||
this->info.tokens.emplace_back("private",1);
|
||||
|
||||
if(std::holds_alternative<int64_t>(this->fileListOrLength))
|
||||
{
|
||||
this->info.tokens.emplace_back("length",std::get<int64_t>(this->fileListOrLength));
|
||||
}
|
||||
else if(std::holds_alternative<std::vector<TorrentFileEntry>>(this->fileListOrLength))
|
||||
{
|
||||
Serialization::Bencode::BeArray a;
|
||||
for(auto& item : std::get<std::vector<TorrentFileEntry>>(this->fileListOrLength))
|
||||
{
|
||||
Serialization::Bencode::BeDictionary dict;
|
||||
dict.tokens.emplace_back("length",item.length);
|
||||
Serialization::Bencode::BeArray path;
|
||||
for(auto& p : item.path.path)
|
||||
path.tokens.push_back(p);
|
||||
dict.tokens.emplace_back("path",path);
|
||||
|
||||
a.tokens.push_back(dict);
|
||||
}
|
||||
this->info.tokens.emplace_back("files", a);
|
||||
}
|
||||
return this->info;
|
||||
}
|
||||
Serialization::Bencode::BeDictionary& TorrentFileInfo::GetInfoDictionary()
|
||||
{
|
||||
return this->info;
|
||||
}
|
||||
|
||||
void TorrentFileInfo::Load(const Serialization::Bencode::BeDictionary& dict)
|
||||
{
|
||||
this->info = dict;
|
||||
auto o=dict.GetValue("name");
|
||||
if(std::holds_alternative<Serialization::Bencode::BeString>(o))
|
||||
this->name = std::get<Serialization::Bencode::BeString>(o);
|
||||
else
|
||||
this->name = {};
|
||||
|
||||
o=dict.GetValue("piece length");
|
||||
if(std::holds_alternative<int64_t>(o))
|
||||
this->piece_length = std::get<int64_t>(o);
|
||||
else
|
||||
this->piece_length = DEFAULT_PIECE_LENGTH;
|
||||
|
||||
o=dict.GetValue("private");
|
||||
if(std::holds_alternative<int64_t>(o))
|
||||
this->isPrivate = std::get<int64_t>(o);
|
||||
else
|
||||
this->isPrivate = 0;
|
||||
|
||||
o=dict.GetValue("pieces");
|
||||
if(std::holds_alternative<Serialization::Bencode::BeString>(o))
|
||||
this->pieces = std::get<Serialization::Bencode::BeString>(o);
|
||||
else
|
||||
this->pieces = {};
|
||||
|
||||
|
||||
|
||||
o=dict.GetValue("files");
|
||||
if(std::holds_alternative<Serialization::Bencode::BeArray>(o))
|
||||
{
|
||||
std::vector<TorrentFileEntry> ents;
|
||||
|
||||
for(auto& item : std::get<Serialization::Bencode::BeArray>(o).tokens)
|
||||
{
|
||||
if(std::holds_alternative<Serialization::Bencode::BeDictionary>(item))
|
||||
{
|
||||
TorrentFileEntry fe;
|
||||
fe.path.relative=true;
|
||||
|
||||
auto& d2=std::get<Serialization::Bencode::BeDictionary>(item);
|
||||
auto o2=d2.GetValue("length");
|
||||
if(std::holds_alternative<int64_t>(o2))
|
||||
{
|
||||
fe.length = std::get<int64_t>(o2);
|
||||
}
|
||||
o2=d2.GetValue("path");
|
||||
if(std::holds_alternative<Serialization::Bencode::BeArray>(o2))
|
||||
{
|
||||
auto& arr=std::get<Serialization::Bencode::BeArray>(o2);
|
||||
for(auto& itm : arr.tokens)
|
||||
{
|
||||
if(std::holds_alternative<Serialization::Bencode::BeString>(itm))
|
||||
{
|
||||
fe.path.path.push_back(std::get<Serialization::Bencode::BeString>(itm));
|
||||
}
|
||||
}
|
||||
}
|
||||
ents.push_back(fe);
|
||||
}
|
||||
}
|
||||
this->fileListOrLength = ents;
|
||||
}
|
||||
else {
|
||||
o = dict.GetValue("length");
|
||||
if(std::holds_alternative<int64_t>(o))
|
||||
{
|
||||
this->fileListOrLength = std::get<int64_t>(o);
|
||||
}
|
||||
else {
|
||||
this->fileListOrLength = (int64_t)0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Serialization::Bencode::BeString TorrentFileInfo::GetInfoHash()
|
||||
{
|
||||
auto strm = std::make_shared<Streams::MemoryStream>(true);
|
||||
Serialization::Bencode::Bencode::Save(strm,this->info);
|
||||
strm->Seek(0L,Streams::SeekOrigin::Begin);
|
||||
return Crypto::Sha1::ComputeHash(strm);
|
||||
}
|
||||
|
||||
|
||||
TorrentFile::TorrentFile()
|
||||
{
|
||||
this->created_by = "TessesFrameworkTorrent";
|
||||
this->creation_date = (int64_t)time(NULL);
|
||||
}
|
||||
TorrentFile::TorrentFile(const Serialization::Bencode::BeDictionary& tkn)
|
||||
{
|
||||
auto o=tkn.GetValue("info");
|
||||
if(std::holds_alternative<Serialization::Bencode::BeDictionary>(o))
|
||||
{
|
||||
this->info.Load(std::get<Serialization::Bencode::BeDictionary>(o));
|
||||
}
|
||||
o=tkn.GetValue("announce");
|
||||
if(std::holds_alternative<Serialization::Bencode::BeString>(o))
|
||||
{
|
||||
this->announce = std::get<Serialization::Bencode::BeString>(o);
|
||||
}
|
||||
o=tkn.GetValue("announce-list");
|
||||
if(std::holds_alternative<Serialization::Bencode::BeArray>(o))
|
||||
{
|
||||
auto& ls = std::get<Serialization::Bencode::BeArray>(o);
|
||||
for(auto& item : ls.tokens)
|
||||
{
|
||||
if(std::holds_alternative<Serialization::Bencode::BeArray>(item))
|
||||
{
|
||||
auto ls2 = std::get<Serialization::Bencode::BeArray>(item);
|
||||
auto item2=ls2.tokens.at(0);
|
||||
if(std::holds_alternative<Serialization::Bencode::BeString>(item2))
|
||||
{
|
||||
this->announce_list.push_back(std::get<Serialization::Bencode::BeString>(item2));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
o=tkn.GetValue("creation date");
|
||||
if(std::holds_alternative<int64_t>(o))
|
||||
{
|
||||
this->creation_date = std::get<int64_t>(o);
|
||||
}
|
||||
o=tkn.GetValue("comment");
|
||||
if(std::holds_alternative<Serialization::Bencode::BeString>(o))
|
||||
{
|
||||
this->comment = std::get<Serialization::Bencode::BeString>(o);
|
||||
}
|
||||
o=tkn.GetValue("created by");
|
||||
if(std::holds_alternative<Serialization::Bencode::BeString>(o))
|
||||
{
|
||||
this->created_by = std::get<Serialization::Bencode::BeString>(o);
|
||||
}
|
||||
o=tkn.GetValue("url-list");
|
||||
if(std::holds_alternative<Serialization::Bencode::BeArray>(o))
|
||||
{
|
||||
auto& li =std::get<Serialization::Bencode::BeArray>(o);
|
||||
for(auto& itm : li.tokens)
|
||||
{
|
||||
if(std::holds_alternative<Serialization::Bencode::BeString>(itm))
|
||||
this->url_list.push_back(std::get<Serialization::Bencode::BeString>(itm));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Serialization::Bencode::BeDictionary TorrentFile::ToDictionary()
|
||||
{
|
||||
Serialization::Bencode::BeDictionary dict;
|
||||
dict.SetValue("info",this->info.GetInfoDictionary());
|
||||
dict.SetValue("announce",this->announce);
|
||||
if(!this->announce_list.empty())
|
||||
{
|
||||
Serialization::Bencode::BeArray a;
|
||||
for(auto& item : this->announce_list)
|
||||
{
|
||||
Serialization::Bencode::BeArray a2;
|
||||
a2.tokens.push_back(item);
|
||||
a.tokens.push_back(a2);
|
||||
}
|
||||
dict.SetValue("announce-list",a);
|
||||
}
|
||||
if(!this->url_list.empty())
|
||||
{
|
||||
Serialization::Bencode::BeArray ls;
|
||||
for(auto& itm : this->url_list)
|
||||
{
|
||||
ls.tokens.push_back(itm);
|
||||
}
|
||||
dict.SetValue("url-list",ls);
|
||||
}
|
||||
|
||||
dict.SetValue("created by", this->created_by);
|
||||
dict.SetValue("creation date", this->creation_date);
|
||||
dict.SetValue("comment",this->comment);
|
||||
return dict;
|
||||
}
|
||||
|
||||
void TorrentFile::Print(std::shared_ptr<TextStreams::TextWriter> writer)
|
||||
{
|
||||
writer->WriteLine("Announce: " + (std::string)this->announce);
|
||||
writer->WriteLine("Announce List:");
|
||||
for(auto& item : this->announce_list)
|
||||
{
|
||||
writer->WriteLine("\t" + (std::string)item);
|
||||
}
|
||||
writer->WriteLine("Comment: " + (std::string)this->comment);
|
||||
writer->WriteLine("Created By: " + (std::string)this->created_by);
|
||||
Date::DateTime dt(this->creation_date);
|
||||
dt.SetToLocal();
|
||||
writer->WriteLine("Creation Date: " + dt.ToString());
|
||||
writer->WriteLine("Info Hash: " + Http::HttpUtils::BytesToHex(this->info.GetInfoHash().data));
|
||||
writer->WriteLine("Info:");
|
||||
writer->WriteLine("\tName: " + (std::string)this->info.name);
|
||||
writer->WriteLine(this->info.isPrivate ? "\tPrivate: true" : "\tPrivate: false");
|
||||
writer->WriteLine("\tPiece Length: " + TF_FileSize(this->info.piece_length));
|
||||
if(std::holds_alternative<int64_t>(this->info.fileListOrLength))
|
||||
{
|
||||
writer->WriteLine("\tIs Single File: true");
|
||||
writer->WriteLine("\tFile length: " + TF_FileSize((uint64_t)std::get<int64_t>(this->info.fileListOrLength)));
|
||||
}
|
||||
else if(std::holds_alternative<std::vector<TorrentFileEntry>>(this->info.fileListOrLength))
|
||||
{
|
||||
writer->WriteLine("\tIs Single File: false");
|
||||
writer->WriteLine("\tFiles:");
|
||||
auto& files = std::get<std::vector<TorrentFileEntry>>(this->info.fileListOrLength);
|
||||
for(auto& file : files)
|
||||
{
|
||||
writer->WriteLine("\t\tPath: " + file.path.ToString());
|
||||
writer->WriteLine("\t\tLength: " + TF_FileSize(file.length));
|
||||
writer->WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<ReadWriteAt> TorrentFileInfo::GetStreamFromFilesystem(std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, const Tesses::Framework::Filesystem::VFSPath& path)
|
||||
{
|
||||
if(std::holds_alternative<std::vector<TorrentFileEntry>>(this->fileListOrLength))
|
||||
{
|
||||
return std::make_shared<TorrentDirectoryStream>(vfs,path / this->name, std::get<std::vector<TorrentFileEntry>>(this->fileListOrLength));
|
||||
}
|
||||
else if(std::holds_alternative<int64_t>(this->fileListOrLength)) {
|
||||
return std::make_shared<TorrentFileStream>(vfs,path / this->name, (uint64_t)std::get<int64_t>(this->fileListOrLength));
|
||||
}
|
||||
return nullptr;
|
||||
|
||||
}
|
||||
|
||||
static void ParsePieces(TorrentFileInfo* fi,std::shared_ptr<ReadWriteAt> rwa, int64_t flength)
|
||||
{
|
||||
int64_t pieces = fi->pieces.data.size()/20;
|
||||
std::vector<uint8_t> buffer;
|
||||
buffer.resize((size_t)fi->piece_length);
|
||||
size_t lastPieceSize = (size_t)(flength % (int64_t)buffer.size());
|
||||
for(int64_t i = 0; i < pieces; i++)
|
||||
{
|
||||
size_t len = buffer.size();
|
||||
if(i == pieces-1 && lastPieceSize != 0) len = std::min(len,lastPieceSize);
|
||||
rwa->ReadBlockAt(i*buffer.size(), buffer.data(), len);
|
||||
auto hsh=Crypto::Sha1::ComputeHash(buffer.data(),len);
|
||||
std::copy(hsh.begin(),hsh.end(),fi->pieces.data.begin()+(i*20));
|
||||
}
|
||||
}
|
||||
void TorrentFileInfo::CreateFromFilesystem(std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, const Tesses::Framework::Filesystem::VFSPath& path, bool isPrivate, int64_t pieceLength)
|
||||
{
|
||||
this->isPrivate=isPrivate;
|
||||
this->piece_length = pieceLength;
|
||||
this->pieces.data.clear();
|
||||
this->name = path.GetFileName();
|
||||
int64_t len=0;
|
||||
if(vfs->FileExists(path))
|
||||
{
|
||||
auto strm = vfs->OpenFile(path,"rb");
|
||||
len = strm->GetLength();
|
||||
this->fileListOrLength= len;
|
||||
int64_t pieces = len / piece_length;
|
||||
if((len % piece_length) != 0) pieces++;
|
||||
|
||||
this->pieces.data.resize(pieces*20);
|
||||
}
|
||||
else if(vfs->DirectoryExists(path))
|
||||
{
|
||||
std::vector<TorrentFileEntry> ents;
|
||||
std::function<void(Tesses::Framework::Filesystem::VFSPath,Tesses::Framework::Filesystem::VFSPath)> crawl;
|
||||
crawl= [&](Tesses::Framework::Filesystem::VFSPath inFS, Tesses::Framework::Filesystem::VFSPath inTorrent)->void{
|
||||
for(auto ent : vfs->EnumeratePaths(inFS))
|
||||
{
|
||||
if(vfs->FileExists(ent))
|
||||
{
|
||||
auto strm = vfs->OpenFile(ent,"rb");
|
||||
auto flen = strm->GetLength();
|
||||
ents.emplace_back(inTorrent / ent.GetFileName(),flen);
|
||||
|
||||
len += flen;
|
||||
}
|
||||
else if(vfs->DirectoryExists(ent))
|
||||
{
|
||||
crawl(ent,inTorrent / ent.GetFileName());
|
||||
}
|
||||
}
|
||||
};
|
||||
Tesses::Framework::Filesystem::VFSPath p2;
|
||||
p2.relative=true;
|
||||
crawl(path, p2);
|
||||
|
||||
this->fileListOrLength = ents;
|
||||
int64_t pieces = len / piece_length;
|
||||
if((len % piece_length) != 0) pieces++;
|
||||
|
||||
this->pieces.data.resize(pieces*20);
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("File or directory does not exist");
|
||||
}
|
||||
ParsePieces(this,this->GetStreamFromFilesystem(vfs,path.GetParent()),len);
|
||||
|
||||
|
||||
this->GenerateInfoDictionary();
|
||||
}
|
||||
|
||||
void TorrentFile::CreateTorrent(std::shared_ptr<Tesses::Framework::Streams::Stream> torrent_file_stream,const std::vector<Serialization::Bencode::BeString>& trackers,const std::vector<Serialization::Bencode::BeString>& webseeds, std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, const Tesses::Framework::Filesystem::VFSPath& path, bool isPrivate, int64_t pieceLength, Serialization::Bencode::BeString comment, Serialization::Bencode::BeString created_by)
|
||||
{
|
||||
TorrentFile file;
|
||||
file.announce = trackers.at(0);
|
||||
file.announce_list = trackers;
|
||||
file.url_list = webseeds;
|
||||
file.comment = comment;
|
||||
file.created_by = created_by;
|
||||
file.info.CreateFromFilesystem(vfs,path,isPrivate,pieceLength);
|
||||
auto dict=file.ToDictionary();
|
||||
Serialization::Bencode::Bencode::Save(torrent_file_stream,dict);
|
||||
}
|
||||
}
|
||||
122
src/BitTorrent/TorrentStream.cpp
Normal file
122
src/BitTorrent/TorrentStream.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
#include "TessesFramework/BitTorrent/TorrentStream.hpp"
|
||||
|
||||
namespace Tesses::Framework::BitTorrent
|
||||
{
|
||||
TorrentFileEntry::TorrentFileEntry()
|
||||
{
|
||||
|
||||
}
|
||||
TorrentFileEntry::TorrentFileEntry(Tesses::Framework::Filesystem::VFSPath path, int64_t length): path(path), length(length)
|
||||
{}
|
||||
|
||||
ReadWriteAt::~ReadWriteAt(){}
|
||||
|
||||
TorrentFileStream::TorrentFileStream(std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, Tesses::Framework::Filesystem::VFSPath path,uint64_t length) : vfs(vfs), path(path), length(length)
|
||||
{
|
||||
}
|
||||
bool TorrentFileStream::ReadBlockAt(uint64_t offset, uint8_t* data, size_t len)
|
||||
{
|
||||
if(!vfs->FileExists(path)) return false;
|
||||
auto strm = vfs->OpenFile(path,"rb");
|
||||
strm->Seek((int64_t)offset,Streams::SeekOrigin::Begin);
|
||||
strm->ReadBlock(data,len);
|
||||
return true;
|
||||
}
|
||||
void TorrentFileStream::WriteBlockAt(uint64_t offset, const uint8_t* data, size_t len)
|
||||
{
|
||||
len = (size_t)std::min((int64_t)len, (int64_t)this->length-(int64_t)offset);
|
||||
if(len == 0) return;
|
||||
|
||||
mtx.Lock();
|
||||
auto strm = vfs->OpenFile(path,"wb");
|
||||
strm->Seek((int64_t)offset,Streams::SeekOrigin::Begin);
|
||||
strm->WriteBlock(data,len);
|
||||
mtx.Unlock();
|
||||
}
|
||||
|
||||
TorrentDirectoryStream::TorrentDirectoryStream(std::shared_ptr<Tesses::Framework::Filesystem::VFS> vfs, Tesses::Framework::Filesystem::VFSPath path,std::vector<TorrentFileEntry> entries) : vfs(vfs), path(path), entries(entries)
|
||||
{
|
||||
if(!vfs->DirectoryExists(path))
|
||||
this->vfs->CreateDirectory(path);
|
||||
this->mtxs.resize(entries.size());
|
||||
}
|
||||
|
||||
// From https://www.seanjoflynn.com/research/bittorrent.html , which is licensed under MIT based on code repo
|
||||
bool TorrentDirectoryStream::ReadBlockAt(uint64_t offset, uint8_t* data, size_t len)
|
||||
{
|
||||
uint64_t currentOffset = 0;
|
||||
uint64_t end = offset + len;
|
||||
for(size_t i = 0; i < this->entries.size(); i++)
|
||||
{
|
||||
if(offset < currentOffset && end < currentOffset) {
|
||||
currentOffset += this->entries[i].length;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(offset > currentOffset + this->entries[i].length && end > currentOffset + this->entries[i].length ){
|
||||
currentOffset += this->entries[i].length;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto path = this->path / this->entries[i].path;
|
||||
if(!vfs->FileExists(path)) return false;
|
||||
|
||||
int64_t fstart = std::max((int64_t)0,(int64_t)offset - (int64_t)currentOffset);
|
||||
int64_t fend = std::min((int64_t)end - (int64_t)currentOffset, (int64_t)this->entries[i].length);
|
||||
int flength = (int)(fend - fstart);
|
||||
int bstart = std::max((int)0,(int)((int64_t)currentOffset - (int64_t)offset));
|
||||
auto strm = vfs->OpenFile(path,"rb");
|
||||
strm->Seek(fstart, Streams::SeekOrigin::Begin);
|
||||
strm->ReadBlock(data+bstart,flength);
|
||||
|
||||
|
||||
|
||||
currentOffset += this->entries[i].length;
|
||||
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// From https://www.seanjoflynn.com/research/bittorrent.html , which is licensed under MIT based on code repo
|
||||
void TorrentDirectoryStream::WriteBlockAt(uint64_t offset, const uint8_t* data, size_t len)
|
||||
{
|
||||
uint64_t currentOffset = 0;
|
||||
uint64_t end = offset + len;
|
||||
for(size_t i = 0; i < this->entries.size(); i++)
|
||||
{
|
||||
if(offset < currentOffset && end < currentOffset) {
|
||||
currentOffset += this->entries[i].length;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(offset > currentOffset + this->entries[i].length && end > currentOffset + this->entries[i].length ){
|
||||
currentOffset += this->entries[i].length;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto path = this->path / this->entries[i].path;
|
||||
auto parent = path.GetParent();
|
||||
if(!vfs->DirectoryExists(parent))
|
||||
vfs->CreateDirectory(parent);
|
||||
|
||||
int64_t fstart = std::max((int64_t)0,(int64_t)offset - (int64_t)currentOffset);
|
||||
int64_t fend = std::min((int64_t)end - (int64_t)currentOffset, (int64_t)this->entries[i].length);
|
||||
int flength = (int)(fend - fstart);
|
||||
int bstart = std::max((int)0,(int)((int64_t)currentOffset - (int64_t)offset));
|
||||
|
||||
this->mtxs[i].Lock();
|
||||
{
|
||||
auto strm = vfs->OpenFile(path,"wb");
|
||||
strm->Seek(fstart, Streams::SeekOrigin::Begin);
|
||||
strm->WriteBlock(data+bstart,flength);
|
||||
}
|
||||
this->mtxs[i].Unlock();
|
||||
|
||||
|
||||
currentOffset += this->entries[i].length;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,13 @@
|
||||
#include <utime.h>
|
||||
#include <sys/statvfs.h>
|
||||
#endif
|
||||
|
||||
#include "TessesFramework/Threading/Thread.hpp"
|
||||
#if defined(__linux__)
|
||||
#include <poll.h>
|
||||
#include <sys/inotify.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
namespace Tesses::Framework::Filesystem
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
@@ -230,7 +237,203 @@ namespace Tesses::Framework::Filesystem
|
||||
#endif
|
||||
}
|
||||
|
||||
std::shared_ptr<LocalFilesystem> LocalFS;
|
||||
|
||||
void LocalFilesystem::Lock(VFSPath path)
|
||||
{
|
||||
auto p2 = VFSPathToSystem(path);
|
||||
const char* fopenPath = p2.c_str();
|
||||
while(true)
|
||||
{
|
||||
FILE* fp = fopen(fopenPath,"wx");
|
||||
if(fp) {
|
||||
fclose(fp);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
void LocalFilesystem::Unlock(VFSPath path)
|
||||
{
|
||||
std::error_code error;
|
||||
std::filesystem::remove(VFSPathToSystem(path),error);
|
||||
}
|
||||
#if defined(__linux__)
|
||||
|
||||
class INotifyWatcher : public FSWatcher {
|
||||
std::shared_ptr<Threading::Thread> thrd;
|
||||
static uint32_t to_linux_mask(FSWatcherEventType flags)
|
||||
{
|
||||
uint32_t lflags = 0;
|
||||
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Accessed) != 0) ? IN_ACCESS : 0;
|
||||
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::AttributeChanged) != 0) ? IN_ATTRIB : 0;
|
||||
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Writen) != 0) ? IN_CLOSE_WRITE : 0;
|
||||
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Read) != 0) ? IN_CLOSE_NOWRITE : 0;
|
||||
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Created) != 0) ? IN_CREATE : 0;
|
||||
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Deleted) != 0) ? IN_DELETE : 0;
|
||||
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::WatchEntryDeleted) != 0) ? IN_DELETE_SELF : 0;
|
||||
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Modified) != 0) ? IN_MODIFY : 0;
|
||||
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::WatchEntryMoved) != 0) ? IN_MOVE_SELF : 0;
|
||||
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::MoveOld) != 0) ? IN_MOVED_FROM : 0;
|
||||
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::MoveNew) != 0) ? IN_MOVED_TO : 0;
|
||||
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Opened) != 0) ? IN_OPEN : 0;
|
||||
|
||||
return lflags;
|
||||
}
|
||||
static FSWatcherEventType from_linux_mask(uint32_t lflags)
|
||||
{
|
||||
uint32_t flags = 0;
|
||||
flags |= ((lflags & IN_ACCESS) != 0) ? (uint32_t)FSWatcherEventType::Accessed : 0;
|
||||
flags |= ((lflags & IN_ATTRIB) != 0) ? (uint32_t)FSWatcherEventType::AttributeChanged : 0;
|
||||
flags |= ((lflags & IN_CLOSE_WRITE) != 0) ? (uint32_t)FSWatcherEventType::Writen : 0;
|
||||
flags |= ((lflags & IN_CLOSE_NOWRITE) != 0) ? (uint32_t)FSWatcherEventType::Read : 0;
|
||||
flags |= ((lflags & IN_CREATE) != 0) ? (uint32_t)FSWatcherEventType::Created : 0;
|
||||
flags |= ((lflags & IN_DELETE) != 0) ? (uint32_t)FSWatcherEventType::Deleted : 0;
|
||||
flags |= ((lflags & IN_DELETE_SELF) != 0) ? (uint32_t)FSWatcherEventType::WatchEntryDeleted : 0;
|
||||
flags |= ((lflags & IN_MODIFY) != 0) ? (uint32_t)FSWatcherEventType::Modified : 0;
|
||||
flags |= ((lflags & IN_MOVE_SELF) != 0) ? (uint32_t)FSWatcherEventType::WatchEntryMoved : 0;
|
||||
flags |= ((lflags & IN_MOVED_FROM) != 0) ? (uint32_t)FSWatcherEventType::MoveOld : 0;
|
||||
flags |= ((lflags & IN_MOVED_TO) != 0) ? (uint32_t)FSWatcherEventType::MoveNew : 0;
|
||||
flags |= ((lflags & IN_OPEN) != 0) ? (uint32_t)FSWatcherEventType::Opened : 0;
|
||||
|
||||
return (FSWatcherEventType)flags;
|
||||
}
|
||||
public:
|
||||
INotifyWatcher(std::shared_ptr<VFS> vfs, VFSPath path) : FSWatcher(vfs,path)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
void SetEnabledImpl(bool enabled)
|
||||
{
|
||||
if(enabled)
|
||||
{
|
||||
int fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
|
||||
if (fd == -1)
|
||||
{
|
||||
throw std::runtime_error("Cannot init inotify");
|
||||
}
|
||||
auto str = this->GetFilesystem()->VFSPathToSystem(this->GetPath());
|
||||
|
||||
int watch = inotify_add_watch(fd, str.c_str(),to_linux_mask(this->events));
|
||||
|
||||
thrd = std::make_shared<Threading::Thread>([this,watch,fd]()-> void {
|
||||
int cnt = 0;
|
||||
struct pollfd pfd = {.fd = fd, .events = POLLIN};
|
||||
std::vector<std::pair<VFSPath,uint32_t>> mvFroms;
|
||||
char buf[4096]
|
||||
__attribute__ ((aligned(__alignof__(struct inotify_event))));
|
||||
const struct inotify_event *event;
|
||||
ssize_t size;
|
||||
|
||||
bool fail=false;
|
||||
|
||||
FSWatcherEvent evt;
|
||||
evt.dest = this->GetPath();
|
||||
while(!fail && this->enabled)
|
||||
{
|
||||
cnt = poll(&pfd,1,-1);
|
||||
if(cnt == -1) break;
|
||||
|
||||
if(cnt > 0)
|
||||
{
|
||||
if(pfd.revents & POLLIN)
|
||||
{
|
||||
for (;;) {
|
||||
size = read(fd, buf, sizeof(buf));
|
||||
if (size == -1 && errno != EAGAIN) {
|
||||
fail=true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (size <= 0)
|
||||
break;
|
||||
|
||||
for (char *ptr = buf; ptr < buf + size;
|
||||
ptr += sizeof(struct inotify_event) + event->len) {
|
||||
|
||||
event = (const struct inotify_event *) ptr;
|
||||
VFSPath path = this->GetPath();
|
||||
|
||||
if(event->len)
|
||||
path = path / std::string(event->name, (size_t)event->len);
|
||||
|
||||
if(((uint32_t)this->events & (uint32_t)FSWatcherEventType::Moved) == (uint32_t)FSWatcherEventType::Moved && event->mask & IN_MOVED_FROM)
|
||||
{
|
||||
mvFroms.emplace_back(path,event->cookie);
|
||||
}
|
||||
else if(((uint32_t)this->events & (uint32_t)FSWatcherEventType::Moved) == (uint32_t)FSWatcherEventType::Moved && event->mask & IN_MOVED_TO)
|
||||
{
|
||||
for(auto ittr = mvFroms.begin(); ittr != mvFroms.end(); ittr++)
|
||||
{
|
||||
if(ittr->second == event->cookie)
|
||||
{
|
||||
evt.src = ittr->first;
|
||||
mvFroms.erase(ittr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
evt.isDir = (event->mask & IN_ISDIR);
|
||||
evt.dest = path;
|
||||
evt.type = FSWatcherEventType::Moved;
|
||||
if(this->event)
|
||||
this->event(evt);
|
||||
}
|
||||
else {
|
||||
|
||||
evt.isDir = (event->mask & IN_ISDIR);
|
||||
evt.src = path;
|
||||
evt.type = from_linux_mask(event->mask);;
|
||||
if(this->event)
|
||||
this->event(evt);
|
||||
}
|
||||
if(event->mask & IN_MOVE_SELF)
|
||||
{
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
if(event->mask & IN_DELETE_SELF)
|
||||
{
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
close(fd);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
thrd = nullptr;
|
||||
}
|
||||
}
|
||||
public:
|
||||
~INotifyWatcher()
|
||||
{
|
||||
this->enabled = false;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
std::shared_ptr<FSWatcher> LocalFilesystem::CreateWatcher(std::shared_ptr<VFS> vfs, VFSPath path)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
return std::make_shared<INotifyWatcher>(vfs, path);
|
||||
#endif
|
||||
return VFS::CreateWatcher(vfs,path);
|
||||
}
|
||||
|
||||
std::shared_ptr<LocalFilesystem> LocalFS = std::make_shared<LocalFilesystem>();
|
||||
|
||||
|
||||
}
|
||||
|
||||
// C:/Users/Jim/Joel
|
||||
|
||||
@@ -311,6 +311,34 @@ namespace Tesses::Framework::Filesystem
|
||||
if(vfs != nullptr)
|
||||
vfs->DeleteFile(destPath);
|
||||
|
||||
}
|
||||
void MountableFilesystem::Lock(VFSPath path)
|
||||
{
|
||||
path = path.CollapseRelativeParents();
|
||||
|
||||
VFSPath destRoot;
|
||||
VFSPath destPath = path;
|
||||
std::shared_ptr<VFS> vfs = root;
|
||||
|
||||
GetFS(path, destRoot, destPath, vfs);
|
||||
|
||||
if(vfs != nullptr)
|
||||
vfs->Lock(destPath);
|
||||
|
||||
}
|
||||
void MountableFilesystem::Unlock(VFSPath path)
|
||||
{
|
||||
path = path.CollapseRelativeParents();
|
||||
|
||||
VFSPath destRoot;
|
||||
VFSPath destPath = path;
|
||||
std::shared_ptr<VFS> vfs = root;
|
||||
|
||||
GetFS(path, destRoot, destPath, vfs);
|
||||
|
||||
if(vfs != nullptr)
|
||||
vfs->Unlock(destPath);
|
||||
|
||||
}
|
||||
void MountableFilesystem::GetDate(VFSPath path, Date::DateTime& lastWrite, Date::DateTime& lastAccess)
|
||||
{
|
||||
|
||||
@@ -87,6 +87,15 @@ namespace Tesses::Framework::Filesystem
|
||||
{
|
||||
this->parent->DeleteFile(ToParent(path));
|
||||
}
|
||||
void SubdirFilesystem::Lock(VFSPath path)
|
||||
{
|
||||
this->parent->Lock(ToParent(path));
|
||||
}
|
||||
|
||||
void SubdirFilesystem::Unlock(VFSPath path)
|
||||
{
|
||||
this->parent->Unlock(ToParent(path));
|
||||
}
|
||||
void SubdirFilesystem::CreateSymlink(VFSPath existingFile, VFSPath symlinkFile)
|
||||
{
|
||||
this->parent->CreateSymlink(ToParent(existingFile),ToParent(symlinkFile));
|
||||
|
||||
@@ -109,6 +109,16 @@ namespace Tesses::Framework::Filesystem {
|
||||
if(this->vfs == nullptr) return;
|
||||
this->vfs->DeleteFile(path);
|
||||
}
|
||||
void TempFS::Lock(VFSPath path)
|
||||
{
|
||||
if(this->vfs == nullptr) return;
|
||||
this->vfs->Lock(path);
|
||||
}
|
||||
void TempFS::Unlock(VFSPath path)
|
||||
{
|
||||
if(this->vfs == nullptr) return;
|
||||
this->vfs->Unlock(path);
|
||||
}
|
||||
void TempFS::CreateSymlink(VFSPath existingFile, VFSPath symlinkFile)
|
||||
{
|
||||
if(this->vfs == nullptr) return;
|
||||
@@ -200,6 +210,13 @@ namespace Tesses::Framework::Filesystem {
|
||||
}
|
||||
TempFS::~TempFS()
|
||||
{
|
||||
Close();
|
||||
VFSPath p;
|
||||
p.relative = false;
|
||||
p.path.push_back(this->tmp_str);
|
||||
this->vfs = nullptr;
|
||||
if(this->deleteOnDestroy && this->parent->DirectoryExists(p))
|
||||
this->parent->DeleteDirectoryRecurse(p);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -232,7 +232,8 @@ namespace Tesses::Framework::Filesystem
|
||||
|
||||
if(i == this->path.size()-1 && i == toMakeRelativeTo.path.size()-1)
|
||||
{
|
||||
VFSPath path({this->path[this->path.size()-1]});
|
||||
std::vector<std::string> paths{this->path[this->path.size()-1]};
|
||||
VFSPath path(paths);
|
||||
path.relative = true;
|
||||
return path;
|
||||
}
|
||||
@@ -330,6 +331,35 @@ namespace Tesses::Framework::Filesystem
|
||||
this->path = p;
|
||||
}
|
||||
|
||||
VFSPath VFSPath::ParseUriPath(std::string path)
|
||||
{
|
||||
std::string builder = {};
|
||||
VFSPath vpath;
|
||||
vpath.relative=true;
|
||||
|
||||
if(!path.empty() && path[0] == '/') vpath.relative=false;
|
||||
|
||||
for(auto item : path)
|
||||
{
|
||||
if(item == '/')
|
||||
{
|
||||
if(!builder.empty())
|
||||
{
|
||||
vpath.path.push_back(builder);
|
||||
builder.clear();
|
||||
}
|
||||
}
|
||||
else {
|
||||
builder.push_back(item);
|
||||
}
|
||||
}
|
||||
if(!builder.empty())
|
||||
{
|
||||
vpath.path.push_back(builder);
|
||||
}
|
||||
return vpath;
|
||||
}
|
||||
|
||||
bool VFSPath::HasExtension() const
|
||||
{
|
||||
if(this->path.empty()) return false;
|
||||
@@ -358,7 +388,8 @@ namespace Tesses::Framework::Filesystem
|
||||
if(ext.empty()) return;
|
||||
if(ext[0] != '.')
|
||||
{
|
||||
str += '.' + ext;
|
||||
str += '.';
|
||||
str += ext;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -377,6 +408,9 @@ namespace Tesses::Framework::Filesystem
|
||||
if(!str.empty())
|
||||
{
|
||||
if(str.front() == '/') this->relative=false;
|
||||
#if defined(_WIN32)
|
||||
if(str.front() == '\\') this->relative=false;
|
||||
#endif
|
||||
if(!this->path.empty())
|
||||
{
|
||||
auto firstPartPath = this->path.front();
|
||||
@@ -520,4 +554,121 @@ namespace Tesses::Framework::Filesystem
|
||||
void VFS::Close() {
|
||||
|
||||
}
|
||||
}
|
||||
void VFS::Lock(VFSPath path)
|
||||
{
|
||||
|
||||
}
|
||||
void VFS::Unlock(VFSPath path)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::shared_ptr<FSWatcher> VFS::CreateWatcher(std::shared_ptr<VFS> vfs,VFSPath path)
|
||||
{
|
||||
return std::make_shared<FSWatcher>(vfs,path);
|
||||
}
|
||||
void FSWatcher::SetEnabled(bool enabled)
|
||||
{
|
||||
if(this->enabled == enabled) return;
|
||||
this->enabled = enabled;
|
||||
this->SetEnabledImpl(enabled);
|
||||
}
|
||||
|
||||
bool FSWatcher::GetEnabled()
|
||||
{
|
||||
return this->enabled;
|
||||
}
|
||||
|
||||
void FSWatcher::SetEnabledImpl(bool enabled)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::shared_ptr<VFS> FSWatcher::GetFilesystem()
|
||||
{
|
||||
return this->vfs;
|
||||
}
|
||||
const VFSPath& FSWatcher::GetPath()
|
||||
{
|
||||
return this->path;
|
||||
}
|
||||
|
||||
FSWatcher::FSWatcher(std::shared_ptr<VFS> vfs, VFSPath path): vfs(vfs), path(path)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::shared_ptr<FSWatcher> FSWatcher::Create(std::shared_ptr<VFS> vfs, VFSPath path)
|
||||
{
|
||||
return vfs->CreateWatcher(vfs,path);
|
||||
}
|
||||
|
||||
bool FSWatcherEvent::IsEvent(FSWatcherEventType e)
|
||||
{
|
||||
if(e == FSWatcherEventType::All) return this->type != FSWatcherEventType::None;
|
||||
if(e == FSWatcherEventType::Moved) return ((uint32_t)this->type & (uint32_t)FSWatcherEventType::Moved) == (uint32_t)FSWatcherEventType::Moved;
|
||||
if(e == FSWatcherEventType::Closed) return ((uint32_t)this->type & (uint32_t)FSWatcherEventType::Closed) != 0;
|
||||
return (uint32_t)this->type & (uint32_t)e;
|
||||
}
|
||||
|
||||
std::string FSWatcherEvent::ToString()
|
||||
{
|
||||
if(IsEvent(FSWatcherEventType::Moved))
|
||||
{
|
||||
return (this->isDir ? "Moved directory " : "Moved file ") + this->src.ToString() + " -> " + this->dest.ToString();
|
||||
}
|
||||
else if(IsEvent(FSWatcherEventType::MoveOld))
|
||||
{
|
||||
return (this->isDir ? "Move source directory " : "Move source file ") + this->src.ToString();
|
||||
}
|
||||
else if(IsEvent(FSWatcherEventType::MoveNew))
|
||||
{
|
||||
return (this->isDir ? "Move destination directory " : "Move destination file ") + this->src.ToString();
|
||||
}
|
||||
else if(IsEvent(FSWatcherEventType::Accessed))
|
||||
{
|
||||
return (this->isDir ? "Accessed directory " : "Accessed file ") + this->src.ToString();
|
||||
}
|
||||
else if(IsEvent(FSWatcherEventType::AttributeChanged))
|
||||
{
|
||||
return (this->isDir ? "Changed attr on directory " : "Changed attr on file ") + this->src.ToString();
|
||||
}
|
||||
else if(IsEvent(FSWatcherEventType::Writen))
|
||||
{
|
||||
return (this->isDir ? "Finished changing directory " : "Finished writing to file ") + this->src.ToString();
|
||||
}
|
||||
else if(IsEvent(FSWatcherEventType::Read))
|
||||
{
|
||||
return (this->isDir ? "Finished reading directory " : "Finished reading from file ") + this->src.ToString();
|
||||
}
|
||||
else if(IsEvent(FSWatcherEventType::Created))
|
||||
{
|
||||
return (this->isDir ? "Created directory " : "Created file ") + this->src.ToString();
|
||||
}
|
||||
else if(IsEvent(FSWatcherEventType::Deleted))
|
||||
{
|
||||
return (this->isDir ? "Deleted directory " : "Deleted file ") + this->src.ToString();
|
||||
}
|
||||
else if(IsEvent(FSWatcherEventType::WatchEntryDeleted))
|
||||
{
|
||||
|
||||
return (this->isDir ? "Deleted watched directory " : "Deleted watched file ") + this->src.ToString();
|
||||
}
|
||||
else if(IsEvent(FSWatcherEventType::Modified))
|
||||
{
|
||||
return (this->isDir ? "Modified directory " : "Modified file ") + this->src.ToString();
|
||||
}
|
||||
else if(IsEvent(FSWatcherEventType::WatchEntryMoved))
|
||||
{
|
||||
return (this->isDir ? "Moved watched directory " : "Moved watched file ") + this->src.ToString();
|
||||
}
|
||||
else if(IsEvent(FSWatcherEventType::Opened))
|
||||
{
|
||||
return (this->isDir ? "Opened directory " : "Opened file ") + this->src.ToString();
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -104,7 +104,7 @@ namespace Tesses::Framework::Http {
|
||||
auto stout = p.GetStdoutStream();
|
||||
Tesses::Framework::TextStreams::StreamReader reader(stout);
|
||||
std::string line;
|
||||
while(reader.ReadLine(line))
|
||||
while(reader.ReadLineHttp(line))
|
||||
{
|
||||
auto v = HttpUtils::SplitString(line,": ", 2);
|
||||
if(v.size() == 2)
|
||||
|
||||
@@ -159,7 +159,7 @@ namespace Tesses::Framework::Http
|
||||
this->handleStrm=nullptr;
|
||||
StreamReader reader(strm);
|
||||
std::string statusLine;
|
||||
if(!reader.ReadLine(statusLine)) return;
|
||||
if(!reader.ReadLineHttp(statusLine)) return;
|
||||
auto statusLinesPart = HttpUtils::SplitString(statusLine," ",3);
|
||||
if(statusLinesPart.size() >= 2)
|
||||
{
|
||||
@@ -167,7 +167,7 @@ namespace Tesses::Framework::Http
|
||||
this->statusCode = (StatusCode)std::stoi(statusLinesPart[1]);
|
||||
}
|
||||
std::string line;
|
||||
while(reader.ReadLine(line))
|
||||
while(reader.ReadLineHttp(line))
|
||||
{
|
||||
if(line.empty()) break;
|
||||
auto v = HttpUtils::SplitString(line,": ", 2);
|
||||
@@ -225,7 +225,7 @@ namespace Tesses::Framework::Http
|
||||
|
||||
StreamReader reader(strm);
|
||||
std::string statusLine;
|
||||
if(!reader.ReadLine(statusLine)) break;
|
||||
if(!reader.ReadLineHttp(statusLine)) break;
|
||||
auto statusLinesPart = HttpUtils::SplitString(statusLine," ",3);
|
||||
if(statusLinesPart.size() >= 2)
|
||||
{
|
||||
@@ -233,7 +233,7 @@ namespace Tesses::Framework::Http
|
||||
this->statusCode = (StatusCode)std::stoi(statusLinesPart[1]);
|
||||
}
|
||||
std::string line;
|
||||
while(reader.ReadLine(line))
|
||||
while(reader.ReadLineHttp(line))
|
||||
{
|
||||
if(line.empty()) break;
|
||||
auto v = HttpUtils::SplitString(line,": ", 2);
|
||||
|
||||
@@ -389,11 +389,8 @@ namespace Tesses::Framework::Http
|
||||
static bool parseUntillBoundaryEnd(std::shared_ptr<Tesses::Framework::Streams::Stream> src, std::shared_ptr<Tesses::Framework::Streams::Stream> dest, std::string boundary)
|
||||
{
|
||||
bool hasMore=true;
|
||||
#if defined(_WIN32)
|
||||
uint8_t* checkBuffer = new uint8_t[boundary.size()];
|
||||
#else
|
||||
uint8_t checkBuffer[boundary.size()];
|
||||
#endif
|
||||
|
||||
int b;
|
||||
size_t i = 0;
|
||||
size_t i2 = 0;
|
||||
@@ -457,9 +454,8 @@ namespace Tesses::Framework::Http
|
||||
{
|
||||
dest->Write(buffer,offsetInMem);
|
||||
}
|
||||
#if defined(_WIN32)
|
||||
delete checkBuffer;
|
||||
#endif
|
||||
delete[] checkBuffer;
|
||||
|
||||
return hasMore;
|
||||
}
|
||||
|
||||
@@ -468,7 +464,7 @@ namespace Tesses::Framework::Http
|
||||
HttpDictionary req;
|
||||
StreamReader reader(ctx->GetStream());
|
||||
std::string line;
|
||||
while(reader.ReadLine(line))
|
||||
while(reader.ReadLineHttp(line))
|
||||
{
|
||||
auto v = HttpUtils::SplitString(line,": ", 2);
|
||||
if(v.size() == 2)
|
||||
@@ -566,7 +562,12 @@ namespace Tesses::Framework::Http
|
||||
this->responseHeaders.SetValue("Transfer-Encoding","chunked");
|
||||
|
||||
this->WriteHeaders();
|
||||
return std::make_shared<HttpStream>(this->strm,length,false,version == "HTTP/1.1");
|
||||
auto strm = std::make_shared<HttpStream>(this->strm,length,false,version == "HTTP/1.1");
|
||||
if(method == "HEAD")
|
||||
{
|
||||
strm->Close();
|
||||
}
|
||||
return strm;
|
||||
}
|
||||
std::shared_ptr<Stream> ServerContext::OpenRequestStream()
|
||||
{
|
||||
@@ -788,23 +789,26 @@ namespace Tesses::Framework::Http
|
||||
this->WithSingleHeader("Content-Range","bytes " + std::to_string(begin) + "-" + std::to_string(end) + "/" + std::to_string(len));
|
||||
this->statusCode = PartialContent;
|
||||
this->WriteHeaders();
|
||||
strm->Seek(begin,SeekOrigin::Begin);
|
||||
if(this->method != "HEAD")
|
||||
{
|
||||
strm->Seek(begin,SeekOrigin::Begin);
|
||||
|
||||
uint8_t buffer[1024];
|
||||
uint8_t buffer[1024];
|
||||
|
||||
size_t read=0;
|
||||
do {
|
||||
read = sizeof(buffer);
|
||||
myLen = (end - begin)+1;
|
||||
if(myLen < read) read = (size_t)myLen;
|
||||
if(read == 0) break;
|
||||
read = strm->Read(buffer,read);
|
||||
if(read == 0) break;
|
||||
this->strm->WriteBlock(buffer,read);
|
||||
|
||||
begin += read;
|
||||
} while(read > 0 && !this->strm->EndOfStream());
|
||||
size_t read=0;
|
||||
|
||||
do {
|
||||
read = sizeof(buffer);
|
||||
myLen = (end - begin)+1;
|
||||
if(myLen < read) read = (size_t)myLen;
|
||||
if(read == 0) break;
|
||||
read = strm->Read(buffer,read);
|
||||
if(read == 0) break;
|
||||
this->strm->WriteBlock(buffer,read);
|
||||
|
||||
begin += read;
|
||||
} while(read > 0 && !this->strm->EndOfStream());
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
@@ -821,6 +825,7 @@ namespace Tesses::Framework::Http
|
||||
this->WithSingleHeader("Accept-Range","bytes");
|
||||
this->WithSingleHeader("Content-Length",std::to_string(len));
|
||||
this->WriteHeaders();
|
||||
if(this->method != "HEAD")
|
||||
strm->CopyTo(this->strm);
|
||||
}
|
||||
}
|
||||
@@ -828,8 +833,10 @@ namespace Tesses::Framework::Http
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
auto chunkedStream = this->OpenResponseStream();
|
||||
|
||||
if(method != "HEAD")
|
||||
strm->CopyTo(chunkedStream);
|
||||
|
||||
|
||||
@@ -944,7 +951,7 @@ namespace Tesses::Framework::Http
|
||||
try{
|
||||
bool firstLine = true;
|
||||
std::string line;
|
||||
while(reader.ReadLine(line))
|
||||
while(reader.ReadLineHttp(line))
|
||||
{
|
||||
if(firstLine)
|
||||
{
|
||||
|
||||
@@ -27,6 +27,7 @@ namespace Tesses::Framework::Http
|
||||
}
|
||||
bool HttpStream::CanWrite()
|
||||
{
|
||||
if(this->done) return false;
|
||||
if(this->recv) return false;
|
||||
return this->strm->CanWrite();
|
||||
}
|
||||
@@ -118,6 +119,7 @@ namespace Tesses::Framework::Http
|
||||
}
|
||||
size_t HttpStream::Write(const uint8_t* buff, size_t len)
|
||||
{
|
||||
if(this->done) return 0;
|
||||
if(this->recv) return 0;
|
||||
if(this->length == 0) return 0;
|
||||
if(this->length > 0)
|
||||
@@ -153,9 +155,20 @@ namespace Tesses::Framework::Http
|
||||
}
|
||||
}
|
||||
}
|
||||
void HttpStream::Close()
|
||||
{
|
||||
if(this->length == -1 && this->http1_1 && !done && !this->recv)
|
||||
{
|
||||
this->done=true;
|
||||
StreamWriter writer(this->strm);
|
||||
writer.newline = "\r\n";
|
||||
writer.WriteLine("0");
|
||||
writer.WriteLine();
|
||||
}
|
||||
}
|
||||
HttpStream::~HttpStream()
|
||||
{
|
||||
if(this->length == -1 && this->http1_1)
|
||||
if(this->length == -1 && this->http1_1 && !done && !this->recv)
|
||||
{
|
||||
StreamWriter writer(this->strm);
|
||||
writer.newline = "\r\n";
|
||||
|
||||
@@ -198,6 +198,19 @@ namespace Tesses::Framework::Http {
|
||||
strm << std::setfill(c) << std::setw(count) << text;
|
||||
return strm.str();
|
||||
}
|
||||
char HttpUtils::NibbleToHex(uint8_t b, bool isUppercase)
|
||||
{
|
||||
if(isUppercase)
|
||||
{
|
||||
b %= 16;
|
||||
if(b >= 0 && b <= 9)
|
||||
return b + '0';
|
||||
if(b >= 10 && b <= 15)
|
||||
return b + ('A' - 10);
|
||||
return 0;
|
||||
}
|
||||
return NibbleToHex(b);
|
||||
}
|
||||
char HttpUtils::NibbleToHex(uint8_t b)
|
||||
{
|
||||
b %= 16;
|
||||
@@ -965,5 +978,63 @@ namespace Tesses::Framework::Http {
|
||||
if(!TryGetFirst(key,val)) return false;
|
||||
return val == "true" || val == "on";
|
||||
}
|
||||
std::string HttpUtils::BytesToHex(const std::vector<uint8_t>& data)
|
||||
{
|
||||
std::string text;
|
||||
BytesToHex(text,data);
|
||||
return text;
|
||||
}
|
||||
std::string HttpUtils::BytesToHex(const std::vector<uint8_t>& data, bool isUpper)
|
||||
{
|
||||
std::string text;
|
||||
BytesToHex(text,data, isUpper);
|
||||
return text;
|
||||
}
|
||||
void HttpUtils::BytesToHex(std::string& text,const std::vector<uint8_t>& data)
|
||||
{
|
||||
if(data.empty()) {
|
||||
text.clear();
|
||||
return;
|
||||
}
|
||||
text.resize(data.size()*2);
|
||||
for(size_t i = 0; i < data.size(); i++)
|
||||
{
|
||||
text[i*2] = NibbleToHex(data[i] >> 4);
|
||||
text[i*2+1] += NibbleToHex(data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void HttpUtils::BytesToHex(std::string& text,const std::vector<uint8_t>& data, bool isUpper)
|
||||
{
|
||||
if(data.empty()) {
|
||||
text.clear();
|
||||
return;
|
||||
}
|
||||
text.resize(data.size()*2);
|
||||
for(size_t i = 0; i < data.size(); i++)
|
||||
{
|
||||
text[i*2] = NibbleToHex(data[i] >> 4, isUpper);
|
||||
text[i*2+1] += NibbleToHex(data[i], isUpper);
|
||||
}
|
||||
}
|
||||
std::vector<uint8_t> HttpUtils::HexToBytes(const std::string& text)
|
||||
{
|
||||
std::vector<uint8_t> data;
|
||||
HexToBytes(data,text);
|
||||
return data;
|
||||
}
|
||||
void HttpUtils::HexToBytes(std::vector<uint8_t>& data,const std::string& text)
|
||||
{
|
||||
if(text.empty()) { data.clear(); return;}
|
||||
if(text.size() % 2 != 0) throw std::runtime_error("Text length is not even");
|
||||
|
||||
|
||||
data.resize(text.size()/2);
|
||||
for(size_t i = 0; i < text.size(); i+=2)
|
||||
{
|
||||
uint8_t b = HexToNibble(text[i]) << 4;
|
||||
b |= HexToNibble(text[i+1]);
|
||||
data[i/2] = b;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
97
src/Http/RouteServer.cpp
Normal file
97
src/Http/RouteServer.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#include "TessesFramework/Http/RouteServer.hpp"
|
||||
|
||||
namespace Tesses::Framework::Http
|
||||
{
|
||||
|
||||
RouteServer::RouteServerRoute::RouteServerRoute(std::string route, std::string method, ServerRequestHandler handler) : method(method), handler(handler)
|
||||
{
|
||||
auto path = Tesses::Framework::Filesystem::VFSPath::ParseUriPath(route);
|
||||
for(auto item : path.path)
|
||||
{
|
||||
if(item.size() > 2 && item[0] == '{' && item[item.size()-1] == '}')
|
||||
{
|
||||
this->parts.emplace_back( item.substr(1,item.size()-2),true);
|
||||
}
|
||||
else {
|
||||
this->parts.emplace_back(item,false);
|
||||
}
|
||||
}
|
||||
}
|
||||
bool RouteServer::RouteServerRoute::Equals(Tesses::Framework::Filesystem::VFSPath& path, HttpDictionary& args)
|
||||
{
|
||||
if(path.path.size() != this->parts.size()) return false;
|
||||
|
||||
|
||||
for(size_t i = 0; i < this->parts.size(); i++)
|
||||
{
|
||||
auto& part = this->parts[i];
|
||||
if(part.second)
|
||||
args.SetValue(part.first, Tesses::Framework::Http::HttpUtils::UrlPathDecode(path.path[i]));
|
||||
else if(part.first != path.path[i]) return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
RouteServer::RouteServer(std::shared_ptr<IHttpServer> root) : root(root)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void RouteServer::Add(std::string method, std::string pattern, ServerRequestHandler handler)
|
||||
{
|
||||
this->routes.emplace_back(pattern,method,handler);
|
||||
}
|
||||
|
||||
bool RouteServer::Handle(ServerContext& ctx)
|
||||
{
|
||||
auto pathArgs = ctx.pathArguments;
|
||||
auto path = Tesses::Framework::Filesystem::VFSPath::ParseUriPath(ctx.path);
|
||||
for(auto& svr : this->routes)
|
||||
{
|
||||
if(svr.method != ctx.method && !((svr.method == "GET" && ctx.method == "HEAD") || (svr.method == "HEAD" && ctx.method == "GET"))) continue;
|
||||
ctx.pathArguments = pathArgs;
|
||||
if(svr.Equals(path, ctx.pathArguments) && svr.handler && svr.handler(ctx))
|
||||
return true;
|
||||
|
||||
}
|
||||
ctx.pathArguments = pathArgs;
|
||||
|
||||
if(this->root)
|
||||
return this->root->Handle(ctx);
|
||||
return false;
|
||||
}
|
||||
|
||||
void RouteServer::Get(std::string pattern, ServerRequestHandler handler)
|
||||
{
|
||||
Add("GET",pattern,handler);
|
||||
}
|
||||
void RouteServer::Post(std::string pattern, ServerRequestHandler handler)
|
||||
{
|
||||
Add("POST",pattern,handler);
|
||||
}
|
||||
void RouteServer::Put(std::string pattern, ServerRequestHandler handler)
|
||||
{
|
||||
Add("PUT",pattern,handler);
|
||||
}
|
||||
void RouteServer::Patch(std::string pattern, ServerRequestHandler handler)
|
||||
{
|
||||
Add("PATCH",pattern,handler);
|
||||
}
|
||||
|
||||
void RouteServer::Delete(std::string pattern, ServerRequestHandler handler)
|
||||
{
|
||||
Add("DELETE",pattern,handler);
|
||||
}
|
||||
|
||||
void RouteServer::Trace(std::string pattern, ServerRequestHandler handler)
|
||||
{
|
||||
Add("TRACE",pattern,handler);
|
||||
}
|
||||
void RouteServer::Options(std::string pattern, ServerRequestHandler handler)
|
||||
{
|
||||
Add("OPTIONS",pattern,handler);
|
||||
}
|
||||
}
|
||||
@@ -22,15 +22,23 @@ namespace Tesses::Framework::Platform::Environment
|
||||
#else
|
||||
const char EnvPathSeperator=':';
|
||||
#endif
|
||||
PortableAppConfig portable_config;
|
||||
|
||||
namespace SpecialFolders
|
||||
{
|
||||
VFSPath GetTemp()
|
||||
{
|
||||
|
||||
if(portable_config.temp)
|
||||
return *portable_config.temp;
|
||||
return std::filesystem::temp_directory_path().string();
|
||||
}
|
||||
VFSPath GetHomeFolder()
|
||||
{
|
||||
|
||||
if(portable_config.user)
|
||||
return *portable_config.user;
|
||||
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS) && !defined(SAGO_DISABLE)
|
||||
return sago::getHomeDir();
|
||||
#elif defined(__EMSCRIPTEN__)
|
||||
@@ -43,6 +51,9 @@ namespace Tesses::Framework::Platform::Environment
|
||||
}
|
||||
VFSPath GetDownloads()
|
||||
{
|
||||
if(portable_config.downloads)
|
||||
return *portable_config.downloads;
|
||||
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS) && !defined(SAGO_DISABLE)
|
||||
return sago::getDownloadFolder();
|
||||
#elif defined(__ANDROID__)
|
||||
@@ -53,6 +64,9 @@ namespace Tesses::Framework::Platform::Environment
|
||||
}
|
||||
VFSPath GetMusic()
|
||||
{
|
||||
|
||||
if(portable_config.music)
|
||||
return *portable_config.music;
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS) && !defined(SAGO_DISABLE)
|
||||
return sago::getMusicFolder();
|
||||
#elif defined(__ANDROID__)
|
||||
@@ -63,6 +77,9 @@ namespace Tesses::Framework::Platform::Environment
|
||||
}
|
||||
VFSPath GetPictures()
|
||||
{
|
||||
|
||||
if(portable_config.pictures)
|
||||
return *portable_config.pictures;
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS) && !defined(SAGO_DISABLE)
|
||||
return sago::getPicturesFolder();
|
||||
#elif defined(__ANDROID__)
|
||||
@@ -73,6 +90,8 @@ namespace Tesses::Framework::Platform::Environment
|
||||
}
|
||||
VFSPath GetVideos()
|
||||
{
|
||||
if(portable_config.videos)
|
||||
return *portable_config.videos;
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS) && !defined(SAGO_DISABLE)
|
||||
return sago::getVideoFolder();
|
||||
#elif defined(__ANDROID__)
|
||||
@@ -83,6 +102,9 @@ namespace Tesses::Framework::Platform::Environment
|
||||
}
|
||||
VFSPath GetDocuments()
|
||||
{
|
||||
|
||||
if(portable_config.documents)
|
||||
return *portable_config.documents;
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS) && !defined(SAGO_DISABLE)
|
||||
return sago::getDocumentsFolder();
|
||||
#elif defined(__ANDROID__)
|
||||
@@ -93,6 +115,9 @@ namespace Tesses::Framework::Platform::Environment
|
||||
}
|
||||
VFSPath GetConfig()
|
||||
{
|
||||
|
||||
if(portable_config.config)
|
||||
return *portable_config.config;
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS) && !defined(SAGO_DISABLE)
|
||||
return sago::getConfigHome();
|
||||
#else
|
||||
@@ -101,6 +126,8 @@ namespace Tesses::Framework::Platform::Environment
|
||||
}
|
||||
VFSPath GetDesktop()
|
||||
{
|
||||
if(portable_config.desktop)
|
||||
return *portable_config.desktop;
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS) && !defined(SAGO_DISABLE)
|
||||
return sago::getDesktopFolder();
|
||||
#else
|
||||
@@ -109,6 +136,9 @@ namespace Tesses::Framework::Platform::Environment
|
||||
}
|
||||
VFSPath GetState()
|
||||
{
|
||||
|
||||
if(portable_config.state)
|
||||
return *portable_config.state;
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS) && !defined(SAGO_DISABLE)
|
||||
return sago::getStateDir();
|
||||
#else
|
||||
@@ -117,6 +147,9 @@ namespace Tesses::Framework::Platform::Environment
|
||||
}
|
||||
VFSPath GetCache()
|
||||
{
|
||||
|
||||
if(portable_config.cache)
|
||||
return *portable_config.cache;
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS) && !defined(SAGO_DISABLE)
|
||||
return sago::getCacheDir();
|
||||
#else
|
||||
@@ -125,6 +158,9 @@ namespace Tesses::Framework::Platform::Environment
|
||||
}
|
||||
VFSPath GetData()
|
||||
{
|
||||
|
||||
if(portable_config.data)
|
||||
return *portable_config.data;
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS) && !defined(SAGO_DISABLE)
|
||||
return sago::getDataHome();
|
||||
#else
|
||||
|
||||
31
src/Random.cpp
Normal file
31
src/Random.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "TessesFramework/Random.hpp"
|
||||
|
||||
namespace Tesses::Framework {
|
||||
Random::Random() : Random((uint64_t)time(NULL))
|
||||
{
|
||||
|
||||
}
|
||||
Random::Random(uint64_t seed) : num(seed)
|
||||
{
|
||||
|
||||
}
|
||||
uint32_t Random::Next(uint32_t max)
|
||||
{
|
||||
return (uint32_t)Next(0,(int32_t)max);
|
||||
}
|
||||
int32_t Random::Next(int32_t min, int32_t max)
|
||||
{
|
||||
uint32_t number = (uint32_t)(Next() >> 31);
|
||||
int32_t range = max-min;
|
||||
|
||||
return (uint32_t)((((double)number / (double)0xFFFFFFFF) * (double)range)+min);
|
||||
}
|
||||
uint64_t Random::Next()
|
||||
{
|
||||
return num = 6364136223846793005 * num + 1;
|
||||
}
|
||||
uint8_t Random::NextByte()
|
||||
{
|
||||
return (uint8_t)Next(0,256);
|
||||
}
|
||||
}
|
||||
253
src/Serialization/Bencode.cpp
Normal file
253
src/Serialization/Bencode.cpp
Normal file
@@ -0,0 +1,253 @@
|
||||
#include "TessesFramework/Serialization/Bencode.hpp"
|
||||
|
||||
namespace Tesses::Framework::Serialization::Bencode {
|
||||
BeToken BeDictionary::GetValue(BeString key) const
|
||||
{
|
||||
for(auto item : this->tokens)
|
||||
if(item.first == key) return item.second;
|
||||
return BeUndefined();
|
||||
}
|
||||
void BeDictionary::SetValue(BeString key, BeToken value)
|
||||
{
|
||||
if(std::holds_alternative<BeUndefined>(value))
|
||||
{
|
||||
for(auto idx = this->tokens.begin(); idx != this->tokens.end(); idx++)
|
||||
{
|
||||
if(idx->first == key) {
|
||||
this->tokens.erase(idx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(auto& item : this->tokens)
|
||||
{
|
||||
if(item.first == key)
|
||||
{
|
||||
item.second = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
this->tokens.emplace_back(key,value);
|
||||
}
|
||||
}
|
||||
|
||||
BeString::BeString()
|
||||
{
|
||||
|
||||
}
|
||||
BeString::BeString(const std::string& text)
|
||||
{
|
||||
this->data.insert(this->data.end(),text.cbegin(),text.cend());
|
||||
}
|
||||
BeString::BeString(const char* text)
|
||||
{
|
||||
size_t len = strlen(text);
|
||||
this->data.insert(this->data.end(),text,text+len);
|
||||
}
|
||||
BeString::BeString(const std::vector<uint8_t>& data) : data(data)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
BeString::operator std::string() const
|
||||
{
|
||||
return std::string(data.cbegin(),data.cend());
|
||||
}
|
||||
|
||||
|
||||
bool operator==(const BeString& lStr, const BeString& rStr)
|
||||
{
|
||||
return lStr.data == rStr.data;
|
||||
}
|
||||
bool operator==(const BeString& lStr, const std::string& rStr)
|
||||
{
|
||||
if(lStr.data.size() != rStr.size()) return false;
|
||||
return std::equal(lStr.data.cbegin(),lStr.data.cend(),rStr.cbegin());
|
||||
}
|
||||
bool operator==(const std::string& lStr, const BeString& rStr)
|
||||
{
|
||||
if(lStr.size() != rStr.data.size()) return false;
|
||||
return std::equal(lStr.cbegin(),lStr.cend(),rStr.data.cbegin());
|
||||
}
|
||||
bool operator==(const BeString& lStr, const char* rStr)
|
||||
{
|
||||
size_t len = strlen(rStr);
|
||||
if(lStr.data.size() != len) return false;
|
||||
return std::equal(lStr.data.cbegin(),lStr.data.cend(),rStr);
|
||||
}
|
||||
bool operator==(const char* lStr, const BeString& rStr)
|
||||
{
|
||||
size_t len = strlen(lStr);
|
||||
if(rStr.data.size() != len) return false;
|
||||
|
||||
return std::equal(lStr,lStr+len,rStr.data.cbegin());
|
||||
}
|
||||
|
||||
bool operator!=(const BeString& lStr, const BeString& rStr)
|
||||
{
|
||||
return !(lStr == rStr);
|
||||
}
|
||||
bool operator!=(const BeString& lStr, const std::string& rStr)
|
||||
{
|
||||
return !(lStr == rStr);
|
||||
}
|
||||
bool operator!=(const std::string& lStr, const BeString& rStr)
|
||||
{
|
||||
return !(lStr == rStr);
|
||||
}
|
||||
bool operator!=(const BeString& lStr, const char* rStr)
|
||||
{
|
||||
return !(lStr == rStr);
|
||||
}
|
||||
bool operator!=(const char* lStr, const BeString& rStr)
|
||||
{
|
||||
return !(lStr == rStr);
|
||||
}
|
||||
|
||||
void Bencode::Save(std::shared_ptr<Tesses::Framework::Streams::Stream> strm,const BeToken& value)
|
||||
{
|
||||
if(std::holds_alternative<BeArray>(value))
|
||||
{
|
||||
auto& array = std::get<BeArray>(value);
|
||||
strm->WriteByte((uint8_t)'l');
|
||||
for(auto& item : array.tokens)
|
||||
{
|
||||
Save(strm,item);
|
||||
}
|
||||
strm->WriteByte((uint8_t)'e');
|
||||
}
|
||||
else if(std::holds_alternative<BeDictionary>(value))
|
||||
{
|
||||
auto& dict = std::get<BeDictionary>(value);
|
||||
strm->WriteByte((uint8_t)'d');
|
||||
for(auto& item : dict.tokens)
|
||||
{
|
||||
Save(strm,item.first);
|
||||
Save(strm,item.second);
|
||||
}
|
||||
strm->WriteByte((uint8_t)'e');
|
||||
}
|
||||
else if(std::holds_alternative<BeString>(value))
|
||||
{
|
||||
auto& str = std::get<BeString>(value);
|
||||
std::string prefix = std::to_string(str.data.size()) + ":";
|
||||
strm->WriteBlock((const uint8_t*)prefix.data(),prefix.size());
|
||||
strm->WriteBlock(str.data.data(),str.data.size());
|
||||
}
|
||||
else if(std::holds_alternative<int64_t>(value))
|
||||
{
|
||||
int64_t val = std::get<int64_t>(value);
|
||||
std::string str = "i" + std::to_string(val) + "e";
|
||||
strm->WriteBlock((const uint8_t*)str.data(),str.size());
|
||||
}
|
||||
}
|
||||
BeToken Bencode::Load(std::shared_ptr<Tesses::Framework::Streams::Stream> strm)
|
||||
{
|
||||
auto chr = strm->ReadByte();
|
||||
switch(chr)
|
||||
{
|
||||
case 'i':
|
||||
{
|
||||
std::string no;
|
||||
while(true) {
|
||||
chr = strm->ReadByte();
|
||||
if(chr == -1) throw std::out_of_range("End of file");
|
||||
if(chr == 'e') break;
|
||||
no.push_back((char)chr);
|
||||
}
|
||||
return std::stoll(no);
|
||||
}
|
||||
break;
|
||||
case 'd':
|
||||
{
|
||||
BeDictionary dict;
|
||||
while(true)
|
||||
{
|
||||
auto key = Load(strm);
|
||||
if(std::holds_alternative<BeUndefined>(key)) break;
|
||||
if(!std::holds_alternative<BeString>(key)) throw std::runtime_error("Key must be a string");
|
||||
auto value = Load(strm);
|
||||
if(std::holds_alternative<BeUndefined>(key)) throw std::runtime_error("Incomplete dictionary entry");
|
||||
dict.tokens.emplace_back(std::get<BeString>(key),value);
|
||||
}
|
||||
return dict;
|
||||
}
|
||||
break;
|
||||
case 'l':
|
||||
{
|
||||
BeArray array;
|
||||
while(true)
|
||||
{
|
||||
auto tkn = Load(strm);
|
||||
if(std::holds_alternative<BeUndefined>(tkn)) break;
|
||||
array.tokens.push_back(tkn);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
break;
|
||||
case 'e':
|
||||
return BeUndefined();
|
||||
case -1:
|
||||
throw std::out_of_range("End of file");
|
||||
default:
|
||||
{
|
||||
std::string no({(char)chr});
|
||||
while(true) {
|
||||
chr = strm->ReadByte();
|
||||
if(chr == -1) throw std::out_of_range("End of file");
|
||||
if(chr == ':') break;
|
||||
no.push_back((char)chr);
|
||||
}
|
||||
auto len = std::stoll(no);
|
||||
if(len < 0) throw std::out_of_range("Less than zero byte string");
|
||||
BeString str;
|
||||
str.data.resize((size_t)len);
|
||||
|
||||
size_t result = strm->ReadBlock(str.data.data(),str.data.size());
|
||||
if(result != str.data.size() || result != (size_t)len) throw std::out_of_range("Didn't read entire string");
|
||||
return str;
|
||||
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
Json::JToken Bencode::ToJson(const BeToken& tkn)
|
||||
{
|
||||
if(std::holds_alternative<BeDictionary>(tkn))
|
||||
{
|
||||
auto& dict = std::get<BeDictionary>(tkn);
|
||||
Json::JObject o;
|
||||
for(auto& itm : dict.tokens)
|
||||
{
|
||||
o.SetValue(itm.first,ToJson(itm.second));
|
||||
}
|
||||
return o;
|
||||
}
|
||||
if(std::holds_alternative<BeArray>(tkn))
|
||||
{
|
||||
auto& array = std::get<BeArray>(tkn);
|
||||
Json::JArray a;
|
||||
for(auto& itm : array.tokens)
|
||||
{
|
||||
a.Add(ToJson(itm));
|
||||
}
|
||||
return a;
|
||||
}
|
||||
if(std::holds_alternative<BeString>(tkn))
|
||||
{
|
||||
return (std::string)std::get<BeString>(tkn);
|
||||
}
|
||||
if(std::holds_alternative<int64_t>(tkn))
|
||||
{
|
||||
return std::get<int64_t>(tkn);
|
||||
}
|
||||
return Json::JUndefined();
|
||||
}
|
||||
void Bencode::Print(std::shared_ptr<Tesses::Framework::TextStreams::TextWriter> writer, BeToken tkn)
|
||||
{
|
||||
writer->WriteLine(Json::Json::Encode(ToJson(tkn),true));
|
||||
}
|
||||
}
|
||||
@@ -4,16 +4,40 @@ namespace Tesses::Framework::Serialization
|
||||
{
|
||||
double BitConverter::ToDoubleBits(uint64_t v)
|
||||
{
|
||||
return *(double*)&v;
|
||||
static_assert(sizeof(double) == sizeof(uint64_t), "double is not the same size as uint64_t");
|
||||
double dest=0;
|
||||
memcpy(&dest,&v, sizeof(uint64_t));
|
||||
return dest;
|
||||
}
|
||||
uint64_t BitConverter::ToUintBits(double v)
|
||||
{
|
||||
return *(uint64_t*)&v;
|
||||
//as static_assert is compile time we don't need it here
|
||||
uint64_t dest = 0;
|
||||
memcpy(&dest,&v, sizeof(uint64_t));
|
||||
return dest;
|
||||
}
|
||||
float BitConverter::ToFloatBits(uint32_t v)
|
||||
{
|
||||
static_assert(sizeof(float) == sizeof(uint32_t), "float is not the same size as uint32_t");
|
||||
float dest=0;
|
||||
memcpy(&dest,&v, sizeof(uint32_t));
|
||||
return dest;
|
||||
}
|
||||
uint32_t BitConverter::ToUint32Bits(float v)
|
||||
{
|
||||
//as static_assert is compile time we don't need it here
|
||||
uint32_t dest = 0;
|
||||
memcpy(&dest,&v, sizeof(uint32_t));
|
||||
return dest;
|
||||
}
|
||||
double BitConverter::ToDoubleBE(uint8_t& b)
|
||||
{
|
||||
return ToDoubleBits(ToUint64BE(b));
|
||||
}
|
||||
float BitConverter::ToFloatBE(uint8_t& b)
|
||||
{
|
||||
return ToFloatBits(ToUint32BE(b));
|
||||
}
|
||||
uint64_t BitConverter::ToUint64BE(uint8_t& b)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
@@ -49,6 +73,50 @@ namespace Tesses::Framework::Serialization
|
||||
v |= (uint16_t)b2[1];
|
||||
return v;
|
||||
}
|
||||
double BitConverter::ToDoubleLE(uint8_t& b)
|
||||
{
|
||||
return ToDoubleBits(ToUint64LE(b));
|
||||
}
|
||||
float BitConverter::ToFloatLE(uint8_t& b)
|
||||
{
|
||||
return ToFloatBits(ToUint32LE(b));
|
||||
}
|
||||
uint64_t BitConverter::ToUint64LE(uint8_t& b)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
uint64_t v = 0;
|
||||
v |= (uint64_t)b2[0];
|
||||
v |= ((uint64_t)b2[1] << 8);
|
||||
v |= ((uint64_t)b2[2] << 16);
|
||||
v |= ((uint64_t)b2[3] << 24);
|
||||
v |= ((uint64_t)b2[4] << 32);
|
||||
v |= ((uint64_t)b2[5] << 40);
|
||||
v |= ((uint64_t)b2[6] << 48);
|
||||
v |= ((uint64_t)b2[7] << 56);
|
||||
|
||||
return v;
|
||||
}
|
||||
uint32_t BitConverter::ToUint32LE(uint8_t& b)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
uint32_t v = 0;
|
||||
v |= (uint32_t)b2[0];
|
||||
v |= ((uint32_t)b2[1] << 8);
|
||||
v |= ((uint32_t)b2[2] << 16);
|
||||
v |= ((uint32_t)b2[3] << 24);
|
||||
|
||||
return v;
|
||||
}
|
||||
uint16_t BitConverter::ToUint16LE(uint8_t& b)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
uint16_t v = 0;
|
||||
|
||||
v |= (uint16_t)b2[0];
|
||||
v |= ((uint16_t)b2[1] << 8);
|
||||
|
||||
return v;
|
||||
}
|
||||
void BitConverter::FromDoubleBE(uint8_t& b, double v)
|
||||
{
|
||||
FromUint64BE(b,ToUintBits(v));
|
||||
@@ -82,4 +150,170 @@ namespace Tesses::Framework::Serialization
|
||||
b2[1] = (uint8_t)v;
|
||||
}
|
||||
|
||||
};
|
||||
void BitConverter::FromDoubleLE(uint8_t& b, double v)
|
||||
{
|
||||
FromUint64BE(b,ToUintBits(v));
|
||||
}
|
||||
void BitConverter::FromFloatLE(uint8_t& b, float v)
|
||||
{
|
||||
FromUint32LE(b,ToUint32Bits(v));
|
||||
}
|
||||
void BitConverter::FromFloatBE(uint8_t& b, float v)
|
||||
{
|
||||
FromUint32BE(b,ToUint32Bits(v));
|
||||
}
|
||||
void BitConverter::FromUint64LE(uint8_t& b, uint64_t v)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
b2[0] = (uint8_t)v;
|
||||
b2[1] = (uint8_t)(v >> 8);
|
||||
b2[2] = (uint8_t)(v >> 16);
|
||||
b2[3] = (uint8_t)(v >> 24);
|
||||
b2[4] = (uint8_t)(v >> 32);
|
||||
b2[5] = (uint8_t)(v >> 40);
|
||||
b2[6] = (uint8_t)(v >> 48);
|
||||
b2[7] = (uint8_t)(v >> 56);
|
||||
|
||||
}
|
||||
void BitConverter::FromUint32LE(uint8_t& b, uint32_t v)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
|
||||
b2[0] = (uint8_t)v;
|
||||
b2[1] = (uint8_t)(v >> 8);
|
||||
b2[2] = (uint8_t)(v >> 16);
|
||||
b2[3] = (uint8_t)(v >> 24);
|
||||
|
||||
}
|
||||
void BitConverter::FromUint16LE(uint8_t& b, uint16_t v)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
|
||||
|
||||
b2[0] = (uint8_t)v;
|
||||
b2[1] = (uint8_t)(v >> 8);
|
||||
}
|
||||
void BitConverter::FromUuid(uint8_t& b, const Uuid& uuid)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
FromUint32BE(b2[0], uuid.time_low);
|
||||
FromUint16BE(b2[4], uuid.time_mid);
|
||||
FromUint16BE(b2[6], uuid.time_hi_and_version);
|
||||
b2[8] = uuid.clock_seq_hi_and_reserved;
|
||||
b2[9] = uuid.clock_seq_low;
|
||||
for(size_t i = 0; i < 6; i++)
|
||||
b2[i+10] = uuid.node[i];
|
||||
|
||||
}
|
||||
|
||||
|
||||
Uuid BitConverter::ToUuid(uint8_t& b)
|
||||
{
|
||||
Uuid uuid;
|
||||
BitConverter::ToUuid(b,uuid);
|
||||
return uuid;
|
||||
}
|
||||
|
||||
|
||||
void BitConverter::ToUuid(uint8_t& b, Uuid& uuid)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
uuid.time_low = ToUint32BE(b2[0]);
|
||||
|
||||
uuid.time_mid = ToUint16BE(b2[4]);
|
||||
|
||||
uuid.time_hi_and_version = ToUint16BE(b2[6]);
|
||||
|
||||
uuid.clock_seq_hi_and_reserved = b2[8];
|
||||
uuid.clock_seq_low = b2[9];
|
||||
for(size_t i = 0; i < 6; i++)
|
||||
uuid.node[i]= b2[i+10];
|
||||
|
||||
}
|
||||
|
||||
int64_t BitConverter::ToSint64BE(uint8_t& b)
|
||||
{
|
||||
uint64_t src = ToUint64BE(b);
|
||||
int64_t dest = 0;
|
||||
memcpy(&dest,&src,sizeof(uint64_t));
|
||||
return dest;
|
||||
}
|
||||
int64_t BitConverter::ToSint64LE(uint8_t& b)
|
||||
{
|
||||
uint64_t src = ToUint64LE(b);
|
||||
int64_t dest = 0;
|
||||
memcpy(&dest,&src,sizeof(uint64_t));
|
||||
return dest;
|
||||
}
|
||||
|
||||
|
||||
int32_t BitConverter::ToSint32BE(uint8_t& b)
|
||||
{
|
||||
uint32_t src = ToUint32BE(b);
|
||||
int32_t dest = 0;
|
||||
memcpy(&dest,&src,sizeof(uint32_t));
|
||||
return dest;
|
||||
}
|
||||
int32_t BitConverter::ToSint32LE(uint8_t& b)
|
||||
{
|
||||
uint32_t src = ToUint32LE(b);
|
||||
int32_t dest = 0;
|
||||
memcpy(&dest,&src,sizeof(uint32_t));
|
||||
return dest;
|
||||
}
|
||||
|
||||
|
||||
int16_t BitConverter::ToSint16BE(uint8_t& b)
|
||||
{
|
||||
uint16_t src = ToUint16BE(b);
|
||||
int16_t dest = 0;
|
||||
memcpy(&dest,&src,sizeof(uint16_t));
|
||||
return dest;
|
||||
}
|
||||
int16_t BitConverter::ToSint16LE(uint8_t& b)
|
||||
{
|
||||
uint16_t src = ToUint16LE(b);
|
||||
int16_t dest = 0;
|
||||
memcpy(&dest,&src,sizeof(uint16_t));
|
||||
return dest;
|
||||
}
|
||||
|
||||
|
||||
void BitConverter::FromSint64BE(uint8_t& b, int64_t v)
|
||||
{
|
||||
uint64_t dest = 0;
|
||||
memcpy(&dest,&v,sizeof(uint64_t));
|
||||
FromUint64BE(b, dest);
|
||||
}
|
||||
void BitConverter::FromSint32BE(uint8_t& b, int32_t v)
|
||||
{
|
||||
uint32_t dest = 0;
|
||||
memcpy(&dest,&v,sizeof(uint32_t));
|
||||
FromUint32BE(b, dest);
|
||||
}
|
||||
void BitConverter::FromSint16BE(uint8_t& b, int16_t v)
|
||||
{
|
||||
uint16_t dest = 0;
|
||||
memcpy(&dest,&v,sizeof(uint16_t));
|
||||
FromUint16BE(b, dest);
|
||||
}
|
||||
|
||||
void BitConverter::FromSint64LE(uint8_t& b, int64_t v)
|
||||
{
|
||||
uint64_t dest = 0;
|
||||
memcpy(&dest,&v,sizeof(uint64_t));
|
||||
FromUint64LE(b, dest);
|
||||
}
|
||||
void BitConverter::FromSint32LE(uint8_t& b, int32_t v)
|
||||
{
|
||||
uint32_t dest = 0;
|
||||
memcpy(&dest,&v,sizeof(uint32_t));
|
||||
FromUint32LE(b, dest);
|
||||
}
|
||||
void BitConverter::FromSint16LE(uint8_t& b, int16_t v)
|
||||
{
|
||||
uint16_t dest = 0;
|
||||
memcpy(&dest,&v,sizeof(uint16_t));
|
||||
FromUint16LE(b, dest);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "TessesFramework/Streams/ByteReader.hpp"
|
||||
#include "TessesFramework/Serialization/BitConverter.hpp"
|
||||
namespace Tesses::Framework::Streams
|
||||
{
|
||||
std::shared_ptr<Stream> ByteReader::GetStream()
|
||||
@@ -149,5 +150,20 @@ namespace Tesses::Framework::Streams
|
||||
auto v=ReadU64LE();
|
||||
return *(double*)&v;
|
||||
}
|
||||
|
||||
|
||||
Uuid ByteReader::ReadUuid()
|
||||
{
|
||||
uint8_t data[16];
|
||||
if(this->strm->ReadBlock(data, 16) != 16)
|
||||
throw std::runtime_error("End of file");
|
||||
return Serialization::BitConverter::ToUuid(data[0]);
|
||||
}
|
||||
void ByteReader::ReadUuid(Uuid& uuid)
|
||||
{
|
||||
uint8_t data[16];
|
||||
if(this->strm->ReadBlock(data, 16) != 16)
|
||||
throw std::runtime_error("End of file");
|
||||
Serialization::BitConverter::ToUuid(data[0],uuid);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "TessesFramework/Streams/ByteWriter.hpp"
|
||||
#include "TessesFramework/Serialization/BitConverter.hpp"
|
||||
namespace Tesses::Framework::Streams
|
||||
{
|
||||
std::shared_ptr<Stream> ByteWriter::GetStream()
|
||||
@@ -129,5 +130,11 @@ namespace Tesses::Framework::Streams
|
||||
uint64_t data = *(uint64_t*)&v;
|
||||
WriteU64LE(data);
|
||||
}
|
||||
void ByteWriter::WriteUuid(const Uuid& uuid)
|
||||
{
|
||||
uint8_t data[16];
|
||||
Serialization::BitConverter::FromUuid(data[0], uuid);
|
||||
this->strm->WriteBlock(data, 16);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -104,7 +104,12 @@ namespace Tesses::Framework::Streams
|
||||
}
|
||||
FileStream::~FileStream()
|
||||
{
|
||||
Close();
|
||||
if(!f) return;
|
||||
if(this->owns)
|
||||
{
|
||||
fclose(this->f);
|
||||
f=NULL;
|
||||
}
|
||||
}
|
||||
void FileStream::Close()
|
||||
{
|
||||
|
||||
@@ -15,6 +15,7 @@ using HttpUtils = Tesses::Framework::Http::HttpUtils;
|
||||
#include <network.h>
|
||||
#define NETWORK_GETSOCKNAME net_getsockname
|
||||
#define NETWORK_RECV net_recv
|
||||
#define NETWORK_POLL net_poll
|
||||
#define sockaddr_storage sockaddr_in
|
||||
#error "Not supported yet"
|
||||
#else
|
||||
@@ -46,6 +47,7 @@ extern "C" {
|
||||
#if defined(AF_UNIX) && !defined(GEKKO) && !defined(__SWITCH__) && !defined(__PS2__)
|
||||
#include <sys/un.h>
|
||||
#endif
|
||||
#include <poll.h>
|
||||
}
|
||||
#endif
|
||||
#if defined(GEKKO)
|
||||
@@ -68,10 +70,13 @@ extern "C" uint32_t if_config( char *local_ip, char *netmask, char *gateway,bool
|
||||
#define NETWORK_GETADDRINFO getaddrinfo
|
||||
#define NETWORK_FREEADDRINFO freeaddrinfo
|
||||
#define NETWORK_GETSOCKNAME getsockname
|
||||
|
||||
#if defined(_WIN32)
|
||||
#define NETWORK_CLOSE closesocket
|
||||
#define NETWORK_POLL WSAPoll
|
||||
#else
|
||||
#define NETWORK_CLOSE close
|
||||
#define NETWORK_POLL poll
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -288,6 +293,28 @@ namespace Tesses::Framework::Streams {
|
||||
uint32_t addr;
|
||||
uint8_t addr_parts[4];
|
||||
} my_addr_t;
|
||||
|
||||
bool NetworkStream::DataAvailable(int timeout)
|
||||
{
|
||||
pollfd fd;
|
||||
fd.events = POLLIN;
|
||||
fd.fd = this->sock;
|
||||
int res= NETWORK_POLL(&fd,1,timeout);
|
||||
if (res == -1) {
|
||||
if(errno == EINTR) return false;
|
||||
throw std::runtime_error("poll() failed");
|
||||
}
|
||||
else if(res == 0) return false;
|
||||
else if(fd.revents & POLLIN)
|
||||
return true;
|
||||
else if(fd.revents & (POLLERR | POLLNVAL))
|
||||
{
|
||||
this->endOfStream=true;
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
NetworkStream::NetworkStream(std::string unixPath,bool isServer)
|
||||
{
|
||||
this->endOfStream=false;
|
||||
@@ -511,7 +538,10 @@ namespace Tesses::Framework::Streams {
|
||||
TcpServer::~TcpServer()
|
||||
{
|
||||
if(this->valid && this->owns)
|
||||
Close();
|
||||
{
|
||||
NETWORK_CLOSE(this->sock);
|
||||
}
|
||||
this->valid=false;
|
||||
}
|
||||
void TcpServer::Close()
|
||||
{
|
||||
@@ -791,7 +821,9 @@ namespace Tesses::Framework::Streams {
|
||||
}
|
||||
NetworkStream::~NetworkStream()
|
||||
{
|
||||
Close();
|
||||
if(this->owns && this->success)
|
||||
NETWORK_CLOSE(this->sock);
|
||||
this->success=0;
|
||||
}
|
||||
void NetworkStream::SetNoDelay(bool noDelay)
|
||||
{
|
||||
@@ -918,5 +950,8 @@ uint16_t TcpServer::GetPort()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
bool NetworkStream::DataAvailable(int to){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -126,7 +126,13 @@ namespace Tesses::Framework::Streams {
|
||||
}
|
||||
PtyStream::~PtyStream()
|
||||
{
|
||||
Close();
|
||||
if(this->eos) return;
|
||||
this->eos=true;
|
||||
#if !defined(GEKKO) && !defined(__APPLE__) && !defined(__PS2__) && !defined(_WIN32) && !defined(__SWITCH__) && !defined(__FreeBSD__) && defined(TESSESFRAMEWORK_ENABLE_PROCESS)
|
||||
close(this->socket);
|
||||
|
||||
kill((pid_t)this->pid,SIGHUP);
|
||||
#endif
|
||||
}
|
||||
void PtyStream::Close()
|
||||
{
|
||||
|
||||
@@ -48,8 +48,13 @@ namespace Tesses::Framework::Streams {
|
||||
if(len < 1024)
|
||||
read = len;
|
||||
if(read > 0)
|
||||
{
|
||||
read=this->Write(buffer,read);
|
||||
|
||||
if(read == 0)
|
||||
{
|
||||
throw std::out_of_range("Failed to write!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
buffer += read;
|
||||
@@ -109,7 +114,6 @@ namespace Tesses::Framework::Streams {
|
||||
read = (size_t)std::min(len-offset,(uint64_t)buffSize);
|
||||
|
||||
read = this->Read(buffer,read);
|
||||
|
||||
strm->WriteBlock(buffer, read);
|
||||
|
||||
offset += read;
|
||||
|
||||
659
src/TF_Init.cpp
659
src/TF_Init.cpp
@@ -2,6 +2,9 @@
|
||||
#include "TessesFramework/Streams/NetworkStream.hpp"
|
||||
#include "TessesFramework/Lazy.hpp"
|
||||
#include "TessesFramework/Filesystem/LocalFS.hpp"
|
||||
#include "TessesFramework/Platform/Environment.hpp"
|
||||
#include "TessesFramework/Filesystem/FSHelpers.hpp"
|
||||
#include "TessesFramework/Serialization/Json.hpp"
|
||||
#include <atomic>
|
||||
#include <csignal>
|
||||
#include <iostream>
|
||||
@@ -182,7 +185,6 @@ namespace Tesses::Framework
|
||||
}
|
||||
void TF_Init()
|
||||
{
|
||||
Tesses::Framework::Filesystem::LocalFS = std::make_shared<Tesses::Framework::Filesystem::LocalFilesystem>();
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_SQLITE)
|
||||
sqlite3_initialize();
|
||||
#if defined(GEKKO) || defined(__SWITCH__) || defined(__PS2__)
|
||||
@@ -303,4 +305,659 @@ if (iResult != 0) {
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
static std::string TF_FileSizeBin(uint64_t bytes)
|
||||
{
|
||||
if(bytes == 1) return "1 Byte";
|
||||
if(bytes < 1024ULL) return std::to_string(bytes) + " Bytes";
|
||||
if(bytes < 1024ULL*1024ULL) return std::to_string(bytes / 1024ULL) + " kiB";
|
||||
if(bytes < 1024ULL*1024ULL*1024ULL) return std::to_string(bytes / (1024ULL*1024ULL)) + " MiB";
|
||||
if(bytes < 1024ULL*1024ULL*1024ULL*1024ULL) return std::to_string(bytes / (1024ULL*1024ULL*1024ULL)) + " GiB";
|
||||
if(bytes < 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL) return std::to_string(bytes / (1024ULL*1024ULL*1024ULL*1024ULL)) + " TiB";
|
||||
return std::to_string(bytes / (1024ULL*1024ULL*1024ULL*1024ULL*1024ULL)) + " PiB";
|
||||
}
|
||||
static std::string TF_FileSizeDec(uint64_t bytes)
|
||||
{
|
||||
if(bytes == 1) return "1 Byte";
|
||||
if(bytes < 1000ULL) return std::to_string(bytes) + " Bytes";
|
||||
if(bytes < 1000ULL*1000ULL) return std::to_string(bytes / 1000ULL) + " kB";
|
||||
if(bytes < 1000ULL*1000ULL*1000ULL) return std::to_string(bytes / (1000ULL*1000ULL)) + " MB";
|
||||
if(bytes < 1000ULL*1000ULL*1000ULL*1000ULL) return std::to_string(bytes / (1000ULL*1000ULL*1000ULL)) + " GB";
|
||||
if(bytes < 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL) return std::to_string(bytes / (1000ULL*1000ULL*1000ULL*1000ULL)) + " TB";
|
||||
return std::to_string(bytes / (1000ULL*1000ULL*1000ULL*1000ULL*1000ULL)) + " PB";
|
||||
}
|
||||
std::string TF_FileSize(uint64_t bytes, bool usesBin)
|
||||
{
|
||||
return usesBin ? TF_FileSizeBin(bytes) : TF_FileSizeDec(bytes);
|
||||
}
|
||||
|
||||
std::optional<std::string> _argv0=std::nullopt;
|
||||
|
||||
|
||||
void TF_AllowPortable(std::string argv0)
|
||||
{
|
||||
using namespace Tesses::Framework::Serialization::Json;
|
||||
using namespace Tesses::Framework::Platform::Environment;
|
||||
using namespace Tesses::Framework::Filesystem;
|
||||
_argv0 = argv0;
|
||||
VFSPath path(argv0);
|
||||
auto realPath=path.MakeAbsolute();
|
||||
auto dir = realPath.GetParent();
|
||||
auto portable=dir / "portable.json";
|
||||
if(LocalFS->FileExists(portable))
|
||||
{
|
||||
|
||||
std::string portable_str;
|
||||
Helpers::ReadAllText(LocalFS,portable, portable_str);
|
||||
auto jsonObj=Json::Decode(portable_str);
|
||||
JObject dict;
|
||||
JObject dict2;
|
||||
if(TryGetJToken(jsonObj,dict) && dict.TryGetValueAsType("portable_data",dict2))
|
||||
{
|
||||
|
||||
if(dict.TryGetValueAsType("portable_type", portable_str))
|
||||
{
|
||||
if(portable_str == "PortableApps.com")
|
||||
{
|
||||
//do the special stuffs for PortableApps.com based apps
|
||||
auto paf_documents = GetVariable("PortableApps.comDocuments");
|
||||
auto paf_music = GetVariable("PortableApps.comMusic");
|
||||
auto paf_pictures = GetVariable("PortableApps.comPictures");
|
||||
auto paf_videos = GetVariable("PortableApps.comVideos");
|
||||
auto paf_data = GetVariable("PAL:DataDir");
|
||||
|
||||
|
||||
|
||||
bool bV=false;
|
||||
if(!dict2.TryGetValueAsType("system_documents",bV) || !bV)
|
||||
{
|
||||
if(paf_documents)
|
||||
{
|
||||
portable_config.documents = LocalFS->SystemToVFSPath(*paf_documents);
|
||||
}
|
||||
}
|
||||
if(!dict2.TryGetValueAsType("system_pictures",bV) || !bV)
|
||||
{
|
||||
if(paf_pictures)
|
||||
{
|
||||
portable_config.pictures = LocalFS->SystemToVFSPath(*paf_pictures);
|
||||
}
|
||||
}
|
||||
if(!dict2.TryGetValueAsType("system_videos",bV) || !bV)
|
||||
{
|
||||
if(paf_videos)
|
||||
{
|
||||
portable_config.videos = LocalFS->SystemToVFSPath(*paf_videos);
|
||||
}
|
||||
}
|
||||
if(!dict2.TryGetValueAsType("system_music",bV) || !bV)
|
||||
{
|
||||
if(paf_music)
|
||||
{
|
||||
portable_config.music = LocalFS->SystemToVFSPath(*paf_music);
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("user",portable_str))
|
||||
{
|
||||
if(portable_str == "app")
|
||||
{
|
||||
if(paf_data)
|
||||
portable_config.user = LocalFS->SystemToVFSPath(*paf_data) / "TF_User";
|
||||
}
|
||||
else if(portable_str == "documents")
|
||||
{
|
||||
if(paf_documents)
|
||||
portable_config.user = LocalFS->SystemToVFSPath(*paf_documents) / "TF_User";
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("desktop",portable_str))
|
||||
{
|
||||
if(portable_str == "tf_user")
|
||||
{
|
||||
if(portable_config.user)
|
||||
portable_config.desktop = *(portable_config.user) / "Desktop";
|
||||
}
|
||||
else if(portable_str == "documents")
|
||||
{
|
||||
if(paf_documents)
|
||||
portable_config.desktop = LocalFS->SystemToVFSPath(*paf_documents) / "Desktop";
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("downloads",portable_str))
|
||||
{
|
||||
if(portable_str == "tf_user")
|
||||
{
|
||||
if(portable_config.user)
|
||||
portable_config.downloads = *(portable_config.user) / "Downloads";
|
||||
}
|
||||
else if(portable_str == "documents")
|
||||
{
|
||||
if(paf_documents)
|
||||
portable_config.downloads = LocalFS->SystemToVFSPath(*paf_documents) / "Downloads";
|
||||
}
|
||||
}
|
||||
if(!dict2.TryGetValueAsType("system_config",bV) || !bV)
|
||||
{
|
||||
if(portable_config.user)
|
||||
portable_config.config = *(portable_config.user) / "Config";
|
||||
}
|
||||
if(!dict2.TryGetValueAsType("system_cache",bV) || !bV)
|
||||
{
|
||||
if(portable_config.user)
|
||||
portable_config.cache = *(portable_config.user) / "Cache";
|
||||
}
|
||||
if(!dict2.TryGetValueAsType("system_data",bV) || !bV)
|
||||
{
|
||||
if(portable_config.user)
|
||||
portable_config.data = *(portable_config.user) / "Data";
|
||||
}
|
||||
if(!dict2.TryGetValueAsType("system_state",bV) || !bV)
|
||||
{
|
||||
if(portable_config.user)
|
||||
portable_config.state = *(portable_config.user) / "State";
|
||||
}
|
||||
if(!dict2.TryGetValueAsType("system_temp",bV) || !bV)
|
||||
{
|
||||
if(portable_config.user)
|
||||
portable_config.temp = *(portable_config.user) / "Temp";
|
||||
}
|
||||
}
|
||||
else if(portable_str == "relative")
|
||||
{
|
||||
if(dict2.TryGetValueAsType("user",portable_str))
|
||||
{
|
||||
if(portable_str != "system")
|
||||
{
|
||||
auto userDir = dir / portable_str;
|
||||
portable_config.user = userDir.CollapseRelativeParents();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
if(dict2.TryGetValueAsType("documents", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.documents = *(portable_config.user) / "Documents";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
auto userDir = dir / portable_str;
|
||||
portable_config.documents = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("downloads", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.downloads = *(portable_config.user) / "Downloads";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
auto userDir = dir / portable_str;
|
||||
portable_config.downloads = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("desktop", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.desktop = *(portable_config.user) / "Desktop";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
auto userDir = dir / portable_str;
|
||||
portable_config.desktop = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("pictures", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.pictures = *(portable_config.user) / "Pictures";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
auto userDir = dir / portable_str;
|
||||
portable_config.pictures = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("videos", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.videos = *(portable_config.user) / "Videos";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
auto userDir = dir / portable_str;
|
||||
portable_config.videos = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("music", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.music = *(portable_config.user) / "Music";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
auto userDir = dir / portable_str;
|
||||
portable_config.music = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("config", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.config = *(portable_config.user) / "Config";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
auto userDir = dir / portable_str;
|
||||
portable_config.config = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("cache", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.cache = *(portable_config.user) / "Cache";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
auto userDir = dir / portable_str;
|
||||
portable_config.cache = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("data", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.data = *(portable_config.user) / "Data";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
auto userDir = dir / portable_str;
|
||||
portable_config.data = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("state", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.state = *(portable_config.user) / "State";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
auto userDir = dir / portable_str;
|
||||
portable_config.state = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("temp", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.temp = *(portable_config.user) / "Temp";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
auto userDir = dir / portable_str;
|
||||
portable_config.temp = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
else if(portable_str == "absolute")
|
||||
{
|
||||
if(dict2.TryGetValueAsType("user",portable_str))
|
||||
{
|
||||
if(portable_str != "system")
|
||||
{
|
||||
VFSPath userDir = portable_str;
|
||||
portable_config.user = userDir.CollapseRelativeParents();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
if(dict2.TryGetValueAsType("documents", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.documents = *(portable_config.user) / "Documents";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
VFSPath userDir = portable_str;
|
||||
portable_config.documents = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("downloads", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.downloads = *(portable_config.user) / "Downloads";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
VFSPath userDir = portable_str;
|
||||
portable_config.downloads = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("desktop", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.desktop = *(portable_config.user) / "Desktop";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
VFSPath userDir = portable_str;
|
||||
portable_config.desktop = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("pictures", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.pictures = *(portable_config.user) / "Pictures";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
VFSPath userDir = portable_str;
|
||||
portable_config.pictures = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("videos", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.videos = *(portable_config.user) / "Videos";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
VFSPath userDir = portable_str;
|
||||
portable_config.videos = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("music", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.music = *(portable_config.user) / "Music";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
VFSPath userDir = portable_str;
|
||||
portable_config.music = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("config", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.config = *(portable_config.user) / "Config";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
VFSPath userDir = portable_str;
|
||||
portable_config.config = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("cache", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.cache = *(portable_config.user) / "Cache";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
VFSPath userDir = portable_str;
|
||||
portable_config.cache = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("data", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.data = *(portable_config.user) / "Data";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
VFSPath userDir = portable_str;
|
||||
portable_config.data = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("state", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.state = *(portable_config.user) / "State";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
VFSPath userDir = portable_str;
|
||||
portable_config.state = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dict2.TryGetValueAsType("temp", portable_str))
|
||||
{
|
||||
|
||||
if(portable_str != "system")
|
||||
{
|
||||
if(portable_str == "default")
|
||||
{
|
||||
if(portable_config.user)
|
||||
{
|
||||
portable_config.temp = *(portable_config.user) / "Temp";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
VFSPath userDir = portable_str;
|
||||
portable_config.temp = userDir.CollapseRelativeParents();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::string> TF_GetCommandName()
|
||||
{
|
||||
return _argv0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace Tesses::Framework::TextStreams
|
||||
{
|
||||
std::string txt;
|
||||
this->ReadBlock(txt,1);
|
||||
if(txt.empty()) return -1;
|
||||
if(txt.empty()) { eof=true; return -1;}
|
||||
return (uint8_t)txt[0];
|
||||
}
|
||||
std::string TextReader::ReadLine()
|
||||
@@ -19,13 +19,14 @@ namespace Tesses::Framework::TextStreams
|
||||
ReadLine(str);
|
||||
return str;
|
||||
}
|
||||
bool TextReader::ReadLine(std::string& str)
|
||||
bool TextReader::ReadLineHttp(std::string& str)
|
||||
{
|
||||
if(eof) return false;
|
||||
bool ret = false;
|
||||
int32_t r = -1;
|
||||
do {
|
||||
r = ReadChar();
|
||||
if(r == -1) break;
|
||||
if(r == -1) {break;}
|
||||
if(r == '\r') continue;
|
||||
if(r == '\n') break;
|
||||
str.push_back((char)(uint8_t)r);
|
||||
@@ -33,8 +34,25 @@ namespace Tesses::Framework::TextStreams
|
||||
} while(r != -1);
|
||||
return ret;
|
||||
}
|
||||
bool TextReader::ReadLine(std::string& str)
|
||||
{
|
||||
|
||||
if(eof) return false;
|
||||
bool ret = false;
|
||||
int32_t r = -1;
|
||||
do {
|
||||
r = ReadChar();
|
||||
if(r == -1) break;
|
||||
if(r == '\r') continue;
|
||||
if(r == '\n') return true;
|
||||
str.push_back((char)(uint8_t)r);
|
||||
ret = true;
|
||||
} while(r != -1);
|
||||
return ret;
|
||||
}
|
||||
void TextReader::ReadAllLines(std::vector<std::string>& lines)
|
||||
{
|
||||
if(eof) return;
|
||||
int32_t r = -1;
|
||||
std::string builder;
|
||||
do {
|
||||
@@ -60,10 +78,14 @@ namespace Tesses::Framework::TextStreams
|
||||
|
||||
void TextReader::ReadToEnd(std::string& str)
|
||||
{
|
||||
|
||||
if(eof) return;
|
||||
while(ReadBlock(str,1024));
|
||||
}
|
||||
void TextReader::CopyTo(TextWriter& writer, size_t buffSz)
|
||||
{
|
||||
|
||||
if(eof) return;
|
||||
std::string str = {};
|
||||
while(ReadBlock(str,buffSz))
|
||||
{
|
||||
|
||||
214
src/Uuid.cpp
Normal file
214
src/Uuid.cpp
Normal file
@@ -0,0 +1,214 @@
|
||||
#include "TessesFramework/Uuid.hpp"
|
||||
#include "TessesFramework/Http/HttpUtils.hpp"
|
||||
#include "TessesFramework/Crypto/Crypto.hpp"
|
||||
|
||||
namespace Tesses::Framework {
|
||||
Uuid Uuid::Generate()
|
||||
{
|
||||
//xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
|
||||
Uuid uuid;
|
||||
Uuid::Generate(uuid);
|
||||
return uuid;
|
||||
}
|
||||
|
||||
void Uuid::Generate(Uuid& uuid)
|
||||
{
|
||||
std::vector<uint8_t> bytes(16);
|
||||
Crypto::RandomBytes(bytes, "TF_UUID");
|
||||
|
||||
uuid.time_low = (uint32_t)bytes[0];
|
||||
uuid.time_low |= (uint32_t)bytes[1] << 8;
|
||||
uuid.time_low |= (uint32_t)bytes[2] << 16;
|
||||
uuid.time_low |= (uint32_t)bytes[3] << 24;
|
||||
uuid.time_mid = (uint16_t)bytes[4];
|
||||
uuid.time_mid |= (uint16_t)bytes[5] << 8;
|
||||
uuid.time_hi_and_version = (uint16_t)bytes[6];
|
||||
uuid.time_hi_and_version |= (uint16_t)bytes[7] << 8;
|
||||
uuid.clock_seq_hi_and_reserved = bytes[8];
|
||||
uuid.clock_seq_low = bytes[9];
|
||||
for(size_t i = 0; i < 6; i++)
|
||||
{
|
||||
uuid.node[i] = bytes[i+10];
|
||||
}
|
||||
uuid.time_hi_and_version &= ~0x00F0;
|
||||
uuid.time_hi_and_version |= 0x0040;
|
||||
uuid.clock_seq_hi_and_reserved &= ~0b11000000;
|
||||
uuid.clock_seq_hi_and_reserved |= 0b10000000;
|
||||
|
||||
|
||||
}
|
||||
|
||||
bool Uuid::TryParse(std::string text, Uuid& uuid)
|
||||
{
|
||||
std::array<uint8_t,32> hex_digits;
|
||||
size_t hex_offset = 0;
|
||||
size_t text_offset = 0;
|
||||
for(; text_offset < text.size(); text_offset++)
|
||||
{
|
||||
if(text[text_offset] == '{' && (text_offset != 0 || hex_offset != 0))
|
||||
return false;
|
||||
if(text[text_offset] == '}' && hex_offset < 32)
|
||||
return false;
|
||||
if(text[text_offset] == '-' && hex_offset != 8 && hex_offset != 12 && hex_offset != 16 && hex_offset != 20)
|
||||
return false;
|
||||
if((text[text_offset] >= 'A' && text[text_offset] <= 'F') || (text[text_offset] >= 'a' && text[text_offset] <= 'f') || text[text_offset] >= '0' && text[text_offset] <= '9')
|
||||
{
|
||||
if(hex_offset >= 32) return false;
|
||||
hex_digits[hex_offset] = Http::HttpUtils::HexToNibble(text[text_offset]);
|
||||
hex_offset++;
|
||||
}
|
||||
else return false;
|
||||
|
||||
}
|
||||
|
||||
uint8_t b = hex_digits[0] << 4 | hex_digits[1];
|
||||
uuid.time_low = (uint32_t)b;
|
||||
b = hex_digits[2] << 4 | hex_digits[3];
|
||||
uuid.time_low |= (uint32_t)b << 8;
|
||||
b = hex_digits[4] << 4 | hex_digits[5];
|
||||
uuid.time_low |= (uint32_t)b << 16;
|
||||
b = hex_digits[6] << 4 | hex_digits[7];
|
||||
uuid.time_low |= (uint32_t)b << 24;
|
||||
|
||||
b = hex_digits[8] << 4 | hex_digits[9];
|
||||
uuid.time_mid = (uint16_t)b;
|
||||
b = hex_digits[10] << 4 | hex_digits[11];
|
||||
uuid.time_mid |= (uint16_t)b << 8;
|
||||
|
||||
b = hex_digits[12] << 4 | hex_digits[13];
|
||||
uuid.time_hi_and_version = (uint16_t)b;
|
||||
b = hex_digits[14] << 4 | hex_digits[15];
|
||||
uuid.time_hi_and_version |= (uint16_t)b << 8;
|
||||
|
||||
uuid.clock_seq_hi_and_reserved = hex_digits[16] << 4 | hex_digits[17];
|
||||
|
||||
uuid.clock_seq_low = hex_digits[18] << 4 | hex_digits[19];
|
||||
|
||||
for(size_t i = 0; i < 6; i++)
|
||||
{
|
||||
uuid.node[i] = hex_digits[20+(i*2)] << 4 | hex_digits[21+(i*2)];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//9c4994e7-3c82-4c30-a459-8fdcd960b4ac
|
||||
|
||||
std::string Uuid::ToString(UuidStringifyConfig cfg) const
|
||||
{
|
||||
bool hasCurly = ((int)cfg & (int)UuidStringifyConfig::HasCurly) != 0;
|
||||
bool isUppercase = ((int)cfg & (int)UuidStringifyConfig::IsUppercase) != 0;
|
||||
bool hasDash = ((int)cfg & (int)UuidStringifyConfig::HasDashes) != 0;
|
||||
|
||||
std::string uuid_str = "";
|
||||
if(hasCurly)
|
||||
uuid_str += "{";
|
||||
|
||||
uint8_t byte = (uint8_t)(this->time_low & 0xFF);
|
||||
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
byte = (uint8_t)((this->time_low >> 8) & 0xFF);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
byte = (uint8_t)((this->time_low >> 16) & 0xFF);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
byte = (uint8_t)((this->time_low >> 24) & 0xFF);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
|
||||
if(hasDash)
|
||||
uuid_str += "-";
|
||||
|
||||
byte = (uint8_t)(this->time_mid & 0xFF);
|
||||
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
|
||||
byte = (uint8_t)((this->time_mid >> 8) & 0xFF);
|
||||
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
|
||||
if(hasDash)
|
||||
uuid_str += "-";
|
||||
|
||||
byte = (uint8_t)(this->time_hi_and_version & 0xFF);
|
||||
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
|
||||
byte = (uint8_t)((this->time_hi_and_version >> 8) & 0xFF);
|
||||
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
if(hasDash)
|
||||
uuid_str += "-";
|
||||
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(this->clock_seq_hi_and_reserved>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(this->clock_seq_hi_and_reserved,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(this->clock_seq_low>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(this->clock_seq_low,isUppercase);
|
||||
if(hasDash)
|
||||
uuid_str += "-";
|
||||
|
||||
for(size_t i = 0; i < 6; i++)
|
||||
{
|
||||
byte = this->node[i];
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
}
|
||||
|
||||
if(hasCurly)
|
||||
uuid_str += "}";
|
||||
return uuid_str;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
bool Uuid::IsEmpty() const
|
||||
{
|
||||
return this->time_low == 0 &&
|
||||
this->time_mid == 0 &&
|
||||
this->time_hi_and_version == 0 &&
|
||||
this->clock_seq_hi_and_reserved == 0 &&
|
||||
this->clock_seq_low == 0 &&
|
||||
this->node[0] == 0 &&
|
||||
this->node[1] == 0 &&
|
||||
this->node[2] == 0 &&
|
||||
this->node[3] == 0 &&
|
||||
this->node[4] == 0 &&
|
||||
this->node[5] == 0;
|
||||
}
|
||||
|
||||
bool operator==(const Uuid& left, const Uuid& right)
|
||||
{
|
||||
return left.time_low == right.time_low &&
|
||||
left.time_mid == right.time_mid &&
|
||||
left.time_hi_and_version == right.time_hi_and_version &&
|
||||
left.clock_seq_hi_and_reserved == right.clock_seq_hi_and_reserved &&
|
||||
left.clock_seq_low == right.clock_seq_low &&
|
||||
left.node[0] == right.node[0] &&
|
||||
left.node[1] == right.node[1] &&
|
||||
left.node[2] == right.node[2] &&
|
||||
left.node[3] == right.node[3] &&
|
||||
left.node[4] == right.node[4] &&
|
||||
left.node[5] == right.node[5];
|
||||
|
||||
}
|
||||
bool operator!=(const Uuid& left, const Uuid& right)
|
||||
{
|
||||
return left.time_low != right.time_low &&
|
||||
left.time_mid != right.time_mid &&
|
||||
left.time_hi_and_version != right.time_hi_and_version &&
|
||||
left.clock_seq_hi_and_reserved != right.clock_seq_hi_and_reserved &&
|
||||
left.clock_seq_low != right.clock_seq_low &&
|
||||
left.node[0] != right.node[0] &&
|
||||
left.node[1] != right.node[1] &&
|
||||
left.node[2] != right.node[2] &&
|
||||
left.node[3] != right.node[3] &&
|
||||
left.node[4] != right.node[4] &&
|
||||
left.node[5] != right.node[5];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user