⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 virtualsocket_unittest.cc

📁 本人收集整理的一份c/c++跨平台网络库
💻 CC
字号:
// Copyright 2006, Google Inc.#include <complex>#include <iostream>#include <cassert>#include "thread.h"#include "virtualsocketserver.h"#include "asyncudpsocket.h"#include "time.h"#ifdef POSIXextern "C" {#include <errno.h>}#endif // POSIXusing namespace utils_base;// Sends at a constant rate but with random packet sizes.struct Sender : public MessageHandler {  Sender(Thread* th, AsyncUDPSocket* s, uint32 rt)      : thread(th), socket(s), done(false), rate(rt), count(0) {    last_send = GetMillisecondCount();    thread->PostDelayed(NextDelay(), this, 1);  }  uint32 NextDelay() {    uint32 size = (rand() % 4096) + 1;    return 1000 * size / rate;  }  void OnMessage(Message* pmsg) {    assert(pmsg->message_id == 1);    if (done)      return;    uint32 cur_time = GetMillisecondCount();    uint32 delay = cur_time - last_send;    uint32 size = rate * delay / 1000;    size = min(size, uint32(4096));    size = max(size, uint32(4));    count += size;    *reinterpret_cast<uint32*>(dummy) = cur_time;    socket->Send(dummy, size);    last_send = cur_time;    thread->PostDelayed(NextDelay(), this, 1);  }  Thread* thread;  AsyncUDPSocket* socket;  bool done;  uint32 rate; // bytes per second  uint32 count;  uint32 last_send;  char dummy[4096];};struct Receiver : public MessageHandler, public sigslot::has_slots<> {  Receiver(Thread* th, AsyncUDPSocket* s, uint32 bw)      : thread(th), socket(s), bandwidth(bw), done(false), count(0),        sec_count(0), sum(0), sum_sq(0), samples(0) {    socket->SignalReadPacket.connect(this, &Receiver::OnReadPacket);    thread->PostDelayed(1000, this, 1);  }  ~Receiver() {    thread->Clear(this);  }  void OnReadPacket(      const char* data, size_t size, const SocketAddress& remote_addr,       AsyncPacketSocket* s) {    assert(s == socket);    assert(size >= 4);    count += (uint32)size;    sec_count += (uint32)size;    uint32 send_time = *reinterpret_cast<const uint32*>(data);    uint32 recv_time = GetMillisecondCount();    uint32 delay = recv_time - send_time;    sum += delay;    sum_sq += delay * delay;    samples += 1;  }  void OnMessage(Message* pmsg) {    assert(pmsg->message_id == 1);    // It is always possible for us to receive more than expected because    // packets can be further delayed in delivery.    if (bandwidth > 0)      assert(sec_count <= 5 * bandwidth / 4);    sec_count = 0;    thread->PostDelayed(1000, this, 1);  }    Thread* thread;  AsyncUDPSocket* socket;  uint32 bandwidth;  bool done;  uint32 count;  uint32 sec_count;  double sum;  double sum_sq;  uint32 samples;};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -