Toemsel / Network

C# Network Library

Home Page:https://push-force.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

An example is needed

aidenpearce79 opened this issue · comments

  1. How to create ClientConnectionContainer, without an asynchronous, events, etc.
private static readonly ClientConnectionContainer _container;

static MyService() {
   _container = CreateClientConnectionContainer("...", 7777);

>    // What i need do here for container initialized/connected without async/await, events (like CreateTcpConnection(...))?
>    // It is correct to immediately send a request here (not in the constructor of course, after this point in GetTickets() for example)

}
  1. What is the correct way to synchronously send packets in parallel use ClientConnectionContainer with the ability of receive a response?
public static TicketsResponse GetTickets(int id) {
   var response =   _container.SendAsync<TicketsResponse>(new TicketsRequest(id))....; 

> // .GetAwaiter().GetResult()?
> // .Result?
> // Wrap to Task.Run with local variable? How get response synchronously without async/await, events, etc.?

   return response;
}

Client code:

static void Main() {
   Parallel.For(0, 100, (i)=>{
      var response = MyService.GetTickets(i);
      if (response.Succes) {
         ...
      }
   });
}

Please add or give link to complex sample with that requirements, or just kick me in the right direction ))

  1. How to create ClientConnectionContainer, without an asynchronous, events, etc.

The network is async and event driven. However, if you like a synchronous behaviour, you could "misapply" the TAP pattern. As an example:

            TcpConnection tcpConnection = ConnectionFactory.CreateTcpConnection("...", xxx, out ConnectionResult tcpRes);
            UdpConnection udpConnection = ConnectionFactory.CreateUdpConnection(tcpConnection, out ConnectionResult udpRes);
            ClientConnectionContainer container = ConnectionFactory.CreateClientConnectionContainer(tcpConnection, udpConnection);

The ClientConnectionContainer is ready to be used, since the TCP and UDP connections are already alive.

If you take a closer look to "CreateTcpConnection" and "CreateUdpConnection" within the factory, both are actually async, but the execution won't be asynchronous, but synchronous. (Compared to CreateTcpConnectionAsync)

  1. What is the correct way to synchronously send packets in parallel use ClientConnectionContainer with the ability of receive a response?

container.SendAsync<TicketsResponse>(new TicketsRequest(id), ConnectionType.TCP).Result;

If you don't like the async pattern, you can stick to objects. However, you would require to write a "wrapper" for each object you create.

            Parallel.For(0, 100, i =>
            {
                TicketRequestWrapper wrapper = new TicketRequestWrapper(i, connectionContainer);
                TicketResponse = wrapper.GetResponse();
                //...
            });

    public class TicketRequestWrapper
    {
        int ticketid;
        private TicketResponse response = null;
        private ManualResetEvent resetEvent;
        private ClientConnectionContainer connectionContainer;

        public TicketRequestWrapper(int id, ClientConnectionContainer connectionContainer)
        {
            this.ticketid = id;
            this.connectionContainer = connectionContainer;
            resetEvent = new ManualResetEvent(false);

            connectionContainer.TCP_RegisterPacketHandler<TicketResponse>(TicketResponse, this);
        }

        public TicketResponse GetResponse()
        {
            connectionContainer.SendSlow(new TicketRequest(ticketid));
            resetEvent.WaitOne();
            return response;
        }

        private void TicketResponse(TicketResponse response, Connection con)
        {
            this.response = response;
            resetEvent.Set();
        }
    }

Please contact if the issue hasn't be resolved yet.