《Unity网络游戏实战》Chapter7: 服务端网络架构

1、介绍

前面第六章完成了一个比较通用的客户端网络架构,但是心跳机制没有实现。第七章会完成心跳机制,以一个在线记事本的案例实现一个通用的服务端程序。服务端依然采用Python2实现。

2、客户端

客户端与第六章的代码相差无几,只是加了几个协议,完善了一下在线笔记本的界面。这里就不细写。直接上界面和代码:

客户端界面:


image.png

BattleMsg.cs


public class MsgMove : MsgBase
{
    public MsgMove() { protoName = "MsgMove"; }

    public int x = 0;
    public int y = 0;
    public int z = 0;
}

public class MsgAttack : MsgBase
{
    public MsgAttack() { protoName = "MsgAttack"; }

    public string desc = NetManager.GetDesc();
}

ByteArray.cs

using System;

public class ByteArray
{
    const int default_size = 1024;
    // 初始缓冲区大小
    int initSize = 0;
    // 缓冲区
    public byte[] buffer;
    // 已发送的索引
    public int readIndex = 0;
    // 整个数据的长度
    public int writeIndex = 0;
    // 缓冲区容量
    public int capacity = 0;
    // 缓冲区剩余空间
    public int Remain { get { return capacity - writeIndex; } }
    // 缓冲区中有效数据长度
    public int Length { get { return writeIndex - readIndex; } }

    public ByteArray(int size = default_size)
    {
        buffer = new byte[size];

        capacity = size;

        initSize = size;

        readIndex = 0;

        writeIndex = 0;
    }


    public ByteArray(byte[] defaultBytes)
    {
        buffer = defaultBytes;

        readIndex = 0;

        writeIndex = defaultBytes.Length;

        capacity = defaultBytes.Length;

        initSize = defaultBytes.Length;
    }

    // 扩容
    public void ReSize(int size)
    {
        if (size < Length || size < initSize)
            return;
        // 指数扩容:从2的倍数中找一个比原来大的
        int n = 1;
        while (n < size)
            n *= 2;
        // 申请新数组并拷贝数据
        capacity = n;
        byte[] newbuffer = new byte[capacity];
        Array.Copy(buffer, readIndex, newbuffer, 0, Length);
        // buffer指向newbuffer
        buffer = newbuffer;
        writeIndex = Length;
        readIndex = 0;
    }

    // 向缓冲区写数据, count是要写入缓冲区的字节数
    public int Write(byte[] bs, int offset, int count)
    {
        if (Remain < count)
            ReSize(Length + count);

        Array.Copy(bs, offset, buffer, writeIndex, count);
        writeIndex += count;
        return count;
    }

    // 读取缓冲区中的数据 将缓冲区中的count字节数据读取到bs中
    public int Read(byte[] bs, int offset, int count)
    {
        count = Math.Min(count, Length);
        Array.Copy(buffer, readIndex, bs, offset, count);
        readIndex += count;
        CheckAndMoveBytes();
        return count;
    }

    //检查并移动数据
    public void CheckAndMoveBytes()
    {
        if (Length < 128)
        {
            MoveBytes();
        }
    }

    //移动数据
    public void MoveBytes()
    {
        Array.Copy(buffer, readIndex, buffer, 0, Length);
        writeIndex = Length;
        readIndex = 0;
    }

    public short ReadInt16()
    {
        if (Length < 2)
            return 0;

        short ret = BitConverter.ToInt16(buffer, readIndex);
        readIndex += 2;
        CheckAndMoveBytes();
        return ret;
    }

    public int ReadInt32()
    {
        if (Length < 4)
            return 0;

        int ret = BitConverter.ToInt32(buffer, readIndex);
        readIndex += 4;
        CheckAndMoveBytes();
        return ret;
    }

    //打印缓冲区
    public override string ToString()
    {
        return BitConverter.ToString(buffer, readIndex, Length);
    }

    //打印调试信息
    public string Debug()
    {
        return string.Format("readIdx({0}) writeIdx({1}) bytes({2})",
            readIndex,
            writeIndex,
            BitConverter.ToString(buffer, 0, capacity)
        );
    }
}

LoginMsg.cs

//注册
public class MsgRegister : MsgBase
{
    public MsgRegister() { protoName = "MsgRegister"; }
    //客户端发
    public string username = "";
    public string password = "";
    //服务端回(0-成功,1-失败)
    public int result = 0;
}


//登陆
public class MsgLogin : MsgBase
{
    public MsgLogin() { protoName = "MsgLogin"; }
    //客户端发
    public string username = "";
    public string password = "";
    //服务端回(0-成功,1-失败)
    public int result = 0;
}


//踢下线(服务端推送)
public class MsgKick : MsgBase
{
    public MsgKick() { protoName = "MsgKick"; }
    //原因(0-其他人登陆同一账号)
    public int reason = 0;
}

//获取记事本内容
public class MsgGetText : MsgBase
{
    public MsgGetText() { protoName = "MsgGetText"; }
    //服务端回
    public string text = "";
}

