Giter Club home page Giter Club logo

Comments (1)

XavierBrassoud avatar XavierBrassoud commented on September 13, 2024

Hello,

I've solved my problem, I've added a List<string> "bufferPacket" property into my main class Session, which is provided by the byte.MessageString of DelimiterDataReceived event. Then I've added a async method which read the List<string> "bufferPacket" and process Task asynchronously.

Here is the code into the client:

Session session = new Session("127.0.0.1", 4321);
await session.ConnectAsync();
await session.Account.LogonAsync("AccountTest", "TestPassword");

Here is the code into my library:

using SimpleTCP;

namespace ClientLibrary
{
    public class Session
    {
        // Network
        private string address;
        private int port;
        internal SimpleTcpClient client;
        private List<string> bufferPacket;
		
	// Core
        public Account Account;
        private PacketManager packetManager;
        internal bool isConnected = false;
		
		
	public Session(string address, int port)
        {
            this.address = address;
            this.port = port;

            client = new SimpleTcpClient
            {
                Delimiter = 0x00
            };
            client.DelimiterDataReceived += Client_DelimiterDataReceived;

            Account = new Account(this);
            packetManager = new PacketManager(this);
        }
		
	private void Client_DelimiterDataReceived(object sender, Message packet)
        {
            bufferPacket.Add(packet.MessageString);
        }
		
	public async Task ConnectAsync()
        {
            client.Connect(address, port);
            await GetReplyAsync();
        }

        internal async Task GetReplyAsync()
        {
            for (int i = 0; i < bufferPacket.Count(); i++)
            {    
                 processTask = packetManager.Process(bufferPacket[i]);
                    
                 try
                 {
                     await processTask;
                 }
                 catch (AggregateException ae)
                 {
                     throw ae.Flatten();
                 }
            }
            bufferPacket.Clear();
        }
    }
	
    public class Account
    {
        private Session session;

        public Account(Session session)
        {
            this.session = session;
        }

        public async Task LogonAsync(string account, string password)
        {
            if (session.isConnected)
            {
	        session.client.WriteLine(account + '|' + password);
                await session.GetReplyAsync();
            }
	}
    }
	
    public class PacketManager
    {
        private Session session;

        public PacketManager(Session session)
        {
            this.session = session;
        }

        public void Process(string data)
        {
	    string flag = data[0];
			
	    switch (flag)
            {
                case 'A':  // Received from Server "Hello"
		{
		    session.isConnected = true;
		    break;
		}
                // this case is never reached
		case 'B': // Received from Server "Ok, it's your credentials"
		{
                    session.Send("Yeah I received your message!");
		    throw new Exception("This exception now appear! :D");
		    break;
		}
	    }
        }
    }
}


This issue is solved ! (sorry for opened this here)...

from simpletcp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.