mirror of
https://github.com/wangyu-/UDPspeeder.git
synced 2025-01-31 12:19:35 +08:00
just commit
This commit is contained in:
parent
44e9f5cb8b
commit
5a4ef7d94d
62
fd_manager.cpp
Normal file
62
fd_manager.cpp
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* 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::fd64_exist(fd64_t fd64)
|
||||||
|
{
|
||||||
|
return fd64_to_fd_mp.find(fd64)!=fd64_to_fd_mp.end();
|
||||||
|
}
|
||||||
|
fd64_t fd_manager_t::fd_to_fd64(int fd)
|
||||||
|
{
|
||||||
|
assert(fd_exist(fd));
|
||||||
|
return fd_to_fd64_mp[fd];
|
||||||
|
}
|
||||||
|
int fd_manager_t::fd64_to_fd(fd64_t fd64)
|
||||||
|
{
|
||||||
|
assert(fd64_exist(fd64));
|
||||||
|
return fd64_to_fd_mp[fd64];
|
||||||
|
}
|
||||||
|
void fd_manager_t::remove_fd(int fd)
|
||||||
|
{
|
||||||
|
assert(fd_exist(fd));
|
||||||
|
fd64_t fd64=fd_to_fd64_mp[fd];
|
||||||
|
fd_to_fd64_mp.erase(fd);
|
||||||
|
fd64_to_fd_mp.erase(fd64);
|
||||||
|
//return 0;
|
||||||
|
}
|
||||||
|
void fd_manager_t::remove_fd64(fd64_t fd64)
|
||||||
|
{
|
||||||
|
assert(fd64_exist(fd64));
|
||||||
|
int fd=fd64_to_fd_mp[fd64];
|
||||||
|
fd64_to_fd_mp.erase(fd64);
|
||||||
|
fd_to_fd64_mp.erase(fd);
|
||||||
|
//return 0;
|
||||||
|
}
|
||||||
|
void fd_manager_t::reserve()
|
||||||
|
{
|
||||||
|
fd_to_fd64_mp.reserve(100007);
|
||||||
|
fd64_to_fd_mp.reserve(100007);
|
||||||
|
//return 0;
|
||||||
|
}
|
||||||
|
u64_t fd_manager_t::insert_fd(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+=2;
|
||||||
|
}
|
33
fd_manager.h
Normal file
33
fd_manager.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* fd_manager.h
|
||||||
|
*
|
||||||
|
* Created on: Sep 25, 2017
|
||||||
|
* Author: root
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FD_MANAGER_H_
|
||||||
|
#define FD_MANAGER_H_
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
typedef u64_t fd64_t;
|
||||||
|
|
||||||
|
struct fd_manager_t //conver fd to a uniq 64bit number,avoid fd value conflict caused by close and re-create
|
||||||
|
//not used currently
|
||||||
|
{
|
||||||
|
u64_t counter;
|
||||||
|
unordered_map<int,u64_t> fd_to_fd64_mp;
|
||||||
|
unordered_map<u64_t,int> fd64_to_fd_mp;
|
||||||
|
int fd_exist(int fd);
|
||||||
|
int fd64_exist(fd64_t fd64);
|
||||||
|
fd64_t fd_to_fd64(int fd);
|
||||||
|
int fd64_to_fd(fd64_t);
|
||||||
|
void remove_fd(int fd);
|
||||||
|
void remove_fd64(fd64_t fd64);
|
||||||
|
void reserve();
|
||||||
|
u64_t insert_fd(int fd);
|
||||||
|
fd_manager_t();
|
||||||
|
};
|
||||||
|
|
||||||
|
extern fd_manager_t fd_manager;
|
||||||
|
#endif /* FD_MANAGER_H_ */
|
Loading…
x
Reference in New Issue
Block a user