//保存记事本内容
public class MsgSaveText : MsgBase
{
    public MsgSaveText() { protoName = "MsgSaveText"; }
    //客户端发
    public string text = "";
    //服务端回(0-成功 1-文字太长)
    public int result = 0;
}

MsgBase.cs

using UnityEngine;
using System;
using System.Linq;

public class MsgBase
{
    public string protoName = "null";

    public static int headSize = 4;

    // 编码
    public static byte[] Encode(MsgBase msgBase)
    {
        // 将消息转换成Json格式
        string s = JsonUtility.ToJson(msgBase);
        // 转换成字节数组
        return System.Text.Encoding.UTF8.GetBytes(s);
    }

    // 解码
    public static MsgBase Decode(string protoName, byte[] bytes, int offset, int count)
    {
        // 将字节数组转换为字符串
        string s = System.Text.Encoding.UTF8.GetString(bytes, offset, count);

        // 将字符串转换成对应协议名的Json格式
        MsgBase msgBase = (MsgBase)JsonUtility.FromJson(s, Type.GetType(protoName));

        return msgBase;
    }


    // 编码协议名
    public static byte[] EncodeName(MsgBase msgBase)
    {
        // 协议名bytes和长度
        byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(msgBase.protoName);
        int length = nameBytes.Length;

        // 组装
        byte[] lenBytes = BitConverter.GetBytes(length);

        if(!BitConverter.IsLittleEndian)
        {
            lenBytes.Reverse();
        }

        byte[] retBytes = lenBytes.Concat(nameBytes).ToArray();

        return retBytes;
    }

    // 解码协议名
    public static string DecodeName(byte[] bytes, int offset, out int count)
    {
        count = 0;

        // 判断字节数是否大于数据长度
        if (bytes.Length < headSize)
            return "";

        // 读取协议名长度
        int namelength = BitConverter.ToInt32(bytes, offset);

        // 判断长度是否足够解析协议名
        if (bytes.Length < headSize + namelength)
            return "";

        // 解析
        count = headSize + namelength;

        string msgName = System.Text.Encoding.UTF8.GetString(bytes, offset + headSize, namelength);

        return msgName;
    }
}

NetManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net.Sockets;
using System;
using System.Linq;


public static class NetManager
{
    static Socket socket;

    static ByteArray readBuffer;

    static Queue writeQueue;

    static bool isConnecting = false;

    static bool isClosing = false;

    static int headSize = 4;

    public static bool isUsePing = true;

    public static int pingInterval = 3;

    static float lastPingTime = 0;

    static float lastPongTime = 0;


    public enum NetEvent
    {
        ConnectSucc = 1,
        ConnectFail = 2,
        Close = 3
    }

    // 事件委托
    public delegate void EventListener(string err);
    // 监听列表 --- 连接服务器的监听列表
    static Dictionary eventListener = new Dictionary();

    public static string GetDesc()
    {
        if (socket == null)
            return "";

        return socket.LocalEndPoint.ToString();
    }
    
    public static void AddEventListener(NetEvent netEvent, EventListener listener)
    {
        // 如果有则添加事件,否则新增事件
        if (eventListener.ContainsKey(netEvent))
            eventListener[netEvent] += listener;
        else
            eventListener[netEvent] = listener;
    }

    public static void RemoveEventListener(NetEvent netEvent, EventListener listener)
    {
        if (eventListener.ContainsKey(netEvent))
            eventListener[netEvent] -= listener;

        if (eventListener[netEvent] == null)
            eventListener.Remove(netEvent);
    }

    // 分发事件
    public static void FireEvent(NetEvent netEvent, string err)
    {
        if (eventListener.ContainsKey(netEvent))
            eventListener[netEvent](err);
    }

    // 消息列表
    static List msgList = new List();
    // 消息列表长度 (用list.count不是可以吗?)
    static int msgCount = 0;
    // 每一次Update处理的消息量
    readonly static int MAX_MESSAGE_FIRE = 10;
    // 消息委托
    public delegate void MsgListener(MsgBase msgBase);
    // 消息监听列表
    static Dictionary msgListeners = new Dictionary();

    // 添加消息监听
    public static void AddMsgListener(string msgName, MsgListener listener)
    {
        if (msgListeners.ContainsKey(msgName))
        {
            msgListeners[msgName] += listener;
        }
        else
        {
            msgListeners[msgName] = listener;
        }
    }

    // 删除消息监听
    public static void RemoveMsgListener(string msgName, MsgListener listener)
    {
        if (msgListeners.ContainsKey(msgName))
            msgListeners[msgName] -= listener;

        if (msgListeners[msgName] == null)
            msgListeners.Remove(msgName);
    }

    // 分发消息
    public static void FireMsg(string msgName, MsgBase msgBase)
    {
        if (msgListeners.ContainsKey(msgName))
            msgListeners[msgName](msgBase);
    }

