Esempio n. 1
0
 public bool Close()
 {
     if (m_socketBroker != null)
     {
         m_socketBroker.Close();
         m_socketBroker = null;
     }
     return(true);
 }
Esempio n. 2
0
        public void HandleMsg(IFFSocket ffsocket, UInt16 cmd, byte[] strMsg)
        {
            //FFLog.Trace(string.Format("FFBroker handleMsg....{0}, {1} [{2}]", cmd, strMsg.Length, System.Threading.Thread.CurrentThread.ManagedThreadId.ToString()));
            try
            {
                switch ((FFRPC_CMD)cmd)
                {
                case FFRPC_CMD.REGISTER_TO_BROKER_REQ:
                {
                    RegisterToBrokerReq reqMsg = new RegisterToBrokerReq();
                    FFNet.DecodeMsg(reqMsg, strMsg);
                    FFLog.Trace(string.Format("FFBroker handleMsg.REGISTER_TO_BROKER_REQ....{0}, {1}", reqMsg.Node_type, reqMsg.Service_name));
                    if (FFRPC_NODE_TYPE.RPC_NODE == (FFRPC_NODE_TYPE)reqMsg.Node_type)
                    {
                        if (m_brokerData.Service2node_id.ContainsKey(reqMsg.Service_name))
                        {
                            FFLog.Error(string.Format("FFBroker handleMsg servicename exist....{0}, {1}", reqMsg.Node_type, reqMsg.Service_name));
                            ffsocket.Close();
                            return;
                        }
                        Int64 nNodeID = allocNodeId();
                        m_dictSockets[nNodeID] = ffsocket;
                        m_brokerData.Service2node_id[reqMsg.Service_name] = nNodeID;
                        m_brokerData.Node_id = nNodeID;
                        SyncNodeInfo(m_brokerData, ffsocket);        //!广播给所有的子节点
                    }
                } break;

                case FFRPC_CMD.BROKER_ROUTE_MSG:
                {
                    BrokerRouteMsgReq reqMsg = new BrokerRouteMsgReq();
                    FFNet.DecodeMsg(reqMsg, strMsg);
                    FFLog.Trace(string.Format("FFBroker.BROKER_ROUTE_MSG service={0},func={1} Callback={2}",
                                              reqMsg.Dest_service_name, reqMsg.Dest_msg_name, reqMsg.Callback_id));
                    if (!m_dictSockets.ContainsKey(reqMsg.Dest_node_id))
                    {
                        return;
                    }
                    IFFSocket destSocket = m_dictSockets[reqMsg.Dest_node_id];
                    FFNet.SendMsg(destSocket, (UInt16)FFRPC_CMD.BROKER_TO_CLIENT_MSG, reqMsg);
                } break;

                default: break;
                }
            }
            catch (Exception ex)
            {
                FFLog.Error("FFBroker.Error:" + ex.Message);
            }
        }
Esempio n. 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;
        }
Esempio n. 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);
                }
            }
        }