ET Framework-Server-Program Study Notes

Program study notes

Please follow me on Weibo: @NormanLin_BadPixel Bad Pixel


Before writing the server, I looked at the client code first. In the ET framework, many of the code of the server and the client are shared, which is also a convenient point of ET. So, if you are looking at the server directly, I hope you have a sufficient understanding of the client-side code, and I will briefly mention the code I talked about on the client-side before.

Here is the entry for the client code study notes.
Learn the ET framework with LandlordsCore .


// 异步方法全部会回掉到主线程
OneThreadSynchronizationContext contex = new OneThreadSynchronizationContext();
SynchronizationContext.SetSynchronizationContext(contex);

About this code, I have been unable to understand.

Later we see that Mongo will be initialized on the server side .

MongoHelper
...
public static void Init()
{
    BsonSerializer.RegisterSerializer(new EnumSerializer<NumericType>(BsonType.String));
}

Let me talk about the function of this code here. I also read it for a long time to understand.

When we define an enumeration type, such as

enum sex{
    male,
    female
}

If we use mongo's default method to sequence the sex object, the last saved may be 1 or 2. Because the underlying type of each element in the enumeration is int . However, we want to save the strings male and female during serialization . So, this code is to register custom serialization.

EnumSerializer specifies that it is an object serialized for enumeration,The specified enumeration type, the storage method specified by BsonType.String , here is stored as a string.

Game.EventSystem.Add(DLLType.Model, typeof(Game).Assembly);
Game.EventSystem.Add(DLLType.Hotfix, DllHelper.GetHotfixAssembly());

Likewise, register with the event system.

Options options = Game.Scene.AddComponent<OptionComponent, string[]>(args).Options;

The server needs to be set up here. Let's take a look, we need to pay attention, here the startup parameters of the program are passed in.
OptionComponent study notes

StartConfig startConfig = Game.Scene.AddComponent<StartConfigComponent, string, int>(options.Config, options.AppId).StartConfig;
public class StartConfig: AConfig
{
    public int AppId { get; set; }
    [BsonRepresentation(BsonType.String)]
    public AppType AppType { get; set; }
    public string ServerIP { get; set; }
}

[BsonRepresentation(BsonType.String)] works in the same way as the custom serialization of the enum before, but this is only for this variable , not all references to the enum.

StartConfigComponent study notes

if (!options.AppType.Is(startConfig.AppType))
{
    Log.Error("命令行参数apptype与配置不一致");
    return;
}

As Log said.

Similarly, the server also needs to start its own game logic. Our game logic design can be added to Game.Scene according to the design mode of the ET component . We will add the corresponding components according to the services required by the server. For example , AppType = AllServer will start all services on this server, basically adding all components. Now, continue with our Component study notes .

while (true)
{
    try
    {
        Thread.Sleep(1);
        contex.Update();
        Game.EventSystem.Update();
    }
    catch (Exception e)
    {
        Log.Error(e.ToString());
    }
}

After that, the logic is updated in the main thread.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325815482&siteId=291194637
Recommended