    public static void Connect(string ip, int port)
    {
        // 判断当前的连接状态
        if (socket != null && socket.Connected)
        {
            Debug.Log("连接失败,当前已连接");
            return;
        }

        if (isConnecting)
        {
            Debug.Log("连接失败,当前正在连接中");
            return;
        }

        // 初始化所有成员
        InitState();

        // 设置socket参数
        socket.NoDelay = true;
        isConnecting = true;
        socket.BeginConnect(ip, port, ConnectCallBack, socket);
    }

    static void ConnectCallBack(IAsyncResult ar)
    {
        try
        {
            Socket socket = (Socket)ar.AsyncState;
            socket.EndConnect(ar);
            Debug.Log("Socket Connect Succ");

            // 连接成功
            FireEvent(NetEvent.ConnectSucc, "");
            isConnecting = false;

            // 接收数据
            socket.BeginReceive(readBuffer.buffer, readBuffer.writeIndex,
                                    readBuffer.Remain, 0, ReceiveCallBack, socket);
        }
        catch (SocketException ex)
        {
            Debug.Log("连接失败: " + ex.ToString());
            FireEvent(NetEvent.ConnectFail, ex.ToString());
            isConnecting = false;
        }
    }

    static void InitState()
    {
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        readBuffer = new ByteArray();

        writeQueue = new Queue();

        isConnecting = false;

        isClosing = false;

        msgList = new List();

        msgCount = 0;

        lastPingTime = Time.time;

        lastPongTime = Time.time;

        // 监听Pong
        if (!msgListeners.ContainsKey("MsgPong"))
            AddMsgListener("MsgPong", OnMsgPong);
    }

    //监听PONG协议: 收到MsgPong之后更新lastPongTime
    private static void OnMsgPong(MsgBase msgBase)
    {
        lastPongTime = Time.time;
    }

    public static void ReceiveCallBack(IAsyncResult ar)
    {
        try
        {
            Socket sokcet = (Socket)ar.AsyncState;

            int count = socket.EndReceive(ar);

            if (count == 0)
            {
                // 服务器关闭客户端的socket
                Close();
                return;
            }

            readBuffer.writeIndex += count;

            //处理消息
            OnReceiveData();

            //继续接收数据
            if (readBuffer.Remain < 16)
            {
                readBuffer.MoveBytes();
                // 扩容成原来的两倍
                readBuffer.ReSize(readBuffer.capacity);
            }

            // 继续接收消息
            socket.BeginReceive(readBuffer.buffer, readBuffer.writeIndex,
                        readBuffer.Remain, 0, ReceiveCallBack, socket);
        }
        catch(SocketException ex)
        {
            Debug.Log("接收消息错误: " + ex.ToString());
        }
    }

    public static void OnReceiveData()
    {
        if (readBuffer.Length < headSize)
            return;

        // 获取消息长度
        int bodyLength = BitConverter.ToInt32(readBuffer.buffer, readBuffer.readIndex);

        if (readBuffer.Length < bodyLength + headSize)
            return;

        readBuffer.readIndex += headSize;

        // 解析协议名
        int nameCount = 0;
        string protoName = MsgBase.DecodeName(readBuffer.buffer, readBuffer.readIndex, out nameCount);

        if (protoName == "")
        {
            Debug.Log("OnReceiveData MsgBase.DecodeName failed.");
            return;
        }

        readBuffer.readIndex += nameCount;

        // 解析协议
        int bodyCount = bodyLength - nameCount;

        MsgBase msgBase = MsgBase.Decode(protoName, readBuffer.buffer, readBuffer.readIndex, bodyCount);
        
        readBuffer.readIndex += bodyCount;
        readBuffer.CheckAndMoveBytes();

        // 加入消息队列
        lock(msgList)
        {
            msgList.Add(msgBase);
            msgCount++;
        }

        if (readBuffer.Length > headSize)
            OnReceiveData();
    }

