-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathecho_stdio_transport_advanced.cc
More file actions
203 lines (166 loc) · 4.6 KB
/
echo_stdio_transport_advanced.cc
File metadata and controls
203 lines (166 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* @file echo_stdio_transport_advanced.cc
* @brief Stdio transport implementation for echo client/server
*/
#include "mcp/echo/echo_stdio_transport_advanced.h"
#include <cstring>
#include <errno.h>
#include <iostream>
#ifdef _WIN32
#include <io.h>
#include <winsock2.h>
typedef SSIZE_T ssize_t;
#define read(fd, buf, len) _read(fd, buf, static_cast<unsigned int>(len))
#define write(fd, buf, len) _write(fd, buf, static_cast<unsigned int>(len))
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
#ifndef EAGAIN
#define EAGAIN WSAEWOULDBLOCK
#endif
#ifndef EWOULDBLOCK
#define EWOULDBLOCK WSAEWOULDBLOCK
#endif
#else
#include <fcntl.h>
#include <unistd.h>
#endif
namespace mcp {
namespace echo {
StdioEchoTransport::StdioEchoTransport() {
stdin_fd_ = STDIN_FILENO;
stdout_fd_ = STDOUT_FILENO;
}
StdioEchoTransport::~StdioEchoTransport() { close(); }
variant<Success, Error> StdioEchoTransport::initialize() {
if (initialized_) {
return Error(-1, "Transport already initialized");
}
// Set stdin to non-blocking mode for reading
setNonBlocking(stdin_fd_);
initialized_ = true;
return Success{};
}
variant<Success, Error> StdioEchoTransport::connect(
const std::string& endpoint) {
if (!initialized_) {
return Error(-1, "Transport not initialized");
}
if (running_) {
return Error(-1, "Already connected");
}
running_ = true;
status_ = Status::Connected;
// Start read thread
read_thread_ = std::thread(&StdioEchoTransport::readThread, this);
// Notify connection
if (callbacks_.onStatusChange) {
callbacks_.onStatusChange(Status::Connected);
}
return Success{};
}
variant<Success, Error> StdioEchoTransport::listen(
const std::string& endpoint) {
// For stdio, listen is the same as connect
return connect(endpoint);
}
variant<Success, Error> StdioEchoTransport::send(const std::string& data) {
if (!running_) {
return Error(-1, "Not connected");
}
std::lock_guard<std::mutex> lock(write_mutex_);
size_t total_written = 0;
while (total_written < data.length()) {
ssize_t written = write(stdout_fd_, data.c_str() + total_written,
data.length() - total_written);
if (written < 0) {
if (errno == EINTR) {
continue; // Interrupted, retry
}
return Error(errno, std::string("Write failed: ") + strerror(errno));
}
total_written += written;
}
// Flush stdout to ensure immediate delivery
if (fflush(stdout) != 0) {
return Error(errno, std::string("Flush failed: ") + strerror(errno));
}
return Success{};
}
void StdioEchoTransport::close() {
if (!running_) {
return;
}
running_ = false;
if (read_thread_.joinable()) {
read_thread_.join();
}
status_ = Status::Disconnected;
// Notify disconnection
if (callbacks_.onStatusChange) {
callbacks_.onStatusChange(Status::Disconnected);
}
}
EchoTransportAdvanced::Status StdioEchoTransport::getStatus() const {
return status_;
}
void StdioEchoTransport::setCallbacks(const Callbacks& callbacks) {
callbacks_ = callbacks;
}
void StdioEchoTransport::readThread() {
char buffer[4096];
std::string partial_data;
while (running_) {
ssize_t bytes_read = read(stdin_fd_, buffer, sizeof(buffer));
if (bytes_read > 0) {
std::string data(buffer, bytes_read);
// Notify data received
if (callbacks_.onDataReceived) {
callbacks_.onDataReceived(data);
}
} else if (bytes_read == 0) {
// EOF reached
break;
} else {
// Error or would block
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// Non-blocking read, no data available
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
} else if (errno == EINTR) {
// Interrupted, retry
continue;
} else {
// Real error
if (callbacks_.onError) {
callbacks_.onError(
Error(errno, std::string("Read failed: ") + strerror(errno)));
}
break;
}
}
}
// Connection closed
status_ = Status::Disconnected;
if (callbacks_.onStatusChange) {
callbacks_.onStatusChange(Status::Disconnected);
}
}
void StdioEchoTransport::setNonBlocking(int fd) {
#ifdef _WIN32
// On Windows, stdin/stdout don't support ioctlsocket
// Non-blocking mode for console I/O requires different approach
// For now, we rely on polling with timeout
(void)fd; // Suppress unused parameter warning
#else
int flags = fcntl(fd, F_GETFL, 0);
if (flags != -1) {
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
#endif
}
} // namespace echo
} // namespace mcp