Beispiel #1
0
    public override void Update()
    {
        MSNetWorker nw = (MSNetWorker)GetOwner();

        byte[] buf;
        while (nw.recvQ.TryDequeue(out buf))
        {
            input.setBuffer(buf);
            MSMessageBase msg = input.read <MSMessageBase>();
            if (msg == null)
            {
                return;
            }
            handle(msg);
            input.reset();
            input.resetCursor();
            buf = null;
        }
    }
Beispiel #2
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;
        }
    }
Beispiel #3
0
    public T read <T>()
    {
        T result = default(T);

        try
        {
            byte firstByte = read(1)[0];
            int  tag       = resolveTag(firstByte);

            if (tag == TYPE_NULL)
            {
            }
            else if (tag == TYPE_INT)
            {
                int len = (firstByte & 0xF0) >> 4;
                result = (T)(object)(int)resolveNum(len);
            }
            else if (tag == TYPE_LONG)
            {
                int len = (firstByte & 0xF0) >> 4;
                result = (T)(object)resolveNum(len);
            }
            else if (tag == TYPE_FLOAT)
            {
                byte[] b = read(LENGTH_INT);
                // 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;
                }

                result = (T)(object)BitConverter.ToSingle(b, 0);
            }
            else if (tag == TYPE_DOUBLE)
            {
                byte[] b = read(LENGTH_LONG);
                // 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;
                }
                result = (T)(object)BitConverter.ToDouble(b, 0);
            }
            else if (tag == TYPE_BOOL)
            {
                result = (T)(object)((((firstByte & 0xF0) >> 4) & 1) == 1);
            }
            else if (tag == TYPE_BYTE)
            {
                result = (T)(object)read(1)[0];
            }
            else if (tag == TYPE_STRING)
            {
                int lenOflen = (firstByte & 0xF0) >> 4;
                int length   = (int)resolveNum(lenOflen);

                byte[] str = read(length);
                result = (T)(object)Encoding.UTF8.GetString(str);
            }
            else if (tag == TYPE_LIST)
            {
                int lenOflen = (firstByte & 0xF0) >> 4;
                int length   = (int)resolveNum(lenOflen);
                int subTag   = resolveTag(buffer[cursor]);
                switch (subTag)
                {
                case TYPE_INT:
                    List <int> li = new List <int>();
                    for (int i = 0; i < length; i++)
                    {
                        li.Add(read <int>());
                    }
                    result = (T)(object)li;
                    break;

                case TYPE_LONG:
                    List <long> ll = new List <long>();
                    for (int i = 0; i < length; i++)
                    {
                        ll.Add(read <long>());
                    }
                    result = (T)(object)ll;
                    break;

                case TYPE_FLOAT:
                    List <float> lf = new List <float>();
                    for (int i = 0; i < length; i++)
                    {
                        lf.Add(read <float>());
                    }
                    result = (T)(object)lf;
                    break;

                case TYPE_DOUBLE:
                    List <double> ld = new List <double>();
                    for (int i = 0; i < length; i++)
                    {
                        ld.Add(read <double>());
                    }
                    result = (T)(object)ld;
                    break;

                case TYPE_MESSAGE:
                    List <MSMessageBase> lm = new List <MSMessageBase>();
                    for (int i = 0; i < length; i++)
                    {
                        lm.Add(read <MSMessageBase>());
                    }
                    result = (T)(object)lm;
                    break;

                default:
                    throw new NotSupportedException("unreadable subTag while reading a list:" + subTag);
                }
            }
            else if (tag == TYPE_MESSAGE)
            {
                int           id = Utils.BytesToInt(read(LENGTH_INT));
                MSMessageBase m  = MSMessageBase.GetEmptyMessageById(id);
                m.read(this);
                result = (T)(object)m;
            }
        }
        catch (Exception e)
        {
            Debug.LogError(e.ToString());
        }

        return((T)result);
    }
Beispiel #4
0
    public static int GetMessageId(MSMessageBase msg)
    {
        if (msg is BVector2)
        {
            return(102);
        }
        else if (msg is BVector3)
        {
            return(103);
        }
        else if (msg is BPlayer)
        {
            return(101);
        }
        else if (msg is CSLogin)
        {
            return(1001);
        }
        else if (msg is SCJoinGame)
        {
            return(1003);
        }
        else if (msg is SCLogin)
        {
            return(1004);
        }
        else if (msg is SCGameSync)
        {
            return(1005);
        }
        else if (msg is CSMove)
        {
            return(2001);
        }
        else if (msg is SCMove)
        {
            return(2002);
        }
        else if (msg is CSJump)
        {
            return(2003);
        }
        else if (msg is SCJump)
        {
            return(2004);
        }
        else if (msg is CSDash)
        {
            return(2005);
        }
        else if (msg is SCDashStart)
        {
            return(2006);
        }
        else if (msg is SCDashStop)
        {
            return(2007);
        }

        return(0);
    }
Beispiel #5
0
    public static MSMessageBase GetEmptyMessageById(int id)
    {
        MSMessageBase msg = null;

        switch (id)
        {
        case 102:
            msg = new BVector2();
            break;

        case 103:
            msg = new BVector3();
            break;

        case 101:
            msg = new BPlayer();
            break;

        case 1001:
            msg = new CSLogin();
            break;

        case 1003:
            msg = new SCJoinGame();
            break;

        case 1004:
            msg = new SCLogin();
            break;

        case 1005:
            msg = new SCGameSync();
            break;

        case 2001:
            msg = new CSMove();
            break;

        case 2002:
            msg = new SCMove();
            break;

        case 2003:
            msg = new CSJump();
            break;

        case 2004:
            msg = new SCJump();
            break;

        case 2005:
            msg = new CSDash();
            break;

        case 2006:
            msg = new SCDashStart();
            break;

        case 2007:
            msg = new SCDashStop();
            break;


        default:
            break;
        }
        return(msg);
    }
Beispiel #6
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());
        }
    }