    // 发送数据
    public static void Send(MsgBase msg)
    {
        if (socket == null || !socket.Connected)
            return;

        if (isConnecting)
            return;

        if (isClosing)
            return;

        // 数据编码
        byte[] nameBytes = MsgBase.EncodeName(msg);
        byte[] bodyBytes = MsgBase.Encode(msg);

        int len = nameBytes.Length + bodyBytes.Length;

        byte[] lenBytes = BitConverter.GetBytes(len);

        // 统一小端编码
        if (!BitConverter.IsLittleEndian)
            lenBytes.Reverse();

        byte[] sendBytes1 = lenBytes.Concat(nameBytes).ToArray();
        byte[] sendBytes = sendBytes1.Concat(bodyBytes).ToArray();

        // 写入队列
        ByteArray ba = new ByteArray(sendBytes);
        int count = 0;

        lock(writeQueue)
        {
            writeQueue.Enqueue(ba);
            count = writeQueue.Count;
        }

        // 发送数据
        if (count == 1)
        {
            socket.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, SendCallBack, socket);
        }

    }

    // send回调
    public static void SendCallBack(IAsyncResult ar)
    {
        Socket socket = (Socket)ar.AsyncState;

        // 状态判断
        if (socket == null || !socket.Connected)
            return;

        // 判断是否发送完整
        int count = socket.EndSend(ar);

        ByteArray ba;
        lock(writeQueue)
        {
            ba = writeQueue.First();
        }

        ba.readIndex += count;
        if(ba.Length == 0)
        {
            lock(writeQueue)
            {
                writeQueue.Dequeue();
                ba = writeQueue.First();
            }
        }

        // 发送不完整,继续发送
        if (ba != null)
        {
            socket.BeginSend(ba.buffer, ba.readIndex, ba.Length, SocketFlags.None, SendCallBack, socket);
        }
        else if (isClosing)
        {
            // 安全关闭
            socket.Close();
        }
    }


    public static void Close()
    {
        // 安全close socket
        if (socket == null || !socket.Connected)
            return;

        if (isConnecting)
            return;

        if (writeQueue.Count > 0)
            isClosing = true;

        else
        {
            // 关闭socket
            socket.Close();
            // 调用关闭连接的委托
            FireEvent(NetEvent.Close, "");
        }
    }

    public static void HandleMsg()
    {
        if (msgCount == 0)
            return;

        // 每一帧处理十条消息
        for (int i = 0; i < MAX_MESSAGE_FIRE; i++)
        {
            MsgBase msg = null;

            lock (msgList)
            {
                if (msgList.Count > 0)
                {
                    msg = msgList[0];
                    msgList.RemoveAt(0);
                    msgCount--;
                }
            }

            if (msg != null)
                FireMsg(msg.protoName, msg);
            else
                break;
        }
    }

    public static void PingMsg()
    {
        if (!isUsePing)
            return;

        if (Time.time - lastPingTime > pingInterval)
        {
            MsgPing msgPing = new MsgPing();

            Send(msgPing);

            lastPingTime = Time.time;
        }

        // Pong 时间
        if (Time.time - lastPongTime > pingInterval * 4)
            Close();
    }
}

NetWorkManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class NetWorkManager : MonoBehaviour
{
    public InputField InputField_username;
    public InputField inputField_password;
    public InputField InputField_notepad;

    public Text text_LoginMsg;
    public Text text_SaveMsg;

    public bool isLogin = false;

    void Start()
    {
        NetManager.AddEventListener(NetManager.NetEvent.ConnectSucc, OnConnectSucc);
        NetManager.AddEventListener(NetManager.NetEvent.ConnectFail, OnConnectFail);
        NetManager.AddEventListener(NetManager.NetEvent.Close, OnClose);

        NetManager.AddMsgListener("MsgMove", OnMsgMove);
        NetManager.AddMsgListener("MsgRegister", OnMsgRegister);
        NetManager.AddMsgListener("MsgLogin", OnMsgLogin);
        NetManager.AddMsgListener("MsgGetText", OnMsgGetText);
        NetManager.AddMsgListener("MsgSaveText", OnMsgSaveText);
    }

    //连接成功回调
    void OnConnectSucc(string err)
    {
        Debug.Log("连接服务器成功");
        //TODO:进入游戏
    }

    void OnConnectFail(string err)
    {
        Debug.Log("连接服务器失败");
        //TODO:弹出提示框(连接失败,请重试)
    }

    void OnClose(string err)
    {
        Debug.Log("连接断开");
        //TODO:弹出提示框(网络断开)
        //TODO:弹出按钮(重新连接)
    }

    public void OnButtonConnectClick()
    {
        NetManager.Connect("127.0.0.1", 8888);
    }

    public void OnButtonCloseClick()
    {
        NetManager.Close();
    }

    public void OnButtonMoveClick()
    {
        MsgMove msg = new MsgMove();

        msg.x = 12;
        msg.y = 10;
        msg.z = 7;

        NetManager.Send(msg);
    }

    public void OnButtonRegisterClick()
    {
        MsgRegister msg = new MsgRegister();

        msg.username = InputField_username.text;
        msg.password = inputField_password.text;

        NetManager.Send(msg);

    }

    public void OnButtonLoginClick()
    {
        if (isLogin)
        {
            Debug.Log("已经登录,不能重复登录.");
            return;
        }

        MsgLogin msg = new MsgLogin();

        msg.username = InputField_username.text;
        msg.password = inputField_password.text;

        NetManager.Send(msg);
    }

    public void OnButtonGetTextClick()
    {
        if (!isLogin)
        {
            Debug.Log("没有登录,无法获取文本");
            return;
        }

        MsgGetText msg = new MsgGetText();

        NetManager.Send(msg);
    }

    public void OnButtonSaveTextClick()
    {
        if (!isLogin)
        {
            Debug.Log("没有登录,无法保存");
            return;
        }

        MsgSaveText msg = new MsgSaveText();

        msg.text = InputField_notepad.text;

        NetManager.Send(msg);
    }

    public void OnMsgMove(MsgBase msg)
    {
        MsgMove msgMove = (MsgMove)msg;

        // 打印坐标
        Debug.Log("x: " + msgMove.x);
        Debug.Log("y: " + msgMove.y);
        Debug.Log("z: " + msgMove.z);
    }

    public void OnMsgRegister(MsgBase msgBase)
    {
        MsgRegister msg = (MsgRegister)msgBase;

        if (msg.result == 0)
            Debug.Log("注册成功");
        else
            Debug.Log("注册失败");
    }

    public void OnMsgLogin(MsgBase msgBase)
    {
        MsgLogin msg = (MsgLogin)msgBase;

        if (msg.result == 0)
        {
            text_LoginMsg.text = "登录成功";
            text_LoginMsg.color = Color.green;
            isLogin = true;
        }
        else
        {
            text_LoginMsg.text = "登录成功";
            text_LoginMsg.color = Color.red;
            isLogin = false;
        }
            
    }

    public void OnMsgGetText(MsgBase msgBase)
    {
        MsgGetText msg = (MsgGetText)msgBase;

        InputField_notepad.text = msg.text;
    }

    public void OnMsgSaveText(MsgBase msgBase)
    {
        MsgSaveText msg = (MsgSaveText)msgBase;

        if (msg.result == 0)
        {
            text_SaveMsg.text = "保存成功";
            text_SaveMsg.color = Color.green;
        }
        else
        {
            text_SaveMsg.text = "保存失败";
            text_SaveMsg.color = Color.red;
        }
    }

    private void Update()
    {
        NetManager.HandleMsg();
        NetManager.PingMsg();
    }
}

