In summary I would like to put an IdMappedPortTcp component on port 3218, then depending on a parameter in the url this is redirected to a specific port. For example
x.x.x.x: 3218 / pi = 1 // be redirected to 8080
x.x.x.x: 3218 / pi = 2 // is redirected to 8081
It can be done?
Perhaps, with some work. Assuming each request actually contains a full URL you can extract.
In the
OnConnect event, manually read the URL from the
Context.Connection.IOHandler, then setup the
TIdMappedPortContext.OutboundClient as I described earlier. Any data that you read that needs to be sent to the destination, put it in the
TIdMappedPortContext.NetData property, and then you can manually send it to the destination via the
TIdMappedPortContext.OutboundClient in the
OnOutboundConnect event.
Alternatively, instead of
reading the data and having to manually re-send it afterwards, have your
OnConnect event handler call the
Context.Connection.IOHandler.CheckForDataOnSource() method in a loop until the URL appears in the
Context.Connection.IOHandler.InputBuffer, then
peek it from the buffer without extracting it, then update the
TIdMappedPortContext.OutboundClient as needed. By leaving the data unread in the
InputBuffer, it will get sent automatically once the connection is made successfully, after the
OnOutboundConnect event.
Things get a bit trickier if you need to handle multiple requests per client connection (such as with HTTP persistent connections). Preferably, each request should use a new TCP connection in order to work with multiple destinations. Otherwise, you would likely have to move your processing to the
OnExecute event, and swap out the
TIdMappedPortContext.OutboundClient.IOHandler as needed per request (but then you still need an initial destination for
TIdMappedPortTCP to connect to in between the
OnConnect and
OnOutboundConnect events).
Depending on what you are actually trying to accomplish,
TIdMappedPortTCP may not be a good choice for you. For HTTP, you might consider
TIdHTTPProxyServer or
TIdHTTPServer+
TIdHTTP instead, either of which will let you handle HTTP requests on a per-request basis. Otherwise, you may just have to resort to using a plain vanilla
TIdTCPServer instead and implement your own forwarding logic as needed in its
OnExecute event using your own per-request
TIdTCPClient.
Can you provide more details about what exactly you are trying to accomplish?