This project is read-only.

Implement your AppServer and AppSession, the first step of using SuperSocket

Implementing a AppServer and a AppSession is the first step of using SuperSocket to build a socket server.

 

What is AppSession?

AppSession represents a logic socket connection, connection based operations should be defined in this class. You can use the instance of this class to send data to tcp clients, receive data from connection or close the connection.

What is AppServer?

AppServer stands for the server instance which listens all clients' connections, hosts all tcp connections. Ideally, we can get any session which we want to find from the AppServer. Application level operations and logics should be defined in it. 

Create your AppSession

Now, we take the EchoServer as a sample:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase;

namespace SuperSocket.QuickStart.EchoService
{
    public class EchoSession : AppSession<EchoSession>
    {
        public override void StartSession()
        {
            SendResponse("Welcome to EchoServer!");
        }

        public override void HandleExceptionalError(Exception e)
        {
            SendResponse("Server side error occurred!");
        }
    }
}

 

 

In this sample, the EchoSession does two things for us:

1. When a new client connect, send the welcome message to client.

2. When an exception is throw in command processing, send the message "Server side error occurred!".

 

Create your AppServer

We still take the EchoServer as sample:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase;

namespace SuperSocket.QuickStart.EchoService
{
    public class EchoServer : AppServer<EchoSession>
    {

    }
}

 

Your AppServer implementation requires a AppSession type as a generic type parameter to inherite the base class AppServer. Because EchoServer doesn't require extra logic, so no more code was wrote in this class.

Then we take the BroadcastServer as sample:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using SuperSocket.Common;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Command;

namespace SuperSocket.QuickStart.BroadcastService
{
    public class BroadcastServer : AppServer<BroadcastSession>
    {
        private Dictionary<string, List<string>> broadcastDict = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
        private object broadcastSyncRoot = new object();

        private Dictionary<string, BroadcastSession> broadcastSessionDict = new Dictionary<string, BroadcastSession>(StringComparer.OrdinalIgnoreCase);
        private object syncRoot = new object();

        public BroadcastServer()
        {
            lock (broadcastSyncRoot)
            {
                //It means that device V001 will receive broadcast messages from C001 and C002,
                //device V002  will receive broadcast messages from C002 and C003
                broadcastDict["C001"] = new List<string> { "V001" };
                broadcastDict["C002"] = new List<string> { "V001", "V002" };
                broadcastDict["C003"] = new List<string> { "V002" };
            }
        }

        internal void RegisterNewSession(BroadcastSession session)
        {
            if (string.IsNullOrEmpty(session.DeviceNumber))
                return;

            lock (syncRoot)
            {
                broadcastSessionDict[session.DeviceNumber] = session;
            }
        }

        internal void RemoveOnlineSession(BroadcastSession session)
        {
            if (string.IsNullOrEmpty(session.DeviceNumber))
                return;

            lock (syncRoot)
            {
                broadcastSessionDict.Remove(session.DeviceNumber);
            }
        }

        internal void BroadcastMessage(BroadcastSession session, string message)
        {
            List<string> targetDeviceNumbers;

            lock (broadcastSyncRoot)
            {
                if(!broadcastDict.TryGetValue(session.DeviceNumber, out targetDeviceNumbers))
                    return;
            }

            if (targetDeviceNumbers == null || targetDeviceNumbers.Count <= 0)
                return;

            List<BroadcastSession> sessions = new List<BroadcastSession>();

            lock (syncRoot)
            {
                BroadcastSession s;

                foreach(var key in targetDeviceNumbers)
                {
                    if (broadcastSessionDict.TryGetValue(key, out s))
                        sessions.Add(s);
                }
            }

            Async.Run(() =>
                {
                    sessions.ForEach(s => s.SendResponse(message));
                });
        }

        protected override void OnAppSessionClosed(object sender, AppSessionClosedEventArgs<BroadcastSession> e)
        {
            RemoveOnlineSession(e.Session);
            base.OnAppSessionClosed(sender, e);
        }
    }
}

 

Now, we can see the BroadcastServer defined lots of logics and operations method of application level which we can use in other places.

Last edited Mar 6, 2011 at 9:30 AM by kerryjiang, version 5

Comments

No comments yet.