Comments (1)
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)
- How to communicate with specific Connected Client
- How to send a message with tcp server to the tcp client in response? HOT 1
- Simpletcp.dll requirements and dependencies --> Missing file or other libray HOT 5
- strong-name related error HOT 3
- memory leak if dont use Delimiter
- creating multiple tcp client connection and sending message to a specific server
- Closing a tcp client without causing NullReferenceExeption in SimpleTCP.dll
- Problem with UWP app
- How can I send a "200" reply to a connected client?
- server.Start(ip, Convert.ToInt32(txtPort.Text)); Null error HOT 1
- strong name missing
- targeting a specific client with a message HOT 3
- How to send Ctrl+A to the client to start session HOT 2
- I need remove - \u0013 HOT 2
- Receber mensagem como cliente
- Why you are not suggesting this item use in Prod? HOT 1
- Is it possible add timeout to .Write() function
- error can't connect to com ip System.OutOfMemoryException
- server.Stop() not close established connections
- How to send message from arduino to Simple TCP server
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from simpletcp.