Client receives stuff

This commit is contained in:
Perttu Ahola 2014-09-18 00:22:37 +03:00
parent 89b02eeb19
commit 1b1b016cf5
7 changed files with 110 additions and 3 deletions

View File

@ -54,6 +54,7 @@ set(CLIENT_EXE_NAME buildat_client)
set(CLIENT_SRCS
src/client/main.cpp
src/client/state.cpp
src/core/log.cpp
src/impl/tcpsocket.cpp
)
add_executable(${CLIENT_EXE_NAME} ${CLIENT_SRCS})

View File

@ -5,6 +5,8 @@
#include "interface/mutex.h"
#include "network/include/api.h"
#include "core/log.h"
#include <cereal/archives/binary.hpp>
#include <cereal/types/string.hpp>
#include <iostream>
using interface::Event;
@ -129,14 +131,23 @@ struct Module: public interface::Module, public network::Interface
log_i(MODULE, "network::send()");
interface::MutexScope ms(m_interface_mutex);
// Grab socket
auto it = m_peers.find(recipient);
if(it == m_peers.end()){
throw Exception(ss_()+"network::send(): Peer "+itos(recipient) +
" doesn't exist");
}
Peer &peer = it->second;
// TODO: Create actual packet including type and length
peer.socket->send_fd(data);
// Create actual packet including type and length
std::ostringstream os(std::ios::binary);
{
cereal::BinaryOutputArchive ar(os);
ar(type, data);
}
// Send packet
peer.socket->send_fd(os.str());
}
void send(PeerInfo::Id recipient, const ss_ &name, const ss_ &data)

View File

@ -9,6 +9,9 @@
#include <OSBasics.h>
#pragma GCC diagnostic pop
#include <c55/getopt.h>
#include <core/log.h>
#include <signal.h>
#define MODULE "__main"
using Polycode::PolycodeView;
using Polycode::SDLCore;
@ -178,6 +181,9 @@ public:
HelloPolycodeApp(Polycode::PolycodeView *view);
~HelloPolycodeApp();
bool Update();
void Shutdown(){
core->Shutdown();
}
private:
Polycode::Core *core;
@ -286,8 +292,38 @@ bool HelloPolycodeApp::Update(){
return core->updateAndRender();
}
bool g_sigint_received = false;
void sigint_handler(int sig)
{
if(!g_sigint_received){
fprintf(stdout, "\n"); // Newline after "^C"
log_i("process", "SIGINT");
g_sigint_received = true;
} else {
(void)signal(SIGINT, SIG_DFL);
}
}
void signal_handler_init()
{
(void)signal(SIGINT, sigint_handler);
}
void basic_init()
{
signal_handler_init();
// Force '.' as decimal point
std::locale::global(std::locale(std::locale(""), "C", std::locale::numeric));
setlocale(LC_NUMERIC, "C");
log_set_max_level(LOG_VERBOSE);
}
int main(int argc, char *argv[])
{
basic_init();
client::Config &config = g_client_config;
const char opts[100] = "hs:p:P:";
@ -334,6 +370,10 @@ int main(int argc, char *argv[])
PolycodeView *view = new PolycodeView("Hello Polycode!");
HelloPolycodeApp *app = new HelloPolycodeApp(view);
while(app->Update());
while(app->Update()){
state->update();
if(g_sigint_received)
app->Shutdown();
}
return 0;
}

View File

@ -1,6 +1,8 @@
#include "client/state.h"
#include "interface/tcpsocket.h"
#include <iostream>
#include <cstring>
#include <sys/socket.h>
namespace client {
@ -28,6 +30,27 @@ struct CState: public State
{
return m_socket->send_fd(data);
}
void update()
{
if(m_socket->wait_data(0)){
read_socket();
}
}
void read_socket()
{
int fd = m_socket->fd();
char buf[1000];
ssize_t r = recv(fd, buf, 1000, 0);
if(r == -1)
throw Exception(ss_()+"Receive failed: "+strerror(errno));
if(r == 0){
std::cerr<<"Peer disconnected"<<std::endl;
return;
}
std::cerr<<"Received "<<r<<" bytes"<<std::endl;
}
};
State* createState()

View File

@ -3,6 +3,7 @@
namespace interface
{
struct TCPSocket;
}
namespace client
@ -12,6 +13,7 @@ namespace client
virtual ~State(){}
virtual bool connect(const ss_ &address, const ss_ &port) = 0;
virtual bool send(const ss_ &data) = 0;
virtual void update() = 0;
};
State* createState();

View File

@ -1,4 +1,5 @@
#include "interface/tcpsocket.h"
#include "core/log.h"
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
@ -267,6 +268,34 @@ struct CTCPSocket: public TCPSocket
}
return true;
}
bool wait_data(int timeout_us)
{
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = timeout_us;
fd_set rfds;
FD_ZERO(&rfds);
int fd_max = m_fd;
FD_SET(m_fd, &rfds);
int r = select(fd_max + 1, &rfds, NULL, NULL, &tv);
if(r == -1){
// Error
log_w("tcpsocket", "select() returned -1: %s", strerror(errno));
return false;
} else if(r == 0){
// Nothing happened
return false;
} else {
// Something happened
if(FD_ISSET(m_fd, &rfds)){
log_d("tcpsocket", "FD_ISSET: %i", m_fd);
return true;
}
}
return false;
}
ss_ get_local_address() const
{
if(m_fd == -1)

View File

@ -15,6 +15,7 @@ namespace interface
virtual bool bind_fd(const ss_ &address, const ss_ &port) = 0;
virtual bool accept_fd(const TCPSocket &listener) = 0;
virtual bool send_fd(const ss_ &data) = 0;
virtual bool wait_data(int timeout_us) = 0;
virtual ss_ get_local_address() const = 0;
virtual ss_ get_remote_address() const = 0;
};