SyncMsg.cs


public class MsgPing : MsgBase
{
    public MsgPing() { protoName = "MsgPing"; }
}

public class MsgPong : MsgBase
{
    public MsgPong() { protoName = "MsgPong"; }
}

3、服务端

服务端采用的数据库为MySQL,服务端的代码比较简单,操作都是接收消息,存入client_state对应的缓冲区,判断数据长度是否足够解析一条数据,够便处理消息,知道缓冲区数据不足一条消息。

加入PlayerManager类用于管理player,编写MySQLManager类用于对数据库进行操作。

整个服务器的代码逻辑比较简单,写的时候想着采用多线程处理客户端的消息,但是想着先实现一个简单的,后续在修改,而完成之后事情也比较多,就不想改了。以后有缘在修改吧。

这里就直接上代码了:
ClientState.py

import Player
class ClientState(object):
    def __init__(self, socket, addr, time_span):
        self.socket = socket
        self.addr = addr
        self.buffer = ""
        self.last_ping_time = time_span
        self.player = Player.Player(self)

Message.py

import json as js

# message base class
class MsgBase(object):
    # init
    def __init__(self):
        self.protoName = "null"

    # transform the class member to dict
    def to_dict(self):
        return {'protoName': self.protoName}

    # encode msg_base to json str
    def encode(self):
        return js.dumps(self.to_dict())

    # decode json str to msg_base
    @staticmethod
    def decode(json_data):
        msg_base = MsgBase()
        data = js.loads(json_data)
        # assign member data
        msg_base.protoName = data['protoName']
        return msg_base

# move message
class MsgMove(MsgBase):
    def __init__(self):
        super(MsgMove, self).__init__()
        self.x = 0
        self.y = 0
        self.z = 0

    def to_dict(self):
        return {'protoName': self.protoName,
                'x': self.x,
                'y': self.y,
                'z': self.z}

    @staticmethod
    def decode(json_data):
        msg_move = MsgMove()
        data = js.loads(json_data)
        msg_move.protoName = data['protoName']
        msg_move.x = data['x']
        msg_move.y = data['y']
        msg_move.z = data['z']
        return msg_move

# attack message
class MsgAttack(MsgBase):
    def __init__(self):
        super(MsgAttack, self).__init__()
        self.protoName = 'MsgAttack'

# ping message
class MsgPing(MsgBase):
    def __init__(self):
        super(MsgPing, self).__init__()
        self.protoName = 'MsgPing'

# pong message, to response ping
class MsgPong(MsgBase):
    def __init__(self):
        super(MsgPong, self).__init__()
        self.protoName = 'MsgPong'

class MsgRegister(MsgBase):
    def __init__(self):
        super(MsgRegister, self).__init__()
        self.protoName = 'MsgRegister'
        self.username = ""
        self.password = ""
        self.result = 0

    def to_dict(self):
        return {'protoName': self.protoName,
                'username': self.username,
                'password': self.password,
                'result': self.result}

    @staticmethod
    def decode(json_data):
        msg_register = MsgRegister()
        data = js.loads(json_data)
        msg_register.protoName = data['protoName']
        msg_register.username = data['username']
        msg_register.password = data['password']
        msg_register.result = data['result']
        return msg_register

class MsgLogin(MsgBase):
    def __init__(self):
        super(MsgLogin, self).__init__()
        self.protoName = 'MsgLogin'
        self.username = ""
        self.password = ""
        self.result = 0

    def to_dict(self):
        return {'protoName': self.protoName,
                'username': self.username,
                'password': self.password,
                'result': self.result}

    @staticmethod
    def decode(json_data):
        msg_login = MsgLogin()
        data = js.loads(json_data)
        msg_login.protoName = data['protoName']
        msg_login.username = data['username']
        msg_login.password = data['password']
        msg_login.result = data['result']
        return msg_login

