#ifndef UTILS_NETWORK_TCP_CONNECTION_HPP
#define UTILS_NETWORK_TCP_CONNECTION_HPP
namespace utils{
namespace thread{
template
class tcp_connection{
private:
int newsockfd;
shared_obj_type obj;
thread_pool_type pool;
serverlet_type serverlet;
bool initialized_;
public:
tcp_connection() : initialized_ (false) {}
tcp_connection(int newsockfd, shared_obj_type obj,
thread_pool_type pool, serverlet_type serverlet)
: newsockfd(newsockfd), obj(obj), pool(pool),
serverlet(serverlet), initialized_(true)
{
}
bool initialized() { return initialized_; }
bool initialize(int newsockfd, shared_obj_type obj,
thread_pool_type pool, serverlet_type serverlet);
void operator ()();
};
template
bool tcp_connection::initialize
(int newsockfd, shared_obj_type obj, thread_pool_type pool,
serverlet_type serverlet)
{
this->newsockfd = newsockfd;
this->obj = obj;
this->pool = pool;
this->serverlet = serverlet;
initialized_ = true;
return initialized_;
}
template
void tcp_connection::operator() ()
{
//if(initialized_) serverlet_type(newsockfd, obj); <----
causes error
pool->release();
}
}
}
#endif
main.cpp: provides definition of serverlet_type
#include "utils/thread/tcp_server.hpp"
#include "utils/thread/thread_pool.h"
#include
#include <vector>
#include <iostream>
typedef std::vector<int> shared_obj_type;
typedef boost::shared_ptr thread_pool_type;
typedef void (*serverlet_type)(int, shared_obj_type);
void servlet(int sock, shared_obj_type ){
std::cout << "got a new socket for a new connection: " << sock <<
std::endl;
}
int main(){
shared_obj_type x;
thread_pool_type p( new boost_thread::thread_pool(20) );
utils::thread::tcp_server server;
server.initialize(x, p, servlet, 10000, 5);
boost::thread s(server);
s.join();
}
/root/cpp-utils-header-lib/include/utils/thread/tcp_connection.hpp: In
member function `void utils::thread::tcp_connection::operator()() [with shared_obj_type =
shared_obj_type, thread_pool_type = thread_pool_type, serverlet_type =
void (*)(int, shared_obj_type)]':
/usr/include/boost/function/function_template.hpp:155: instantiated
from `static void
boost::detail::function::void_function_obj_invoker0::invoke(boost::detail::function::function_buffer&) [with FunctionObj
= utils::thread::tcp_connection, R = void]'
/usr/include/boost/function/function_template.hpp:368: instantiated
from `void boost::detail::function::basic_vtable0::init(FunctionObj, boost::detail::function::function_obj_tag)
[with FunctionObj = utils::thread::tcp_connection, R = void, Allocator =
std::allocatorboost::function_base]'
/usr/include/boost/function/function_template.hpp:298: instantiated
from `void boost::detail::function::basic_vtable0::init(F)
[with F = utils::thread::tcp_connection, R = void, Allocator =
std::allocatorboost::function_base]'
/usr/include/boost/function/function_template.hpp:277: instantiated
from `boost::detail::function::basic_vtable0::basic_vtable0(F) [with F =
utils::thread::tcp_connection, R = void, Allocator =
std::allocatorboost::function_base]'
/usr/include/boost/function/function_template.hpp:655: instantiated
from `void boost::function0::assign_to(Functor) [with
Functor = utils::thread::tcp_connection, R = void, Allocator =
std::allocatorboost::function_base]'
/usr/include/boost/function/function_template.hpp:513: instantiated
from `boost::function0::function0(Functor, typename
boost::enable_if_c< boost::type_traits::ice_not<
boost::is_integral<Functor>::value>::value, int>::type) [with Functor =
utils::thread::tcp_connection, R = void, Allocator =
std::allocatorboost::function_base]'
/root/cpp-utils-header-lib/include/utils/thread/tcp_server.hpp:107:
instantiated from `void utils::thread::tcp_server::operator()()
[with shared_obj_type = shared_obj_type, thread_pool_type =
thread_pool_type, serverlet_type = void (*)(int, shared_obj_type),
accept_fail_policy = utils::thread::default_accept_fail_policy]'
/usr/include/boost/function/function_template.hpp:155: instantiated
from `static void
boost::detail::function::void_function_obj_invoker0::invoke(boost::detail::function::function_buffer&) [with FunctionObj
= utils::thread::tcp_server, R
= void]'
/usr/include/boost/function/function_template.hpp:368: instantiated
from `void boost::detail::function::basic_vtable0::init(FunctionObj, boost::detail::function::function_obj_tag)
[with FunctionObj = utils::thread::tcp_server, R = void, Allocator =
std::allocatorboost::function_base]'
/usr/include/boost/function/function_template.hpp:298: instantiated
from `void boost::detail::function::basic_vtable0::init(F)
[with F = utils::thread::tcp_server, R = void, Allocator =
std::allocatorboost::function_base]'
/usr/include/boost/function/function_template.hpp:277: instantiated
from `boost::detail::function::basic_vtable0::basic_vtable0(F) [with F =
utils::thread::tcp_server, R
= void, Allocator = std::allocatorboost::function_base]'
/usr/include/boost/function/function_template.hpp:655: instantiated
from `void boost::function0::assign_to(Functor) [with
Functor = utils::thread::tcp_server, R = void, Allocator =
std::allocatorboost::function_base]'
/usr/include/boost/function/function_template.hpp:513: instantiated
from `boost::function0::function0(Functor, typename
boost::enable_if_c< boost::type_traits::ice_not<
boost::is_integral<Functor>::value>::value, int>::type) [with Functor =
utils::thread::tcp_server, R
= void, Allocator = std::allocatorboost::function_base]'
tests/boost_tcp_server_test.cpp:24: instantiated from here
/root/cpp-utils-header-lib/include/utils/thread/tcp_connection.hpp:61:
error: functional cast expression list treated as compound expression
/root/cpp-utils-header-lib/include/utils/thread/tcp_connection.hpp:61:
warning: left-hand operand of comma has no effect
/root/cpp-utils-header-lib/include/utils/thread/tcp_connection.hpp:61:
error: cannot convert
`((void)((utils::thread::tcp_connection*)this)->utils::thread::tcp_connection::newsockfd,
((utils::thread::tcp_connection*)this)->utils::thread::tcp_connection::obj)' from type
`shared_obj_type' to type `void (*)(int, shared_obj_type)'