[SetNet & Console Application] First steps. SetNet.PeerBase. Part 2

    Hello, today we continue the tutorial course on the SetNet Server network solution . And today we will talk about the class of the client and the processing of data that is received from customers. Let's get started.

    Let's create a class called “ Peer ”, then connect the namespaces “ SetNet ” and inherit the class “ Peer ” from the base class “ PeerBase “. The class should look like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using SetNet;
    using SetNet;
    namespace Server
    {
        class Peer:PeerBase
        {
        }
    }
    


    Now create the constructor of the Peer class. It should look like this:

    public Peer(ClientInfo info)
                : base(info)
            {
            }
    


    In this case, the constructor accepts an instance of the ClientInfo class in which client information is stored.
    Next, you need to implement an abstract class from “ PeerBase ”, after which the class will look like:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using SetNet;
    namespace ServerTest
    {
        class Peer:PeerBase
        {
            public override void ReceiveData(EventData data)
            {
                throw new NotImplementedException();
            }
            public override void ClientDisconected()
            {
                throw new NotImplementedException();
            }
        }
    }
    


    The “ ReceiveData ” method interacts with messages from the client.

    Consider the “ ReceiveData “ method :

    • The EventData class contains a message code called “ eventCode ” which is of type “ byte “. For eventCode, we will distinguish messages from the client.


    " EventData " contains a Dictionary called " dic " which contains all the transmitted data.
    throw new NotImplementedException (); can be deleted since this line is not needed. It is created after the implementation of the abstract class.

    Consider the interaction with the object " EventData " in method " ReceiveData ":

    First you need to handle the event. To do this, we will sort out:
    switch (data.eventCode)
                {
                    case 1:
                        Console.WriteLine("Received message from the client with {0} eventCode. Message {1}",data.eventCode,data.dic[1].ToString());
                        break;
                    default:
                        Console.WriteLine("Received unknown message from the client with {0} eventCode. Message {1}",data.eventCode,data.dic[1].ToString());
                        break;
                }
    


    Here you can see the search by message code and output. Next, create a server response to the client. To do this, create a new instance of the “ EventData ” class and give it the name “ response ” before the class constructor. Next, in the “ ReceiveData ” method, create an answer in “ case 1 ”.
    EventData response = new EventData(1, new Dictionary { { 1, "Response from the server" } });
    

    Consider the answer:

    SendEvent(response);
    


    The SendEvent method accepts an instance of the EventData class and sends it to the client.
    The ReceiveData method after the changes:
    public override void ReceiveData(EventData data)
            {
                switch (data.eventCode)
                {
                    case 1:
                        Console.WriteLine("Received message from the client with {0} eventCode. Message {1}", data.eventCode, data.dic[1].ToString());
                        EventData response = new EventData(1, new Dictionary { { 1, "Response from the server" } });
                        SendEvent(response);
                        break;
                    default:
                        Console.WriteLine("Received unknown message from the client with {0} eventCode. Message {1}", data.eventCode, data.dic[1].ToString());
                        break;
                }
            }
    


    We can only create an instance of " Peer " in the classroom " GameServer ": go to "class GameServer ". In the “ NewClient ” method, delete the line “ throw new NotImplementedException (); ”In the“ NewClient ” method, create an instance of the“ Peer “ class .

    Now the method will look like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using SetNet;
    namespace Server
    {
        public class GameServer:SetNet.Server
        {
            public GameServer()
                : base()
            {
            }
            public override void NewClient(ClientInfo info)
            {
                Peer peer = new Peer(info);
            }
        }
    }
    

    This is the end of this lesson. Thanks for attention. If you have any questions, write to rebegin@list.ru or skype haker954.

    Also popular now: