-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsse.cpp
More file actions
205 lines (173 loc) · 6.28 KB
/
sse.cpp
File metadata and controls
205 lines (173 loc) · 6.28 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
204
205
#include "fastmcpp/server/sse_server.hpp"
#include "fastmcpp/util/json.hpp"
#include <atomic>
#include <chrono>
#include <httplib.h>
#include <iostream>
#include <thread>
using fastmcpp::Json;
using fastmcpp::server::SseServerWrapper;
int main()
{
// Create a simple echo handler
auto handler = [](const Json& request) -> Json
{
Json response;
response["jsonrpc"] = "2.0";
if (request.contains("id"))
response["id"] = request["id"];
if (request.contains("method"))
{
std::string method = request["method"];
if (method == "echo")
response["result"] = request.value("params", Json::object());
else
response["error"] = Json{{"code", -32601}, {"message", "Method not found"}};
}
return response;
};
// Start SSE server
int port = 18106; // Unique port
SseServerWrapper server(handler, "127.0.0.1", port, "/sse", "/messages");
if (!server.start())
{
std::cerr << "Failed to start SSE server\n";
return 1;
}
// Wait for server to be ready - longer delay for macOS compatibility
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
std::cout << "Server started on port " << port << "\n";
// Verify server is running
if (!server.running())
{
std::cerr << "Server not running after start\n";
return 1;
}
// Create HTTP clients: one dedicated to SSE stream, one for POST requests
httplib::Client sse_client("127.0.0.1", port);
sse_client.set_read_timeout(std::chrono::seconds(20));
sse_client.set_connection_timeout(std::chrono::seconds(10));
std::atomic<bool> sse_connected{false};
std::atomic<int> events_received{0};
Json received_event;
std::mutex event_mutex;
// Start SSE connection in background thread (retry a few times for robustness)
std::thread sse_thread(
[&]()
{
// Give server a moment to fully initialize before first connection attempt
std::this_thread::sleep_for(std::chrono::milliseconds(100));
auto sse_receiver = [&](const char* data, size_t len)
{
sse_connected = true;
std::string chunk(data, len);
// Parse SSE format: "data: <json>\n\n"
if (chunk.find("data: ") == 0)
{
size_t start = 6; // After "data: "
size_t end = chunk.find("\n\n");
if (end != std::string::npos)
{
std::string json_str = chunk.substr(start, end - start);
try
{
Json event = Json::parse(json_str);
{
std::lock_guard<std::mutex> lock(event_mutex);
received_event = event;
events_received++;
}
}
catch (...)
{
std::cerr << "Failed to parse SSE event: " << json_str << "\n";
}
}
}
return true; // Continue receiving
};
// Retry loop to establish SSE connection in flaky environments
for (int attempt = 0; attempt < 20 && !sse_connected; ++attempt)
{
auto res = sse_client.Get("/sse", sse_receiver);
if (!res)
{
std::cerr << "SSE GET request failed: " << res.error() << " (attempt "
<< (attempt + 1) << ")\n";
std::this_thread::sleep_for(std::chrono::milliseconds(200));
continue;
}
if (res->status != 200)
{
std::cerr << "SSE GET returned status: " << res->status << " (attempt "
<< (attempt + 1) << ")\n";
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
}
});
// Wait for SSE connection to establish (allow up to 5 seconds)
for (int i = 0; i < 500 && !sse_connected; ++i)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if (!sse_connected)
{
std::cerr << "SSE connection failed to establish\n";
server.stop();
if (sse_thread.joinable())
sse_thread.detach();
return 1;
}
// Send a message via POST
Json request;
request["jsonrpc"] = "2.0";
request["id"] = 1;
request["method"] = "echo";
request["params"] = Json{{"message", "Hello SSE"}};
httplib::Client post_client("127.0.0.1", port);
post_client.set_connection_timeout(std::chrono::seconds(10));
post_client.set_read_timeout(std::chrono::seconds(10));
auto post_res = post_client.Post("/messages", request.dump(), "application/json");
if (!post_res || post_res->status != 200)
{
std::cerr << "POST request failed\n";
server.stop();
if (sse_thread.joinable())
sse_thread.detach();
return 1;
}
// Wait for SSE event
for (int i = 0; i < 200 && events_received == 0; ++i)
std::this_thread::sleep_for(std::chrono::milliseconds(20));
// Stop server to close SSE connection
server.stop();
if (sse_thread.joinable())
sse_thread.join();
// Verify we received the event
if (events_received == 0)
{
std::cerr << "No events received via SSE\n";
return 1;
}
// Verify event content
{
std::lock_guard<std::mutex> lock(event_mutex);
if (!received_event.contains("result"))
{
std::cerr << "Event missing 'result' field\n";
return 1;
}
auto result = received_event["result"];
if (!result.contains("message"))
{
std::cerr << "Result missing 'message' field\n";
return 1;
}
std::string msg = result["message"];
if (msg != "Hello SSE")
{
std::cerr << "Unexpected message: " << msg << "\n";
return 1;
}
}
std::cout << "SSE server test passed\n";
return 0;
}