--- nono/host/comdriver_tcp.cpp 2026/04/29 17:05:11 1.1 +++ nono/host/comdriver_tcp.cpp 2026/04/29 17:06:02 1.1.1.4 @@ -17,18 +17,27 @@ // コンストラクタ COMDriverTCP::COMDriverTCP(HostDevice *hostdev_) - : inherited(hostdev_, "TCP") + : inherited(hostdev_, "tcp") { + port = -1; } // デストラクタ COMDriverTCP::~COMDriverTCP() { + if (sock.Valid()) { + hostdev->DelOuter(sock); + sock.Close(); + } + if (ls.Valid()) { + hostdev->DelListen(ls); + ls.Close(); + } } // ドライバ初期化 bool -COMDriverTCP::InitDriver() +COMDriverTCP::InitDriver(bool startup) { struct addrinfo hints; struct addrinfo *res, *ai; @@ -38,10 +47,12 @@ COMDriverTCP::InitDriver() const char *host = "::1"; std::string key = hostdev->GetConfigKey(); const ConfigItem& item = gConfig->Find(key + "-tcp-port"); - std::string port = item.AsString(); - int portnum = atoi(port.c_str()); - if (portnum < 1 || portnum > 65535) { - item.Err(); + std::string portstr = item.AsString(); + port = atoi(portstr.c_str()); + if (port < 1 || port > 65535) { + port = -1; + errmsg = item.ErrMsg(); + putmsg(1, "%s", errmsg.c_str()); return false; } @@ -51,19 +62,21 @@ COMDriverTCP::InitDriver() hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; - r = getaddrinfo(host, port.c_str(), &hints, &res); + r = getaddrinfo(host, portstr.c_str(), &hints, &res); if (r != 0) { - const char *errmsg; + const char *em; if (r == EAI_SYSTEM) { - errmsg = strerror(errno); + em = strerror(errno); } else { - errmsg = gai_strerror(r); + em = gai_strerror(r); } - putmsg(0, "getaddrinfo: %s: %s", host, errmsg); + errmsg = item.ErrMsg("getaddrinfo(%s): %s", host, em); + putmsg(1, "%s", errmsg.c_str()); return false; } if (res == NULL) { - putmsg(0, "getaddrinfo: %s: no address found", host); + errmsg = item.ErrMsg("getaddrinfo(%s): no address found", host); + putmsg(1, "%s", errmsg.c_str()); return false; } @@ -74,31 +87,38 @@ COMDriverTCP::InitDriver() ls = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (ls == -1) { - putmsg(0, "socket(family=%d): %s", (int)ai->ai_family, - strerror(errno)); + errmsg = string_format("socket(family=%d): %s", + (int)ai->ai_family, strerror(errno)); + putmsg(1, "%s", errmsg.c_str()); continue; } if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) { - putmsg(0, "setsockopt(SO_REUSEADDR): %s", strerror(errno)); + errmsg = string_format("setsockopt(SO_REUSEADDR): %s", + strerror(errno)); + putmsg(1, "%s", errmsg.c_str()); ls.Close(); continue; } if (setsockopt(ls, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on))) { - putmsg(0, "setsockopt(SO_REUSEPORT): %s", strerror(errno)); + errmsg = string_format("setsockopt(SO_REUSEPORT): %s", + strerror(errno)); + putmsg(1, "%s", errmsg.c_str()); ls.Close(); continue; } if (bind(ls, ai->ai_addr, ai->ai_addrlen) == -1) { - putmsg(0, "bind: %s", strerror(errno)); + errmsg = string_format("bind: %s", strerror(errno)); + putmsg(1, "%s", errmsg.c_str()); ls.Close(); continue; } if (listen(ls, 0) == -1) { - putmsg(0, "listen: %s", strerror(errno)); + errmsg = string_format("listen: %s", strerror(errno)); + putmsg(1, "%s", errmsg.c_str()); ls.Close(); continue; } @@ -113,7 +133,8 @@ COMDriverTCP::InitDriver() return false; } if (hostdev->AddListen(ls, EV_ADD) < 0) { - putmsg(0, "AddListen(ADD): %s", strerror(errno)); + errmsg = string_format("AddListen(ADD): %s", strerror(errno)); + putmsg(1, "%s", errmsg.c_str()); return false; } @@ -442,7 +463,7 @@ COMDriverTCP::GetCommandName(uint8 code) // モニタ (ドライバ依存情報のみ) void -COMDriverTCP::MonitorUpdateMD(TextScreen& screen) +COMDriverTCP::MonitorScreenMD(TextScreen& screen, int y) { - // 何を表示したもんか + screen.Print(0, y++, "Listen Port: %u", port); }