Sign in to follow this  
AbanoubNassem

boost::asio errors

Recommended Posts

AbanoubNassem    114
the question is how can i convert this[code] `boost::asio::async_read(socket_,boost::asio::buffer((char*)buffer, 1000),
boost::bind(&Connection::Receive, this,
boost::asio::placeholders::error));`[/code] to [code]int[/code] so i can check if i recived something?

[code] #ifndef _CONNECTION_H_
#define _CONNECTION_H_

#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include <stdint.h>

class Connection
{
public:
Connection(boost::asio::io_service& io_service);
~Connection();
boost::asio::ip::tcp::socket& socket(){return socket_;}
void Send(uint8_t* buffer,int length);
bool Receive();

protected:
virtual void OnReceived(uint8_t* buffer, int len) = 0;

private:
boost::asio::ip::tcp::socket socket_;
};

#endif
--------------------------------------------------------------------------------------------------
#include "Connection.h"


Connection::Connection(boost::asio::io_service& io_service)
: socket_(io_service){
}

Connection::~Connection(){
}

void Connection::Send(uint8_t* buffer,int length){
boost::asio::async_write(socket_,boost::asio::buffer(buffer,length),
boost::bind(&Connection::Send, this,boost::asio::placeholders::error));
}

bool Connection::Receive(){
uint8_t* buffer = new uint8_t[1000];
int recvlen = boost::asio::async_read(socket_,boost::asio::buffer((char*)buffer, 1000),
boost::bind(&Connection::Receive, this,
boost::asio::placeholders::error));

if (recvlen <= 0) {
delete[] buffer;
return false;
}
this->OnReceived(buffer, recvlen);
delete [] buffer;
return true;
}
[/code]
errors:

[code] Error 1 error C2825: 'F': must be a class or namespace when followed by '::' e:\boost_1_46_1\boost_1_46_1\boost\bind\bind.hpp 69
Error 2 error C2039: 'result_type' : is not a member of '`global namespace'' e:\boost_1_46_1\boost_1_46_1\boost\bind\bind.hpp 69
Error 3 error C2146: syntax error : missing ';' before identifier 'type' e:\boost_1_46_1\boost_1_46_1\boost\bind\bind.hpp 69
Error 4 error C2208: 'boost::_bi::type' : no members defined using this type e:\boost_1_46_1\boost_1_46_1\boost\bind\bind.hpp 69
Error 5 error C1903: unable to recover from previous error(s); stopping compilation e:\boost_1_46_1\boost_1_46_1\boost\bind\bind.hpp 69
6 IntelliSense: a value of type "void" cannot be used to initialize an entity of type "int" d:\c++\ugs\common\connection.cpp 18
7 IntelliSense: the #endif for this directive is missing d:\c++\ugs\common\connection.h 1
8 IntelliSense: this declaration has no storage class or type specifier d:\c++\ugs\common\connection.h 26[/code]

whats wrong and how can i fix it thanks :) i appreciate it.

Share this post


Link to post
Share on other sites
Antheus    2409
[quote name='Mr_PoP' timestamp='1305927081' post='4813644']
whats wrong and how can i fix it thanks :) i appreciate it.
[/quote]

You have angered the ancient gods. The evil has been awoken...


[quote]to int so i can check if i recived something?[/quote]
You can't, the call is asynchronous.

The result will be received in your callback OnReceived. The len there will be how much has been read.

See the tutorial on how to pass the recv parameters properly, I think your call is missing the length parameter, it only has error.

Share this post


Link to post
Share on other sites
AbanoubNassem    114
[quote name='Antheus' timestamp='1305927912' post='4813651']
You have angered the ancient gods. The evil has been awoken...
[/quote]

ahahah LOL

[quote name='Antheus' timestamp='1305927912' post='4813651']
You can't, the call is asynchronous.

The result will be received in your callback OnReceived. The len there will be how much has been read.

See the tutorial on how to pass the recv parameters properly, I think your call is missing the length parameter, it only has error.
[/quote]

aha I C, okey can you link me to the Tut. thnx for your answer :)

Share this post


Link to post
Share on other sites
Antheus    2409
Just [url="http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/tutorial/tutdaytime6.html"]the calls[/url].

[url="http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/tutorial.html"]Basic overview[/url].

[url="http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio.html"]The rest.[/url]

Share this post


Link to post
Share on other sites
AbanoubNassem    114
[quote name='Antheus' timestamp='1305929475' post='4813663']
Just [url="http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/tutorial/tutdaytime6.html"]the calls[/url].

[url="http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/tutorial.html"]Basic overview[/url].

[url="http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio.html"]The rest.[/url]
[/quote]

thnx i appreciate it.

the calls link u send me is for UPD is it the same for the TCP?
and it's an asynchronous while i needs it asynchronous coz it's a server for a gameserver :)

Share this post


Link to post
Share on other sites
hplus0603    11356
[quote name='Mr_PoP' timestamp='1305929784' post='4813666']
and it's an asynchronous while i needs it asynchronous coz it's a server for a gameserver :)
[/quote]



Actually, the list of the tutorials talks about sync and async TCP and UDP, so you can find what you need right there: [url="http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/tutorial.html"]http://www.boost.org/doc/libs/1_46_0/doc/html/boost_asio/tutorial.html[/url]
All the functions are documented in the reference: [url="http://www.boost.org/doc/libs/1_46_0/doc/html/boost_asio/reference.html"]http://www.boost.org/doc/libs/1_46_0/doc/html/boost_asio/reference.html[/url]
If you know C++, and understand asynchronous/event-driven/callback-based/reactor-style programming (many names for the same thing), boost::asio can serve your C++ networking needs very well.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this