ET框架-服务端-Program学习笔记

Program学习笔记

请大家关注我的微博:@NormanLin_BadPixel坏像素


在写服务端之前,我是先看的客户端代码。而ET框架,服务端和客户端的代码很多都是共用的,这也是ET方便的一点。所以,如果你是直接来看服务端的,希望你对客户端的代码已经有了足够的了解,之前在客户端讲过的代码我会一笔带过。

这里是客户端代码学习笔记的入口。
通过LandlordsCore 学习ET框架


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

关于这段代码,我一直搞不懂。

之后我们看到,会在服务端对Mongo进行初始化。

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

这里讲一下这段代码的作用,我也是翻阅好久才搞明白。

我们在定一个枚举类型的时候,比如

enum sex{
    male,
    female
}

如果我们用mongo默认的方法序列话sex对象的话,最后保存的可能是1或2。因为枚举中每个元素的基础类型是int。但是,我们想在序列化的时候保存的是malefemale这字符串。所以,这段代码就是注册自定义序列化的。

EnumSerializer指定是针对枚举序列化的对象, 指定的枚举类型,BsonType.String指定的储存方式,这里是用字符串储存。

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

同样的,注册事件系统。

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

这里需要对服务器进行设置。我们去看看,需要注意一下,这里把程序的启动参数传进去了。
OptionComponent学习笔记

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)] 的作用跟之前对枚举的自定义序列化方式差不多,不过这里是只针对这个变量,而不是应用到这个枚举的所有引用。

StartConfigComponent学习笔记

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

Log所说。

同样的,服务端也要启动自己的游戏逻辑,我们对游戏逻辑的设计,根据ET组件的设计模式,都可以添加到Game.Scene里面。我们会根据服务器需要的服务添加对应的组件。比如AppType = AllServer说明会在这个服务器上启动所有的服务,基本上添加所有的组件,现在,继续我们的Component学习笔记

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

之后,就是在主线程内更新逻辑了。

猜你喜欢

转载自blog.csdn.net/norman_lin/article/details/79992417