Skip to content

Commit 12a59b1

Browse files
committed
UPD | slices: 4K instead of 64K
1 parent b2836e1 commit 12a59b1

26 files changed

+451
-191
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,11 @@ int main () {
283283
- [ ] Windows
284284

285285
## Tested
286-
- Tested on Hyprland Arch Linux x86_64 kernel 6.9.3-zen1-1-zen wayland
286+
- Hyprland Arch Linux x86_64 kernel 6.9.3-zen1-1-zen wayland Debug/Release
287+
- Windows 11 Pro 22h2 x86_64 MSVC Debug
288+
289+
## Made by
290+
291+
[![ГБОУ РИЛИ](assets/rili.png)](https://rilirb.ru)
292+
293+
[![Башкортостан](assets/rb2.png)](https://ru.wikipedia.org/wiki/%D0%91%D0%B0%D1%88%D0%BA%D0%BE%D1%80%D1%82%D0%BE%D1%81%D1%82%D0%B0%D0%BD)

assets/rb2.png

1.98 KB
Loading

assets/rili.png

4.33 KB
Loading

include/ManapiErrors.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ namespace manapi {
9292
}
9393

9494
T value () {
95+
this->throw_it();
9596
return std::move(this->value_.value());
9697
}
9798

include/components/ManapiEventStructures.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,18 +389,29 @@ namespace manapi::ev {
389389
int accept (tcp *parent) MANAPI_EV_NOEXPECT;
390390

391391
int read_start () MANAPI_EV_NOEXPECT;
392+
392393
int read_start (uv_alloc_cb alloc, uv_read_cb cb) MANAPI_EV_NOEXPECT;
393394

394395
int read_stop () MANAPI_EV_NOEXPECT;
395396

396397
ssize_t try_write (const void *buff, ssize_t len) MANAPI_EV_NOEXPECT;
398+
399+
ssize_t try_write (const ev::buff_t *buff, uint32_t nbuff) MANAPI_EV_NOEXPECT;
400+
397401
int s_bind (const sockaddr *addr, int flags) MANAPI_EV_NOEXPECT;
402+
398403
int getpeername (sockaddr *name, int *namelen) MANAPI_EV_NOEXPECT;
404+
399405
int getsockname (sockaddr *name, int *namelen) MANAPI_EV_NOEXPECT;
406+
400407
int close_reset (uv_close_cb close_cb) MANAPI_EV_NOEXPECT;
408+
401409
int close_reset () MANAPI_EV_NOEXPECT;
410+
402411
int keepalive (int enable, unsigned int delay) MANAPI_EV_NOEXPECT;
412+
403413
int nodelay (int enable) MANAPI_EV_NOEXPECT;
414+
404415
int simultaneous_accepts (int enable) MANAPI_EV_NOEXPECT;
405416
private:
406417
uv_tcp_t s_;

include/components/ManapiSlice.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ namespace manapi {
2929
void *buffer ();
3030

3131
[[nodiscard]] std::size_t size () const;
32+
33+
[[nodiscard]] bool is_last () const;
3234
private:
3335
slice_part_t *part;
3436
slice_base *base_;

include/http/HTTPv2.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ namespace manapi::net::http {
139139
int http_v2_on_write (http_v2_t *ctx);
140140
int http_v2_on_read_stream (const worker::shared_conn &conn, http_v2_stream_t *s);
141141
int http_v2_work (http_v2_t *ctx, http::config *config, const char **nbuffer, ssize_t *nsize);
142-
ssize_t http_v2_write (const worker::shared_conn &conn, http_v2_stream_t *s, const void *buffer, ssize_t size, bool finish);
142+
ssize_t http_v2_write (const worker::shared_conn &conn, http_v2_stream_t *s, ev::buff_t *buff, uint32_t nbuff, bool finish);
143143
int http_v2_rst_stream (http_v2_stream_t *s, int errcode);
144144
manapi::future<ssize_t> http_v2_response (worker::base *worker, const worker::shared_conn &connection, http_v2_stream_t *s, int status, std::map<std::string, std::string> headers, bool finish);
145145
}

include/worker/HTTPv3_clouflare_quiche.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ namespace manapi::net::worker {
4848

4949
void onrecv(std::shared_ptr<ev::udp> &watcher, char *buff, ssize_t size, const sockaddr *addr, unsigned flags) override;
5050

51-
ssize_t sync_write(const shared_conn &conn, const void *buff, ssize_t size, bool finish) override;
51+
ssize_t sync_write(const shared_conn &conn, ev::buff_t *buff, uint32_t nbuff, bool finish) override;
5252

53-
ssize_t sync_write_ex(const shared_conn &conn, const void *buff, ssize_t size, bool finish, int maxcnt) override;
53+
ssize_t sync_write_ex(const shared_conn &conn, ev::buff_t *buff, uint32_t nbuff, ssize_t size, bool finish, int maxcnt) override;
5454

5555
void close_connection(shared_conn conn, bool clean_disconnect) override;
5656

include/worker/TCP.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ namespace manapi::net::worker {
6161

6262
void feed_event (const shared_conn &conn, int flags, const char *buff, ssize_t size, ibuffpool_t *p) override;
6363

64-
ssize_t sync_write_ex(const worker::shared_conn &conn, const void *buff, ssize_t size, bool finish, int maxcnt);
64+
ssize_t sync_write_ex(const worker::shared_conn &conn, ev::buff_t *buff, uint32_t nbuff, ssize_t size, bool finish, int maxcnt) override;
6565

66-
ssize_t sync_write(const worker::shared_conn &conn, const void *buff, ssize_t size, bool finish) override;
66+
ssize_t sync_write(const worker::shared_conn &conn, ev::buff_t *buff, uint32_t nbuff, bool finish) override;
6767

6868
std::unique_ptr<worker_watcher_cb> event_on(const shared_conn & conn, std::unique_ptr<worker_watcher_cb> callback) override;
6969

include/worker/TLS.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ namespace manapi::net::worker {
3131

3232
void close_connection(shared_conn conn, bool clean_disconnect) override;
3333

34-
ssize_t sync_write_ex(const shared_conn &conn, const void *buff, ssize_t size, bool finish, int maxcnt) override;
34+
ssize_t sync_write_ex(const shared_conn &conn, ev::buff_t *buff, uint32_t nbuff, ssize_t size, bool finish, int maxcnt) override;
3535

36-
ssize_t sync_write(const shared_conn &conn, const void *buff, ssize_t size, bool finish) override;
36+
ssize_t sync_write(const shared_conn &conn, ev::buff_t *buff, uint32_t nbuff, bool finish) override;
3737

3838
int event_flags(const shared_conn & conn, int flags) noexcept(true) override;
3939

include/worker/base_worker.hpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,26 @@ namespace manapi::net::worker {
139139

140140
virtual void configure_connection (const shared_conn &conn, oncont_cb cb) = 0;
141141

142-
virtual ssize_t sync_write_ex (const shared_conn &conn, const void *buff, ssize_t size, bool finish, int maxcnt) = 0;
142+
virtual ssize_t sync_write_ex (const shared_conn &conn, ev::buff_t *buff, uint32_t nbuff, ssize_t size, bool finish, int maxcnt) = 0;
143143

144-
virtual ssize_t sync_write (const shared_conn &conn, const void *buff, ssize_t size, bool finish) = 0;
144+
virtual ssize_t sync_write (const shared_conn &conn, ev::buff_t *buff, uint32_t nbuff, bool finish) = 0;
145+
146+
ssize_t sync_write_ex (const shared_conn &conn, const void *buff, ssize_t size, bool finish, int maxcnt);
147+
148+
ssize_t sync_write (const shared_conn &conn, const void *buff, ssize_t size, bool finish);
145149

146150
virtual bool is_writable (const shared_conn &conn) = 0;
147151

148152
virtual void waiting (const shared_conn &conn, bool state) = 0;
149153

150154
manapi::future<ssize_t> write (const shared_conn &conn, const void *buff, ssize_t size, bool finish);
151155

156+
manapi::future<ssize_t> write (const shared_conn &conn, ev::buff_t *buff, uint32_t nbuff, bool finish);
157+
152158
manapi::future<ssize_t> fwrite (const shared_conn &conn, const void *buff, ssize_t size, bool finish);
153159

160+
manapi::future<ssize_t> fwrite (const shared_conn &conn, manapi::slice_view slice, bool finish);
161+
154162
virtual future<ssize_t> response (const shared_conn &connection, http::response *resp, bool finish);
155163

156164
virtual void stop (std::function<void()> cb) = 0;
@@ -178,7 +186,10 @@ namespace manapi::net::worker {
178186
static void connection_io_send_start (struct connection_io_part *top, const char *buffer, ssize_t size, object_pool *bufferpool, int buffer_size, ibuffpool_t *buff, int *cnt);
179187

180188
static void connection_io_trim (struct connection_io_part *top, buffer_deque *parent, int *cnt);
189+
190+
static ssize_t buffs_cut_by_size (ev::buff_t *buff, uint32_t &nbuff, ssize_t limit_size, bool &fin);
181191
protected:
192+
182193
void feed_event_read_ (const shared_conn &conn, worker_watcher_cb *cb, connection_io_part *recv, int *recv_size, int conn_flags, int flags, const char *buff, ssize_t size, ibuffpool_t *p);
183194
};
184195

include/worker/default_http2.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ namespace manapi::net::worker {
5050

5151
void stop(std::function<void()> cb) override;
5252

53-
ssize_t sync_write(const shared_conn &conn, const void *buff, ssize_t size, bool finish) override;
53+
ssize_t sync_write(const shared_conn &conn, ev::buff_t *buff, uint32_t nbuff, bool finish) override;
5454

55-
ssize_t sync_write_ex(const shared_conn &conn, const void *buff, ssize_t size, bool finish, int maxcnt) override;
55+
/* size must always be -1 */
56+
ssize_t sync_write_ex(const shared_conn &conn, ev::buff_t *buff, uint32_t nbuff, ssize_t size, bool finish, int maxcnt) override;
5657

5758
void update_limit_rate_stream (const shared_conn &conn);
5859
private:

main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,14 @@ int main () {
280280

281281
router.GET("/video", [] (manapi::net::http::request &req, manapi::net::http::response &resp)
282282
-> manapi::future<> {
283-
std::cout<<"video send to "<<req.ip_data().ip <<":"<<(int)req.ip_data().port<<"\n";
283+
std::cout<<"video is sending to "<<req.ip_data().ip <<":"<<(int)req.ip_data().port<<"\n";
284284
resp.compress_enabled(false);
285285
resp.partial_enabled(true);
286286
co_return resp.file("/home/Timur/Downloads/VideoDownloader/ufa.mp4");
287287
});
288288

289+
router.GET("/mem", "/home/Timur/Downloads/VideoDownloader");
290+
289291
router.GET("/timeout", [] (manapi::net::http::request &req, manapi::net::http::response &resp)
290292
-> manapi::future<> {
291293
std::cout << "wait\n";
@@ -300,8 +302,6 @@ int main () {
300302
co_return resp.text("GET param 'text' doesn't exists");
301303
}
302304

303-
304-
305305
std::string ip = "https://localhost:8885/video";
306306
int timeout = 2000;
307307
if (req.contains_get_param("timeout")) {

src/ManapiErrors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ std::string_view manapi::error::status::status_msg() const {
102102

103103
void manapi::error::status::throw_it() const {
104104
if (this->code_ != ERR_OK)
105-
THROW_MANAPIHTTP_EXCEPTION2(this->code_, "msg: {}, data: {}", this->msg_, this->data_.dump());
105+
THROW_MANAPIHTTP_EXCEPTION(this->code_, "msg: {}, data: {}", this->msg_, this->data_.dump());
106106
}
107107

108108
manapi::error::status manapi::error::status_ok() {

src/ManapiFilesystem.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -799,12 +799,12 @@ manapi::future<ssize_t> manapi::filesystem::async_read(ev::file file, ev::buff_t
799799
while (nbuff) {
800800
ssize_t rhs = ev::fs::try_read(file, buff->base + shift, buff->len - shift, offset);
801801

802-
if (rhs > 0)
803-
co_return rhs;
804-
805802
if (!rhs)
806803
break;
807804

805+
if (offset >= 0)
806+
offset += rhs;
807+
808808
shift += rhs;
809809
res += rhs;
810810

@@ -815,7 +815,7 @@ manapi::future<ssize_t> manapi::filesystem::async_read(ev::file file, ev::buff_t
815815
}
816816
}
817817

818-
if (!nbuff)
818+
if (res || !nbuff)
819819
co_return res;
820820

821821
async_read_data_t dd {};

src/ManapiHttpPool.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ manapi::future<void> manapi::net::http_pool::_pool() {
101101
auto wrkptr = workerptr->wrk_global();
102102

103103
if (implementation != "quiche") {
104-
for (auto version : this->config->http_versions) {
104+
auto versions = this->config->http_versions;
105+
versions.insert(http::versions::HTTP_v1_1);
106+
for (auto version : versions) {
105107
if (version >= http::versions::HTTP_v0_9 && version < http::versions::HTTP_v1_1)
106108
version = http::versions::HTTP_v1_1;
107109

src/components/ManapiEventStructures.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ ssize_t manapi::ev::tcp::try_write(const void *buff, ssize_t len) MANAPI_EV_NOEX
199199
return uv_try_write(MANAPI_EV_CAST_STREAM(&this->s_), &buffs, 1);
200200
}
201201

202+
ssize_t manapi::ev::tcp::try_write(const ev::buff_t *buff, uint32_t nbuff) noexcept(true) {
203+
return uv_try_write(MANAPI_EV_CAST_STREAM(&this->s_), buff, nbuff);
204+
}
205+
202206
int manapi::ev::tcp::s_bind(const sockaddr *addr, int flags) MANAPI_EV_NOEXPECT {
203207
return uv_tcp_bind(&this->s_, addr, flags);
204208
}

src/components/ManapiObjectPool.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
enum buffer_level {
1313
BUFF_LEVEL_32 = 0,
1414
BUFF_LEVEL_256,
15-
BUFF_LEVEL_2048,
15+
BUFF_LEVEL_4096,
1616
BUFF_LEVEL_16384,
1717
BUFF_LEVEL_65536,
1818
BUFF_LEVEL_MAX
@@ -31,8 +31,8 @@ int bufflen2level (int len) {
3131
if (len <= 256) {
3232
return BUFF_LEVEL_256;
3333
}
34-
if (len <= 2048) {
35-
return BUFF_LEVEL_2048;
34+
if (len <= 4096) {
35+
return BUFF_LEVEL_4096;
3636
}
3737
if (len <= 16384) {
3838
return BUFF_LEVEL_16384;
@@ -48,7 +48,7 @@ int level2bufflen (int lvl) {
4848
switch (lvl) {
4949
case BUFF_LEVEL_32: return 32;
5050
case BUFF_LEVEL_256: return 256;
51-
case BUFF_LEVEL_2048: return 2048;
51+
case BUFF_LEVEL_4096: return 4096;
5252
case BUFF_LEVEL_16384: return 16384;
5353
case BUFF_LEVEL_65536: return 65536;
5454
}
@@ -138,7 +138,7 @@ manapi::slice manapi::object_pool::slice(std::size_t suggested) {
138138
void *buffptr;
139139
std::size_t buffsize;
140140

141-
object_pool_malloc(this->data.get(), &buffptr, &buffsize, left);
141+
object_pool_malloc(this->data.get(), &buffptr, &buffsize, area_size);
142142

143143
cur->buff.base = static_cast<char *>(buffptr);
144144
cur->buff.len = buffsize;

src/components/ManapiSlice.cpp

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ manapi::slice_base::slice_base(slice_part_t *first, slice_part_t *last, uint32_t
4747
this->shift_ = shift;
4848
this->rshift_ = rshift;
4949

50-
assert(this->size_ == size);
50+
assert(this->size() == size);
5151
}
5252

5353
manapi::slice_iterator::slice_iterator(slice_part_t *part, slice_base *base) {
@@ -81,11 +81,15 @@ std::size_t manapi::slice_iterator::size() const {
8181
std::size_t s = this->part->buff.len;
8282
if (this->base_->slices_begin() == this->part)
8383
s -= this->base_->shift();
84-
if (this->base_->slices_end() == this->part->next)
84+
if (this->is_last())
8585
s -= this->base_->rshift();
8686
return s;
8787
}
8888

89+
bool manapi::slice_iterator::is_last() const {
90+
return this->base_->slices_end() == this->part->next;
91+
}
92+
8993
void manapi::slice_base::slice_part_deleter::operator()(slice_part_t *ptr) {
9094
if (ptr) {
9195
manapi::slice_base::slice_part_deleter::operator()(ptr->next);
@@ -244,6 +248,10 @@ manapi::error::status_or<manapi::slice_base> manapi::slice_base::subslice(std::s
244248
if (!size)
245249
size = this->size_ - pos;
246250

251+
if (pos == this->shift_ && size == this->size_ - this->shift_) {
252+
return *this;
253+
}
254+
247255
if (pos + size > this->size_) {
248256
return manapi::error::status_out_of_range("subslice size is too large");
249257
}
@@ -259,7 +267,7 @@ manapi::error::status_or<manapi::slice_base> manapi::slice_base::subslice(std::s
259267
if (current == this->last) {
260268
return manapi::slice_base{nullptr, nullptr, 0, 0, 0, 0};
261269
}
262-
size += this->shift_;
270+
size += pos;
263271
auto scurrent = current;
264272
while (this->last != scurrent
265273
&& scurrent->buff.len <= size) {
@@ -270,19 +278,18 @@ manapi::error::status_or<manapi::slice_base> manapi::slice_base::subslice(std::s
270278
if (size && this->last == scurrent)
271279
return error::status_out_of_range("pos and size too large");
272280

273-
std::size_t rshift;
281+
ssize_t rshift;
274282

275283
if (size) {
276284
rshift = scurrent->buff.len - size;
277285
cnt++;
278-
size=0;
279286
scurrent=scurrent->next;
280287
}
281288
else {
282289
rshift = 0;
283290
}
284291

285-
return manapi::slice_base{current, scurrent, cnt, pos, rshift, tmp_size};
292+
return manapi::slice_base{current, scurrent, cnt, pos, static_cast<std::size_t>(rshift), tmp_size};
286293
}
287294

288295
std::size_t manapi::slice_base::shift() const {
@@ -377,9 +384,34 @@ manapi::slice::slice(slice_base n) : slice_base(std::move(n)) {
377384

378385
}
379386

380-
manapi::slice::slice(slice &&n) noexcept = default;
387+
manapi::slice::slice(slice &&n) noexcept : slice() {
388+
this->first = n.first;
389+
this->last = n.last;
390+
this->count = n.count;
391+
this->rshift_ = n.rshift_;
392+
this->shift_ = n.shift_;
393+
394+
n.shift_ = 0;
395+
n.rshift_ = 0;
396+
n.count = 0;
397+
n.first = nullptr;
398+
n.last = nullptr;
399+
}
381400

382-
manapi::slice & manapi::slice::operator=(slice &&n) noexcept = default;
401+
manapi::slice & manapi::slice::operator=(slice &&n) noexcept {
402+
this->first = n.first;
403+
this->last = n.last;
404+
this->count = n.count;
405+
this->rshift_ = n.rshift_;
406+
this->shift_ = n.shift_;
407+
408+
n.shift_ = 0;
409+
n.rshift_ = 0;
410+
n.count = 0;
411+
n.first = nullptr;
412+
n.last = nullptr;
413+
return *this;
414+
}
383415

384416
manapi::slice::~slice() {
385417
this->clear();

0 commit comments

Comments
 (0)