-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from sultanko/cpp_client
cpp client (tested on Linux with C++11)
- Loading branch information
Showing
2 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
#include "Debug.h" | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <netdb.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <string.h> | ||
|
||
std::string Debug::DEFAULT_HOST = "127.0.0.1"; | ||
std::string Debug::DEFAULT_PORT = "13579"; | ||
const int Debug::BUF_SIZE = 1024; | ||
|
||
Debug::Debug() | ||
: sfd(-1) | ||
{ | ||
struct addrinfo hints; | ||
struct addrinfo *result, *rp; | ||
int s; | ||
|
||
/* Obtain address(es) matching host/port */ | ||
|
||
memset(&hints, 0, sizeof(struct addrinfo)); | ||
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ | ||
hints.ai_socktype = SOCK_STREAM; /* Datagram socket */ | ||
hints.ai_flags = 0; | ||
hints.ai_protocol = 0; /* Any protocol */ | ||
|
||
s = getaddrinfo(DEFAULT_HOST.c_str(), DEFAULT_PORT.c_str(), &hints, &result); | ||
if (s != 0) | ||
{ | ||
fprintf(stderr, "Could not get address"); | ||
return; | ||
} | ||
|
||
for (rp = result; rp != NULL; rp = rp->ai_next) { | ||
sfd = socket(rp->ai_family, rp->ai_socktype, | ||
rp->ai_protocol); | ||
if (sfd == -1) | ||
continue; | ||
|
||
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) | ||
break; /* Success */ | ||
|
||
close(sfd); | ||
} | ||
|
||
if (rp == NULL) { /* No address succeeded */ | ||
fprintf(stderr, "Could not connect\n"); | ||
sfd = -1; | ||
return; | ||
} | ||
freeaddrinfo(result); | ||
} | ||
|
||
void Debug::sendCommand(const char* str) | ||
{ | ||
if (sfd == -1) | ||
return; | ||
int len = strlen(str); | ||
int pos = 0; | ||
while (pos < len) | ||
{ | ||
ssize_t res = write(sfd, str + pos, len); | ||
if (res == -1) | ||
{ | ||
fprintf(stderr, "Couldn't send command"); | ||
return; | ||
} | ||
pos += res; | ||
} | ||
} | ||
|
||
void Debug::beginPre() | ||
{ | ||
sendCommand("begin pre\n"); | ||
} | ||
|
||
void Debug::endPre() | ||
{ | ||
sendCommand("end pre\n"); | ||
} | ||
|
||
void Debug::beginPost() | ||
{ | ||
sendCommand("begin post\n"); | ||
} | ||
|
||
void Debug::endPost() | ||
{ | ||
sendCommand("end post\n"); | ||
} | ||
|
||
void Debug::writeWithColor(char* buf, int32_t color) | ||
{ | ||
size_t len = strlen(buf); | ||
float r = ((color & 0xFF0000) >> 16) / 256.0; | ||
float g = ((color & 0x00FF00) >> 8) / 256.0; | ||
float b = ((color & 0x0000FF)) / 256.0; | ||
sprintf(buf + len, " %f %f %f\n", r, g, b); | ||
sendCommand(buf); | ||
} | ||
|
||
void Debug::circle(double x, double y, double r, int32_t color) | ||
{ | ||
char buf[BUF_SIZE]; | ||
sprintf(buf, "circle %lf %lf %lf", x, y, r); | ||
writeWithColor(buf, color); | ||
} | ||
|
||
void Debug::fillCircle(double x, double y, double r, int32_t color) | ||
{ | ||
char buf[BUF_SIZE]; | ||
sprintf(buf, "fill_circle %lf %lf %lf", x, y, r); | ||
writeWithColor(buf, color); | ||
} | ||
|
||
void Debug::rect(double x1, double y1, double x2, double y2, int32_t color) | ||
{ | ||
char buf[BUF_SIZE]; | ||
sprintf(buf, "rect %lf %lf %lf %lf", x1, y1, x2, y2); | ||
writeWithColor(buf, color); | ||
} | ||
|
||
void Debug::fillRect(double x1, double y1, double x2, double y2, int32_t color) | ||
{ | ||
char buf[BUF_SIZE]; | ||
sprintf(buf, "fill_rect %lf %lf %lf %lf", x1, y1, x2, y2); | ||
writeWithColor(buf, color); | ||
} | ||
|
||
void Debug::line(double x1, double y1, double x2, double y2, int32_t color) | ||
{ | ||
char buf[BUF_SIZE]; | ||
sprintf(buf, "line %lf %lf %lf %lf", x1, y1, x2, y2); | ||
writeWithColor(buf, color); | ||
} | ||
|
||
void Debug::text(double x, double y, const char* text, int32_t color) | ||
{ | ||
char buf[BUF_SIZE]; | ||
sprintf(buf, "text %lf %lf %s", x, y, text); | ||
writeWithColor(buf, color); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef VISUAL_DEBUG_CLIENT_ | ||
#define VISUAL_DEBUG_CLIENT_ | ||
|
||
#include <cstdlib> | ||
#include <cstdio> | ||
#include <string> | ||
|
||
class Debug | ||
{ | ||
public: | ||
static std::string DEFAULT_HOST; | ||
static std::string DEFAULT_PORT; | ||
static const int BUF_SIZE; | ||
|
||
Debug(); | ||
void beginPre(); | ||
void endPre(); | ||
void beginPost(); | ||
void endPost(); | ||
void circle(double x, double y, double r, int32_t color = 0xFF0000); | ||
void fillCircle(double x, double y, double r, int32_t color = 0xFF0000); | ||
void rect(double x1, double y1, double x2, double y2, int32_t color = 0x00FF00); | ||
void fillRect(double x1, double y1, double x2, double y2, int32_t color = 0x00FF00); | ||
void line(double x1, double y1, double x2, double y2, int32_t color = 0x0000FF); | ||
void text(double x, double y, const char* text, int32_t color = 0x000000); | ||
|
||
private: | ||
void sendCommand(const char* str); | ||
void writeWithColor(char* buf, int32_t color); | ||
|
||
int sfd; | ||
}; | ||
|
||
#endif |