Chat Tutorial Server Code Listing

<Previous Chat Client Code><Next Chat Tutorial Client Code Listing>

PixiChatServer

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Pixi.Server;
using Pixi.Server.DataStructures;
using PixiChat.Common;

namespace PixiChatServer
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a new instance of the PixiServer class
            PixiServer pixiServer = new PixiServer();
            
            //Add and application domain and a zone
            IApplicationDomain appDomain = pixiServer.AddApplicationDomain(ChatConstants.ApplicationDomainID,
                "PixiChatDomain", SimulationStorageType.Quadtree);

            appDomain.AddZone(ChatConstants.ZoneID, "Zone1", 0, 0, 5000, 5000);

            //Start the server
            pixiServer.StartServer("192.168.1.114", 11000, 11001);

            //Wait till the user hits a key and then exit the program
            Console.ReadKey();

            //Remember to stop the server
            pixiServer.StopServer();

        }
    }
}

PixiChat.Common

ChatConstants.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PixiChat.Common
{
    public class ChatConstants
    {
        public const short ApplicationDomainID = 0;
        public const short ZoneID = 1;
        public const byte PlayerObjectType = 0;
    }
}
GameMessageClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PixiChat.Common
{
    public enum GameMessageClass : byte
    {
        ChatMessage
    }
}
GameMessageType.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PixiChat.Common
{
    public enum GameMessageType : short
    {
        ChatSend,
        ChatReply
    }
}

PixiChatServer.Game

LoginHandler.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Pixi.Server;
using Pixi.Server.DataStructures;
using Pixi.Server.Network;
using Pixi.Logging;
using Pixi.Network;
using Pixi.Math;
using PixiChat.Common;
using Lidgren.Network;

namespace PixiChatServer.Game
{
    public class LoginHandler : IServerEventHandler
    {
        #region IServerEventHandler Members

        public void HandleMessage(ServerMessage message, ILogger logger, IApplicationDomain applicationDomain)
        {
            //get the username from the message and update the client information
            string userName = message.Payload.ToString();
            
            //Was this message sent via UDP
            bool isUdp = message.IsSourceChannelUDP;
            message.ClientInfo.SetUserName(userName, isUdp);
            
            //Create a player object
            Player player = new Player(message.ClientInfo.SessionId, userName, 
                ChatConstants.PlayerObjectType, Vector3F.Zero, Vector3F.Zero, 
                ChatConstants.ZoneID, new System.Drawing.RectangleF(0, 0, 10, 10));

            player.ClientId = message.ClientInfo.SessionId;

            //Add the player object to the zone
            applicationDomain.AddStateObject(player);

            //Send a login success message back to the client
            //Get the current network timestamp for the return message
            float now = (float)NetTime.Now;

            ServerMessage successMessage = new ServerMessage(now, 
                (byte)SystemMessageClass.System, (short)SystemMessageType.LoginSuccess);

            successMessage.SourceId = 0;
            
            //SourceID of the message sent in becomes the TargetID on the way back
            successMessage.TargetId = message.SourceId;
            
            //Send using tcp
            successMessage.UseUdp = false;

            //Add the message to the Application Domain to be sent back to the client
            applicationDomain.AddOutgoingMessage(successMessage);
        }

        public byte MessageClassHandled
        {
            get { return (byte)SystemMessageClass.System; }
        }

        public short MessageTypeIDHandled
        {
            get { return (short)SystemMessageType.LoginRequest; }
        }

        #endregion
    }
}
ChatHandler.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Pixi.Server;
using Pixi.Server.DataStructures;
using Pixi.Server.Network;
using Pixi.Logging;
using PixiChat.Common;
using Lidgren.Network;

namespace PixiChatServer.Game
{
    public class ChatHandler : IServerEventHandler
    {
        #region IServerEventHandler Members

        public void HandleMessage(ServerMessage message, ILogger logger, IApplicationDomain applicationDomain)
        {
            logger.Debug("Received Chat Message from " + message.ClientInfo.UserName + " message = " + 
                message.Payload.ToString());
            //Get the current timestamp
            float now = (float)NetTime.Now;

            string chatMessage = message.Payload.ToString();

            //Get all players in the game
            List<StateDynamicEntity> entities = applicationDomain.GetStateDynamicEntitiesByType(
                ChatConstants.ZoneID, ChatConstants.PlayerObjectType);

            //Go through each one and send the incoming message.
            foreach (StateDynamicEntity client in entities)
            {
                //if this client was the one who send the message
                if (client.StateObjectId == message.ClientInfo.SessionId)
                    chatMessage = "You said: " + chatMessage;
                else
                    chatMessage = message.ClientInfo.UserName + " said: " + chatMessage;

                //create the servermessage to send to this client
                ServerMessage chatReplyMessage = new ServerMessage(now, 
                    (byte)GameMessageClass.ChatMessage, (short)GameMessageType.ChatReply);
                chatReplyMessage.SourceId = 0;

                //SourceID of the message sent in becomes the TargetID on the way back
                chatReplyMessage.TargetId = client.ClientId;

                //Send back using udp
                chatReplyMessage.UseUdp = true;
                chatReplyMessage.TransportType = NetDeliveryMethod.ReliableOrdered;
                
                //Set the payload
                chatReplyMessage.Payload = chatMessage;

                //Add the message to the Application Domain to be sent back to the client
                applicationDomain.AddOutgoingMessage(chatReplyMessage);

            }
        }

        public byte MessageClassHandled
        {
            get { return (byte)GameMessageClass.ChatMessage; }
        }

        public short MessageTypeIDHandled
        {
            get { return (short)GameMessageType.ChatSend; }
        }

        #endregion
    }
}
Player.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using Pixi.Server;
using Pixi.Server.DataStructures;
using Pixi.Server.Network;
using Pixi.Logging;
using Pixi.Math;

namespace PixiChatServer.Game
{
    public class Player : StateDynamicEntity
    {
        public Player(int stateObjectId, string name, byte stateObjectType, 
            Vector3F originalPosition, Vector3F heading, short zoneId, 
            RectangleF size)
            : base(stateObjectId, name, stateObjectType, originalPosition, heading, 
                zoneId, size)
        {
        }

        public override List<ServerMessage> Update(IApplicationDomain applicationDomain, ILogger logger)
        {
            //Create a list of ServerMessages to return
            List<ServerMessage> returnMessages = new List<ServerMessage>();

            return returnMessages;
        }
    }
}

Last edited Feb 3, 2012 at 4:04 AM by pixiserver, version 1

Comments

No comments yet.