Annotation of nono/cli/cuimessage.cpp, revision 1.1.1.3

1.1       root        1: //
                      2: // nono
                      3: // Copyright (C) 2021 nono project
                      4: // Licensed under nono-license.txt
                      5: //
                      6: 
1.1.1.2   root        7: //
                      8: // UI スレッドへのメッセージ機構 (CLI 側)
                      9: //
                     10: 
1.1.1.3 ! root       11: #include "nono.h"
1.1       root       12: #include "cuimessage.h"
1.1.1.2   root       13: #include "kevent.h"
1.1       root       14: 
                     15: std::array<CUIMessage::Func, UIMessage::ID_MAX> CUIMessage::func_table;
                     16: 
1.1.1.2   root       17: /*static*/ autofd CUIMessage::wpipe;
                     18: /*static*/ autofd CUIMessage::rpipe;
                     19: 
1.1       root       20: // UIMessage を func に接続する
                     21: /*static*/ void
                     22: CUIMessage::Connect(UIMessage::ID id, CUIMessage::Func func)
                     23: {
                     24:        func_table[id] = func;
                     25: }
                     26: 
1.1.1.2   root       27: // CUIMessage の初期化。パイプを用意して kqueue ディスクリプタを返す。
                     28: int
                     29: CUIMessage::Init()
                     30: {
                     31:        int kq;
                     32:        int fds[2];
                     33: 
                     34:        kq = kqueue();
                     35:        if (kq < 0) {
                     36:                warn("CUIMessage::Init: kqueue");
                     37:                return -1;
                     38:        }
                     39: 
                     40:        if (pipe(fds) < 0) {
                     41:                warn("CUIMessage::Init: pipe");
                     42:                close(kq);
                     43:                return -1;
                     44:        }
                     45: 
                     46:        rpipe = fds[0];
                     47:        wpipe = fds[1];
                     48: 
                     49:        if (kevent_add(kq, rpipe, EVFILT_READ, EV_ADD, 0) < 0) {
                     50:                warn("CUIMessage::Init: kevent_add");
                     51:                close(kq);
                     52:                return -1;
                     53:        }
                     54: 
                     55:        return kq;
                     56: }
                     57: 
1.1       root       58: // UIMessage を処理する
                     59: /*static*/ void
                     60: CUIMessage::Process(UIMessage::Queue& queue)
                     61: {
                     62:        UIMessage m;
                     63: 
                     64:        while (queue.Dequeue(&m)) {
                     65:                UIMessage::ID id = m.GetID();
1.1.1.2   root       66: 
1.1.1.3 ! root       67:                if (id >= UIMessage::ID_MAX) {
        !            68:                        PANIC("Invalid UIMessage %u", id);
1.1       root       69:                }
1.1.1.2   root       70: 
                     71:                // パイプで通知。クラス(構造体)をそのまま書き込む。
                     72:                int r;
                     73:                r = write(wpipe, &m, sizeof(m));
                     74:                if (r < 0) {
                     75:                        warn("CUIMessage::Process: write");
                     76:                        continue;
                     77:                }
                     78:                if (r < sizeof(m)) {
                     79:                        warnx("CUIMessage::Process: write: too short");
                     80:                        continue;
1.1       root       81:                }
                     82:        }
                     83: }
1.1.1.2   root       84: 
                     85: // UIMessage をディスパッチする (メインスレッドで呼ばれる)
                     86: /*static*/ void
                     87: CUIMessage::Dispatch(const UIMessage& m)
                     88: {
                     89:        auto func = func_table[m.GetID()];
                     90:        if (func) {
                     91:                func(m);
                     92:        }
                     93: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.