annotate seobeo/os/s_macos_edge.c @ 17:d97ec3ded2ae

[Seobeo] Few changes... - Fixed seobeo edge for macos - Updated so that socket creation can be used for both client and server - Started on a cutelient library for making connection to the server.
author June Park <parkjune1995@gmail.com>
date Sat, 04 Oct 2025 07:53:12 -0700
parents fb2cff495a60
children fa2b8af609d9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
1 #include <sys/event.h>
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
2 #include "seobeo/seobeo.h"
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
3
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
4
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
5 void *Seobeo_Web_Edge_Worker(void *vargs)
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
6 {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
7 WorkerArgs *args = vargs;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
8 struct kevent evlist[64];
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
9 while (1) {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
10 int ne = kevent(args->evfd, NULL, 0, evlist, 64, NULL);
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
11 if (ne < 0) continue;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
12 for (int i = 0; i < ne; i++) {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
13 Seobeo_PHandle h = evlist[i].udata;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
14 if (h == args->srv) {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
15 Seobeo_PHandle cli =
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
16 Seobeo_Stream_Handle_Accept(args->srv);
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
17 if (!cli) continue;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
18 struct kevent kev = {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
19 .ident = cli->socket,
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
20 .filter = EVFILT_READ,
16
fb2cff495a60 [Seobeo] Fixed the problem with edge server.
June Park <parkjune1995@gmail.com>
parents: 7
diff changeset
21 .flags = EV_ADD | EV_ONESHOT,
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
22 .udata = cli
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
23 };
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
24 kevent(args->evfd, &kev, 1, NULL, 0, NULL);
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
25 } else {
16
fb2cff495a60 [Seobeo] Fixed the problem with edge server.
June Park <parkjune1995@gmail.com>
parents: 7
diff changeset
26 if (h != args->srv) {
fb2cff495a60 [Seobeo] Fixed the problem with edge server.
June Park <parkjune1995@gmail.com>
parents: 7
diff changeset
27 struct kevent kev = {
fb2cff495a60 [Seobeo] Fixed the problem with edge server.
June Park <parkjune1995@gmail.com>
parents: 7
diff changeset
28 .ident = h->socket,
fb2cff495a60 [Seobeo] Fixed the problem with edge server.
June Park <parkjune1995@gmail.com>
parents: 7
diff changeset
29 .filter = EVFILT_READ,
fb2cff495a60 [Seobeo] Fixed the problem with edge server.
June Park <parkjune1995@gmail.com>
parents: 7
diff changeset
30 .flags = EV_DELETE,
fb2cff495a60 [Seobeo] Fixed the problem with edge server.
June Park <parkjune1995@gmail.com>
parents: 7
diff changeset
31 };
fb2cff495a60 [Seobeo] Fixed the problem with edge server.
June Park <parkjune1995@gmail.com>
parents: 7
diff changeset
32 kevent(args->evfd, &kev, 1, NULL, 0, NULL); // Remove from kqueue first
fb2cff495a60 [Seobeo] Fixed the problem with edge server.
June Park <parkjune1995@gmail.com>
parents: 7
diff changeset
33
fb2cff495a60 [Seobeo] Fixed the problem with edge server.
June Park <parkjune1995@gmail.com>
parents: 7
diff changeset
34 Seobeo_Web_HandleClientRequest(h, args->cache); // this frees
fb2cff495a60 [Seobeo] Fixed the problem with edge server.
June Park <parkjune1995@gmail.com>
parents: 7
diff changeset
35 }
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
36 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
37 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
38 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
39 return NULL;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
40 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
41
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
42 void Seobeo_Web_Edge(
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
43 Seobeo_PHandle p_server_handle,
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
44 int thread_count,
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
45 Dowa_PHashMap p_html_cache)
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
46 {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
47 int kq = kqueue();
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
48 struct kevent kev = {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
49 .ident = p_server_handle->socket,
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
50 .filter = EVFILT_READ,
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
51 .flags = EV_ADD,
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
52 .udata = p_server_handle
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
53 };
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
54 kevent(kq, &kev, 1, NULL, 0, NULL);
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
55
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
56 pthread_attr_t attr;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
57 pthread_attr_init(&attr);
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
58 pthread_attr_setstacksize(&attr, 100 * 1024 * 1024); // 100 MB
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
59
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
60 pthread_t threads[thread_count];
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
61 for (int i = 0; i < thread_count; i++)
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
62 {
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
63 WorkerArgs args = { p_server_handle, p_html_cache, kq };
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
64 pthread_create(&threads[i], NULL,
17
d97ec3ded2ae [Seobeo] Few changes...
June Park <parkjune1995@gmail.com>
parents: 16
diff changeset
65 Seobeo_Web_Edge_Worker, &args);
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
66 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
67 for (int i = 0; i < thread_count; i++)
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
68 {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
69 pthread_join(threads[i], NULL);
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
70 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
71 return;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
72 }