class MsgKick(MsgBase):
    def __init__(self):
        super(MsgKick, self).__init__()
        self.protoName = 'MsgKick'
        self.reason = 0

    def to_dict(self):
        return {'protoName': self.protoName,
                'reason': self.reason}

    @staticmethod
    def decode(json_data):
        msg_kick = MsgKick()
        data = js.loads(json_data)
        msg_kick.protoName = data['protoName']
        msg_kick.reason = data['reason']
        return msg_kick

class MsgGetText(MsgBase):
    def __init__(self):
        super(MsgGetText, self).__init__()
        self.protoName = 'MsgGetText'
        self.text = ""

    def to_dict(self):
        return {'protoName': self.protoName,
                'text': self.text}

    @staticmethod
    def decode(json_data):
        msg_get_text = MsgGetText()
        data = js.loads(json_data)
        msg_get_text.protoName = data['protoName']
        msg_get_text.text = data['text']
        return msg_get_text

class MsgSaveText(MsgBase):
    def __init__(self):
        super(MsgSaveText, self).__init__()
        self.protoName = 'MsgSaveText'
        self.text = ""
        self.result = 0

    def to_dict(self):
        return {'protoName': self.protoName,
                'text': self.text,
                'result': self.result}

    @staticmethod
    def decode(json_data):
        msg_save_text = MsgSaveText()
        data = js.loads(json_data)
        msg_save_text.protoName = data['protoName']
        msg_save_text.text = data['text']
        msg_save_text.result = data['result']
        return msg_save_text

MessageHandler.py

import Message
import time
import MySQLManager as dbm
import Player

class MessageHandler(object):
    def __init__(self, server, player_manager):
        self.events = {'MsgMove': self.move_response,
                       'MsgPing': self.ping_response,
                       'MsgRegister': self.register_response,
                       'MsgLogin': self.login_response,
                       'MsgGetText': self.get_text_response,
                       'MsgSaveText': self.save_text_response,
                       'UpdateDataBase': self.update_data_base}
        self.game_server = server
        self.pm = player_manager
        self.db = dbm.DataBaseManager()
        self.db.connect_mysql()

    def handle_msg(self, msg_name, json_msg, client_state):
        self.events[msg_name](json_msg, client_state)

    def move_response(self, json_msg, client_state):
        msg = Message.MsgMove.decode(json_msg)
        msg.x += 10
        msg.y += 10
        msg.z += 10
        self.game_server.send(client_state, msg)

    def ping_response(self, json_msg, client_state):
        msg = Message.MsgPing.decode(json_msg)
        # check msg ping
        if msg.protoName != 'MsgPing':
            print 'Receive Error MsgPing.'
            return
        # update client last ping time
        client_state.last_ping_time = time.time()
        # send MsgPong to client
        msg_pong = Message.MsgPong()
        self.game_server.send(client_state, msg_pong)

    def register_response(self, json_msg, client_state):
        msg_register = Message.MsgRegister.decode(json_msg)
        # register new account and player
        if self.db.register_new_user(msg_register.username, msg_register.password):
            if self.db.create_player(msg_register.username):
                msg_register.result = 0
            else:
                msg_register.result = 1
        else:
            msg_register.result = 1
        # send to client
        self.game_server.send(client_state, msg_register)

    def login_response(self, json_msg, client_state):
        msg_login = Message.MsgLogin.decode(json_msg)
        # check username and password
        if not self.db.check_password(msg_login.username, msg_login.password):
            # username or password error
            msg_login.result = 1
            self.game_server.send(client_state, msg_login)
            return
        # if the client is online
        if self.pm.is_online(msg_login.username):
            # kick the player offline
            pre_player = self.pm.get_player(msg_login.username)
            msg_kick = Message.MsgKick()
            self.game_server.send(pre_player.client_state, msg_kick)
            # close, but not update the data in database
            self.game_server.connect_sockets.remove(pre_player.client_state.socket)
            self.game_server.client_states.pop(pre_player.client_state.socket)
            pre_player.client_state.socket.close()
            self.pm.remove_player(msg_login.username)
        # get player data
        player_data = self.db.get_player_data(msg_login.username)
        if not player_data:
            # query failed
            msg_login.result = 1
            self.game_server.send(client_state, msg_login)
            return
        # add new player
        new_player = Player.Player(client_state)
        new_player.username = msg_login.username
        new_player.player_data = player_data
        self.pm.add_player(new_player.username, new_player)
        client_state.player = new_player
        # return msg to client
        msg_login.result = 0
        self.game_server.send(client_state, msg_login)

    def get_text_response(self, json_msg, client_state):
        msg_get_text = Message.MsgGetText.decode(json_msg)
        player = client_state.player
        msg_get_text.text = player.player_data.text
        self.game_server.send(client_state, msg_get_text)

    def save_text_response(self, json_msg, client_state):
        msg_save_text = Message.MsgSaveText.decode(json_msg)
        player = client_state.player
        player.player_data.text = msg_save_text.text
        if self.update_data_base(client_state):
            msg_save_text.result = 0
        else:
            msg_save_text.result = 1
        self.game_server.send(client_state, msg_save_text)

    def update_data_base(self, client_state):
        # update database when player quit
        player_data = client_state.player.player_data.to_json()
        # update database
        try:
            self.db.update_player_data(client_state.player.username, player_data)
            return True
        except Exception:
            return False

