I've two classes (Negotiator, Client), both has their own boost::asio::ip::tcp::socket. Is there a way to transfer socket object to Client after negotiation is finished. I'm looking forward to do something like that:
boost::asio::ip::tcp::socket sock1(io);
//...
boost::asio::ip::tcp::socket sock2;
sock2.assign(sock1);
This operation must guarantee that the connection won't be closed when sock1 is destroyed.
I think that you could:
obtain sock1's native handle with the native() member function
dup() (or WSADuplicateSocket()) sock1's native handle
pass the dup()-ed handle to sock2 with the assing() member function
However:
I'm not sure as I never tried that
If you want to transfer (instead of sharing) the socket from Negotiator to Client, Dan's suggestion of using dynamic allocation is probably simpler, using unique_ptr (or auto_ptr)
Create the socket on the heap (new), and pass the pointer from the negotiator to the client.
As of the current version of Boost, you would now get the handle with
boost::asio::ip::tcp::socket::my_socket;
auto my_handle = my_socket.native_handle();
instead of the old native() member function.
Related
I'm need help with websocketspp / websockets++ please (https://github.com/zaphoyd/websocketpp).
I'm open to other simpler libraries, also C, if that's an overall better option :)
My overall goal is to have a websockets webpage as a replacement for a telnet client for DikuMUD.
I've been using the "echo_server" example which is running fine.
I'm trying to save the connection handler "hdl" from one callback and then re-use it later to send another message back to the client. Looks to me like hdl is a class that will get created / destroyed on the stack with each function call to e.g. on_message.
I would like to store the hdl somehow, e.g. in a std::map so that I can look it up and use that looked up hdl to send another message later to the same client.
Here's the example. Sorry for the void , I'm used to C and lightweight C++ :)
std::map<void *, void *> g_cMapHandler;
// Define a callback to handle incoming messages
void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg)
{
void *myExample = 0; // A value I need to be able to retrieve
// Using &hdl here doesn't make sense, I presume hdl gets destroyed when on_message ends.
g_cMapHandler[&hdl] = myExample;
// But I can't figure out what really represents hdl? Maybe there a fd / file descriptor
// I can store somehow, and then how do I rebuild a hdl from it?
}
Thank you :-)
connection_hdl is istelf a pointer, store connection_hdl. It is a weak pointer.
Generally, suggest avoiding void* with asio, and using reference-counted smart pointers. Even though you can control lifetime of object in a synchronous program, and call free or delete when needed, in asynchronous program the flow is varying, so the right place to free pointer could be different place each time.
asio may use boost::weak_ptr or std::weak_ptr. boost one has operator <, so can be directly used in a map. For std, there's std::weak_ptr<T>::owner_before to be used for ordering, can be used via std::owner_less
std::map<websocketpp::connection_hdl, void *, std::owner_less<websocketpp::connection_hdl>> g_cMapHandler;
I'm using the standard C/C++ socket function, but I'd like to encapsulate them into a C++ class. The problem is that the functions for sending and receive returns (or require) pointers to void. Is there any way to use an object that encapsulates those values?
For example, in Java the Socket class uses both ObjectOutputStream and ObjectInputStream in order to work with Object type so every object can be sent via Sockets.
I know that in Java the approach is quite different because the pointers are hidden to the programmer, but is there any similar solution in C++?
socket isn't a c++ function. It's a system level function and it doesn't know anything about objects (or indeed anything in c++), so you have to arrange to provide it with a pointer to the data you want transferred.
As #GCT says, socket isn't a function but is a system level function which is used to handle network connections. In C/C++ each socket is identified with an Integer value, so it's not easy, as you want, to handle it as an object.
I recommend you to read this tutorial to know more about socket.
Maybe it can help you: I have a project that show how to use sockets in C++. Server and client are contained in their own class. You can get it by this link.
The title pretty much says it all. I'm new to Winsock, and I need to know what the scope of a SOCKET object is.
Do I need to worry about it going out of scope when using it in a class member variable (since when it's returned, it's not dynamic memory)?
Thanks.
I'm pretty sure the answer to this is no, but since I can't find the info, I figured I would put it out there, for quick reference to others in the future.
The MSDN documentation for socket says the following:
When a session has been completed, a closesocket must be performed.
And the accompanying sample does just that. The documentation for closesocket is more forceful:
An application should always have a matching call to closesocket for each successful call to socket to return any socket resources to the system.
So as long as you keep the SOCKET descriptor somewhere you can use it until you call closesocket. You could consider putting it inside your own RAII type (or use an existing one) to avoid leaks. If you "forget" the descriptor, you will leak the internal resources.
Internally, SOCKET is just some ID, which is refers to some internal Windows structure. You can work with it like with HANDLE or with usual pointer.
I.e. nothing will happen, if it will go out of scope (but it can leak resources, like HANDLE, if you forgot CloseHandle), if you copy it - you will go 2 same sockets, which are referring to same Windows structure, etc.
I have a C++ Node.js extension that does network communication. Currently, it creates its own TCP connections in C. I would like to have it use sockets created in Node to take advantage of standard libraries like Cluster and to be able to use the existing event loop.
The solution I see is to simply create a net.Socket object and then extract the uv_tcp_t value from the underlying TCPWrap object. There are a couple of issues I see with this option:
The Socket documentation seems to indicate that a socket immediately starts reading when it connects. I would expect that to cause data loss if I want to read on the underlying UV socket in the extension instead of listening for the 'data' event in JavaScript.
While the TCPWrap class has a function to get the underlying uv_tcp_t struct, it does not seem to have an API to relinquish ownership of that struct. I expect this to cause problems later related to disposing of the struct and ownership of its data member (used for user data).
Is there any way to avoid these issues and use the socket in the extension?
I have been working with boost::asio for a while now and while I do understand the concept of the asynchronous calls I am still somewhat befuddled by the memory management implications. In normal synchrous code the object lifetime is clear. But consider a scenario similar to the case of the daytime server:
There might be multiple active connections which have been accepted. Each connection now sends and receives some data from a socket, does some work internally and then decides to close the connection. It is safe to assume that the data related to the connection needs to stay accessible during the processing but the memory can be freed as soon as the connection is closed. But how can I implement the creation/destruction of the data correctly? Assuming that I use classes and bind the callback to member functions, should I create a class using new and call delete this; as soon as the processing is done or is there a better way?
But how can I implement the creation/destruction of the data correctly?
Use shared_ptr.
Assuming that I use classes and bind the callback to member functions, should I create a class using new and call delete this; as soon as the processing is done or is there a better way?
Make your class inherit from enable_shared_from_this, create instances of your classes using make_shared, and when you bind your callbacks bind them to shared_from_this() instead of this. The destruction of your instances will be done automatically when they have gone out of the last scope where they are needed.