2019-07-27 22:44:32 +08:00
|
|
|
|
#pragma once
|
2018-12-12 11:16:12 +08:00
|
|
|
|
#include <vector>
|
|
|
|
|
#include "sock_addr.h"
|
|
|
|
|
|
|
|
|
|
class udp_sock;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* UDP 鏁版嵁鍖呯被
|
2018-12-12 11:16:12 +08:00
|
|
|
|
*/
|
|
|
|
|
class udp_pkt
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 鏋勯€犲嚱鏁<EFBFBD>
|
|
|
|
|
* @param size {size_t} 鎸囧畾浜嗘瘡涓<EFBFBD> udp 鏁版嵁鍖呯殑鏈€澶у€<EFBFBD>
|
2018-12-12 11:16:12 +08:00
|
|
|
|
*/
|
|
|
|
|
udp_pkt(size_t size = 1460);
|
|
|
|
|
~udp_pkt(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 璁剧疆瑕佸彂閫佺殑鏁版嵁鍖呮暟鎹<EFBFBD>
|
|
|
|
|
* @param data {const void*} 鏁版嵁鍖呭湴鍧€锛岄潪 NULL
|
|
|
|
|
* @param len {size_t} 鏁版嵁鍖呭ぇ灏忥紝椤<EFBFBD> > 0 涓<EFBFBD> <= 鏋勯€犲嚱鏁伴噷璁剧疆鐨勬渶澶у€<EFBFBD>
|
|
|
|
|
* @return {bool} 鍙傛暟闈炴硶鏃惰繑鍥<EFBFBD> false
|
2018-12-12 11:16:12 +08:00
|
|
|
|
*/
|
|
|
|
|
bool set_data(const void* data, size_t len);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 璁剧疆鏁版嵁鍖呮帴鏀剁鍦板潃锛屾湰鏂规硶浠呯敤鍦ㄥ鎴风鏂瑰紡涓<EFBFBD>
|
|
|
|
|
* @param addr {const char*} udp 鐩爣鍦板潃
|
|
|
|
|
* @return {bool} 杩斿洖 false 琛ㄧず鍦板潃闈炴硶
|
2018-12-12 11:16:12 +08:00
|
|
|
|
*/
|
|
|
|
|
bool set_peer(const char* addr);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 鑾峰緱璇诲埌鐨勬暟鎹寘鍐呭锛岄渶鍏堥€氳繃 get_dlen 鑾峰緱鏁版嵁澶у皬
|
2018-12-12 11:16:12 +08:00
|
|
|
|
* @return {void*}
|
|
|
|
|
*/
|
|
|
|
|
void* get_data(void) const
|
|
|
|
|
{
|
|
|
|
|
return iov_.iov_base;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 鑾峰緱璇诲埌鐨勬暟鎹寘澶у皬
|
2018-12-12 11:16:12 +08:00
|
|
|
|
* @return {size_t}
|
|
|
|
|
*/
|
|
|
|
|
size_t get_dlen(void) const
|
|
|
|
|
{
|
|
|
|
|
return iov_.iov_len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 褰撹鍒版暟鎹寘鍚庯紝鏈柟娉曡繑鍥炲彂閫佺鐨勭鍙e彿
|
|
|
|
|
* @return {int} 杩斿洖鍊<EFBFBD> < 0 琛ㄧず鍑洪敊
|
2018-12-12 11:16:12 +08:00
|
|
|
|
*/
|
|
|
|
|
int get_port(void) const;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 褰撹鍒版暟鎹寘鍚庯紝鏈柟娉曡繑鍥炲彂閫佺鐨<EFBFBD> IP 鍦板潃
|
|
|
|
|
* @return {const char*} 杩斿洖 NULL 琛ㄧず鏃犳硶鑾峰緱鍙戦€佺鍦板潃
|
2018-12-12 11:16:12 +08:00
|
|
|
|
*/
|
|
|
|
|
const char* get_ip(void) const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
friend class udp_sock;
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
struct iovec iov_; // 瀛樻斁鏀<E69681>/鍙戠殑鏁版嵁鍦板潃鍙婇暱搴<E69AB1>
|
|
|
|
|
const size_t size_; // 鏋勯€犳椂璁惧畾鍚庝笉鑳藉啀淇敼
|
|
|
|
|
SOCK_ADDR addr_; // 婧愬湴鍧€
|
2018-12-12 11:16:12 +08:00
|
|
|
|
socklen_t addr_len_;
|
|
|
|
|
char ipbuf_[64];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* UDP 鏁版嵁鍖呴泦鍚堝璞<EFBFBD>
|
2018-12-12 11:16:12 +08:00
|
|
|
|
*/
|
|
|
|
|
class udp_pkts
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 鏋勯€犲嚱鏁<EFBFBD>
|
|
|
|
|
* @param max {size_t} 鎸囧畾鎺ユ敹 UDP 鏃舵渶澶х殑鎺ユ敹鏁版嵁鍖呬釜鏁帮紝璇ュ€奸渶 > 0
|
2018-12-12 11:16:12 +08:00
|
|
|
|
*/
|
|
|
|
|
udp_pkts(size_t max);
|
|
|
|
|
~udp_pkts(void);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 鑾峰緱鎸囬拡涓嬫爣鐨勬暟鎹寘瀵硅薄
|
|
|
|
|
* @param i {size_t} 涓嬫爣鍊<EFBFBD>
|
|
|
|
|
* @return {udp_pkt*} 杩斿洖 NULL 琛ㄧず涓嬫爣瓒婄晫
|
2018-12-12 11:16:12 +08:00
|
|
|
|
*/
|
|
|
|
|
udp_pkt* operator[](size_t i);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 鑾峰緱宸茶鍒扮殑鎴栧皢瑕佸彂閫佺殑鏁版嵁鍖呬釜鏁<EFBFBD>
|
2018-12-12 11:16:12 +08:00
|
|
|
|
* @return {size_t}
|
|
|
|
|
*/
|
|
|
|
|
size_t get_npkt(void) const
|
|
|
|
|
{
|
|
|
|
|
return npkt_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 璁剧疆鎹寘涓暟锛屽簲鐢ㄥ彲璋冪敤姝ゆ柟娉曡缃鍙戦€佺殑鏁版嵁鍖呬釜鏁帮紱
|
|
|
|
|
* upd_sock 绫诲湪璇诲埌鏁版嵁鏃朵篃浼氶€氳繃姝ゆ柟娉曡缃鍒版暟鎹寘涓暟
|
|
|
|
|
* @param n {size_t} 鎹寘涓暟锛岃鍊间笉鑳借秴杩囨瀯閫犳椂鎸囧畾鐨勬渶澶у€<EFBFBD>
|
2018-12-12 11:16:12 +08:00
|
|
|
|
*/
|
|
|
|
|
void set_npkt(size_t n);
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
friend class udp_sock;
|
|
|
|
|
|
|
|
|
|
std::vector<udp_pkt*>& get_pkts(void)
|
|
|
|
|
{
|
|
|
|
|
return pkts_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<udp_pkt*> pkts_;
|
|
|
|
|
size_t max_;
|
|
|
|
|
size_t npkt_;
|
|
|
|
|
};
|