当前位置: 首页>>代码示例>>C#>>正文


C# ServerMessage.EncodeMessage方法代码示例

本文整理汇总了C#中ServerMessage.EncodeMessage方法的典型用法代码示例。如果您正苦于以下问题:C# ServerMessage.EncodeMessage方法的具体用法?C# ServerMessage.EncodeMessage怎么用?C# ServerMessage.EncodeMessage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ServerMessage的用法示例。


在下文中一共展示了ServerMessage.EncodeMessage方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SendRegistration

        private void SendRegistration()
        {
            if (NetTime.Now > m_lastRegistered + 30)
            {
                NetOutgoingMessage netOutgoingMessage = this.m_netServer.CreateMessage();

                IPAddress IPAddressMask;
                IPAddress IPaddress = NetUtility.GetMyAddress(out IPAddressMask);

                ServerMessage serverMessage = new ServerMessage
                {
                    ID = this.m_netServer.UniqueIdentifier,
                    IPAddress = IPaddress.ToString(),
                    Port = APIMain.ServerPort,
                    Hostname = "L.O.G. Server",
                    Players = this.m_playersList.Count,
                    MaximumPlayers = 32,
                    Ping = 0,

                    MessageType = ServerMessageTypes.RegisterHost
                };

                serverMessage.EncodeMessage(netOutgoingMessage);
                netOutgoingMessage.Write(new IPEndPoint(IPaddress, APIMain.ServerPort));

                this.m_netServer.SendUnconnectedMessage(netOutgoingMessage, this.m_masterServer);

                m_lastRegistered = (float)NetTime.Now;
            }
        }
开发者ID:WopsS,项目名称:L.O.G,代码行数:30,代码来源:Network.cs

示例2: HandleMessage

        /// <summary>
        /// Handle messages from server or client and choose what to do with them.
        /// </summary>
        private void HandleMessage()
        {
            NetIncomingMessage netIncomingMessage;

            while ((netIncomingMessage = m_netPeer.ReadMessage()) != null)
            {
                switch (netIncomingMessage.MessageType)
                {
                    case NetIncomingMessageType.UnconnectedData:
                        {
                            switch ((ServerMessageTypes)netIncomingMessage.ReadByte())
                            {
                                case ServerMessageTypes.RegisterHost:
                                    {
                                        ServerMessage serverMessage = new ServerMessage(netIncomingMessage); // Decode server message.

                                        serverMessage.IPendPoint = new IPEndPoint[]
                                        {
                                            netIncomingMessage.ReadIPEndPoint(),
										    netIncomingMessage.SenderEndPoint
                                        };

                                        if (m_registredServers.ContainsKey(serverMessage.ID) == false)
                                        {
                                            m_registredServers.Add(serverMessage.ID, serverMessage);
                                        }
                                        else
                                        {
                                            m_registredServers[serverMessage.ID] = serverMessage;
                                        }

                                        Log.HandleLog(LOGMessageTypes.Info, "Got registration for host", String.Format("{0}.", serverMessage.ID));
                                        break;
                                    }
                                case ServerMessageTypes.RequestHostList:
                                    {
                                        Log.HandleLog(LOGMessageTypes.Info, "Sending list of", m_registredServers.Count, "hosts to client", String.Format("{0}.", netIncomingMessage.SenderEndPoint));

                                        foreach (KeyValuePair<long, ServerMessage> RegistredServer in this.m_registredServers)
                                        {
                                            NetOutgoingMessage netOutgoingMessage = m_netPeer.CreateMessage();

                                            IServerMessage IserverMessage = new ServerMessage
                                            {
                                                ID = RegistredServer.Value.ID,
                                                IPAddress = RegistredServer.Value.IPAddress,
                                                Port = RegistredServer.Value.Port,
                                                Hostname = RegistredServer.Value.Hostname,
                                                Players = RegistredServer.Value.Players,
                                                MaximumPlayers = RegistredServer.Value.MaximumPlayers,

                                                Ping = 0,
                                                IPendPoint = null,
                                                MessageType = ServerMessageTypes.RequestHostList
                                            };

                                            IserverMessage.EncodeMessage(netOutgoingMessage); // Encode message.

                                            m_netPeer.SendUnconnectedMessage(netOutgoingMessage, netIncomingMessage.SenderEndPoint); // Send encoded message to client.
                                        }
                                        break;
                                    }
                                case ServerMessageTypes.RequestIntroduction:
                                    {
                                        IPEndPoint clientInternal = netIncomingMessage.ReadIPEndPoint();
                                        long ServerID = netIncomingMessage.ReadInt64();
                                        string Token = netIncomingMessage.ReadString();

                                        Log.HandleLog(LOGMessageTypes.Info, netIncomingMessage.SenderEndPoint, "requesting introduction to", ServerID, String.Format("(token{0}).", Token));

                                        if (this.m_registredServers.ContainsKey(ServerID))
                                        {
                                            Log.HandleLog(LOGMessageTypes.Info, "Sending introduction...");

                                            m_netPeer.Introduce(this.m_registredServers[ServerID].IPendPoint[0], this.m_registredServers[ServerID].IPendPoint[1], clientInternal, 
                                                netIncomingMessage.SenderEndPoint, Token);
                                        }
                                        else
                                            Console.WriteLine("Client requested introduction to nonlisted host!");
                                        break;
                                    }
                            }

                            break;
                        }
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.VerboseDebugMessage:
                        {
                            Log.HandleLog(LOGMessageTypes.Debug, netIncomingMessage.ReadString());
                            break;
                        }
                    case NetIncomingMessageType.WarningMessage:
                        {
                            Log.HandleLog(LOGMessageTypes.Warning, netIncomingMessage.ReadString());
                            break;
                        }
                    case NetIncomingMessageType.ErrorMessage:
//.........这里部分代码省略.........
开发者ID:WopsS,项目名称:L.O.G,代码行数:101,代码来源:Network.cs


注:本文中的ServerMessage.EncodeMessage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。