jcon-cpp icon indicating copy to clipboard operation
jcon-cpp copied to clipboard

How to pass client's IP to tcp server?

Open SiriusKan opened this issue 6 years ago • 6 comments

Hi First thanks for this great project! What I want to know is, when use the tcp server mode, is there's a simple way to pass the client's IP to server such as the ExampleService in the README?

Thank you for any reply.

SiriusKan avatar Jun 26 '19 04:06 SiriusKan

Hi,

Just curious: why does the server need to know the client's IP?

If you look at https://github.com/joncol/jcon-cpp/blob/master/src/jcon/json_rpc_tcp_server.cpp#L70, you can see that the client's IP is logged. All connected client's are also saved in m_client_endpoints of that class. If you really need this, I could look into adding some better way of retrieving all connected clients' IP addresses... If that's what you need?

joncol avatar Jun 26 '19 05:06 joncol

Thanks for your reply.

In some case, for example, a weather info RPC service, I want to let server response the request accoding to client's location, and I don't want client specify it's location in the request because I want to reduce the request data size, so I want to get client's IP in the server side such as in the ExampleService,

I've seen the code in json_rpc_tcp_server.cpp, I can get peerAddress from the endpoint, and now I can do it by insert a "cli_ip" data into the request param in the jsonRequestReceived() method in the json_rpc_server.cpp, but I think this is a "stupid" way, So I want know if there's a way to add a Interface to the JsonRpcServer class and pass the obj to ExampleService when calling the registerServices.

SiriusKan avatar Jun 26 '19 07:06 SiriusKan

So I want know if there's a way to add a Interface to the JsonRpcServer class and pass the obj to ExampleService when calling the registerServices.

Not sure I understand what you mean by this? What is the "obj" that you want to pass to the service?

joncol avatar Jun 26 '19 10:06 joncol

Sorry for not said clearly.

I mean in json_rpc_server.cpp there's the endpoint object when dispatch request to ExampleServer, so I think it'll be better to pass a obj which contains the endpoint info to ExampleServer, for example, when call rpc_server->registerServices({ new ExampleService() }) , can we pass a such a obj pointer as the ExampleService's param ?

Thx again.

SiriusKan avatar Jun 26 '19 10:06 SiriusKan

I'll try and whip something together, whenever I can find the time!

joncol avatar Jun 28 '19 12:06 joncol

Hi again,

If you have the opportunity, please take a look at https://github.com/joncol/jcon-cpp/pull/41, and see if this would work for you. It's a bit ugly solution perhaps, but it's simple and doesn't complicate the automatic service method lookup.

If you have any comments/feedback please get back to me and I'll try and take them into consideration.

joncol avatar Jul 03 '19 07:07 joncol