Photon Server的服务器端配置
Photon Server与Unity3D的交互分为3篇博文实现
(3)Photon Server与Unity3D客户端的交互
Photon Server是一款实时的Socket服务器和开发框架,快速、使用方便、容易扩展,服务端架构在Windows系统平台上,采用C#语言编写,Photon Server发布包括两个部分,Client SDK Release和Server SDK Update,Server SDK的版本是v2.4.5,而Client SDK的版本是v6.2.0。客户端SDK提供了多种平台的开发API,包括DotNet,Unity3D,C/C++等。SDK就是指可以为第三方开发者提供特定的软件包、软件框架、硬件平台、操作系统等创建应用软件开发工具的集合,并且SDK还能简单的为某个程序设计语言提供应用程序接口API的一些文件。
PhotonServer官网:https://www.photonengine.com/en/OnPremise。帮助文档:官网Documentatain右边的Tutorials、Reference和\Photon-OnPremise-Server-SDK_v4-0-29-11263\doc
1.下载Server SDK(On-Premises)
进入官网,点击页面右上角的SDKs,然后在Choose for your project的条件中选中Server,可以看到图标,点击图标后点Download SDK下载exe文件。运行或右键解压服务器端,不要出现中文目录,解压出\ Photon-OnPremise-Server-SDK_v4-0-29-11263。
\deploy:部署服务器应用,放置开发服务器的代码及相关文件(\deploy\bin_Win64\PhotonControl.exe:服务器程序运行文件,证书存放的在\deploy\bin_Win64)
\doc:存放帮助文档
\lib:存放动态链接库(Photon3Unity3D.dll是用来开发基于Unity3D的客户端,ExitGamesLibs.dll、Photon.SocketServer.dll、PhotonHostRuntimeInterfaces.dll是用来开发服务器端)
\src-server:提供一些项目的源码
2. 配置server端的PhotonServer.config文件
打开deploy\bin_Win64\PhotonServer.config。一个Photon instance代表一类配置,一个Photon instance可以包含多个服务器端的应用。
<MMoInstance <!--这个Photon instances的名称--> MaxMessageSize="512000" MaxQueuedDataPerPeer="512000" PerPeerMaxReliableDataInTransit="51200" PerPeerTransmitRateLimitKBSec="256" PerPeerTransmitRatePeriodMilliseconds="200" MinimumTimeout="5000" MaximumTimeout="30000" DisplayName="MMO" <!--显示在Photon instances的名称--> > <!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. --> <!-- Port 5055 is Photon's default for UDP connections. --> <UDPListeners> <UDPListener IPAddress="0.0.0.0" Port="5055" OverrideApplication="Minecraft">"<!--指明这个端口号是给哪个Application使用的--> </UDPListener> </UDPListeners> <!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. --> <!-- Port 4530 is Photon's default for TCP connecttions. --> <!-- A Policy application is defined in case that policy requests are sent to this listener (known bug of some some flash clients) --> <TCPListeners> <TCPListener IPAddress="0.0.0.0" Port="4530" PolicyFile="Policy\assets\socket-policy.xml" InactivityTimeout="10000" OverrideApplication="Minecraft" > </TCPListener> </TCPListeners> <!-- Policy request listener for Unity and Flash (port 843) and Silverlight (port 943) --> <PolicyFileListeners> <!-- multiple Listeners allowed for different ports --> <PolicyFileListener IPAddress="0.0.0.0" Port="843" PolicyFile="Policy\assets\socket-policy.xml" InactivityTimeout="10000"> </PolicyFileListener> <PolicyFileListener IPAddress="0.0.0.0" Port="943" PolicyFile="Policy\assets\socket-policy-silverlight.xml" InactivityTimeout="10000"> </PolicyFileListener> </PolicyFileListeners> <!-- WebSocket (and Flash-Fallback) compatible listener --> <WebSocketListeners> <WebSocketListener IPAddress="0.0.0.0" Port="9090" DisableNagle="true" InactivityTimeout="10000" OverrideApplication="Minecraft"> </WebSocketListener> </WebSocketListeners> <!-- Defines the Photon Runtime Assembly to use. --> <Runtime Assembly="PhotonHostRuntime, Culture=neutral" Type="PhotonHostRuntime.PhotonDomainManager" UnhandledExceptionPolicy="Ignore"> </Runtime> <!-- Defines which applications are loaded on start and which of them is used by default. Make sure the default application is defined. --> <!-- Application-folders must be located in the same folder as the bin_win32 folders. The BaseDirectory must include a "bin" folder. --> <Applications Default="Minecraft"><!--客户端连接服务器未指定Application时连接默认的Application-->
<!-- MMO Demo Application --> <Application Name="Minecraft"<!--应用名称--> BaseDirectory="MinecraftServer"<!--\deploy下这个服务器应用的文件名称--> Assembly="MinecraftServer"<!-—程序集名称--> Type="MinecraftServer.MinecraftServer"<!--主类名称--> ForceAutoRestart="true"<!--是否自动重启--> WatchFiles="dll;config" ExcludeFiles="log4net.config"> </Application> <!-- CounterPublisher Application --> <Application Name="CounterPublisher" BaseDirectory="CounterPublisher" Assembly="CounterPublisher" Type="Photon.CounterPublisher.Application" ForceAutoRestart="true" WatchFiles="dll;config" ExcludeFiles="log4net.config"> </Application> </Applications> </MMoInstance>
3.创建server端类库
(1)创建类库:文件-新建-项目-类库,创建类库MinecraftServer
(2)创建Application文件夹:在\deploy下创建文件夹MinecraftServer(与配置文件PhotonServer.config里Application中的BaseDirectory对应),然后在Minecraft下创建文件夹bin
(3)设置dll文件生成路径:右键项目-属性-生成-输出路径,设置为\deploy\MinecraftServer\bin
(4)修改程序集名称跟默认命名空间:右键项目-属性-应用程序,都设置为MinecraftServer(与配置文件PhotonServer.config里Application中的Assembly对应)。
(5)生成dll文件:右键项目-生成。以后每次修改都要重新生成。
4.添加server端动态链接库
在\lib里,将ExitGamesLibs.dll、Photon.SocketServer.dll、PhotonHostRuntimeInterfaces.dll 添加到类库引用
5.创建Server端主类与客户端连接类
主类命名用项目名称MinecraftServer并继承ApplicationBase,该类为服务器端程序入口。
using Photon.SocketServer; namespace MinecraftServer { //所有的server端主类都要继承自Application public class MinecraftServer: ApplicationBase {
//server端启动的时候调用,作初始化
protected override void Setup()
{
}
//当一个客户端请求连接时调用,我们使用一个PeerBase表示Server端和一个客户端的连接,用来管理server端与客户端请求的发送与接收 protected override PeerBase CreatePeer(InitRequest initRequest) {
return new ClientPeer(initRequest);//ClientPeer继承自PeerBase,InitRequest包含连接请求的各种参数。new出来后PhotonServer会帮我们管理ClientPeer。 } //server端关闭的时候调用 protected override void TearDown() {
} } }
客户端连接类ClientPeer继承PeerBase,每一个客户端连接进来都会创建一个ClientPeer,用来管理server端与客户端请求的发送与接收
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;
namespace MinecraftServer { //每一个客户端连接进来都会创建一个ClientPeer public class ClientPeer : Photon.SocketServer.ClientPeer {
public ClientPeer(InitRequest initRequest):base(initRequest)
{
}
//处理客户端断开连接的后续工作 protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail) { } //处理客户端的请求 protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) { } } }
6.配置日志
(1)添加动态链接库log4net.dll(log4net是做日志输出的插件)和ExitGames.Logging.Log4Net.dll(Photon提供用来连接Photon与log4net)。右键引用-添加引用-浏览,在\lib里。
(2)配置log4net.config。参考\src-server\Mmo\Photon.MmoDemo.Server\log4net.config。详细学习推荐上官网http://logging.apache.org/log4net或博客园。
<?xml version="1.0" encoding="utf-8" ?> <log4net debug="false" update="Overwrite"> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="%property{Photon:ApplicationLogPath}\\Minecraft.Server.log" /><!--value表示日志文件所在的完整路径,property{Photon:ApplicationLogPath}是根目录在MyGameServer.cs里配置,Minecraft.Server.log日志的名称 --> <appendToFile value="true" /> <maximumFileSize value="5000KB" /> <maxSizeRollBackups value="2" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%d [%t] %-5p %c - %m%n" /> </layout> </appender> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="DEBUG" /> <levelMax value="FATAL" /> </filter> </appender> <!-- logger --> <root> <level value="INFO" /> <!--<appender-ref ref="ConsoleAppender" />--> <appender-ref ref="RollingFileAppender" /> </root> <logger name="OperationData"> <level value="INFO" /> </logger> </log4net> <!--这个文档的复制到输出目录要选择始终复制 -->
(3)日志初始化
using Photon.SocketServer; using ExitGames.Logging; using ExitGames.Logging.Log4Net; using System.IO; using log4net.Config; namespace MinecraftServer { //所有的server端主类都要继承自Application public class MinecraftServer: ApplicationBase {
//需using ExitGames.Logging;log用来做日志输出
public static readonly ILogger log = LogManager.GetCurrentClassLogger();
//server端启动的时候调用,作初始化 protected override void Setup() { //日志的初始化 //this.ApplicationRootPath获取photonServer应用的根目录(D:\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy) log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(Path.Combine(this.ApplicationRootPath, "bin_Win64"), "log"); //Path.Combine() 会屏蔽平台的差异。this.BinaryPath获取输出路径(D:\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\MyGameServer\bin) FileInfo configFileInfo = new FileInfo(Path.Combine(this.BinaryPath, "log4net.config"));//需using System.IO; if(configFileInfo.Exists) { LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);//让photon知道使用的是Log4Net的日志插件。需using ExitGames.Logging.Log4Net; XmlConfigurator.ConfigureAndWatch(configFileInfo);//让log4net这个插件读取配置文件。需using log4net.Config; } //日志输出 log.Info("服务器应用启动成功!"); } //当一个客户端请求连接时调用,我们使用一个PeerBase表示Server端和一个客户端的连接,用来管理server端与客户端请求的发送与接收 protected override PeerBase CreatePeer(InitRequest initRequest) { log.Info("一个客户端应用连接进来!"); return new ClientPeer(initRequest);//ClientPeer继承自PeerBase,InitRequest包含连接请求的各种参数 }
//server端关闭的时候调用 protected override void TearDown() { log.Info("服务器应用关闭!"); } } }
MinecraftServer.log.Info输出Info类信息,MinecraftServer.log.Debug输出Debug类信息,MinecraftServer.log.Error输出Error类信息等为日志的输出做分类。
一个应用有3个日志文件:Photon.CLR(输出应用的配置信息跟Listeners的信息)、Photon-MMoInstance-20180212.log(MMoInstance模块应用启动顺序的输出)、Minecraft.Server.log(自定义的输出)。