Skip to content

Commit f45fe3d

Browse files
committed
feat(terminal):1.12.5, 添加stdio的终端方式
1 parent 6f50bbc commit f45fe3d

File tree

13 files changed

+486
-4
lines changed

13 files changed

+486
-4
lines changed

examples/network/stdio_stream/01_stdin_out/main.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ int main()
3232
auto sp_stdin = new network::StdinStream(sp_loop);
3333
auto sp_stdout = new network::StdoutStream(sp_loop);
3434

35+
sp_stdin->initialize();
36+
sp_stdout->initialize();
37+
3538
sp_stdin->bind(sp_stdout);
3639

3740
sp_stdin->enable();

examples/network/stdio_stream/02_stdio/main.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ int main()
3131
auto sp_loop = event::Loop::New();
3232
auto sp_stdio = new network::StdioStream(sp_loop);
3333

34+
sp_stdio->initialize();
35+
3436
sp_stdio->bind(sp_stdio);
3537
sp_stdio->enable();
3638

@@ -53,3 +55,4 @@ int main()
5355
delete sp_loop;
5456
return 0;
5557
}
58+

examples/terminal/build_nodes.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ void BuildNodes(TerminalNodes &term, Loop *wp_loop)
137137

138138
term.mountNode(dir1_1_token, term.rootNode(), "root"); //! 循环引用
139139

140+
//! 演示使用 AddFuncNode() 函数直接添加函数结点
140141
auto add_func_dir_node = term.createDirNode();
141142
term.mountNode(term.rootNode(), add_func_dir_node, "add_func");
142143

@@ -145,4 +146,44 @@ void BuildNodes(TerminalNodes &term, Loop *wp_loop)
145146
AddFuncNode(term, add_func_dir_node, "int_value", int_value, 0, 100);
146147
AddFuncNode(term, add_func_dir_node, "double_value", double_value, 0, 1);
147148
AddFuncNode(term, add_func_dir_node, "str_value", str_value);
149+
150+
//! 演示使用 XxxxxFuncNodeProfile 来添加函数结点
151+
auto profile_func_dir_node = term.createDirNode();
152+
term.mountNode(term.rootNode(), profile_func_dir_node, "profile_func");
153+
154+
{
155+
BooleanFuncNodeProfile profile;
156+
profile.set_func = [&] (bool value) { bool_value = value; return true; };
157+
profile.get_func = [&] () { return bool_value; };
158+
159+
AddFuncNode(term, profile_func_dir_node, "bool_value", profile);
160+
}
161+
162+
{
163+
StringFuncNodeProfile profile;
164+
profile.set_func = [&] (const std::string &value) { str_value = value; return true; };
165+
profile.get_func = [&] () { return str_value; };
166+
167+
AddFuncNode(term, profile_func_dir_node, "str_value", profile);
168+
}
169+
170+
{
171+
IntegerFuncNodeProfile profile;
172+
profile.set_func = [&] (int value) { int_value = value; return true; };
173+
profile.get_func = [&] () { return int_value; };
174+
profile.min_value = 1;
175+
profile.max_value = 5;
176+
177+
AddFuncNode(term, profile_func_dir_node, "int_value", profile);
178+
}
179+
180+
{
181+
DoubleFuncNodeProfile profile;
182+
profile.set_func = [&] (double value) { double_value = value; return true; };
183+
profile.get_func = [&] () { return double_value; };
184+
profile.min_value = -1;
185+
profile.max_value = 1;
186+
187+
AddFuncNode(term, profile_func_dir_node, "double_value", profile);
188+
}
148189
}

examples/terminal/stdio/Makefile

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#
2+
# .============.
3+
# // M A K E / \
4+
# // C++ DEV / \
5+
# // E A S Y / \/ \
6+
# ++ ----------. \/\ .
7+
# \\ \ \ /\ /
8+
# \\ \ \ /
9+
# \\ \ \ /
10+
# -============'
11+
#
12+
# Copyright (c) 2018 Hevake and contributors, all rights reserved.
13+
#
14+
# This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
15+
# Use of this source code is governed by MIT license that can be found
16+
# in the LICENSE file in the root of the source tree. All contributing
17+
# project authors may be found in the CONTRIBUTORS.md file in the root
18+
# of the source tree.
19+
#
20+
21+
PROJECT := examples/terminal/stdio
22+
EXE_NAME := ${PROJECT}
23+
24+
CPP_SRC_FILES := main.cpp ../build_nodes.cpp
25+
26+
CXXFLAGS := -DMODULE_ID='"$(EXE_NAME)"' $(CXXFLAGS)
27+
LDFLAGS += \
28+
-ltbox_terminal \
29+
-ltbox_network \
30+
-ltbox_event \
31+
-ltbox_util \
32+
-ltbox_base \
33+
-ldl
34+
35+
include $(TOP_DIR)/mk/exe_common.mk

