Bredbandskollen CLI  1.2
Asynchronous network task engine
shortmessageconnection.h
1 // Copyright (c) 2019 Internetstiftelsen
2 // Written by Göran Andersson <initgoran@gmail.com>
3 
4 #pragma once
5 
6 #include "socketconnection.h"
7 
20 public:
22  ShortMessageConnection(const std::string &label, Task *owner,
23  const std::string &hostname, uint16_t port);
24 
26  ShortMessageConnection(const std::string &label, Task *owner, int fd,
27  const char *ip = "unknown", uint16_t port = 0);
28 
30  PollState connected() override;
31 
33  PollState readData(char *buf, size_t len) override;
34 
36  void sendMessage(const std::string &msg);
37 
38 private:
39  // Message we're currently receiving, or empty.
40  std::string msg;
41 
42  // Bytes left for the above message to be complete,
43  // or 0 if we're not currently receiving a message
44  size_t bytes_left = 0;
45 
46  bool reading_header = true;
47 };
std::string label() const
Return the object's log label.
Definition: logger.h:251
Simple protocol for exchanging messages.
Definition: shortmessageconnection.h:19
void sendMessage(const std::string &msg)
Send message to peer.
Definition: shortmessageconnection.cpp:65
ShortMessageConnection(const std::string &label, Task *owner, const std::string &hostname, uint16_t port)
For client sockets, connecting to a server.
Definition: shortmessageconnection.cpp:8
PollState connected() override
Will notify owner task that a new connection is available.
Definition: shortmessageconnection.cpp:20
PollState readData(char *buf, size_t len) override
Will notify owner when a complete message has been retrieved.
Definition: shortmessageconnection.cpp:24
This class implements low-level socket connection operations. Inherit from it to implement protocols ...
Definition: socketconnection.h:47
Task * owner() const
Return task owning the socket.
Definition: socket.h:27
std::string hostname() const
Return name of the host to which the socket is supposed to connect.
Definition: socket.h:32
uint16_t port() const
Return port number to which the socket is supposed to connect.
Definition: socket.h:37
The purpose of a task is to manage socket connections, and/or to execute timers.
Definition: task.h:39
PollState
Definition: pollstate.h:11