working on client gui and fixed some issues with the tcp.connect functions

main
Steffen Pohle 2 years ago
parent 8ca01af2d1
commit 42109edda7

@ -118,31 +118,97 @@ void NetworkThread::ClientSendStatustext(char *txt) {
}
#define BUFFER_SIZE 0x10000
void NetworkThread::Thread() {
int i;
long int len;
TCP tcp;
char buffer[BUFFER_SIZE];
printf ("%s:%d NetworkThread::Thread Started\n", __FILE__, __LINE__);
TCP tcp;
ClientSendStatustext((char*)"connecting");
Lock();
req_fc = 0;
UnLock();
//
// connect to ip address
//
SetState (NWT_connect);
if (tcp.Connect(host, port) != 1) {
i = tcp.Connect(host, port);
if (i < 0) {
//
// error on connect, stop thread.
ClientSendStatustext(strerror(errno));
SetState(NWT_nothing);
ClientSendStatustext(NULL);
printf ("%s:%d NetworkThread::Thread Finished on connect\n", __FILE__, __LINE__);
return;
}
else {
if (i == 1) ClientSendStatustext((char*)"connected");
// FIXME: else: still connecting need some timeout and checks.
SetState(NWT_running);
//
// connection established
// go into the main loop
while (GetState() == NWT_running) {
//
// destination host and port should be set already
SetState(NWT_running);
while (GetState() == NWT_running) {
ClientSendStatustext((char*)"connected");
usleep (1000);
// check for new data
i = tcp.IsData(1000);
if (i < 0) {
SetState(NWT_error);
break;
}
if(GetState() == NWT_close) {
ClientSendStatustext((char*)"");
else if (i == 0) continue;
//
// we got some data
printf ("read data\n");
len = tcp.Read(buffer, BUFFER_SIZE);
Lock();
if (len < 0) state = NWT_close;
else {
//
// we could get some data, if busy reset connection
if (req_fc == 0) {
// got data without request? - Protokol error
errno = EPROTO;
state = NWT_error;
break;
}
//
// return data
}
UnLock();
}
//
// main loop stopped, clean up
i = GetState();
switch (i) {
case NWT_error:
ClientSendStatustext(strerror(errno));
tcp.Close();
break;
case NWT_close:
ClientSendStatustext((char*)"Connection Closed");
tcp.Close();
break;
default:
ClientSendStatustext((char*)"Unknown Error");
tcp.Close();
break;
}
SetState(NWT_nothing);
ClientSendStatustext(NULL);
printf ("%s:%d NetworkThread::Thread Finished\n", __FILE__, __LINE__);
};

@ -11,11 +11,13 @@
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <glib.h>
#include <sys/time.h>
enum {
NWT_nothing,
NWT_connect,
NWT_running,
NWT_error,
NWT_close
};
@ -25,6 +27,13 @@ private:
GMutex mutex;
GThread *thread;
int state;
int req_fc; // 0 .. is no request
int req_offset;
int req_size;
int req_timeout; // timeout[ms]
struct timeval req_time;
std::string host, port;
void SetState(int s);
@ -41,6 +50,7 @@ public:
int Disconnect();
int GetState();
int SendRequest();
void Thread();
};

@ -218,6 +218,9 @@ int TCP::Connect(string hostport, int defaultport) {
};
//
// try to connect.
// return: -1 on error, 0 on in progress, 1 on connected
int TCP::Connect() {
int err, s;
struct addrinfo hints, *res, *rp;
@ -232,7 +235,7 @@ int TCP::Connect() {
err = getaddrinfo(remote_host.c_str(), remote_port.c_str(), &hints, &res);
if (err != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
return 0;
return -1;
}
//
@ -249,8 +252,16 @@ int TCP::Connect() {
if (s == -1) continue;
if (connect(s, rp->ai_addr, rp->ai_addrlen) != -1) {
sock = s;
err = 1; // set return value
break;
}
if (errno == EINPROGRESS) {
// socket set up, but in NONBLOCKED mode
sock = s;
err = 0; // set return value
break;
}
close(s);
}
@ -259,12 +270,12 @@ int TCP::Connect() {
//
// connect not successfull
//
if (rp == NULL) return 0;
writecnt = 0;
readcnt = 0;
return 1;
if (rp == NULL) return -1;
return err;
};
@ -290,7 +301,9 @@ long int TCP::Read(char *buffer, long int len) {
len_ = -2;
}
else if (len_ == 0) len_ = -1;
if (len_ < 0) Close();
if (len_ < 0) {
Close();
}
readcnt += len_;
@ -375,6 +388,11 @@ int TCP::IsConnected() {
};
/*
* is there any data or changed on the socket?
* Parmeter: timeout[ms]
* Result: -1 on error, 0 no data, 1 new data
*/
int TCP::IsData(int timeout) {
fd_set sockset;
struct timeval tval;

@ -14,6 +14,7 @@
<property name="margin-top">4</property>
<property name="margin-bottom">4</property>
<property name="orientation">vertical</property>
<property name="spacing">4</property>
<child>
<object class="GtkFrame">
<property name="visible">True</property>
@ -116,7 +117,9 @@
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Refresh [ms]:</property>
<property name="label" translatable="yes">Refresh [ms]:
(&lt; 100 = On Demand)</property>
<property name="justify">right</property>
</object>
<packing>
<property name="expand">False</property>
@ -309,10 +312,287 @@
</packing>
</child>
<child>
<placeholder/>
<object class="GtkFrame">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-start">4</property>
<property name="margin-end">4</property>
<property name="label-xalign">0</property>
<child>
<object class="GtkAlignment">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="left-padding">12</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="spacing">8</property>
<child>
<object class="GtkCheckButton" id="cli_Update">
<property name="label" translatable="yes">Update / Read</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">False</property>
<property name="draw-indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="cli_FC4">
<property name="label" translatable="yes">FC4</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">False</property>
<property name="active">True</property>
<property name="draw-indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack-type">end</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="cli_FC3">
<property name="label" translatable="yes">FC3</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">False</property>
<property name="active">True</property>
<property name="draw-indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack-type">end</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="cli_FC2">
<property name="label" translatable="yes">FC2</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">False</property>
<property name="active">True</property>
<property name="draw-indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack-type">end</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="cli_FC1">
<property name="label" translatable="yes">FC1</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">False</property>
<property name="active">True</property>
<property name="draw-indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack-type">end</property>
<property name="position">4</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">4</property>
<property name="position">0</property>
</packing>
</child>
<child>
<!-- n-columns=4 n-rows=2 -->
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="valign">start</property>
<property name="margin-start">16</property>
<property name="row-spacing">4</property>
<property name="column-spacing">4</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Register / Flag:</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Numbers:</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
<object class="GtkEntry">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="max-length">5</property>
<property name="width-chars">5</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkEntry">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="max-length">5</property>
<property name="width-chars">5</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="cli_dontuse_FC1516">
<property name="label" translatable="yes">Dont Use FC15/16
(will only request one value)</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">False</property>
<property name="draw-indicator">True</property>
</object>
<packing>
<property name="left-attach">3</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="padding">4</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Request</property>
<attributes>
<attribute name="weight" value="bold"/>
<attribute name="scale" value="1"/>
</attributes>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<placeholder/>
<object class="GtkFrame">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-start">4</property>
<property name="margin-end">4</property>
<property name="label-xalign">0</property>
<child>
<object class="GtkAlignment">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="left-padding">12</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="shadow-type">in</property>
<child>
<object class="GtkTextView" id="cli_RawResult">
<property name="visible">True</property>
<property name="can-focus">True</property>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Result</property>
<attributes>
<attribute name="weight" value="bold"/>
<attribute name="scale" value="1"/>
</attributes>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>

Loading…
Cancel
Save