diff --git a/Snap.workspace.layout b/Snap.workspace.layout index 9ba52b42..e6163315 100644 --- a/Snap.workspace.layout +++ b/Snap.workspace.layout @@ -1,5 +1,5 @@ - + diff --git a/common/queue.h b/common/queue.h index 78852fc6..a38497df 100644 --- a/common/queue.h +++ b/common/queue.h @@ -53,7 +53,7 @@ public: queue_.pop(); } - void push(const T& item) + void push(const T item) { std::unique_lock mlock(mutex_); queue_.push(item); diff --git a/server/Makefile b/server/Makefile index 94057e25..d2e956fd 100644 --- a/server/Makefile +++ b/server/Makefile @@ -1,7 +1,7 @@ VERSION = 0.01 CC = /usr/bin/g++ -CFLAGS = -std=gnu++0x -Wall -Wno-unused-function -g -O3 -D_REENTRANT -DVERSION=\"$(VERSION)\" -I.. -LDFLAGS = -lrt -lpthread -lportaudio -lboost_system +CFLAGS = -std=gnu++0x -Wall -Wno-unused-function -D_REENTRANT -DVERSION=\"$(VERSION)\" -I.. +LDFLAGS = -lrt -lpthread -lportaudio -lboost_system -lboost_program_options OBJ = snapServer.o BIN = snapserver diff --git a/server/snapServer.cpp b/server/snapServer.cpp index 4222ca7b..a8d76da0 100644 --- a/server/snapServer.cpp +++ b/server/snapServer.cpp @@ -10,10 +10,8 @@ #include #include -#include -#include #include -#include +#include #include #include #include // localtime @@ -27,6 +25,7 @@ using boost::asio::ip::tcp; +namespace po = boost::program_options; const int max_length = 1024; @@ -114,9 +113,9 @@ public: sessions.push_back(session); } } - + void send(shared_ptr chunk) - { + { for (size_t n=0; n(&port)->default_value(98765), "port to listen on") + ("fifo,f", po::value(&fifoName)->default_value("/tmp/snapfifo"), "name of fifo file") + ; + + po::variables_map vm; + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + + if (vm.count("help")) + { + cout << desc << "\n"; + return 1; + } + +/* if (vm.count("port") == 0) + { + cout << "Please specify server port\n"; + return 1; + } + +// cout << "Compression level was set to " << vm["compression"].as() << ".\n"; + + if (argc == 1) { - std::cerr << "Usage: blocking_tcp_echo_server \n"; + std::cerr << desc << "\n"; return 1; } - +*/ using namespace std; // For atoi. - Server* server = new Server(atoi(argv[1])); + Server* server = new Server(port); server->start(); - char c[2]; timeval tvChunk; gettimeofday(&tvChunk, NULL); long nextTick = getTickCount(); - while (cin.good()) - { - shared_ptr chunk(new WireChunk()); - for (size_t n=0; (npayload[n] = (int)c[0] + ((int)c[1] << 8); - } - // if (!cin.good()) - // cin.clear(); + /* open, read, and display the message from the FIFO */ + mkfifo(fifoName.c_str(), 0777); + while (true) + { + int fd = open(fifoName.c_str(), O_RDONLY); + try + { + shared_ptr chunk;//(new WireChunk()); + while (true)//cin.good()) + { + chunk.reset(new WireChunk()); + int toRead = sizeof(WireChunk::payload); +// cout << "tr: " << toRead << ", size: " << WIRE_CHUNK_SIZE << "\t"; + char* payload = (char*)(&chunk->payload[0]); + int len = 0; + do + { + int count = read(fd, payload + len, toRead - len); + cout.flush(); + if (count <= 0) + throw new std::exception(); + len += count; + } + while (len < toRead); - chunk->tv_sec = tvChunk.tv_sec; - chunk->tv_usec = tvChunk.tv_usec; - server->send(chunk); + chunk->tv_sec = tvChunk.tv_sec; + chunk->tv_usec = tvChunk.tv_usec; + server->send(chunk); + + addMs(tvChunk, WIRE_CHUNK_MS); + nextTick += WIRE_CHUNK_MS; + long currentTick = getTickCount(); + if (nextTick > currentTick) + { + usleep((nextTick - currentTick) * 1000); + } + else + { + cin.sync(); + gettimeofday(&tvChunk, NULL); + nextTick = getTickCount(); + } + } + } + catch(const std::exception&) + { + cout << "Exception\n"; + } + close(fd); + } - addMs(tvChunk, WIRE_CHUNK_MS); - nextTick += WIRE_CHUNK_MS; - long currentTick = getTickCount(); - if (nextTick > currentTick) - { - usleep((nextTick - currentTick) * 1000); - } - else - { - cin.sync(); - gettimeofday(&tvChunk, NULL); - nextTick = getTickCount(); - } - } return 0; - server->stop(); + server->stop(); } catch (std::exception& e) {