协议解析器

using UnityEngine;
using System.Collections;
using System.Text;
/*
 * 1. higher is First
 * 2. string add length
 **/
/// <summary>发送包</summary>
public class SendPackage
{
    private ArrayList send = new ArrayList();
    public int Length;
    public static int AccountID;
    public short protocolID;
    public SendPackage(short protocolNumber){
        protocolID = protocolNumber;
        addShort((short)0);
        addShort(protocolNumber);
        addInt(AccountID);
    }
    
    public void addByte(byte s) {
        send.Add(s);
    }
    
    public void addShort(short s) {
        send.Add((byte)((s & 0xff00) >> 8));
        send.Add((byte)(s & 0xff));
    }
    
    public void addInt(int s) {
         send.Add((byte) ((0xff000000 & s) >> 24));
        send.Add((byte) ((0xff0000 & s) >> 16));
        send.Add((byte) ((0xff00 & s) >> 8));
        send.Add((byte) (0xff & s));
    }
public void addString(string s){ byte[] str = Encoding.UTF8.GetBytes(s); short sl= (short)str.Length; addShort(sl); for(int i = 0; i < sl; i++){ send.Add(str[i]); } } /// <summary>发送 是否触发发送事件</summary> public void Send(bool sm = true){ Length = (short)(send.Count - 8); send[0] = (byte)((Length & 0xff00) >> 8); send[1] = (byte)(Length & 0xff); NetWorkManager.instance.SendSocket((byte[])send.ToArray (typeof(byte)), sm); } }

 

using UnityEngine;
using System.Collections;
using System.Text;

public class ReceiveParse 
{    
    public int point = 0;
    public int length;
    public short protocolID;
    public int userId;
    public byte[] receiveSave;

    public ReceiveParse(byte[] receive)
    {
        receiveSave = receive;
        length = getShort();
        protocolID = getShort();
        userId = getInt();
        length += 8;
    }

    public byte getByte() 
    {
        byte r = receiveSave[point];
        point += 1;
        return r;
    }
    
    public short getShort() 
    {
        byte[] buf = new byte[2] {receiveSave[point + 1] , receiveSave[point + 0]};
        if (buf == null) 
        {
           Debug.Log("getShort is Null!!");
        }
        if (buf.Length > 2) 
        {
          Debug.Log("getShort is OverRide!!");
        }
        short r = 0;
        for (int i = buf.Length - 1; i >= 0; i--) 
        {
            r <<= 8;
            r |= (short)(buf[i] & 0xff);
        }
        point += 2;
        return r;
      }
    
    public int getInt() 
    {
        byte[] buf = new byte[4] { receiveSave[point + 3], receiveSave[point + 2], receiveSave[point + 1], receiveSave[point] };
        if (buf == null) 
        {
           Debug.Log("getInt is Null!!");
        }
        if (buf.Length > 4) 
        {
          Debug.Log("getInt is OverRide!!");
        }
        int r = 0;
           for (int i = buf.Length - 1; i >= 0; i--) 
        {
            r <<= 8;
            r |= (buf[i] & 0xff);
         }
        point += 4;
        return r; 
      }

    public long getLong()
    {
        byte[] buf = new byte[8] {receiveSave[point + 7], receiveSave[point + 6], receiveSave[point + 5] , receiveSave[point + 4],receiveSave[point + 3], receiveSave[point + 2], receiveSave[point + 1] , receiveSave[point]};
        if (buf == null) 
        {
            Debug.Log("getInt is Null!!");
        }
        if (buf.Length > 8) 
        {
            Debug.Log("getInt is OverRide!!");
        }
        long r = 0;
        for (int i = buf.Length - 1; i >= 0; i--) 
        {
            r <<= 8;
            r |= (long)((long)buf[i] & 0xff);
        }
        point += 8;
        return r;
    }
    
    public string getString()
    {
        short lengh = getShort();
        byte[] buf = new byte[lengh];
        for(int i = 0; i < lengh; i++)
        {
            buf[i] = receiveSave[point + i];
        }
        point += lengh;
        return Encoding.UTF8.GetString(buf);
    }
    

}

ReceiveParse rd = new ReceiveParse(recvBytes); 

包解析

ReceiveParse rp = NetWorkManager.instance.GetReceive();
int id = rp.getShort();
int code = rp.getShort();

发包

SendPackage sp = new SendPackage(14000);
sp.addShort(1);
sp.addShort(5);
sp.Send();

 

posted @ 2015-06-18 17:24  泥潭里的金鱼  阅读(198)  评论(0编辑  收藏  举报