UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
control_port.cpp
Go to the documentation of this file.
1#include <stdio.h>
2#include <iostream>
3#include <QHostAddress>
4
5#include "control_port.hpp"
6
8 QObject::connect(&socket, &QTcpSocket::readyRead, this, &ControlPort::readReady);
9
10 QObject::connect(&socket, &QTcpSocket::connected, this, &ControlPort::connected);
11 QObject::connect(&socket, &QTcpSocket::disconnected, this, &ControlPort::disconnected);
12}
13
14void ControlPort::connect(int local_port){
15 /* Prevents failure in case the socket is alredy connecting. This also
16 * discards pending data in the send buffer, but we don't care about
17 * that.*/
18 socket.abort();
19
20 socket.connectToHost(QHostAddress::LocalHost, local_port);
21}
22
23void ControlPort::parseLine(std::string_view line){
24 constexpr std::string_view prefix = "Control socket listening on port";
25 if(line.substr(0, prefix.length()) != prefix)
26 return;
27
28 auto tmp = std::string(line.substr(prefix.length()));
29 int port = 0;
30 sscanf(tmp.c_str(), " %d", &port);
31
32 connect(port);
33}
34
35QAbstractSocket::SocketState ControlPort::getState() const{
36 return socket.state();
37}
38
39void ControlPort::writeLine(std::string_view line){
40 int toWrite = line.length();
41 const char *data = line.data();
42 while(toWrite > 0){
43 auto ret = socket.write(data, toWrite);
44 if(ret < 0){
45 break;
46 }
47 data += ret;
48 toWrite -= ret;
49 }
50}
51
53 char buf[512];
54
55 while(true){
56 int ret = socket.read(buf, sizeof(buf));
57 if(ret == 0 || ret == -1)
58 break;
59
60 lineBuf.write(std::string_view(buf, ret));
61
62 while(lineBuf.hasLine()){
63 for(auto& c : lineCallbacks){
64 c(lineBuf.peek());
65 }
66 lineBuf.pop();
67 }
68 }
69}
70
void connected()
QTcpSocket socket
LineBuffer lineBuf
QAbstractSocket::SocketState getState() const
void writeLine(std::string_view line)
void disconnected()
void connect(int local_port)
std::vector< LineCallback > lineCallbacks
void parseLine(std::string_view line)
bool hasLine() const
void write(std::string_view str)
std::string_view peek() const