Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
modern_usage.cpp File Reference

Example of using network_system with modern API and Result<T> pattern. More...

#include <iostream>
#include <memory>
#include <thread>
#include <chrono>
#include <vector>
#include <future>
#include "kcenon/network/network_system.h"
Include dependency graph for modern_usage.cpp:

Go to the source code of this file.

Classes

class  ModernServer
 Modern server using new API. More...
 
class  ModernClient
 Modern client using new API. More...
 

Functions

void demonstrate_advanced_features ()
 Demonstrate advanced features.
 
int main (int argc, char *argv[])
 Main function demonstrating modern usage.
 

Detailed Description

Example of using network_system with modern API and Result<T> pattern.

This example demonstrates:

  • Modern network_system API with Result<T> error handling
  • Type-safe error checking with VoidResult return types
  • Integration with thread pool and container systems
  • Proper error handling and recovery patterns

Note: All primary APIs (start_server, start_client, send_packet, etc.) now return Result<T> for type-safe error handling instead of throwing exceptions or using callback-only error reporting.

Author
kcenon
Date
2025-09-20 @updated 2025-10-10 - Added Result<T> pattern support

Definition in file modern_usage.cpp.

Function Documentation

◆ demonstrate_advanced_features()

void demonstrate_advanced_features ( )

Demonstrate advanced features.

Definition at line 224 of file modern_usage.cpp.

224 {
225 std::cout << "\n=== Advanced Features Demo ===" << std::endl;
226
227 // Custom container with serialization
228 auto custom_container = std::make_shared<integration::basic_container>();
229
230 // Set custom serializer for complex types
231 custom_container->set_serializer([](const std::any& data) {
232 std::vector<uint8_t> result;
233 if (data.type() == typeid(std::vector<int>)) {
234 auto vec = std::any_cast<std::vector<int>>(data);
235 result.reserve(vec.size() * sizeof(int));
236 for (int val : vec) {
237 auto bytes = reinterpret_cast<const uint8_t*>(&val);
238 result.insert(result.end(), bytes, bytes + sizeof(int));
239 }
240 }
241 return result;
242 });
243
244 // Register custom container
246 "custom_vector_serializer", custom_container
247 );
248
249 // Test custom serialization
250 std::vector<int> test_data = {1, 2, 3, 4, 5};
251 auto serialized = custom_container->serialize(std::any(test_data));
252 std::cout << "Custom serialized " << test_data.size()
253 << " integers to " << serialized.size() << " bytes" << std::endl;
254
255 // Thread pool advanced usage
257 std::vector<std::future<void>> tasks;
258
259 // Submit multiple delayed tasks
260 for (int i = 1; i <= 3; ++i) {
261 auto future = thread_mgr.submit_delayed_task(
262 [i]() {
263 std::cout << "[Delayed Task " << i << "] Executed after delay" << std::endl;
264 },
265 std::chrono::milliseconds(i * 100)
266 );
267 tasks.push_back(std::move(future));
268 }
269
270 // Wait for all tasks
271 for (auto& future : tasks) {
272 future.wait();
273 }
274
275 std::cout << "All advanced features demonstrated successfully" << std::endl;
276}
void register_container(const std::string &name, std::shared_ptr< container_interface > container)
Register a container implementation.
static container_manager & instance()
Get the singleton instance.
static thread_integration_manager & instance()
Get the singleton instance.

References kcenon::network::integration::container_manager::instance(), kcenon::network::integration::thread_integration_manager::instance(), and kcenon::network::integration::container_manager::register_container().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int argc,
char * argv[] )

Main function demonstrating modern usage.

Definition at line 281 of file modern_usage.cpp.

281 {
282 std::cout << "=== Modern Network System Usage Demo ===" << std::endl;
283 std::cout << "Demonstrating the new API with all integration features" << std::endl;
284
285 // Initialize using modern API
286 kcenon::network::compat::initialize();
287 std::cout << "\n✓ Network system initialized" << std::endl;
288
289 try {
290 // Create modern server
291 ModernServer server("modern_server_001");
292 server.enable_async_processing();
293
294 if (!server.start(9090)) {
295 std::cerr << "Failed to start server, aborting demo" << std::endl;
296 return 1;
297 }
298
299 // Allow server to start
300 std::this_thread::sleep_for(500ms);
301
302 // Create modern client
303 ModernClient client("modern_client_001");
304
305 if (!client.connect("127.0.0.1", 9090)) {
306 std::cerr << "Failed to connect client, stopping server" << std::endl;
307 server.stop();
308 return 1;
309 }
310
311 // Send batch messages
312 std::vector<std::string> batch = {
313 "Modern message 1",
314 "Modern message 2",
315 "Modern message 3"
316 };
317 size_t sent = client.send_batch(batch);
318
319 if (sent < batch.size()) {
320 std::cout << "[Warning] Not all messages were sent successfully" << std::endl;
321 }
322
323 // Send async messages
324 for (int i = 1; i <= 3; ++i) {
325 client.send_async("Async message " + std::to_string(i));
326 }
327
328 // Wait for async operations
329 std::this_thread::sleep_for(500ms);
330
331 // Show server statistics
332 server.show_statistics();
333
334 // Disconnect
335 if (!client.disconnect()) {
336 std::cout << "[Warning] Client disconnection had issues" << std::endl;
337 }
338
339 // Demonstrate advanced features
341
342 // Stop server
343 if (!server.stop()) {
344 std::cout << "[Warning] Server shutdown had issues" << std::endl;
345 }
346
347 } catch (const std::exception& e) {
348 std::cerr << "Error: " << e.what() << std::endl;
349 return 1;
350 }
351
352 // Shutdown
353 kcenon::network::compat::shutdown();
354 std::cout << "\n✓ Network system shutdown complete" << std::endl;
355
356 std::cout << "\n=== Modern Usage Demo Complete ===" << std::endl;
357 std::cout << "All modern features working perfectly!" << std::endl;
358
359 return 0;
360}
Modern client using new API.
Modern server using new API.
void demonstrate_advanced_features()
Demonstrate advanced features.
@ server
Server-side handling of a request.

References demonstrate_advanced_features().

Here is the call graph for this function: