コード例 #1
0
    void handle(MSMessageBase msg)
    {
        int id = MSMessageBase.GetMessageId(msg);

        switch (id)
        {
        case 1003:
            OnSCJoinGame((SCJoinGame)msg);
            break;

        case 1004:
            OnSCLogin((SCLogin)msg);
            break;

        case 1005:
            OnSCGameSync(msg as SCGameSync);
            break;

        case 2002:
            OnSCMove(msg as SCMove);
            break;

        case 2004:
            OnSCJump(msg as SCJump);
            break;

        case 2006:
            OnSCDashStart(msg as SCDashStart);
            break;

        case 2007:
            OnSCDashStop(msg as SCDashStop);
            break;

        default:
            Debug.LogError("can't handle this message, msgId:" + id);
            break;
        }
    }
コード例 #2
0
    private void writeOut <T>(object obj)
    {
        if (obj == null)
        {
            write(new byte[] { (byte)TYPE_NULL }); return;
        }

        if (obj is int)
        {
            byte[] bytes = resolveNumber((int)obj);
            writeCompressedTL(TYPE_INT, bytes.Length);
            write(bytes);
        }
        else if (obj is long)
        {
            byte[] bytes = resolveNumber((long)obj);
            writeCompressedTL(TYPE_LONG, bytes.Length);
            write(bytes);
        }
        else if (obj is byte)
        {
            write(new byte[] { (byte)TYPE_BYTE, (byte)obj });
        }
        else if (obj is bool)
        {
            write(new byte[] { (byte)((((bool)obj ? 1 : 0) << 4) | TYPE_BOOL) });
        }
        else if (obj is float)
        {
            write(new byte[] { (byte)TYPE_FLOAT });
            byte[] b = BitConverter.GetBytes((float)obj);
            // reverse the bytes
            for (int i = b.Length / 2; i < b.Length; i++)
            {
                byte t = b[i];
                b[i] = b[i - (1 + 2 * (i - b.Length / 2))];
                b[i - (1 + 2 * (i - b.Length / 2))] = t;
            }
            write(b);
        }
        else if (obj is double)
        {
            write(new byte[] { (byte)TYPE_DOUBLE });
            byte[] b = BitConverter.GetBytes((double)obj);
            // reverse the bytes
            for (int i = b.Length / 2; i < b.Length; i++)
            {
                byte t = b[i];
                b[i] = b[i - (1 + 2 * (i - b.Length / 2))];
                b[i - (1 + 2 * (i - b.Length / 2))] = t;
            }
            write(b);
        }
        else if (obj is string)
        {
            byte[] bytes = Encoding.UTF8.GetBytes((string)obj);
            byte[] bNum  = resolveNumber(bytes.Length);
            writeTandVL(TYPE_STRING, bNum.Length, bNum);
            write(bytes);
        }
        else if (obj.GetType().IsGenericType&& obj.GetType().GetGenericTypeDefinition() == typeof(List <>))
        {
            IList <T> l    = (IList <T>)obj;
            byte[]    lenB = resolveNumber(l.Count);
            writeTandVL(TYPE_LIST, lenB.Length, lenB);
            foreach (var v in l)
            {
                writeOut <T>(v);
            }
        }
        else if (obj is MSMessageBase)
        {
            writeTandId(TYPE_MESSAGE, MSMessageBase.GetMessageId((MSMessageBase)obj));
            ((MSMessageBase)obj).write(this);
        }
        else
        {
            throw new NotSupportedException("Unserializable class: " + obj.GetType());
        }
    }