mirror of
https://github.com/wangyu-/udp2raw.git
synced 2025-01-19 14:29:34 +08:00
added missing files
This commit is contained in:
parent
6a825dc51e
commit
c591902be1
63
fd_manager.cpp
Normal file
63
fd_manager.cpp
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* fd_manager.cpp
|
||||||
|
*
|
||||||
|
* Created on: Sep 25, 2017
|
||||||
|
* Author: root
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include "fd_manager.h"
|
||||||
|
int fd_manager_t::fd_exist(int fd)
|
||||||
|
{
|
||||||
|
return fd_to_fd64_mp.find(fd)!=fd_to_fd64_mp.end();
|
||||||
|
}
|
||||||
|
int fd_manager_t::exist(fd64_t fd64)
|
||||||
|
{
|
||||||
|
return fd64_to_fd_mp.find(fd64)!=fd64_to_fd_mp.end();
|
||||||
|
}
|
||||||
|
int fd_manager_t::to_fd(fd64_t fd64)
|
||||||
|
{
|
||||||
|
assert(exist(fd64));
|
||||||
|
return fd64_to_fd_mp[fd64];
|
||||||
|
}
|
||||||
|
void fd_manager_t::fd64_close(fd64_t fd64)
|
||||||
|
{
|
||||||
|
assert(exist(fd64));
|
||||||
|
int fd=fd64_to_fd_mp[fd64];
|
||||||
|
fd64_to_fd_mp.erase(fd64);
|
||||||
|
fd_to_fd64_mp.erase(fd);
|
||||||
|
if(exist_info(fd64))
|
||||||
|
{
|
||||||
|
fd_info_mp.erase(fd64);
|
||||||
|
}
|
||||||
|
assert(close(fd)==0);
|
||||||
|
}
|
||||||
|
void fd_manager_t::reserve(int n)
|
||||||
|
{
|
||||||
|
fd_to_fd64_mp.reserve(n);
|
||||||
|
fd64_to_fd_mp.reserve(n);
|
||||||
|
fd_info_mp.reserve(n);
|
||||||
|
}
|
||||||
|
u64_t fd_manager_t::create(int fd)
|
||||||
|
{
|
||||||
|
assert(!fd_exist(fd));
|
||||||
|
fd64_t fd64=counter++;
|
||||||
|
fd_to_fd64_mp[fd]=fd64;
|
||||||
|
fd64_to_fd_mp[fd64]=fd;
|
||||||
|
return fd64;
|
||||||
|
}
|
||||||
|
fd_manager_t::fd_manager_t()
|
||||||
|
{
|
||||||
|
counter=u32_t(-1);
|
||||||
|
counter+=100;
|
||||||
|
reserve(10007);
|
||||||
|
}
|
||||||
|
fd_info_t & fd_manager_t::get_info(fd64_t fd64)
|
||||||
|
{
|
||||||
|
assert(exist(fd64));
|
||||||
|
return fd_info_mp[fd64];
|
||||||
|
}
|
||||||
|
int fd_manager_t::exist_info(fd64_t fd64)
|
||||||
|
{
|
||||||
|
return fd_info_mp.find(fd64)!=fd_info_mp.end();
|
||||||
|
}
|
43
fd_manager.h
Normal file
43
fd_manager.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* fd_manager.h
|
||||||
|
*
|
||||||
|
* Created on: Sep 25, 2017
|
||||||
|
* Author: root
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FD_MANAGER_H_
|
||||||
|
#define FD_MANAGER_H_
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
//#include "packet.h"
|
||||||
|
#include "connection.h"
|
||||||
|
|
||||||
|
struct fd_info_t
|
||||||
|
{
|
||||||
|
//ip_port_t ip_port;
|
||||||
|
conn_info_t *p_conn_info;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct fd_manager_t //conver fd to a uniq 64bit number,avoid fd value conflict caused by close and re-create
|
||||||
|
//this class is not strictly necessary,it just makes epoll fd handling easier
|
||||||
|
{
|
||||||
|
fd_info_t & get_info(fd64_t fd64);
|
||||||
|
int exist_info(fd64_t);
|
||||||
|
int exist(fd64_t fd64);
|
||||||
|
int to_fd(fd64_t);
|
||||||
|
void fd64_close(fd64_t fd64);
|
||||||
|
void reserve(int n);
|
||||||
|
u64_t create(int fd);
|
||||||
|
fd_manager_t();
|
||||||
|
private:
|
||||||
|
u64_t counter;
|
||||||
|
unordered_map<int,fd64_t> fd_to_fd64_mp;
|
||||||
|
unordered_map<fd64_t,int> fd64_to_fd_mp;
|
||||||
|
unordered_map<fd64_t,fd_info_t> fd_info_mp;
|
||||||
|
int fd_exist(int fd);
|
||||||
|
//void remove_fd(int fd);
|
||||||
|
//fd64_t fd_to_fd64(int fd);
|
||||||
|
};
|
||||||
|
|
||||||
|
extern fd_manager_t fd_manager;
|
||||||
|
#endif /* FD_MANAGER_H_ */
|
Loading…
x
Reference in New Issue
Block a user