1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-27 04:32:55 +00:00

Merge read_tcp and read_udp as much as possible

This commit is contained in:
Nikolas Garofil 2010-04-17 02:31:37 +02:00
parent 19ad7d2cc3
commit 0047a132ab
4 changed files with 37 additions and 61 deletions

View File

@ -36,7 +36,7 @@ endif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/build.h)
set(conky_sources colours.cc combine.cc common.cc conky.cc core.cc set(conky_sources colours.cc combine.cc common.cc conky.cc core.cc
diskio.cc entropy.cc exec.cc fs.cc mail.cc mixer.cc net_stat.cc template.cc diskio.cc entropy.cc exec.cc fs.cc mail.cc mixer.cc net_stat.cc template.cc
mboxscan.cc read_tcp.cc scroll.cc specials.cc tailhead.cc mboxscan.cc read_tcpip.cc scroll.cc specials.cc tailhead.cc
temphelper.cc text_object.cc timeinfo.cc top.cc algebra.cc prioqueue.cc proc.cc temphelper.cc text_object.cc timeinfo.cc top.cc algebra.cc prioqueue.cc proc.cc
user.cc) user.cc)

View File

@ -64,7 +64,7 @@
#ifdef BUILD_NVIDIA #ifdef BUILD_NVIDIA
#include "nvidia.h" #include "nvidia.h"
#endif #endif
#include "read_tcp.h" #include "read_tcpip.h"
#include "scroll.h" #include "scroll.h"
#include "specials.h" #include "specials.h"
#include "temphelper.h" #include "temphelper.h"
@ -209,13 +209,13 @@ struct text_object *construct_text_object(char *s, const char *arg, long
} }
obj->callbacks.print = &print_freq_g; obj->callbacks.print = &print_freq_g;
END OBJ_ARG(read_tcp, 0, "read_tcp: Needs \"(host) port\" as argument(s)") END OBJ_ARG(read_tcp, 0, "read_tcp: Needs \"(host) port\" as argument(s)")
parse_read_tcp_arg(obj, arg, free_at_crash); parse_read_tcpip_arg(obj, arg, free_at_crash);
obj->callbacks.print = &print_read_tcp; obj->callbacks.print = &print_read_tcp;
obj->callbacks.free = &free_read_tcp; obj->callbacks.free = &free_read_tcpip;
END OBJ_ARG(read_udp, 0, "read_udp: Needs \"(host) port\" as argument(s)") END OBJ_ARG(read_udp, 0, "read_udp: Needs \"(host) port\" as argument(s)")
parse_read_tcp_arg(obj, arg, free_at_crash); parse_read_tcpip_arg(obj, arg, free_at_crash);
obj->callbacks.print = &print_read_udp; obj->callbacks.print = &print_read_udp;
obj->callbacks.free = &free_read_tcp; obj->callbacks.free = &free_read_tcpip;
#if defined(__linux__) #if defined(__linux__)
END OBJ(voltage_mv, 0) END OBJ(voltage_mv, 0)
get_cpu_count(); get_cpu_count();

View File

