Beispiel #1
0
        public static void SendMsg(IFFSocket ffsocket, UInt16 cmdSrc, byte[] strData)
        {
            if (ffsocket.GetProtocolType() == "websocket-text")
            {
                byte[] cmddata = Util.String2Byte(string.Format("cmd:{0}\n", cmdSrc));
                byte[] wsData  = Util.MergeArray(cmddata, strData);
                ffsocket.AsyncSend(wsData);
                return;
            }
            int len = strData.Length;

            len = System.Net.IPAddress.HostToNetworkOrder(len);
            UInt16 cmd = (UInt16)System.Net.IPAddress.HostToNetworkOrder((Int16)cmdSrc);

            byte[] lenArray = BitConverter.GetBytes(len);
            byte[] cmdArray = BitConverter.GetBytes(cmd);
            byte[] resArray = new byte[2] {
                0, 0
            };
            byte[][] p        = { lenArray, cmdArray, resArray, strData };
            byte[]   sendData = Util.MergeArray(p);
            ffsocket.AsyncSend(sendData);
            //FFLog.Trace(string.Format("SendMsg cmd:{0},len:{1},len2:{2}", cmdSrc, strData.Length, sendData.Length));
        }
Beispiel #2
0
        public static void SendMsg(IFFSocket ffsocket, UInt16 cmdSrc, byte[] strData)
        {
            int len = strData.Length;

            len = System.Net.IPAddress.HostToNetworkOrder(len);
            UInt16 cmd = (UInt16)System.Net.IPAddress.HostToNetworkOrder((Int16)cmdSrc);

            byte[] lenArray = BitConverter.GetBytes(len);
            byte[] cmdArray = BitConverter.GetBytes(cmd);
            byte[] resArray = new byte[2] {
                0, 0
            };
            byte[][] p        = { lenArray, cmdArray, resArray, strData };
            byte[]   sendData = Util.MergeArray(p);
            ffsocket.AsyncSend(sendData);
            FFLog.Trace(string.Format("SendMsg cmd:{0},len:{1},len2:{2}", cmdSrc, strData.Length, sendData.Length));
        }
Beispiel #3
0
        public void HandleRecv(IFFSocket ffsocket, byte[] strData)
        {
            if (m_oWSProtocol.HandleRecv(strData))
            {
                if (ffsocket.GetProtocolType().Length == 0)
                {
                    ffsocket.SetProtocolType("websocket");
                }
                foreach (var eachWaitSend in m_oWSProtocol.GetSendPkg())
                {
                    ffsocket.AsyncSend(eachWaitSend, false);
                }
                m_oWSProtocol.ClearSendPkg();

                foreach (var eachRecvPkg in m_oWSProtocol.GetRecvPkg())
                {
                    if (eachRecvPkg.Length == 0)
                    {
                        continue;
                    }

                    UInt16 nCmd     = 0;
                    byte[] dataBody = eachRecvPkg;


                    int nHeadEndIndex = -1;
                    if (eachRecvPkg[0] == 'c' || eachRecvPkg[0] == 'C')
                    {
                        ffsocket.SetProtocolType("websocket-text");
                        for (int i = 0; i < eachRecvPkg.Length; ++i)
                        {
                            if (eachRecvPkg[i] == '\n')
                            {
                                nHeadEndIndex = i;
                                break;
                            }
                        }
                    }
                    if (nHeadEndIndex > 0)
                    {
                        byte[] bytesHead = new byte[nHeadEndIndex];
                        dataBody = new byte[eachRecvPkg.Length - nHeadEndIndex - 1];
                        Array.Copy(eachRecvPkg, 0, bytesHead, 0, bytesHead.Length);
                        Array.Copy(eachRecvPkg, nHeadEndIndex + 1, dataBody, 0, dataBody.Length);

                        string[] strHeads = Util.Byte2String(bytesHead).Split(",");
                        string[] strCmds  = strHeads[0].Split(":");
                        if (strCmds.Length == 2 && strCmds[1].Length > 0)
                        {
                            nCmd = Convert.ToUInt16(strCmds[1]);
                        }
                    }
                    else
                    {
                        if (eachRecvPkg.Length < 8)
                        {
                            continue;
                        }
                        size     = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt32(eachRecvPkg, 0));
                        nCmd     = (UInt16)System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt16(eachRecvPkg, 4));
                        flag     = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt16(eachRecvPkg, 6));
                        dataBody = new byte[eachRecvPkg.Length - 8];
                        Array.Copy(eachRecvPkg, 8, dataBody, 0, dataBody.Length);
                    }
                    //FFLog.Trace(string.Format("cmd={0},data={1}", nCmd, dataBody.Length));

                    try
                    {
                        m_funcMsgHandler(ffsocket, nCmd, dataBody);
                    }
                    catch (Exception ex)
                    {
                        FFLog.Error("wsscoket.HandleRecv error:" + ex.Message);
                    }
                }
                m_oWSProtocol.ClearRecvPkg();
                if (m_oWSProtocol.IsClose())
                {
                    ffsocket.Close();
                }
                return;
            }

            Array.Copy(strData, 0, m_strRecvData, nLeftSize, strData.Length);
            nLeftSize += strData.Length;
            //Util.MergeArray(m_strRecvData, strData);
            int nRead = 0;

            while (true)
            {
                if (nLeftSize < nRead + 8)
                {
                    Array.Copy(m_strRecvData, nRead, m_strRecvData, 0, nLeftSize - nRead);
                    break;
                }
                size = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt32(m_strRecvData, nRead + 0));
                cmd  = (UInt16)System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt16(m_strRecvData, nRead + 4));
                flag = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt16(m_strRecvData, nRead + 6));
                //FFLog.Trace(string.Format("HandleRecv cmd:{0},len:{1},recvlen:{2}", cmd, size, m_strRecvData.Length));

                if (cmd == 0 || size == 0)
                {
                    string st = "";
                    foreach (byte b in m_strRecvData)
                    {
                        st += b.ToString();
                    }
                    FFLog.Trace(st);

                    m_strRecvData = new byte[0];
                    string st2 = "";
                    foreach (byte b in strData)
                    {
                        st2 += b.ToString();
                    }
                    FFLog.Trace(st2);
                    break;
                }
                if (nLeftSize < nRead + 8 + size)
                {
                    //FFLog.Error("scoket.HandleRecv 111111111111111111");
                    Array.Copy(m_strRecvData, nRead, m_strRecvData, 0, nLeftSize - nRead);
                    break;
                }
                byte[] msgBody = new byte[size];
                Array.Copy(m_strRecvData, nRead + 8, msgBody, 0, size);
                nRead += (8 + size);
                // if (strData.Length == 8 + size)
                // {
                // m_strRecvData = new byte[0];
                // }
                // else{
                // FFLog.Trace(string.Format("HandleRecv cmd:{0},len:{1},recvlen:{2},leftData:{3}", cmd, size, m_strRecvData.Length, leftData.Length));
                // }
                try
                {
                    m_funcMsgHandler(ffsocket, cmd, msgBody);
                }
                catch (Exception ex)
                {
                    FFLog.Error("scoket.HandleRecv error:" + ex.Message);
                }
            }
            nLeftSize -= nRead;
        }
