MCPcopy Create free account
hub / github.com/Pagghiu/SaneCppLibraries / lineServer

Function lineServer

Examples/AwaitLineProtocol/AwaitLineProtocol.cpp:49–79  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

47}
48
49static AwaitTask lineServer(AwaitEventLoop& await, const SocketDescriptor& serverSocket, SocketDescriptor& accepted)
50{
51 char requestBuffer[64] = {};
52 char ackBuffer[32] = {};
53 AwaitSocketReceiveLineResult request;
54 AwaitSocketReceiveLineResult ack;
55
56 constexpr char expectedRequest[] = "READ temperature";
57 constexpr char valueReply[] = "VALUE temperature=23.5C\r\n";
58 constexpr char expectedAck[] = "ACK temperature";
59 constexpr char doneReply[] = "DONE\r\n";
60
61 SC_CO_TRY(co_await await.accept(serverSocket, accepted));
62 SC_CO_TRY(co_await await.receiveLine(accepted, requestBuffer, request));
63 if (not request.lineComplete or request.disconnected or
64 not equalsAscii(request.line, {expectedRequest, sizeof(expectedRequest) - 1}))
65 {
66 co_return Result::Error("AwaitLineProtocol server received unexpected request");
67 }
68
69 SC_CO_TRY(co_await await.sendAll(accepted, {valueReply, sizeof(valueReply) - 1}));
70 SC_CO_TRY(co_await await.receiveLine(accepted, ackBuffer, ack));
71 if (not ack.lineComplete or ack.disconnected or not equalsAscii(ack.line, {expectedAck, sizeof(expectedAck) - 1}))
72 {
73 co_return Result::Error("AwaitLineProtocol server received unexpected ack");
74 }
75
76 SC_CO_TRY(co_await await.sendAll(accepted, {doneReply, sizeof(doneReply) - 1}));
77
78 co_return Result(true);
79}
80
81static AwaitTask lineClient(AwaitEventLoop& await, const SocketDescriptor& client, SocketIPAddress address,
82 Span<char> valueBuffer, AwaitSocketReceiveLineResult& valueLine, Span<char> doneBuffer,

Callers 1

lineProtocolConversationFunction · 0.85

Calls 6

equalsAsciiFunction · 0.85
receiveLineMethod · 0.80
sendAllMethod · 0.80
ErrorEnum · 0.50
ResultClass · 0.50
acceptMethod · 0.45

Tested by

no test coverage detected