2017-03-09 18:42:16 +08:00
|
|
|
#pragma once
|
2017-03-06 20:37:51 +08:00
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
2017-08-03 21:26:48 +08:00
|
|
|
#include <random>
|
2017-03-06 20:37:51 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2017-06-21 17:29:13 +08:00
|
|
|
#include <glog/logging.h>
|
|
|
|
#include <gtest/gtest.h>
|
2017-03-06 20:37:51 +08:00
|
|
|
|
2017-04-15 21:14:12 +08:00
|
|
|
#include "communication/bolt/v1/decoder/buffer.hpp"
|
2017-03-06 20:37:51 +08:00
|
|
|
#include "communication/server.hpp"
|
|
|
|
#include "io/network/epoll.hpp"
|
|
|
|
#include "io/network/socket.hpp"
|
|
|
|
|
|
|
|
static constexpr const int SIZE = 60000;
|
|
|
|
static constexpr const int REPLY = 10;
|
|
|
|
|
|
|
|
using endpoint_t = io::network::NetworkEndpoint;
|
|
|
|
using socket_t = io::network::Socket;
|
|
|
|
|
2017-08-03 21:26:48 +08:00
|
|
|
class TestData {};
|
|
|
|
|
2017-03-06 20:37:51 +08:00
|
|
|
class TestSession {
|
|
|
|
public:
|
2017-10-09 16:53:03 +08:00
|
|
|
TestSession(socket_t &&socket, TestData &) : socket_(std::move(socket)) {
|
2017-03-28 18:42:04 +08:00
|
|
|
event_.data.ptr = this;
|
2017-03-06 20:37:51 +08:00
|
|
|
}
|
|
|
|
|
2017-03-28 18:42:04 +08:00
|
|
|
bool Alive() { return socket_.IsOpen(); }
|
2017-03-06 20:37:51 +08:00
|
|
|
|
2017-10-09 16:53:03 +08:00
|
|
|
int Id() const { return socket_.fd(); }
|
2017-03-06 20:37:51 +08:00
|
|
|
|
2017-04-15 21:14:12 +08:00
|
|
|
void Execute() {
|
|
|
|
if (buffer_.size() < 2) return;
|
|
|
|
const uint8_t *data = buffer_.data();
|
|
|
|
size_t size = data[0];
|
|
|
|
size <<= 8;
|
|
|
|
size += data[1];
|
|
|
|
if (buffer_.size() < size + 2) return;
|
2017-03-06 20:37:51 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < REPLY; ++i)
|
2017-04-15 21:14:12 +08:00
|
|
|
ASSERT_TRUE(this->socket_.Write(data + 2, size));
|
|
|
|
|
|
|
|
buffer_.Shift(size + 2);
|
|
|
|
}
|
2017-03-06 20:37:51 +08:00
|
|
|
|
2017-06-21 17:29:13 +08:00
|
|
|
io::network::StreamBuffer Allocate() { return buffer_.Allocate(); }
|
2017-04-15 21:14:12 +08:00
|
|
|
|
2017-06-21 17:29:13 +08:00
|
|
|
void Written(size_t len) { buffer_.Written(len); }
|
2017-03-06 20:37:51 +08:00
|
|
|
|
2017-03-28 18:42:04 +08:00
|
|
|
void Close() {
|
2017-06-21 17:29:13 +08:00
|
|
|
DLOG(INFO) << "Close session!";
|
2017-03-28 18:42:04 +08:00
|
|
|
this->socket_.Close();
|
2017-03-06 20:37:51 +08:00
|
|
|
}
|
|
|
|
|
2017-04-15 21:14:12 +08:00
|
|
|
communication::bolt::Buffer<SIZE * 2> buffer_;
|
2017-03-28 18:42:04 +08:00
|
|
|
socket_t socket_;
|
|
|
|
io::network::Epoll::Event event_;
|
2017-03-06 20:37:51 +08:00
|
|
|
};
|
|
|
|
|
2017-08-04 16:49:00 +08:00
|
|
|
using test_server_t = communication::Server<TestSession, socket_t, TestData>;
|
2017-03-06 20:37:51 +08:00
|
|
|
|
2017-06-21 17:29:13 +08:00
|
|
|
void server_start(void *serverptr, int num) {
|
|
|
|
((test_server_t *)serverptr)->Start(num);
|
2017-03-06 20:37:51 +08:00
|
|
|
}
|
|
|
|
|
2017-06-21 17:29:13 +08:00
|
|
|
void client_run(int num, const char *interface, const char *port,
|
|
|
|
const unsigned char *data, int lo, int hi) {
|
2017-03-06 20:37:51 +08:00
|
|
|
std::stringstream name;
|
|
|
|
name << "Client " << num;
|
|
|
|
unsigned char buffer[SIZE * REPLY], head[2];
|
|
|
|
int have, read;
|
|
|
|
endpoint_t endpoint(interface, port);
|
|
|
|
socket_t socket;
|
|
|
|
ASSERT_TRUE(socket.Connect(endpoint));
|
2017-04-15 21:14:12 +08:00
|
|
|
ASSERT_TRUE(socket.SetTimeout(2, 0));
|
2017-10-09 16:53:03 +08:00
|
|
|
DLOG(INFO) << "Socket create: " << socket.fd();
|
2017-03-06 20:37:51 +08:00
|
|
|
for (int len = lo; len <= hi; len += 100) {
|
|
|
|
have = 0;
|
|
|
|
head[0] = (len >> 8) & 0xff;
|
|
|
|
head[1] = len & 0xff;
|
|
|
|
ASSERT_TRUE(socket.Write(head, 2));
|
|
|
|
ASSERT_TRUE(socket.Write(data, len));
|
2017-10-09 16:53:03 +08:00
|
|
|
DLOG(INFO) << "Socket write: " << socket.fd();
|
2017-03-06 20:37:51 +08:00
|
|
|
while (have < len * REPLY) {
|
|
|
|
read = socket.Read(buffer + have, SIZE);
|
2017-10-09 16:53:03 +08:00
|
|
|
DLOG(INFO) << "Socket read: " << socket.fd();
|
2017-03-06 20:37:51 +08:00
|
|
|
if (read == -1) break;
|
|
|
|
have += read;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < REPLY; ++i)
|
|
|
|
for (int j = 0; j < len; ++j) ASSERT_EQ(buffer[i * len + j], data[j]);
|
|
|
|
}
|
2017-10-09 16:53:03 +08:00
|
|
|
DLOG(INFO) << "Socket done: " << socket.fd();
|
2017-03-06 20:37:51 +08:00
|
|
|
socket.Close();
|
|
|
|
}
|
|
|
|
|
2017-06-21 17:29:13 +08:00
|
|
|
void initialize_data(unsigned char *data, int size) {
|
2017-03-06 20:37:51 +08:00
|
|
|
std::random_device rd;
|
|
|
|
std::mt19937 gen(rd());
|
|
|
|
std::uniform_int_distribution<> dis(0, 255);
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
data[i] = dis(gen);
|
|
|
|
}
|
|
|
|
}
|