Beispiel #4
0
        public void HandleRecv(IFFSocket ffsocket, byte[] strData)
        {
            if (m_oWSProtocol.HandleRecv(strData))
            {
                if (ffsocket.GetProtocolType().Length == 0)
                {
                    ffsocket.SetProtocolType("websocket");
                }
                foreach (var eachWaitSend in m_oWSProtocol.GetSendPkg())
                {
                    ffsocket.AsyncSend(eachWaitSend, false);
                }
                m_oWSProtocol.ClearSendPkg();

                foreach (var eachRecvPkg in m_oWSProtocol.GetRecvPkg())
                {
                    int nHeadEndIndex = -1;
                    for (int i = 0; i < eachRecvPkg.Length; ++i)
                    {
                        if (eachRecvPkg[i] == '\n')
                        {
                            nHeadEndIndex = i;
                            break;
                        }
                    }
                    UInt16 nCmd     = 0;
                    byte[] dataBody = eachRecvPkg;
                    if (nHeadEndIndex > 0)
                    {
                        byte[] bytesHead = new byte[nHeadEndIndex];
                        dataBody = new byte[eachRecvPkg.Length - nHeadEndIndex - 1];
                        Array.Copy(eachRecvPkg, 0, bytesHead, 0, bytesHead.Length);
                        Array.Copy(eachRecvPkg, nHeadEndIndex + 1, dataBody, 0, dataBody.Length);

                        string[] strHeads = Util.Byte2String(bytesHead).Split(",");
                        string[] strCmds  = strHeads[0].Split(":");
                        if (strCmds.Length == 2 && strCmds[1].Length > 0)
                        {
                            nCmd = Convert.ToUInt16(strCmds[1]);
                        }
                    }
                    FFLog.Trace(string.Format("cmd={0},data={1}", nCmd, Util.Byte2String(dataBody)));

                    try
                    {
                        m_funcMsgHandler(ffsocket, nCmd, dataBody);
                    }
                    catch (Exception ex)
                    {
                        FFLog.Error("wsscoket.HandleRecv error:" + ex.Message);
                    }
                }
                m_oWSProtocol.ClearRecvPkg();
                if (m_oWSProtocol.IsClose())
                {
                    ffsocket.Close();
                }
                return;
            }

            m_strRecvData = Util.MergeArray(m_strRecvData, strData);
            while (m_strRecvData.Length >= 8)
            {
                size = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt32(m_strRecvData, 0));
                cmd  = (UInt16)System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt16(m_strRecvData, 4));
                flag = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt16(m_strRecvData, 6));
                FFLog.Trace(string.Format("HandleRecv cmd:{0},len:{1}", cmd, size));
                if (m_strRecvData.Length < 8 + size)
                {
                    break;
                }
                byte[] msgBody = new byte[size];
                Array.Copy(m_strRecvData, 8, msgBody, 0, size);
                if (m_strRecvData.Length == 8 + size)
                {
                    m_strRecvData = new byte[0];
                }
                else
                {
                    byte[] leftData = new byte[m_strRecvData.Length - (8 + size)];
                    Array.Copy(m_strRecvData, 8 + size, leftData, 0, size);
                    m_strRecvData = leftData;
                }
                try
                {
                    m_funcMsgHandler(ffsocket, cmd, msgBody);
                }
                catch (Exception ex)
                {
                    FFLog.Error("scoket.HandleRecv error:" + ex.Message);
                }
            }
        }