MySQLManager.py

import MySQLdb as mysql
import re
import Player

class DataBaseManager(object):
    def __init__(self):
        self.db = None
        self.cursor = None

    def connect_mysql(self):
        try:
            self.db = mysql.connect('127.0.0.1', 'root', 'xxxxxx', 'base', charset='utf8', autocommit=True)
            self.cursor = self.db.cursor()
            print '[DataBase] Connect MySQL Successfully.'
        except Exception:
            print '[DataBase] Connect MySQL Failed!'

    def is_safe_string(self, s):
        # search the special char, like ';', '*' and so on
        pattern = r"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']"
        # use the re.search
        result = re.search(pattern, s)
        if result:
            return False
        else:
            return True

    def is_account_exist(self, username):
        # check
        if self.db is None:
            print '[DataBase] DataBase not connected.'
            return
        # check the string, if the string is not safe, return
        if not self.is_safe_string(username):
            print '[DataBase] The username is illegal!'
            return
        # get the sql
        sql = 'select * from account where username=\"{0}\"'.format(username)
        # query
        try:
            self.cursor.execute(sql)
            query_result = self.cursor.fetchone()
        except Exception:
            print '[DataBase] query account failed!'
            return False
        # if the username not exist, the query result is none
        if query_result:
            return True
        else:
            return False

    def register_new_user(self, username, password):
        # check
        if not self.is_safe_string(username):
            print '[DataBase] The username is illegal!'
            return False
        if not self.is_safe_string(password):
            print '[DataBase] The password is illegal!'
            return False
        if self.is_account_exist(username):
            print '[DataBase] The username already exist!'
            return False
        # create a new account
        try:
            sql = 'insert into account values(\"{0}\", \"{1}\")'.format(username, password)
            self.cursor.execute(sql)
            return True
        except Exception:
            print '[DataBase] create account failed!'
            return False

    def create_player(self, username):
        # check
        if not self.is_safe_string(username):
            print '[DataBase] The username is illegal!'
            return False
        # write the player data
        player_data = Player.PlayerData()
        data = player_data.to_json()
        try:
            sql = 'insert into player values(\"{0}\", \"{1}\")'.format(username, data.replace('"', '\\"'))
            self.cursor.execute(sql)
            return True
        except Exception:
            print '[DataBase] create player failed!'
            return False

    def check_password(self, username, password):
        # check
        if not self.is_safe_string(username):
            print '[DataBase] The username is illegal!'
            return False
        if not self.is_safe_string(password):
            print '[DataBase] The password is illegal!'
            return False
        # query
        sql = 'select password from account where username=\"{0}\"'.format(username)
        # get player password
        try:
            self.cursor.execute(sql)
            data = self.cursor.fetchone()
        except Exception:
            print '[DataBase] get user password failed!'
            return False
        # check the password
        if data:
            # username exist
            if str(data[0]) == password:
                return True
            else:
                print '[DataBase] Password Error.'
                return False
        else:
            print '[DataBase] Username not exist.'
            return False

    def get_player_data(self, username):
        # check
        if not self.is_safe_string(username):
            print '[DataBase] The username is illegal!'
            return False
        sql = 'select data from player where username=\"{0}\"'.format(username)
        try:
            self.cursor.execute(sql)
            # data is a tuple
            data = self.cursor.fetchone()
            return Player.PlayerData.decode(str(data[0]))
        except Exception:
            print '[DataBase] get player data failed!'
            return False

    def update_player_data(self, username, player_data):
        sql = 'update player set data=\"{0}\" where username=\"{1}\"'.format(player_data.replace('"', '\\"'), username)
        try:
            self.cursor.execute(sql)
            return True
        except Exception:
            print 'update player data failed!'
            return False

Player.py

import json
# the data save in mysql
class PlayerData(object):
    def __init__(self):
        self.coin = 0
        self.text = 'new text'

    def to_json(self):
        data = {'coin': self.coin,
                'text': self.text}
        return json.dumps(data)

    @staticmethod
    def decode(json_data):
        player_data = PlayerData()
        data = json.loads(json_data)
        player_data.coin = data['coin']
        player_data.text = data['text']
        return player_data

class Player(object):
    def __init__(self, cs):
        self.username = ""
        self.client_state = cs
        self.x = 0
        self.y = 0
        self.z = 0
        self.player_data = PlayerData()

class PlayerManager(object):
    def __init__(self):
        self.players = {}

    def is_online(self, username):
        if username in self.players:
            return True
        else:
            return False

    def get_player(self, username):
        return self.players[username]

    def add_player(self, username, player):
        self.players[username] = player

    def remove_player(self, username):
        self.players.pop(username)

Server.py

import socket
import select
import ClientState as cs
import MessageHandler as mh
import time
import struct
import Player

