Exemple #1
0
        //解析
        private void ParseData(byte[] buff, UInt32 buffindex, UInt32 PacketLength)
        {
            int command = (buff[buffindex] + (buff[buffindex + 1] << 8));
            if (command == (int)COMMAND_ID.COMMAND_KEEP_ALIVE_RESPONSE)
            {
            }
            else if (command == (int)COMMAND_ID.COMMAND_REG_RESPONSE)
            {
                byte Regresult = buff[buffindex + 2];
                if (Regresult == 1)
                {
                    Reged = true;
                    Reging = false;
                    sendblockEvent.Set();
                }
                else
                {
                    Reged = false;
                    Reging = false;
                    sendblockEvent.Reset();
                    Exception e = new Exception("应用注册失败,同类应用可能已经在运行。");
                    if (ExceptionEvent != null) ExceptionEvent(e);
                }
            }
            else if (command == (int)COMMAND_ID.COMMAND_PACKET_SERVERDOWN)
            {
                if (SendStatusEvent != null)
                {
                    UInt16 serverid = BitConverter.ToUInt16(buff, (int)buffindex + 2);
                    UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 4);
                    lock (Synsendinglist)
                    {
                        if (sendinglist.ContainsKey(dataid))
                        {
                            SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_SERVERDOWN, sendinglist[dataid]);
                            sendinglist.Remove(dataid);
                            SendStatusEvent(e);
                        }
                    }
                }
            }
            else if (command == (int)COMMAND_ID.COMMAND_PACKET_APPDOWN)
            {
                if (SendStatusEvent != null)
                {
                    UInt16 serverid = BitConverter.ToUInt16(buff, (int)buffindex + 2);
                    UInt16 sourceappid = BitConverter.ToUInt16(buff, (int)buffindex + 4);
                    UInt16 destappid = BitConverter.ToUInt16(buff, (int)buffindex + 6);
                    UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 8);
                    lock (Synsendinglist)
                    {
                        if (sendinglist.ContainsKey(dataid))
                        {
                            SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_APPDOWN, sendinglist[dataid]);
                            sendinglist.Remove(dataid);
                            SendStatusEvent(e);
                        }
                    }
                }
            }
            else if (command == (int)COMMAND_ID.COMMAND_PACKET_ERR)
            {
                if (SendStatusEvent != null)
                {
                    UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 14);
                    lock (Synsendinglist)
                    {
                        if (sendinglist.ContainsKey(dataid))
                        {
                            SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_ERR, sendinglist[dataid]);
                            sendinglist.Remove(dataid);
                            SendStatusEvent(e);
                        }
                    }
                }
            }
            else if (command == (int)COMMAND_ID.COMMAND_PACKET_RECEIVED)
            {
                if(SendStatusEvent != null)
                {
                    UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 14);
                    lock (Synsendinglist)
                    {
                        if (sendinglist.ContainsKey(dataid))
                        {
                            sendinglist[dataid].outtime = DateTime.Now;
                            SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_RECEIVED, sendinglist[dataid]);
                            sendinglist.Remove(dataid);
                            SendStatusEvent(e);
                        }
                    }
                }
            }
            else if (command == (int)COMMAND_ID.COMMAND_QUERY_SERVER_RESPONSE)
            {
                if(updateClientEvent != null)
                {
                    char[] trimChars = {(char)0 };
                    List<ServerClass> servers = new List<ServerClass>();
                    int servercount = BitConverter.ToUInt16(buff, (int)buffindex + 2);
                    UInt32 pos = buffindex + 4;
                    for(int i = 0; i<servercount; ++i)
                    {
                        ushort serverid = BitConverter.ToUInt16(buff, (int)pos); pos = pos + 2;
                        string stationid = Encoding.Default.GetString(buff, (int)pos, 33).Trim(trimChars); pos = pos + 33;
                        byte online = buff[pos]; ++pos;
                        ServerClass s = new ServerClass();
                        s.m_Serverid = serverid;
                        s.m_szStationID = stationid;
                        s.isonline = (online == 1);
                        servers.Add(s);
                    }
                    ClienUpdateEventArgs e = new ClienUpdateEventArgs(CLIENTUPDATE.updateServer);
                    e.data = servers;
                    updateClientEvent(e);
                }
            }
            else if ((command == (int)COMMAND_ID.COMMAND_QUERY_APP_RESPONSE) || (command == (int)COMMAND_ID.COMMAND_SYN_APP))
            {
                if (updateClientEvent != null)
                {
                    List<AppObj> applist = new List<AppObj>();
                    UInt32 pos = buffindex + 2;
                    ushort serverid = BitConverter.ToUInt16(buff, (int)pos); pos = pos + 2;
                    ushort appcount = BitConverter.ToUInt16(buff, (int)pos); pos = pos + 2;
                    for (int i = 0; i < appcount; ++i)
                    {
                        ushort appid = BitConverter.ToUInt16(buff, (int)pos); pos = pos + 2;
                        byte isadd = buff[pos]; ++pos;
                        AppObj a = new AppObj();
                        a.m_Serverid = serverid;
                        a.app = (APPID)appid;
                        a.isonline = (isadd == 1);
                        applist.Add(a);
                    }
                    ClienUpdateEventArgs e = new ClienUpdateEventArgs(CLIENTUPDATE.updateApp);
                    e.data = applist;
                    updateClientEvent(e);
                }
            }
            //接受到数据包
            else if (command == (int)COMMAND_ID.COMMAND_PACKET)
            {
                if (ReceivedEvent != null)
                {
                    const int CommnadSize = 2;     //命令大小
                    const int ConnectSize = 12;     //连接大小
                    const int DataidSize = 4;      //数据块编号大小
                    const int DataLengthSize = 4;  //数据块长度大小
                    const int DataOffsetSize = 4;  //数据块偏移大小
                    const int MD5Length = 16;
                    const int PacketHead = CommnadSize + ConnectSize + DataidSize + DataLengthSize + DataOffsetSize;
                    bool datacorrect = true;
                    UInt16 SourServerid = BitConverter.ToUInt16(buff, (int)buffindex + 2);
                    UInt16 DestServerid = BitConverter.ToUInt16(buff, (int)buffindex + 4);
                    UInt16 SourAppid = BitConverter.ToUInt16(buff, (int)buffindex + 6);
                    UInt16 DestAppid = BitConverter.ToUInt16(buff, (int)buffindex + 8);
                    UInt32 connid = BitConverter.ToUInt32(buff, (int)buffindex + 10);
                    UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 14);

                    byte[] smd5 = new byte[MD5Length];
                    Array.Copy(buff, buffindex + PacketHead, smd5, 0, MD5Length);
                    byte[] dmd5 = md5Pro.ComputeHash(buff, (int)buffindex + PacketHead + MD5Length, (int)PacketLength - PacketHead - MD5Length);
                    for (int i = 0; i < MD5Length; i++)
                    {
                        if (smd5[i] != dmd5[i]) datacorrect = false;
                    }
                    //验证数据正确性
                    if (datacorrect)
                    {
                        icPacket packet = new icPacket();
                        object data = null;
                        MemoryStream stream = new MemoryStream(buff, (int)buffindex + PacketHead + MD5Length, (int)PacketLength - PacketHead - MD5Length);
                        data = rformatter.Deserialize(stream);
                        packet.data = data;
                        packet.Serverid = SourServerid;
                        packet.Appid = (APPID)SourAppid;
                        packet.dataid = dataid;
                        ReceivedEventArgs e = new ReceivedEventArgs(packet);
                        ReceivedEvent(e);
                        socket.Send(icParse.makePacketReceived(DestServerid, DestAppid, SourServerid, SourAppid, 0, dataid));
                    }
                    else
                    {
                        socket.Send(icParse.makePacketErr(DestServerid, DestAppid, SourServerid, SourAppid, 0, dataid));
                    }
                }
            }
        }
        //解析
        private void ParseData(byte[] buff, UInt32 buffindex, UInt32 PacketLength)
        {
            int command = (buff[buffindex] + (buff[buffindex + 1] << 8));

            if (command == (int)COMMAND_ID.COMMAND_KEEP_ALIVE_RESPONSE)
            {
            }
            else if (command == (int)COMMAND_ID.COMMAND_REG_RESPONSE)
            {
                byte Regresult = buff[buffindex + 2];
                if (Regresult == 1)
                {
                    Reged  = true;
                    Reging = false;
                    sendblockEvent.Set();
                }
                else
                {
                    Reged  = false;
                    Reging = false;
                    sendblockEvent.Reset();
                    Exception e = new Exception("应用注册失败,同类应用可能已经在运行。");
                    if (ExceptionEvent != null)
                    {
                        ExceptionEvent(e);
                    }
                }
            }
            else if (command == (int)COMMAND_ID.COMMAND_PACKET_SERVERDOWN)
            {
                if (SendStatusEvent != null)
                {
                    UInt16 serverid = BitConverter.ToUInt16(buff, (int)buffindex + 2);
                    UInt32 dataid   = BitConverter.ToUInt32(buff, (int)buffindex + 4);
                    lock (Synsendinglist)
                    {
                        if (sendinglist.ContainsKey(dataid))
                        {
                            SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_SERVERDOWN, sendinglist[dataid]);
                            sendinglist.Remove(dataid);
                            SendStatusEvent(e);
                        }
                    }
                }
            }
            else if (command == (int)COMMAND_ID.COMMAND_PACKET_APPDOWN)
            {
                if (SendStatusEvent != null)
                {
                    UInt16 serverid    = BitConverter.ToUInt16(buff, (int)buffindex + 2);
                    UInt16 sourceappid = BitConverter.ToUInt16(buff, (int)buffindex + 4);
                    UInt16 destappid   = BitConverter.ToUInt16(buff, (int)buffindex + 6);
                    UInt32 dataid      = BitConverter.ToUInt32(buff, (int)buffindex + 8);
                    lock (Synsendinglist)
                    {
                        if (sendinglist.ContainsKey(dataid))
                        {
                            SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_APPDOWN, sendinglist[dataid]);
                            sendinglist.Remove(dataid);
                            SendStatusEvent(e);
                        }
                    }
                }
            }
            else if (command == (int)COMMAND_ID.COMMAND_PACKET_ERR)
            {
                if (SendStatusEvent != null)
                {
                    UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 14);
                    lock (Synsendinglist)
                    {
                        if (sendinglist.ContainsKey(dataid))
                        {
                            SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_ERR, sendinglist[dataid]);
                            sendinglist.Remove(dataid);
                            SendStatusEvent(e);
                        }
                    }
                }
            }
            else if (command == (int)COMMAND_ID.COMMAND_PACKET_RECEIVED)
            {
                if (SendStatusEvent != null)
                {
                    UInt32 dataid = BitConverter.ToUInt32(buff, (int)buffindex + 14);
                    lock (Synsendinglist)
                    {
                        if (sendinglist.ContainsKey(dataid))
                        {
                            sendinglist[dataid].outtime = DateTime.Now;
                            SendStatusEventArgs e = new SendStatusEventArgs(SENDSTATUS.PACKET_RECEIVED, sendinglist[dataid]);
                            sendinglist.Remove(dataid);
                            SendStatusEvent(e);
                        }
                    }
                }
            }
            else if (command == (int)COMMAND_ID.COMMAND_QUERY_SERVER_RESPONSE)
            {
                if (updateClientEvent != null)
                {
                    char[]             trimChars = { (char)0 };
                    List <ServerClass> servers   = new List <ServerClass>();
                    int    servercount           = BitConverter.ToUInt16(buff, (int)buffindex + 2);
                    UInt32 pos = buffindex + 4;
                    for (int i = 0; i < servercount; ++i)
                    {
                        ushort      serverid  = BitConverter.ToUInt16(buff, (int)pos); pos = pos + 2;
                        string      stationid = Encoding.Default.GetString(buff, (int)pos, 33).Trim(trimChars); pos = pos + 33;
                        byte        online    = buff[pos]; ++pos;
                        ServerClass s         = new ServerClass();
                        s.m_Serverid    = serverid;
                        s.m_szStationID = stationid;
                        s.isonline      = (online == 1);
                        servers.Add(s);
                    }
                    ClienUpdateEventArgs e = new ClienUpdateEventArgs(CLIENTUPDATE.updateServer);
                    e.data = servers;
                    updateClientEvent(e);
                }
            }
            else if ((command == (int)COMMAND_ID.COMMAND_QUERY_APP_RESPONSE) || (command == (int)COMMAND_ID.COMMAND_SYN_APP))
            {
                if (updateClientEvent != null)
                {
                    List <AppObj> applist  = new List <AppObj>();
                    UInt32        pos      = buffindex + 2;
                    ushort        serverid = BitConverter.ToUInt16(buff, (int)pos); pos = pos + 2;
                    ushort        appcount = BitConverter.ToUInt16(buff, (int)pos); pos = pos + 2;
                    for (int i = 0; i < appcount; ++i)
                    {
                        ushort appid = BitConverter.ToUInt16(buff, (int)pos); pos = pos + 2;
                        byte   isadd = buff[pos]; ++pos;
                        AppObj a     = new AppObj();
                        a.m_Serverid = serverid;
                        a.app        = (APPID)appid;
                        a.isonline   = (isadd == 1);
                        applist.Add(a);
                    }
                    ClienUpdateEventArgs e = new ClienUpdateEventArgs(CLIENTUPDATE.updateApp);
                    e.data = applist;
                    updateClientEvent(e);
                }
            }
            //接受到数据包
            else if (command == (int)COMMAND_ID.COMMAND_PACKET)
            {
                if (ReceivedEvent != null)
                {
                    const int CommnadSize    = 2;  //命令大小
                    const int ConnectSize    = 12; //连接大小
                    const int DataidSize     = 4;  //数据块编号大小
                    const int DataLengthSize = 4;  //数据块长度大小
                    const int DataOffsetSize = 4;  //数据块偏移大小
                    const int MD5Length      = 16;
                    const int PacketHead     = CommnadSize + ConnectSize + DataidSize + DataLengthSize + DataOffsetSize;
                    bool      datacorrect    = true;
                    UInt16    SourServerid   = BitConverter.ToUInt16(buff, (int)buffindex + 2);
                    UInt16    DestServerid   = BitConverter.ToUInt16(buff, (int)buffindex + 4);
                    UInt16    SourAppid      = BitConverter.ToUInt16(buff, (int)buffindex + 6);
                    UInt16    DestAppid      = BitConverter.ToUInt16(buff, (int)buffindex + 8);
                    UInt32    connid         = BitConverter.ToUInt32(buff, (int)buffindex + 10);
                    UInt32    dataid         = BitConverter.ToUInt32(buff, (int)buffindex + 14);

                    byte[] smd5 = new byte[MD5Length];
                    Array.Copy(buff, buffindex + PacketHead, smd5, 0, MD5Length);
                    byte[] dmd5 = md5Pro.ComputeHash(buff, (int)buffindex + PacketHead + MD5Length, (int)PacketLength - PacketHead - MD5Length);
                    for (int i = 0; i < MD5Length; i++)
                    {
                        if (smd5[i] != dmd5[i])
                        {
                            datacorrect = false;
                        }
                    }
                    //验证数据正确性
                    if (datacorrect)
                    {
                        icPacket     packet = new icPacket();
                        object       data   = null;
                        MemoryStream stream = new MemoryStream(buff, (int)buffindex + PacketHead + MD5Length, (int)PacketLength - PacketHead - MD5Length);
                        data            = rformatter.Deserialize(stream);
                        packet.data     = data;
                        packet.Serverid = SourServerid;
                        packet.Appid    = (APPID)SourAppid;
                        packet.dataid   = dataid;
                        ReceivedEventArgs e = new ReceivedEventArgs(packet);
                        ReceivedEvent(e);
                        socket.Send(icParse.makePacketReceived(DestServerid, DestAppid, SourServerid, SourAppid, 0, dataid));
                    }
                    else
                    {
                        socket.Send(icParse.makePacketErr(DestServerid, DestAppid, SourServerid, SourAppid, 0, dataid));
                    }
                }
            }
        }