Annotation of nono/debugger/consio.cpp, revision 1.1

1.1     ! root        1: //
        !             2: // nono
        !             3: // Copyright (C) 2019 [email protected]
        !             4: //
        !             5: 
        !             6: #include "header.h"
        !             7: #include "configfile.h"
        !             8: #include "consio.h"
        !             9: #include <poll.h>
        !            10: #include <netdb.h>
        !            11: #include <sys/socket.h>
        !            12: 
        !            13: // コンストラクタ
        !            14: ConsioTCP::ConsioTCP()
        !            15: {
        !            16:        ls = -1;
        !            17:        sock = -1;
        !            18: }
        !            19: 
        !            20: // デストラクタ
        !            21: ConsioTCP::~ConsioTCP()
        !            22: {
        !            23:        if (res) {
        !            24:                freeaddrinfo(res);
        !            25:                res = NULL;
        !            26:        }
        !            27:        if (sock != -1) {
        !            28:                close(sock);
        !            29:                sock = -1;
        !            30:        }
        !            31:        if (ls != -1) {
        !            32:                close(ls);
        !            33:                ls = -1;
        !            34:        }
        !            35: }
        !            36: 
        !            37: // 初期化
        !            38: bool
        !            39: ConsioTCP::Init()
        !            40: {
        !            41:        struct addrinfo hints;
        !            42:        int r;
        !            43:        int on = 1;
        !            44: 
        !            45:        memset(&hints, 0, sizeof(hints));
        !            46:        hints.ai_family = AF_UNSPEC;
        !            47:        hints.ai_socktype = SOCK_STREAM;
        !            48:        hints.ai_flags = AI_PASSIVE;
        !            49: 
        !            50:        const char *host = "::1";
        !            51:        std::string port = gConfig->ReadStr("debugger_port", "9999");
        !            52: printf("debugger_port=%s\n", port.c_str());
        !            53: 
        !            54:        r = getaddrinfo(host, port.c_str(), &hints, &res);
        !            55:        if (r != 0) {
        !            56:                const char *errmsg;
        !            57:                if (r == EAI_SYSTEM) {
        !            58:                        errmsg = strerror(errno);
        !            59:                } else {
        !            60:                        errmsg = gai_strerror(r);
        !            61:                }
        !            62:                warnx("getaddrinfo: %s: %s", host, errmsg);
        !            63:                return false;
        !            64:        }
        !            65:        if (res == NULL) {
        !            66:                warnx("getaddrinfo: %s: no address found", host);
        !            67:                return false;
        !            68:        }
        !            69: 
        !            70:        // res を順番に試して成功したらそれで待ち受ける
        !            71:        ls = -1;
        !            72:        for (ai = res; ai && ls == -1; ai = ai->ai_next) {
        !            73:                ls = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
        !            74:                if (ls == -1) {
        !            75:                        warn("socket");
        !            76:                        continue;
        !            77:                }
        !            78: 
        !            79:                if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) {
        !            80:                        warn("setsockopt(SO_REUSEADDR)");
        !            81:                        close(ls);
        !            82:                        continue;
        !            83:                }
        !            84: 
        !            85:                if (setsockopt(ls, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on))) {
        !            86:                        warn("setsockopt(SO_REUSEPORT)");
        !            87:                        close(ls);
        !            88:                        continue;
        !            89:                }
        !            90: 
        !            91:                if (bind(ls, ai->ai_addr, ai->ai_addrlen) == -1) {
        !            92:                        warn("bind");
        !            93:                        close(ls);
        !            94:                        continue;
        !            95:                }
        !            96: 
        !            97:                if (listen(ls, 0) == -1) {
        !            98:                        warn("listen");
        !            99:                        close(ls);
        !           100:                        continue;
        !           101:                }
        !           102: 
        !           103:                // ここまで来たら成功
        !           104:                break;
        !           105:        }
        !           106: 
        !           107:        if (ls == -1) {
        !           108:                return false;
        !           109:        }
        !           110: 
        !           111:        return true;
        !           112: }
        !           113: 
        !           114: bool
        !           115: ConsioTCP::Open()
        !           116: {
        !           117:        // 待ち受け
        !           118:        for (;;) {
        !           119:                sock = accept(ls, ai->ai_addr, &ai->ai_addrlen);
        !           120:                if (sock == -1) {
        !           121:                        if (errno == EINTR) {
        !           122:                                continue;
        !           123:                        }
        !           124:                        warn("accept");
        !           125:                        return false;
        !           126:                }
        !           127:                break;
        !           128:        }
        !           129: 
        !           130:        return true;
        !           131: }
        !           132: 
        !           133: void
        !           134: ConsioTCP::Close()
        !           135: {
        !           136:        if (sock != -1) {
        !           137:                close(sock);
        !           138:                sock = -1;
        !           139:        }
        !           140: }
        !           141: 
        !           142: void
        !           143: ConsioTCP::Print(const char *fmt, ...)
        !           144: {
        !           145:        char buf[1024];
        !           146:        va_list ap;
        !           147:        int len;
        !           148:        int r;
        !           149: 
        !           150:        if (sock == -1) {
        !           151:                errno = EBADF;
        !           152:                return;
        !           153:        }
        !           154: 
        !           155:        va_start(ap, fmt);
        !           156:        len = vsnprintf(buf, sizeof(buf), fmt, ap);
        !           157:        va_end(ap);
        !           158: 
        !           159:        len = std::min(len, (int)sizeof(buf));
        !           160:        r = write(sock, buf, len);
        !           161:        if (r == -1)
        !           162:                Close();
        !           163: }
        !           164: 
        !           165: void
        !           166: ConsioTCP::Flush()
        !           167: {
        !           168: }
        !           169: 
        !           170: int
        !           171: ConsioTCP::GetChar()
        !           172: {
        !           173:        char ch;
        !           174:        int r;
        !           175: 
        !           176:        if (sock == -1) {
        !           177:                errno = EBADF;
        !           178:                return -1;
        !           179:        }
        !           180: 
        !           181:        r = read(sock, &ch, 1);
        !           182:        if (r == -1) {
        !           183:                Close();
        !           184:                return -1;
        !           185:        }
        !           186:        return ch;
        !           187: }
        !           188: 
        !           189: int
        !           190: ConsioTCP::Gets(char *buf, int bufsize)
        !           191: {
        !           192:        int len;
        !           193:        int r;
        !           194: 
        !           195:        if (sock == -1) {
        !           196:                errno = EBADF;
        !           197:                return -1;
        !           198:        }
        !           199: 
        !           200:        for (len = 0; len < bufsize - 1; ) {
        !           201:                r = read(sock, buf + len, 1);
        !           202:                if (r == 0) {
        !           203:                        goto done;
        !           204:                }
        !           205:                if (r == -1) {
        !           206:                        Close();
        !           207:                        return -1;
        !           208:                }
        !           209:                char c = buf[len];
        !           210:                len++;
        !           211: 
        !           212:                if (c == '\n') {
        !           213:                        break;
        !           214:                }
        !           215:        }
        !           216: 
        !           217:  done:
        !           218:        buf[len] = '\0';
        !           219:        return len;
        !           220: }
        !           221: 
        !           222: int
        !           223: ConsioTCP::Poll()
        !           224: {
        !           225:        struct pollfd pfd;
        !           226: 
        !           227:        if (sock == -1) {
        !           228:                errno = EBADF;
        !           229:                return -1;
        !           230:        }
        !           231: 
        !           232:        pfd.fd = sock;
        !           233:        pfd.events = POLLIN;
        !           234:        pfd.revents = 0;
        !           235:        int r = poll(&pfd, 1, 0);
        !           236:        return r;
        !           237: }

unix.superglobalmegacorp.com

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