examples/terminal/stdio/main.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* .============.
3+
* // M A K E / \
4+
* // C++ DEV / \
5+
* // E A S Y / \/ \
6+
* ++ ----------. \/\ .
7+
* \\ \ \ /\ /
8+
* \\ \ \ /
9+
* \\ \ \ /
10+
* -============'
11+
*
12+
* Copyright (c) 2025 Hevake and contributors, all rights reserved.
13+
*
14+
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
15+
* Use of this source code is governed by MIT license that can be found
16+
* in the LICENSE file in the root of the source tree. All contributing
17+
* project authors may be found in the CONTRIBUTORS.md file in the root
18+
* of the source tree.
19+
*/
20+
#include <string>
21+
#include <signal.h>
22+
23+
#include <tbox/base/scope_exit.hpp>
24+
#include <tbox/base/log_output.h>
25+
26+
#include <tbox/event/loop.h>
27+
#include <tbox/event/signal_event.h>
28+
29+
#include <tbox/terminal/terminal.h>
30+
#include <tbox/terminal/service/stdio.h>
31+
32+
using namespace tbox;
33+
34+
void BuildNodes(terminal::TerminalNodes &term, event::Loop *wp_loop);
35+
36+
int main()
37+
{
38+
//LogOutput_Enable();
39+
auto sp_loop = event::Loop::New();
40+
SetScopeExitAction([sp_loop] { delete sp_loop; });
41+
42+
terminal::Terminal term(sp_loop);
43+
terminal::Stdio stdio(sp_loop, &term);
44+
stdio.initialize();
45+
46+
term.setWelcomeText("Welcome to Terminal STDIO demo! \r\n");
47+
48+
//! 注册ctrl+C停止信号
49+
auto *sp_stop_ev = sp_loop->newSignalEvent();
50+
SetScopeExitAction([sp_stop_ev] { delete sp_stop_ev; });
51+
sp_stop_ev->initialize({SIGINT,SIGTERM}, event::Event::Mode::kOneshot);
52+
//! 指定ctrl+C时要做的事务
53+
sp_stop_ev->setCallback(
54+
[&] (int) {
55+
stdio.stop();
56+
sp_loop->exitLoop(); //! (3) 退出事件循环
57+
}
58+
);
59+
sp_stop_ev->enable();
60+
61+
BuildNodes(term, sp_loop);
62+
63+
stdio.start();
64+
65+
sp_loop->runLoop();
66+
67+
stdio.cleanup();
68+
return 0;
69+
}

modules/network/stdio_stream.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ namespace network {
2626

2727
StdinStream::StdinStream(event::Loop *wp_loop) :
2828
buff_fd_(wp_loop)
29+
{ }
30+
31+
bool StdinStream::initialize()
2932
{
3033
buff_fd_.initialize(STDIN_FILENO, BufferedFd::kReadOnly);
34+
return true;
3135
}
3236

3337
bool StdinStream::enable()
@@ -47,8 +51,12 @@ bool StdinStream::disable()
4751

4852
StdoutStream::StdoutStream(event::Loop *wp_loop) :
4953
buff_fd_(wp_loop)
54+
{ }
55+
56+
bool StdoutStream::initialize()
5057
{
5158
buff_fd_.initialize(STDOUT_FILENO, BufferedFd::kWriteOnly);
59+
return true;
5260
}
5361

5462
void StdoutStream::setSendCompleteCallback(const SendCompleteCallback &cb)
@@ -74,19 +82,23 @@ bool StdoutStream::disable()
7482
StdioStream::StdioStream(event::Loop *wp_loop) :
7583
in_buff_fd_(wp_loop),
7684
out_buff_fd_(wp_loop)
85+
{ }
86+
87+
bool StdioStream::initialize()
7788
{
7889
in_buff_fd_.initialize(STDIN_FILENO, BufferedFd::kReadOnly);
7990
out_buff_fd_.initialize(STDOUT_FILENO, BufferedFd::kWriteOnly);
91+
return true;
8092
}
8193

8294
void StdioStream::setReceiveCallback(const ReceiveCallback &cb, size_t threshold)
8395
{
84-
out_buff_fd_.setReceiveCallback(cb, threshold);
96+
in_buff_fd_.setReceiveCallback(cb, threshold);
8597
}
8698

8799
void StdioStream::setSendCompleteCallback(const SendCompleteCallback &cb)
88100
{
89-
in_buff_fd_.setSendCompleteCallback(cb);
101+
out_buff_fd_.setSendCompleteCallback(cb);
90102
}
91103

92104
bool StdioStream::send(const void *data_ptr, size_t data_size)

modules/network/stdio_stream.h

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class StdinStream : public ByteStream {
4747
virtual void unbind() override { buff_fd_.unbind(); }
4848
virtual Buffer* getReceiveBuffer() override { return buff_fd_.getReceiveBuffer(); }
4949

50+
bool initialize();
51+
5052
bool enable();
5153
bool disable();
5254

@@ -69,6 +71,8 @@ class StdoutStream : public ByteStream {
6971
virtual void unbind() override { }
7072
virtual Buffer* getReceiveBuffer() override { return nullptr; }
7173

74+
bool initialize();
75+
7276
bool enable();
7377
bool disable();
7478

@@ -91,6 +95,8 @@ class StdioStream : public ByteStream {
9195
virtual void unbind() override { in_buff_fd_.unbind(); }
9296
virtual Buffer* getReceiveBuffer() override { return in_buff_fd_.getReceiveBuffer(); }
9397

98+
bool initialize();
99+
94100
bool enable();
95101
bool disable();
96102

modules/terminal/Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ HEAD_FILES = \
3434
terminal.h \
3535
service/telnetd.h \
3636
service/tcp_rpc.h \
37+
service/stdio.h \
3738

3839
CPP_SRC_FILES = \
3940
terminal.cpp \
4041
session.cpp \
4142
service/telnetd.cpp \
42-
helper.cpp \
4343
service/tcp_rpc.cpp \
44+
service/stdio.cpp \
45+
helper.cpp \
4446
impl/terminal.cpp \
4547
impl/terminal_key_events.cpp \
4648
impl/terminal_commands.cpp \
@@ -50,6 +52,7 @@ CPP_SRC_FILES = \
5052
impl/func_node.cpp \
5153
impl/service/telnetd.cpp \
5254
impl/service/tcp_rpc.cpp \
55+
impl/service/stdio.cpp \
5356

5457
CXXFLAGS := -DMODULE_ID='"tbox.terminal"' $(CXXFLAGS)
5558

0 commit comments

Comments
 (0)