M
M
Mozzarella2018-03-23 16:05:02
C++ / C#
Mozzarella, 2018-03-23 16:05:02

How to organize a "client-server" architecture on TCP for CCI?

I am developing a training project in c#: a CCG like HearthStone. The network protocol used is TCP.
There are some sketches.
Server class.

public class TCPServer
    {
        private readonly TcpListener _server;
        private readonly List<Message> _messages;
        public TCPServer(int port = 8888)
        {
            var localAddr = IPAddress.Parse("127.0.0.1");
            _server = new TcpListener(localAddr, port);
            Clients = new List<Client>();
        }

        public ICollection<Client> Clients { get; set; }

        public void Start()
        {
            _server.Start();
            var thread = new Thread(Listen);
            thread.Start();
        }

        private void Listen()
        {
            while (true)
            {
                var tcpclient = _server.AcceptTcpClient();
                var client = new Client(tcpclient);
                Clients.Add(client);

            }
        }

        public void Stop()
        {
            _server.Stop();
        }

        public void SendMessage(Message message)
        {
            throw new NotImplementedException();
        }

        public void ReadMessage(Message message)
        {
            throw new NotImplementedException();
        }
    }

Client class
public class Client
    {
        private readonly TcpClient _tcpClient;

        public Client(TcpClient client)
        {
            _tcpClient = client;
        }

        public string Identificator { get; set; }
        public IMessageConverter<string> Converter { get; set; }

        public void Connect()
        {
            //_tcpClient.Connect(_adress,_port);
        }

        public void Disconnect()
        {
            _tcpClient.Close();
        }

        public void SendMessage(Message message)
        {
            var stream = _tcpClient.GetStream();
            var data = Converter.ConvertToBytes(message);
            stream.Write(data, 0, data.Length);
        }

        public void ReadMessage(Message message)
        {
            var stream = _tcpClient.GetStream();
            var data = new byte[64];
            var bytes = 0;
            do
            {
                bytes += stream.Read(data, 0, data.Length);
            } while (stream.DataAvailable);
        }
    }

Message class
public class Message
    {
        public byte[] Content { get; set; }
        public int Length { get; set; }
    }

Interface for message converters to specific formats
public interface IMessageConverter<out T>
    {
        byte[] ConvertToBytes(Message message);
        T ConvertFromBytes(Message message);
    }

How best to organize the connection of clients, create a thread for each and read requests and form a response in an endless loop. Or create a thread that, in the order in which messages from clients are received, will process the message queue

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Peter, 2018-03-23
@sfreaky

1. You have an extra thread for Accept. Where is it better to make the connection of new clients through BeginAcceptTcpClient
2. Further, it is also better to use asynchronous requests

var stream = tcpClient.GetStream();
var asyncResult = stream.BeginRead(buffer, 0, READ_BUFFER_SIZE, ReadStreamCallback, null);
, which will allow you to easily manage the lifetime of the client.
3. But how to execute the requests themselves depends heavily on the architecture of your application. If the requests are all fast (received a request and instantly issued a response), then everything can be served in one thread, and if the requests are heavy, then in order not to slow down with the response to other clients, you can parallelize it.

A
Alexander Yudakov, 2018-03-23
@AlexanderYudakov

For a study project, it doesn't matter. These questions will only matter in production.
To begin with, it makes sense to make a mechanism that will somehow work.
For example, Client.ReadMessage will have to be rewritten a couple more times, pretty scratching the back of the head, so that it outputs a Message without bugs at the output.
So, don’t worry about streams, do what is easier, clearer and more convenient for you.
And only then, after the lapse of time, you will remake it into a cooler version. Work there for 10 minutes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question