|
|
91ef56 |
|
|
|
91ef56 |
#include <cstring></cstring>
|
|
|
91ef56 |
|
|
|
2499ad |
#include <unistd.h></unistd.h>
|
|
|
2499ad |
|
|
|
5a39be |
#include "tcpprotocol.h"
|
|
|
91ef56 |
#include "main.h"
|
|
|
91ef56 |
|
|
|
91ef56 |
|
|
|
5a39be |
namespace {
|
|
|
5a39be |
class MyConnection: public Connection {
|
|
|
5a39be |
private:
|
|
|
2499ad |
enum { BUFSIZE = 16 };
|
|
|
5a39be |
const bool serverSide;
|
|
|
2499ad |
const bool serverHello;
|
|
|
5a39be |
char buffer[2][BUFSIZE + 1];
|
|
|
5a39be |
|
|
|
5a39be |
public:
|
|
|
5a39be |
std::string name;
|
|
|
5a39be |
|
|
|
2499ad |
explicit MyConnection(const char *name, bool serverSide, bool serverHello = false):
|
|
|
2499ad |
serverSide(serverSide), serverHello(serverHello), buffer(), name(name) { }
|
|
|
5a39be |
|
|
|
5a39be |
protected:
|
|
|
5a39be |
void sendData() {
|
|
|
5a39be |
for(int i = 0; i < BUFSIZE; ++i) buffer[1][i] = '0' + rand()%10;
|
|
|
2499ad |
buffer[1][BUFSIZE - 2] = '\n';
|
|
|
2499ad |
buffer[1][BUFSIZE - 1] = '\n';
|
|
|
5a39be |
printf("%s: send: %s\n", name.c_str(), buffer[1]);
|
|
|
5a39be |
writeReq(buffer[1], BUFSIZE);
|
|
|
5a39be |
}
|
|
|
5a39be |
|
|
|
5a39be |
ErrorCode onOpen() override {
|
|
|
5a39be |
printf("%s: MyConnection::onOpen()\n", name.c_str());
|
|
|
2499ad |
if (serverSide == serverHello) sendData();
|
|
|
5a39be |
readReq(buffer[0], BUFSIZE);
|
|
|
5a39be |
return ERR_NONE;
|
|
|
5a39be |
}
|
|
|
5a39be |
|
|
|
5a39be |
void onCloseRequested() override {
|
|
|
5a39be |
printf("%s: MyConnection::onCloseRequested()\n", name.c_str());
|
|
|
5a39be |
if (isAllDone()) close();
|
|
|
5a39be |
}
|
|
|
5a39be |
|
|
|
5a39be |
void onClose(ErrorCode errorCode) override {
|
|
|
5a39be |
printf("%s: MyConnection::onClose(%u)\n", name.c_str(), errorCode);
|
|
|
5a39be |
}
|
|
|
5a39be |
|
|
|
5a39be |
void onReadReady(const ReadReq&) override {
|
|
|
5a39be |
printf("%s: MyConnection::onReadReady()\n", name.c_str());
|
|
|
5a39be |
printf("%s: received: %s\n", name.c_str(), buffer[0]);
|
|
|
5a39be |
|
|
|
5a39be |
if (isCloseRequested()) {
|
|
|
5a39be |
if (serverSide) sendData(); else
|
|
|
5a39be |
if (isAllDone()) close();
|
|
|
5a39be |
} else {
|
|
|
5a39be |
sendData();
|
|
|
5a39be |
readReq(buffer[0], BUFSIZE);
|
|
|
5a39be |
}
|
|
|
5a39be |
}
|
|
|
5a39be |
|
|
|
5a39be |
void onWriteReady(const WriteReq&) override {
|
|
|
5a39be |
printf("%s: MyConnection::onWriteReady()\n", name.c_str());
|
|
|
5a39be |
if (isCloseRequested() && isAllDone() && serverSide) close();
|
|
|
5a39be |
}
|
|
|
5a39be |
};
|
|
|
91ef56 |
|
|
|
91ef56 |
|
|
|
5a39be |
class MyServer: public Server {
|
|
|
5a39be |
private:
|
|
|
5a39be |
unsigned int lastId;
|
|
|
5a39be |
|
|
|
5a39be |
public:
|
|
|
5a39be |
MyServer(): lastId() { }
|
|
|
5a39be |
|
|
|
5a39be |
protected:
|
|
|
5a39be |
ErrorCode onStart() override {
|
|
|
5a39be |
printf("MyServer::onStart()\n");
|
|
|
5a39be |
return ERR_NONE;
|
|
|
91ef56 |
}
|
|
|
91ef56 |
|
|
|
5a39be |
void onStopRequested() override {
|
|
|
5a39be |
printf("MyServer::onStopRequested()\n");
|
|
|
5a39be |
}
|
|
|
5a39be |
|
|
|
5a39be |
void onStop(ErrorCode errorCode) override {
|
|
|
5a39be |
printf("MyServer::onStop(%u)\n", errorCode);
|
|
|
5a39be |
}
|
|
|
5a39be |
|
|
|
5a39be |
Connection::Handle onConnect(const Address &remoteAddress) override {
|
|
|
5a39be |
printf("MyServer::onConnect( ");
|
|
|
5a39be |
for(int i = 0; i < (int)remoteAddress.size; ++i) printf("%d.", remoteAddress.data[i]);
|
|
|
5a39be |
printf(" )\n");
|
|
|
5a39be |
|
|
|
5a39be |
char buf[1024] = {};
|
|
|
5a39be |
sprintf(buf, "remote%d", ++lastId);
|
|
|
5a39be |
printf("new connection: %s\n", buf);
|
|
|
5a39be |
|
|
|
5a39be |
return Connection::Handle(new MyConnection(buf, true));
|
|
|
5a39be |
}
|
|
|
5a39be |
|
|
|
5a39be |
void onDisconnect(const Connection::Handle &connection) override {
|
|
|
5a39be |
printf("MyServer::onDisconnect( %s )\n", ((MyConnection*)connection.pointer())->name.c_str());
|
|
|
5a39be |
}
|
|
|
5a39be |
};
|
|
|
5a39be |
}
|
|
|
5a39be |
|
|
|
5a39be |
|
|
|
5a39be |
int main(int argc, char** argv) {
|
|
|
5a39be |
if (argc != 3)
|
|
|
5a39be |
{ printf("two args expected\n"); return 1; }
|
|
|
5a39be |
|
|
|
5a39be |
bool client = (0 == strcmp(argv[1], "tcpclient"));
|
|
|
5a39be |
bool server = (0 == strcmp(argv[1], "tcpserver"));
|
|
|
5a39be |
if (!client && !server)
|
|
|
5a39be |
{ printf("wrong args\n"); return 1; }
|
|
|
5a39be |
|
|
|
5a39be |
printf("start tcp protocol\n");
|
|
|
5a39be |
TcpProtocol::Handle protocol(new TcpProtocol());
|
|
|
5a39be |
protocol->start();
|
|
|
91ef56 |
|
|
|
5a39be |
Address address;
|
|
|
5a39be |
address.set(Address::COMMON_STRING, argv[2]);
|
|
|
5a39be |
ErrorCode errorCode = protocol->resolve(address);
|
|
|
5a39be |
if (errorCode) {
|
|
|
5a39be |
printf("cannot resolve address: %s, errorCode: %u\n", argv[2], errorCode);
|
|
|
5a39be |
} else
|
|
|
5a39be |
if (client) {
|
|
|
5a39be |
printf("tcpclient, %s\n", argv[2]);
|
|
|
91ef56 |
|
|
|
5a39be |
Connection::Handle connection(new MyConnection("client", false));
|
|
|
5a39be |
errorCode = protocol->connect(connection, address);
|
|
|
5a39be |
if (errorCode) {
|
|
|
5a39be |
printf("cannot open connection, errorCode: %u\n", errorCode);
|
|
|
5a39be |
} else {
|
|
|
5a39be |
printf("connected\n");
|
|
|
2499ad |
usleep(5000000ll);
|
|
|
2499ad |
//fgetc(stdin);
|
|
|
5a39be |
printf("disconnection\n");
|
|
|
5a39be |
connection->closeWait(10);
|
|
|
5a39be |
printf("disconnected\n");
|
|
|
5a39be |
}
|
|
|
5a39be |
} else {
|
|
|
5a39be |
printf("tcpserver, %s\n", argv[2]);
|
|
|
91ef56 |
|
|
|
5a39be |
Server::Handle server(new MyServer());
|
|
|
5a39be |
errorCode = protocol->listen(server, address);
|
|
|
5a39be |
if (errorCode) {
|
|
|
5a39be |
printf("cannot start server, errorCode: %u\n", errorCode);
|
|
|
5a39be |
} else {
|
|
|
5a39be |
printf("server started\n");
|
|
|
5a39be |
fgetc(stdin);
|
|
|
5a39be |
printf("stop server\n");
|
|
|
5a39be |
server->stopWait(10);
|
|
|
5a39be |
printf("server stopped\n");
|
|
|
5a39be |
}
|
|
|
91ef56 |
}
|
|
|
5a39be |
|
|
|
5a39be |
printf("stop tcp protocol\n");
|
|
|
5a39be |
protocol->stopWait(10*1000000ll);
|
|
|
5a39be |
printf("finished, last error: %u\n", errorCode);
|
|
|
5a39be |
|
|
|
5a39be |
return errorCode;
|
|
|
91ef56 |
}
|
|
|
91ef56 |
|
|
|
91ef56 |
|