|
|
71057f |
|
|
|
51b3f0 |
#include <cstdio></cstdio>
|
|
|
51b3f0 |
#include <cstring></cstring>
|
|
|
51b3f0 |
|
|
|
51b3f0 |
#include <netinet in.h=""></netinet>
|
|
|
51b3f0 |
#include <arpa inet.h=""></arpa>
|
|
|
51b3f0 |
|
|
|
71057f |
#include "protocol.h"
|
|
|
71057f |
|
|
|
71057f |
|
|
|
61c265 |
#define myprintf(...) do { printf(__VA_ARGS__); fflush(stdout); } while(0);
|
|
|
61c265 |
|
|
|
61c265 |
|
|
|
51b3f0 |
class MyConnection: public Connection {
|
|
|
51b3f0 |
public:
|
|
|
51b3f0 |
const char *name;
|
|
|
51b3f0 |
Task readTask, writeTask;
|
|
|
51b3f0 |
char data[2][100];
|
|
|
51b3f0 |
MyConnection(const char *name):
|
|
|
51b3f0 |
name(name), readTask(data[0], 16), writeTask(data[1], 16), data()
|
|
|
b42f0a |
{ strcpy(data[1], "01234567890123456789"); }
|
|
|
51b3f0 |
protected:
|
|
|
51b3f0 |
void onOpeningError() override
|
|
|
61c265 |
{ myprintf("%s: onOpeningError\n", name); }
|
|
|
51b3f0 |
void onOpen(const void*, size_t) override
|
|
|
61c265 |
{ myprintf("%s: onOpen\n", name); read(readTask); write(writeTask); }
|
|
|
6ebd3b |
|
|
|
b42f0a |
void onReadReady(Task &task, bool error) override {
|
|
|
6ebd3b |
data[0][task.completion] = 0;
|
|
|
6ebd3b |
myprintf("%s: onReadReady: %lu, %lu, %s, %d\n", name, task.completion, task.size, (char*)task.data, error);
|
|
|
6ebd3b |
task.completion = 0;
|
|
|
6ebd3b |
read(task);
|
|
|
6ebd3b |
}
|
|
|
6ebd3b |
|
|
|
b42f0a |
void onWriteReady(Task &task, bool error) override {
|
|
|
6ebd3b |
myprintf("%s: onWriteReady: %d\n", name, error);
|
|
|
6ebd3b |
task.completion = 0;
|
|
|
6ebd3b |
write(task);
|
|
|
6ebd3b |
}
|
|
|
6ebd3b |
|
|
|
51b3f0 |
void onCloseReqested() override
|
|
|
b42f0a |
{ myprintf("%s: onCloseReqested\n", name); close(false); }
|
|
|
51b3f0 |
void onClose(bool error) override
|
|
|
61c265 |
{ myprintf("%s: onClose: %d\n", name, (int)error); }
|
|
|
51b3f0 |
};
|
|
|
51b3f0 |
|
|
|
51b3f0 |
|
|
|
51b3f0 |
class MyServer: public Server {
|
|
|
51b3f0 |
protected:
|
|
|
51b3f0 |
void onOpeningError() override
|
|
|
61c265 |
{ myprintf("Server: onOpeningError\n"); }
|
|
|
b42f0a |
void onOpen(const void*, size_t) override
|
|
|
61c265 |
{ myprintf("Server: onOpen\n"); }
|
|
|
51b3f0 |
Connection* onConnect(const void*, size_t) override
|
|
|
61c265 |
{ myprintf("Server: onConnect\n"); return new MyConnection("server connection"); }
|
|
|
51b3f0 |
void onDisconnect(Connection *connection, bool error) override
|
|
|
61c265 |
{ myprintf("Server: onDisconnect: %d\n", error); delete connection; }
|
|
|
51b3f0 |
void onCloseReqested() override
|
|
|
61c265 |
{ myprintf("Server: onCloseReqested\n"); }
|
|
|
51b3f0 |
void onClose(bool error) override
|
|
|
61c265 |
{ myprintf("Server: onClose: %d\n", error); }
|
|
|
51b3f0 |
};
|
|
|
51b3f0 |
|
|
|
51b3f0 |
|
|
|
51b3f0 |
|
|
|
51b3f0 |
int main(int argc, char **) {
|
|
|
51b3f0 |
bool server = argc == 1;
|
|
|
51b3f0 |
|
|
|
61c265 |
myprintf("init protocol\n");
|
|
|
51b3f0 |
Protocol protocol;
|
|
|
51b3f0 |
protocol.open();
|
|
|
61c265 |
myprintf("protocol open\n");
|
|
|
51b3f0 |
|
|
|
51b3f0 |
struct sockaddr_in addr = {};
|
|
|
51b3f0 |
addr.sin_family = AF_INET;
|
|
|
61c265 |
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
51b3f0 |
addr.sin_port = htons(1532);
|
|
|
b42f0a |
//addr.sin_port = htons(80);
|
|
|
51b3f0 |
|
|
|
51b3f0 |
if (server) {
|
|
|
61c265 |
myprintf("init server\n");
|
|
|
51b3f0 |
MyServer server;
|
|
|
61c265 |
if (!server.open(protocol, &addr, sizeof(addr))) {
|
|
|
61c265 |
myprintf("cannot open server\n");
|
|
|
61c265 |
} else {
|
|
|
61c265 |
myprintf("server open\n");
|
|
|
61c265 |
myprintf("wait 10s\n");
|
|
|
b42f0a |
//std::this_thread::sleep_for( std::chrono::seconds(10) );
|
|
|
b42f0a |
fgetc(stdin);
|
|
|
61c265 |
myprintf("close server\n");
|
|
|
61c265 |
server.closeWait();
|
|
|
61c265 |
}
|
|
|
51b3f0 |
} else {
|
|
|
61c265 |
myprintf("init connection\n");
|
|
|
51b3f0 |
MyConnection connection("client connection");
|
|
|
61c265 |
if (!connection.open(protocol, &addr, sizeof(addr))) {
|
|
|
61c265 |
myprintf("cannot open connection\n");
|
|
|
61c265 |
} else {
|
|
|
61c265 |
myprintf("connection open\n");
|
|
|
61c265 |
myprintf("wait 10s\n");
|
|
|
b42f0a |
//std::this_thread::sleep_for( std::chrono::seconds(10) );
|
|
|
b42f0a |
fgetc(stdin);
|
|
|
61c265 |
myprintf("close connection\n");
|
|
|
61c265 |
connection.closeWait();
|
|
|
61c265 |
}
|
|
|
51b3f0 |
}
|
|
|
51b3f0 |
|
|
|
61c265 |
myprintf("close protocol\n");
|
|
|
51b3f0 |
protocol.closeWait();
|
|
|
61c265 |
myprintf("protocol closed\n");
|
|
|
51b3f0 |
|
|
|
71057f |
return 0;
|
|
|
71057f |
}
|
|
|
71057f |
|
|
|
71057f |
|