@ -37,17 +37,17 @@
#include <unistd.h> #include <unistd.h>
#include <netinet/in.h> #include <netinet/in.h>
struct read_tcp_data { struct read_tcpip_data {
char *host; char *host;
unsigned int port; unsigned int port;
}; };
void parse_read_tcp_arg(struct text_object *obj, const char *arg, void *free_at_crash) void parse_read_tcpip_arg(struct text_object *obj, const char *arg, void *free_at_crash)
{ {
struct read_tcp_data *rtd; struct read_tcpip_data *rtd;
rtd = (struct read_tcp_data *) malloc(sizeof(struct read_tcp_data)); rtd = (struct read_tcpip_data *) malloc(sizeof(struct read_tcpip_data));
memset(rtd, 0, sizeof(struct read_tcp_data)); memset(rtd, 0, sizeof(struct read_tcpip_data));
rtd->host = (char *) malloc(text_buffer_size); rtd->host = (char *) malloc(text_buffer_size);
sscanf(arg, "%s", rtd->host); sscanf(arg, "%s", rtd->host);
@ -63,24 +63,24 @@ void parse_read_tcp_arg(struct text_object *obj, const char *arg, void *free_at_
obj->data.opaque = rtd; obj->data.opaque = rtd;
} }
void print_read_tcp(struct text_object *obj, char *p, int p_max_size) void print_read_tcpip(struct text_object *obj, char *p, int p_max_size, int protocol)
{ {
int sock, received; int sock, received;
struct sockaddr_in addr; struct sockaddr_in addr;
struct hostent* he; struct hostent* he;
fd_set readfds; fd_set readfds;
struct timeval tv; struct timeval tv;
struct read_tcp_data *rtd = (struct read_tcp_data *) obj->data.opaque; struct read_tcpip_data *rtd = (struct read_tcpip_data *) obj->data.opaque;
if (!rtd) if (!rtd)
return; return;
if (!(he = gethostbyname(rtd->host))) { if (!(he = gethostbyname(rtd->host))) {
NORM_ERR("read_tcp: Problem with resolving the hostname"); NORM_ERR("%s: Problem with resolving the hostname", protocol == IPPROTO_TCP ? "read_tcp" : "read_udp");
return; return;
} }
if ((sock = socket(he->h_addrtype, SOCK_STREAM, IPPROTO_TCP)) == -1) { if ((sock = socket(he->h_addrtype, protocol == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM, protocol)) == -1) {
NORM_ERR("read_tcp: Couldn't create a socket"); NORM_ERR("%s: Couldn't create a socket", protocol == IPPROTO_TCP ? "read_tcp" : "read_udp");
return; return;
} }
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
@ -88,49 +88,15 @@ void print_read_tcp(struct text_object *obj, char *p, int p_max_size)
addr.sin_port = rtd->port; addr.sin_port = rtd->port;
memcpy(&addr.sin_addr, he->h_addr, he->h_length); memcpy(&addr.sin_addr, he->h_addr, he->h_length);
if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) != 0) { if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) != 0) {
if(protocol == IPPROTO_TCP) {
NORM_ERR("read_tcp: Couldn't create a connection"); NORM_ERR("read_tcp: Couldn't create a connection");
} else {
NORM_ERR("read_udp: Couldn't listen"); //other error because udp is connectionless
}
return; return;
} }
FD_ZERO(&readfds); if(protocol == IPPROTO_UDP)
FD_SET(sock, &readfds); write(sock, NULL, 0); //when using udp send a zero-length packet to let the other end know of our existence
tv.tv_sec = 1;
tv.tv_usec = 0;
if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
received = recv(sock, p, p_max_size, 0);
p[received] = 0;
}
close(sock);
}
void print_read_udp(struct text_object *obj, char *p, int p_max_size)
{
int sock, received;
struct sockaddr_in addr;
struct hostent* he;
fd_set readfds;
struct timeval tv;
struct read_tcp_data *rtd = (struct read_tcp_data *) obj->data.opaque;
if (!rtd)
return;
if (!(he = gethostbyname(rtd->host))) {
NORM_ERR("read_udp: Problem with resolving the hostname");
return;
}
if ((sock = socket(he->h_addrtype, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
NORM_ERR("read_udp: Couldn't create a socket");
return;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = rtd->port;
memcpy(&addr.sin_addr, he->h_addr, he->h_length);
if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) != 0) {
NORM_ERR("read_udp: Couldn't create a connection");
return;
}
write(sock, NULL, 0); //when using UDP i have to start writing to the socket before i can read from it, even if i don't actually write anything
FD_ZERO(&readfds); FD_ZERO(&readfds);
FD_SET(sock, &readfds); FD_SET(sock, &readfds);
tv.tv_sec = 1; tv.tv_sec = 1;
@ -142,9 +108,19 @@ void print_read_udp(struct text_object *obj, char *p, int p_max_size)
close(sock); close(sock);
} }
void free_read_tcp(struct text_object *obj) void print_read_tcp(struct text_object *obj, char *p, int p_max_size)
{ {
struct read_tcp_data *rtd = (struct read_tcp_data *) obj->data.opaque; print_read_tcpip(obj, p, p_max_size, IPPROTO_TCP);
}
void print_read_udp(struct text_object *obj, char *p, int p_max_size)
{
print_read_tcpip(obj, p, p_max_size, IPPROTO_UDP);
}
void free_read_tcpip(struct text_object *obj)
{
struct read_tcpip_data *rtd = (struct read_tcpip_data *) obj->data.opaque;
if (!rtd) if (!rtd)
return; return;

View File

@ -31,9 +31,9 @@
#ifndef _READ_TCP_H #ifndef _READ_TCP_H
#define _READ_TCP_H #define _READ_TCP_H
void parse_read_tcp_arg(struct text_object *, const char *, void *); void parse_read_tcpip_arg(struct text_object *, const char *, void *);
void print_read_tcp(struct text_object *, char *, int); void print_read_tcp(struct text_object *, char *, int);
void print_read_udp(struct text_object *, char *, int); void print_read_udp(struct text_object *, char *, int);
void free_read_tcp(struct text_object *); void free_read_tcpip(struct text_object *);
#endif /* _READ_TCP_H */ #endif /* _READ_TCP_H */