我正在寻找修改 Boost Asio HTTP Server 3 示例的最佳方法,以维护当前连接的客户端列表。
如果我将示例中的 server.hpp 修改为:
class server : private boost::noncopyable
{
public:
typedef std::vector< connection_ptr > ConnectionList;
// ...
ConnectionList::const_iterator GetClientList() const
{
return connection_list_.begin();
};
void handle_accept(const boost::system::error_code& e)
{
if (!e)
{
connection_list_.push_back( new_connection_ );
new_connection_->start();
// ...
}
}
private:
ConnectionList connection_list_;
};
Run Code Online (Sandbox Code Playgroud)
然后我搞乱了连接对象的生命周期,这样它就不会超出范围并与客户端断开连接,因为它仍然在 ConnectionList 中维护着一个引用。
相反,如果我的 ConnectionList 被定义为,typedef std::vector< boost::weak_ptr< connection > > ConnectionList;
那么当有人从 .NET 中使用它时,我将面临客户端断开连接并使其指针无效的风险GetClientList()
。
有人有关于一个好的和安全的方法来做到这一点的建议吗?
谢谢,保罗
我有一个包含不可复制句柄的C++类.但是,该类必须具有复制构造函数.所以,我已经实现了一个将句柄的所有权转移到新对象(如下所示),
class Foo
{
public:
Foo() : h_( INVALID_HANDLE_VALUE )
{
};
// transfer the handle to the new instance
Foo( const Foo& other ) : h_( other.Detach() )
{
};
~Foo()
{
if( INVALID_HANDLE_VALUE != h_ )
CloseHandle( h_ );
};
// other interesting functions...
private:
/// disallow assignment
const Foo& operator=( const Foo& );
HANDLE Detach() const
{
HANDLE h = h_;
h_ = INVALID_HANDLE_VALUE;
return h;
};
/// a non-copyable handle
mutable HANDLE h_;
}; // class …
Run Code Online (Sandbox Code Playgroud) 我有一个Windows Mobile 6的Visual Studio 2008 C++项目,有两个进程.我想要访问process1中包含的同一个函数.
那个功能是Buzz
:
struct TEST_STRUCT
{
int bar;
WCHAR foo[ 20 ];
};
typedef int( *pfn_Buzz )( TEST_STRUCT* );
int Buzz( TEST_STRUCT* info );
Run Code Online (Sandbox Code Playgroud)
Process1包含定义,Buzz
并在内存映射文件中为其创建函数指针:
int Buzz( TEST_STRUCT* info )
{
info->bar = 1;
wsprintf( info->foo, L"Hello!" );
return 100;
}
int _tmain( int argc, _TCHAR* argv[] )
{
// create a memory-mapped file shared memory space that can be read by any process
HANDLE mapping = ::CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, …
Run Code Online (Sandbox Code Playgroud) 如果我有一个返回STL容器的函数,我会生成标准容器的全部内容的副本吗?
这是这样的:
void Foo( std::vector< std::string >* string_list );
Run Code Online (Sandbox Code Playgroud)
比这更好:
std::vector< std::string > Foo();
Run Code Online (Sandbox Code Playgroud)
容器里的东西是否重要?例如,会返回这样的容器:
struct buzz {
int a;
char b;
float c;
}
std::map< int, buzz > Foo();
Run Code Online (Sandbox Code Playgroud)
比这更昂贵的操作:
std::map< int, int > Foo();
Run Code Online (Sandbox Code Playgroud)
谢谢,PaulH
编辑: 这是与C++ 03.遗憾的是,C++ 0x解决方案是不可接受的.
Edit2: 我正在使用Microsoft Visual Studio 2008编译器.
我有一个在Visual Studio 2008中创建的C#.NET 3.5应用程序崩溃在没有开发环境的Windows XP SP3(x86)PC上.
我已经能够从PC获取.dmp文件并将其恢复到我的Windows 7 64位开发PC并将其加载到WinDbg 6.12中.
但是,我无法从C#应用程序中看到调用堆栈中的任何代码.看起来它完全是本机调用堆栈.
结果!analyze -v
如下.
我有相关的EXE,DLL和PDB文件与.DMP在同一目录中.崩溃的可执行文件是在调试模式下编译的.
我也有Visual Studio 2008,如果它更容易使用.但是在那里打开转储文件也只显示本机调用堆栈,而不是我的代码.
如何查看CLR调用堆栈?
0:004> !analyze -v
*******************************************************************************
* *
* Exception Analysis *
* *
*******************************************************************************
FAULTING_IP:
kernel32!RaiseException+53
7c812afb 5e pop esi
EXCEPTION_RECORD: 0392f018 -- (.exr 0x392f018)
ExceptionAddress: 7c812afb (kernel32!RaiseException+0x00000053)
ExceptionCode: e0434f4d (CLR exception)
ExceptionFlags: 00000001
NumberParameters: 1
Parameter[0]: 80070057
PROCESS_NAME: foo.exe
ERROR_CODE: (NTSTATUS) 0xe0434f4d - <Unable to get error code text>
EXCEPTION_CODE: (NTSTATUS) 0xe0434f4d - <Unable to get error code text>
EXCEPTION_PARAMETER1: …
Run Code Online (Sandbox Code Playgroud) 我有一个带有模板类的Visual Studio 2008 C++项目,该模板类在构造函数中获取模板化值,如下所示:
template< typename A >
struct Foo
{
const A& a_;
Foo( const A& a ) : a_( a ) { };
};
Run Code Online (Sandbox Code Playgroud)
因此,我必须像这样构造这个类:
int myval = 0;
Foo< int > foo( myval );
Run Code Online (Sandbox Code Playgroud)
int
当它已在构造函数中指定时,必须指定为模板参数似乎是多余的.我想用某种方式像这样使用它:
Foo foo( myval );
Run Code Online (Sandbox Code Playgroud)
因为,我得到编译器错误:
error C2955: 'Foo' : use of class template requires template argument list
Run Code Online (Sandbox Code Playgroud)
谢谢,PaulH
我有一个Visual Studio 2008 C++ 03应用程序,我想从std :: string复制到char数组,但我需要将char数组空终止,即使它必须截断字符串才能这样做.
例如,这可以按预期工作:
inline void CopyAndNullTerminate( const std::string& source,
char* dest,
size_t dest_size )
{
source.copy( dest, dest_size );
using std::min;
char* end = dest + min( dest_size - 1, source.size() );
*end = '\0';
}
int main()
{
enum{ MAX_STRING_SIZE = 15 };
char dest[ MAX_STRING_SIZE ];
std::string test = "This is a test";
CopyAndNullTerminate( test, dest, MAX_STRING_SIZE );
assert( strcmp( test.c_str(), dest ) == 0 );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我有一个Visual Studio 2008 C++ 03项目,我想在std :: map中读取键值对的文件.为此,我创建了istreambuf_pair_iterator
如下:
typedef std::map< std::string, std::string > Properties;
class istreambuf_pair_iterator :
public boost::iterator_adaptor< istreambuf_pair_iterator,
std::pair< std::string, std::string >*,
boost::use_default,
boost::forward_traversal_tag >
{
public:
istreambuf_pair_iterator() : sb_( 0 ) { };
explicit istreambuf_pair_iterator( std::istream& is ) : sb_( is.rdbuf() ) { };
private:
void increment()
{
std::string line;
std::istream is( sb_ );
std::getline( is, line );
// TODO: parse the key=value to a std::pair
// where do I store the pair???
};
friend class …
Run Code Online (Sandbox Code Playgroud) 在Boost 1.5.1源代码下smart_ptr\detail\atomic_count_win32.hpp
是一个简洁的小原子参考计数器boost::detail::atomic_count
.
在第48行,他们做了一个我很好奇的演员:
class atomic_count
{
public:
// ...
operator long() const
{
return static_cast<long const volatile &>( value_ );
}
private:
long value_;
Run Code Online (Sandbox Code Playgroud)
为什么计数器值转换为a-reference-to-volatile-constant-long(long const volatile&
)?
我有很多函数要以这种格式声明:
int foo_100(int, int);
int foo_200(int, int);
int foo_300(int, int);
int foo_400(int, int);
typedef int (*foo)(int, int);
struct foo foo_library[] = {
foo_100,
foo_200,
foo_300,
foo_400
};
Run Code Online (Sandbox Code Playgroud)
有没有办法可以使用C预处理器来部分自动执行此任务?理想情况下,这样的事情:
foo.txt的
100
200
300
400
Run Code Online (Sandbox Code Playgroud)
foo.h中
typedef int (*foo)(int, int);
#define DEFINE_FOO(id_) int foo_##id_(int, int);
DEFINE_FOO(#include"foo.txt")
struct foo foo_library[] = {
#include "foo.txt"
};
Run Code Online (Sandbox Code Playgroud) c++ ×8
boost ×2
boost-asio ×1
c ×1
c# ×1
containers ×1
debugging ×1
map ×1
optimization ×1
stl ×1
string ×1
templates ×1
windbg ×1
windows ×1