Home Forums Support Closed/closing connection strategy

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2259
    Anonymous
    Inactive

    Hi Marc,

    I’m encountering the error “Attempting to send packet on connection which has been closed or is currently closing.” in my application without really knowing why the connection would fail or disconnect itself. I now want to establish a reconnection strategy in some proxy class so that my application doesn’t have to know about a disconnected state between my processes. Would you have a suggestion on how to do that in a clean way? The idea I have for now is that if the server gets the ConnectionCloseHandler fired, I should setup the server again right away, does that sound right? It does sound straightforward though!

    Also, what does that error really mean? Do I have to setup the server side again? On a different port may be?

    Just to be clear about what would be my ideal usage of your library, is that I would not deal with this.

    Best regards,

    David

    #2262
    Anonymous
    Inactive

    The error means the connection you are attempting to use is marked as disconnected. If connections go over the internet there are many reasons why they might be closed, outside of your control. If all connections are within the same network then a closed connection suggests either an error or that you are closing a connection unintentionally in code.

    The best solution to find out why connections are being closed is to enable logging (link). Look for all references starting “Closing connection with“.

    If this is an issue client side whenever you need access to a connection it is preferable to use:

    TCPConnection.GetConnection(ConnectionInfo).SendObject("Data", customObject);

    which maximises the chances of having an established connection, rather than maintaining a reference to a previously established connection and always using that. An alternative, as you suggest, is to use the ConnectionCloseHandler to reconnect.

    Hope that points you in the right direction to a suitable solution. Please post back with any further issues.

    Regards,
    Marc

    #2263
    Anonymous
    Inactive

    Is using TCPConnection.GetConnection at every call going to slow down my application? Is it thread safe?

    On the client side, if I use AppendShutdownHandler to get my connection when necessary is it going to have the same result as always getting the connection?

    So on the server side there is ConnectionCloseHandler. On which case should that be triggered? Once I start listening, is there a chance that the server stops by itself? If so, do I restart it in a new port?

    #2264
    Anonymous
    Inactive

    By the way, I do two ways communications. My TCPConnection has many packet handlers. With that said, does using GetConnection all the time seem like a good approach?

    #2269
    Anonymous
    Inactive

    Is using TCPConnection.GetConnection at every call going to slow down my application? Is it thread safe?

    The only additional step performed when using TCPConnection.GetConnection is to lookup the connection in a dictionary. If it has been disconnected a new established connection will be returned instead.

    If you make extensive use of Connection specific packet handlers a better solution would be to catch the CommunicationException if the connection is closed and reconnect manually.

    Yes, most NetworkComms.Net methods are thread safe.

    Once I start listening, is there a chance that the server stops by itself? If so, do I restart it in a new port?

    The server will only stop listening if an exception occurs. You can enable logging or query the IsListening methods to determine what may be happening.

    When you restart you can use the same port if you choose.

    If hope the above answers help. You asked a lot of questions and for some of them I am not sure what you were asking. If you have any further questions please feel free to post back.

    Regards,
    Marc

    #2270
    Anonymous
    Inactive

    Hi Marc,

    Thanks for your response.

    I would be okay using TCPConnection.GetConnection all the time on my clientside if I woudln’t have packet handlers on that connection. Would it be possible for TCPConnection.GetConnection to tell me if it returned a new connection so that I can set the handlers on that connection as well?

    Best regards,

    David

    #2272
    Anonymous
    Inactive

    Would it be possible for TCPConnection.GetConnection to tell me if it returned a new connection so that I can set the handlers on that connection as well?

    No, but you could check each connection before it is used to ensure that the necessary packet handlers exist, if not add them:

    Connection conn = TCPConnection.GetConnection();
    
    if (!conn.IncomingPacketHandlerExists("data", packetHandler))
        conn.AppendIncomingPacketHandler("data", packetHandler);

    A question though, why not just set global packet handlers on the clientside:

    NetworkComms.AppendGlobalIncomingPacketHandler("data", packetHandler);

    Then you don’t have to worry about connection specific packet handlers.

    Regards,
    Marc

    #2273
    Anonymous
    Inactive

    Thanks for your response.

    My client is to be connected on various servers.

    Best regards,

    David

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.