Home Forums Support Forwarding packets caused NetworkComms.Net to hang

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #4430
    Anonymous
    Inactive

    Hi,

    I have created a test app based on the UnmanagedConnectionExample. I have implemented the server as per the example and I want the data it receives from a client to be forwarded to another server and the data that server returns to be sent back to the client, like a proxy server so to speak. However, when I create a new connection in the GlobalIncomingUnmanagedPacketHandler, the TCPConnection.GetConnection method hangs (code never gets past this point), what am I doing wrong?

    using NetworkCommsDotNet;
    using NetworkCommsDotNet.Connections;
    using NetworkCommsDotNet.Connections.TCP;
    using NetworkCommsDotNet.DPSBase;
    using NetworkCommsDotNet.Tools;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Net;
    
    namespace ExampleTCA
    {
        public class TCA
        {
            //Create suitable send receive options for use with unmanaged connections
    
            public TCA()
            {
            }
    
            private void SetDebugTimeouts()
            {
                NetworkComms.ConnectionEstablishTimeoutMS = int.MaxValue;
                NetworkComms.PacketConfirmationTimeoutMS = int.MaxValue;
                NetworkComms.ConnectionAliveTestTimeoutMS = int.MaxValue;
            }
    
            public void Start()
            {
                SetDebugTimeouts();
                NetworkComms.AppendGlobalIncomingUnmanagedPacketHandler(IncomingPacketHandler);
    
                //Create suitable send receive options for use with unmanaged connections
                SendReceiveOptions optionsToUse = new SendReceiveOptions<NullSerializer>();
    
                //Get the local IPEndPoints we intend to listen on
                //The port provided is '0' meaning select a random port.
                List<IPEndPoint> localIPEndPoints = (from current in HostInfo.IP.FilteredLocalAddresses()
                                                     select new IPEndPoint(current, 4100)).ToList();
    
                //Create suitable listeners depending on the desired connectionType
                List<ConnectionListenerBase> listeners;
                //For each localIPEndPoint get a TCP listener
                //We need to set the ApplicationLayerProtocolStatus to Disabled
                listeners = (from current in localIPEndPoints
                             select (ConnectionListenerBase)new TCPConnectionListener(optionsToUse, ApplicationLayerProtocolStatus.Disabled)).ToList();
    
                //Start listening
                Connection.StartListening(listeners, localIPEndPoints, true);
    
                Console.WriteLine("Listening for incoming byte[] on:");
                List<EndPoint> localListeningEndPoints = Connection.ExistingLocalListenEndPoints(ConnectionType.TCP);
                foreach (IPEndPoint localEndPoint in localListeningEndPoints)
                    Console.WriteLine("{0}:{1}", localEndPoint.Address, localEndPoint.Port);
            }
    
            private void TraceData(string aPrompt, byte[] aRequestData)
            {
                Trace.WriteLine(aPrompt + ":");
                for (int i = 0; i < aRequestData.Length; i++)
                    Trace.Write(" "+aRequestData[i].ToString());
                Trace.WriteLine("");
            }
    
            private void IncomingPacketHandler(PacketHeader aRequestHeader, Connection aRequestConnection, byte[] aRequestData)
            {
                // TBD: handle the case where get incomplete request (chunked - shouldn't happen but could)
                TraceData("RequestData from " + aRequestConnection.ToString(), aRequestData);
                Forward(aRequestConnection, aRequestData);
            }
    
            private void Forward(Connection aRequestConnection, byte[] aRequestData)
            {
                // Want to add connection pooling here (or does the NetworkCommsDotNetComplete do it already?)
                ConnectionInfo lResponseConnectionInfo = new ConnectionInfo(IPTools.ParseEndPointFromString("172.0.0.1:5100"), ApplicationLayerProtocolStatus.Disabled);
                SendReceiveOptions optionsToUse = new SendReceiveOptions<NullSerializer>();
                Connection lResponseConnection = TCPConnection.GetConnection(lResponseConnectionInfo, optionsToUse);
                lResponseConnection.AppendIncomingUnmanagedPacketHandler((PacketHeader aResponseHeader, Connection aResponseConnection, byte[] aResponseData) =>
                {
                    // TBD: handle the case where get incomplete request (chunked - shouldn't happen but could)
                    TraceData("ResponseData from " + aResponseConnection.ToString(), aResponseData);
                    aRequestConnection.SendUnmanagedBytes(aResponseData);
                });
                lResponseConnection.SendUnmanagedBytes(aRequestData);
            }
    
            public void Stop()
            {
                Connection.StopListening();
            }
        }
    }
    

    Regards,

    Vincent van der Vlis

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.