class TcpServer(object):
    def __init__(self, ip, port):
        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.ip = ip
        self.port = port
        self.buffer_size = 1024
        self.header_size = 4
        self.read_buffer = ""
        self.connect_sockets = []
        self.client_states = {}
        self.player_manager = Player.PlayerManager()
        self.handler = mh.MessageHandler(self, self.player_manager)
        self.pingInterval = 3
        self.client_drops_time = self.pingInterval * 4

    def parse_msg_length(self, head):
        # get message body length
        length = 0
        for i in range(len(head)):
            length += ord(head[i]) * 256 ** i
        return length

    # data: DataLength(4Bytes) + MessageNameLength(4Bytes) + MessageName + DataBody
    def handle_message(self, client_state):
        readIndex = 0
        read_buffer = client_state.buffer
        # check buffer > head size
        buffer_count = len(read_buffer)
        if buffer_count < self.header_size:
            return False, 0

        # check buffer > one whole data
        msg_body_size = self.parse_msg_length(read_buffer[readIndex:readIndex + self.header_size])
        if buffer_count < self.header_size + msg_body_size:
            return False, 0

        # move read index
        readIndex += self.header_size
        # parse Message Name length
        msg_name_len = self.parse_msg_length(read_buffer[readIndex:readIndex + self.header_size])

        # move read index
        readIndex += self.header_size
        # get the message name
        msg_name = read_buffer[readIndex:readIndex + msg_name_len]

        # move read index
        readIndex += msg_name_len
        # get the message body
        data_body = read_buffer[readIndex:msg_body_size + self.header_size]

        # handler message
        self.handler.handle_msg(msg_name, data_body, client_state)

        # update the client read buffer
        client_state.buffer = client_state.buffer[self.header_size + msg_body_size:]
        # self.client_states[socket].buffer = self.client_states[socket].buffer[self.header_size + msg_body_size:]

        # continue handle message
        if len(client_state.buffer) > self.header_size:
            self.handle_message(client_state)

    def send(self, cs, msg):
        # send msg to client...Build the json msg
        # msg: DataLength(4Bytes) + MessageNameLength(4Bytes) + MessageName + MessageBody
        msg_body = msg.encode()
        msg_name = msg.protoName
        msg_name_length = len(msg_name)
        data = msg_name + msg_body
        data_length = len(data) + self.header_size
        pack_format = 'I' + 'I' + str(len(data)) + 's'
        # json str is unicode, need to transform to str
        send_data = (data_length, msg_name_length, str(data))
        send_bytes = struct.pack(pack_format, *send_data)
        try:
            # send data to client socket. do the complete send data later
            cs.socket.send(send_bytes)
        except socket.error:
            print cs.addr, 'socket close on send!'

    def check_ping(self):
        now = time.time()
        # check all client ping time
        for cs in self.client_states.values():
            if now - cs.last_ping_time > self.client_drops_time:
                # close client
                print '[GameServer] close client:', cs.addr
                cs.socket.close()
                self.connect_sockets.remove(cs.socket)
                self.client_states.pop(cs.socket)
                self.player_manager.remove_player(cs.player.username)

    def receive(self, read_fds):
        for fd in read_fds:
            try:
                if fd is self.server:
                    # client connect
                    client_socket, client_addr = self.server.accept()
                    print '[GameServer] ', client_addr, 'connected'
                    self.connect_sockets.append(client_socket)
                    self.client_states[client_socket] = cs.ClientState(client_socket, client_addr, time.time())
                else:
                    # client send message to server
                    receive_data = fd.recv(self.buffer_size)
                    if receive_data:
                        # add data to client read buffer
                        self.client_states[fd].buffer += receive_data
                        self.handle_message(self.client_states[fd])
                    else:
                        # client quit
                        self.player_manager.remove_player(self.client_states[fd].player.username)
                        self.connect_sockets.remove(fd)
                        self.client_states.pop(fd)
                        fd.close()
            except socket.error:
                self.player_manager.remove_player(self.client_states[fd].player.username)
                if fd in self.connect_sockets:
                    self.connect_sockets.remove(fd)
                if fd in self.client_states:
                    self.client_states.pop(fd)
                fd.close()

    def run(self):
        self.server.bind((self.ip, self.port))
        self.server.listen(5)
        self.connect_sockets.append(self.server)
        print "[GameServer] GameServer Start."
        while True:
            read_fds, write_fds, error_fds = select.select(self.connect_sockets, [], [], 1)
            # time.sleep(3)
            self.receive(read_fds)
            # check client last ping time
            self.check_ping()

# run server
server = TcpServer("127.0.0.1", 8888)
server.run()

MySQL数据库中创建两个表,一个是account,一个是player。
account存放的是玩家的username和password,使用text或char都行。

player中存放的是player的username和data,data的类型是text,存放的是json格式的字符串。


image.png

4、结语

《Unity网络游戏实战》这本书前面七章都比较简单,后面8-12章是做一个坦克大战游戏,比较难的部分就是同步算法的使用,其他相对都比较简单。

加油,好好学习天天向上!

你可能感兴趣的:(《Unity网络游戏实战》Chapter7: 服务端网络架构)