Exemple #1
0
        public void sendAndRecData()
        {
            #if NET_MULTHREAD
            using (MLock mlock = new MLock(m_visitMutex))
            #endif
            {
                // 从原始缓冲区取数据,然后放到解压和解密后的消息缓冲区中
                foreach (NetTCPClient client in m_id2ClientDic.Values)
                {
                    if (!client.brecvThreadStart && client.isConnected)
                    {
                        client.brecvThreadStart = true;
                        client.Receive();
                    }

                    // 处理接收到的数据
                    //socket.dataBuffer.moveRaw2Msg();
                    // 处理发送数据
                    if (client.canSendNewData())        // 只有上一次发送的数据全部发送出去后,才能继续发送新的数据
                    {
                        client.Send();
                    }
                }
            }
        }
Exemple #2
0
        public T RemoveAt(int index)
        {
            using (MLock mlock = new MLock(m_visitMutex))
            {
                if (index < m_dynamicBuffer.m_size)
                {
                    m_retItem = m_dynamicBuffer.m_buff[index];

                    if (index < m_dynamicBuffer.m_size)
                    {
                        if (index != m_dynamicBuffer.m_size - 1 && 1 != m_dynamicBuffer.m_size) // 如果删除不是最后一个元素或者总共就大于一个元素
                        {
                            Array.Copy(m_dynamicBuffer.m_buff, index + 1, m_dynamicBuffer.m_buff, index, m_dynamicBuffer.m_size - 1 - index);
                        }

                        --m_dynamicBuffer.m_size;
                    }
                }
                else
                {
                    m_retItem = default(T);
                }

                return(m_retItem);
            }
        }
Exemple #3
0
        /**
         * @brief 关闭当前 socket
         */
        public void closeCurSocket()
        {
            if (m_curClient != null)
            {
                string ip;
                int    port;

                ip   = m_curClient.m_ip;
                port = m_curClient.m_port;

                string key = ip + "&" + port;

                // 关闭 socket 之前要等待所有的数据都发送完成
                //m_id2SocketDic[key].msgSendEndEvent.Reset();        // 重置信号
                //m_id2SocketDic[key].msgSendEndEvent.WaitOne();      // 阻塞等待数据全部发送完成

                if (m_id2ClientDic.ContainsKey(key))
                {
                    #if NET_MULTHREAD
                    using (MLock mlock = new MLock(m_visitMutex))
                    #endif
                    {
                        m_id2ClientDic[key].Disconnect(0);
                        m_id2ClientDic.Remove(key);
                    }
                    m_curClient = null;
                }
            }
        }
Exemple #4
0
        public T this[int index]
        {
            get
            {
                using (MLock mlock = new MLock(m_visitMutex))
                {
                    if (index < m_dynamicBuffer.m_size)
                    {
                        return(m_dynamicBuffer.m_buff[index]);
                    }
                    else
                    {
                        return(default(T));
                    }
                }
            }

            set
            {
                using (MLock mlock = new MLock(m_visitMutex))
                {
                    m_dynamicBuffer.m_buff[index] = value;
                }
            }
        }
Exemple #5
0
        // 发送消息
        public void Send()
        {
#if NET_MULTHREAD
            using (MLock mlock = new MLock(m_sendMutex))
#endif
            {
                if (!checkAndUpdateConnect())
                {
                    return;
                }

                //checkThread();

                if (m_socket == null)
                {
                    return;
                }

                if (m_clientBuffer.sendBuffer.bytesAvailable == 0)            // 如果发送缓冲区没有要发送的数据
                {
                    if (m_clientBuffer.sendTmpBuffer.circularBuffer.size > 0) // 如果发送临时缓冲区有数据要发
                    {
                        m_clientBuffer.getSocketSendData();
                    }

                    if (m_clientBuffer.sendBuffer.bytesAvailable == 0)        // 如果发送缓冲区中确实没有数据
                    {
#if NET_MULTHREAD
                        m_msgSendEndEvent.Set();        // 通知等待线程,所有数据都发送完成
#endif
                        return;
                    }
                }

                try
                {
                    Ctx.m_instance.m_logSys.log(string.Format("开始发送字节数 {0} ", m_clientBuffer.sendBuffer.bytesAvailable));

                    IAsyncResult asyncSend = m_socket.BeginSend(m_clientBuffer.sendBuffer.dynBuff.buff, (int)m_clientBuffer.sendBuffer.position, (int)m_clientBuffer.sendBuffer.bytesAvailable, 0, new System.AsyncCallback(SendCallback), 0);
                    //bool success = asyncSend.AsyncWaitHandle.WaitOne(m_sendTimeout, true);
                    //if (!success)
                    //{
                    //    Ctx.m_instance.m_logSys.asyncLog(string.Format("SendMsg Timeout {0} ", m_sendTimeout));
                    //}
                }
                catch (System.Exception e)
                {
#if NET_MULTHREAD
                    m_msgSendEndEvent.Set();        // 发生异常,通知等待线程,所有数据都发送完成,防止等待线程不能解锁
#endif
                    // 输出日志
                    Ctx.m_instance.m_logSys.error(e.Message);
                    //Disconnect(0);
                }
            }
        }
Exemple #6
0
        public void Add(T item)
        {
            using (MLock mlock = new MLock(m_visitMutex))
            {
                if (m_dynamicBuffer.m_size >= m_dynamicBuffer.m_iCapacity)
                {
                    m_dynamicBuffer.extendDeltaCapicity(1);
                }

                m_dynamicBuffer.m_buff[m_dynamicBuffer.m_size] = item;
                ++m_dynamicBuffer.m_size;
            }
        }
Exemple #7
0
        public bool Remove(T item)
        {
            using (MLock mlock = new MLock(m_visitMutex))
            {
                int idx = 0;
                foreach (var elem in m_dynamicBuffer.m_buff)
                {
                    if (item.Equals(elem))       // 地址比较
                    {
                        this.RemoveAt(idx);
                        return(true);
                    }

                    ++idx;
                }
                return(false);
            }
        }
Exemple #8
0
        //发送回调
        private void SendCallback(System.IAsyncResult ar)
        {
#if NET_MULTHREAD
            using (MLock mlock = new MLock(m_sendMutex))
#endif
            {
                if (!checkAndUpdateConnect())
                {
                    return;
                }

                //checkThread();

                try
                {
                    int bytesSent = m_socket.EndSend(ar);
                    Ctx.m_instance.m_logSys.log(string.Format("结束发送字节数 {0} ", bytesSent));

                    if (m_clientBuffer.sendBuffer.length < m_clientBuffer.sendBuffer.position + (uint)bytesSent)
                    {
                        Ctx.m_instance.m_logSys.log(string.Format("结束发送字节数错误 {0}", bytesSent));
                        m_clientBuffer.sendBuffer.setPos(m_clientBuffer.sendBuffer.length);
                    }
                    else
                    {
                        m_clientBuffer.sendBuffer.setPos(m_clientBuffer.sendBuffer.position + (uint)bytesSent);
                    }

                    if (m_clientBuffer.sendBuffer.bytesAvailable > 0) // 如果上一次发送的数据还没发送完成,继续发送
                    {
                        Send();                                       // 继续发送数据
                    }
                }
                catch (System.Exception e)
                {
                    // 输出日志
                    Ctx.m_instance.m_logSys.error(e.Message);
                    //Disconnect(0);
                }
            }
        }
Exemple #9
0
        /**
         *@brief 打开到 socket 的连接
         */
        public bool openSocket(string ip, int port)
        {
            string key = ip + "&" + port;
            if (!m_id2ClientDic.ContainsKey(key))
            {
                m_curClient = new NetTCPClient(ip, port);
                m_curClient.Connect(ip, port);
                #if NET_MULTHREAD
                using (MLock mlock = new MLock(m_visitMutex))
                #endif
                {
                    m_id2ClientDic.Add(key, m_curClient);
                }
            }
            else
            {
                return false;
            }

            return true;
        }
Exemple #10
0
        /**
         * @brief 关闭 socket
         */
        public void closeSocket(string ip, int port)
        {
            string key = ip + "&" + port;
            if (m_id2ClientDic.ContainsKey(key))
            {
                // 关闭 socket 之前要等待所有的数据都发送完成,如果发送一直超时,可能就卡在这很长时间
                #if NET_MULTHREAD
                m_id2ClientDic[key].msgSendEndEvent.Reset();        // 重置信号
                m_id2ClientDic[key].msgSendEndEvent.WaitOne();      // 阻塞等待数据全部发送完成
                #endif

                #if NET_MULTHREAD
                using (MLock mlock = new MLock(m_visitMutex))
                #endif
                {
                    m_id2ClientDic[key].Disconnect(0);
                    m_id2ClientDic.Remove(key);
                }
                m_curClient = null;
            }
        }
Exemple #11
0
        /**
         *@brief 打开到 socket 的连接
         */
        public bool openSocket(string ip, int port)
        {
            string key = ip + "&" + port;

            if (!m_id2ClientDic.ContainsKey(key))
            {
                m_curClient = new NetTCPClient(ip, port);
                m_curClient.Connect(ip, port);
                #if NET_MULTHREAD
                using (MLock mlock = new MLock(m_visitMutex))
                #endif
                {
                    m_id2ClientDic.Add(key, m_curClient);
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
Exemple #12
0
        /**
         * @brief 关闭 socket
         */
        public void closeSocket(string ip, int port)
        {
            string key = ip + "&" + port;

            if (m_id2ClientDic.ContainsKey(key))
            {
                // 关闭 socket 之前要等待所有的数据都发送完成,如果发送一直超时,可能就卡在这很长时间
                #if NET_MULTHREAD
                m_id2ClientDic[key].msgSendEndEvent.Reset();        // 重置信号
                m_id2ClientDic[key].msgSendEndEvent.WaitOne();      // 阻塞等待数据全部发送完成
                #endif

                #if NET_MULTHREAD
                using (MLock mlock = new MLock(m_visitMutex))
                #endif
                {
                    m_id2ClientDic[key].Disconnect(0);
                    m_id2ClientDic.Remove(key);
                }
                m_curClient = null;
            }
        }
Exemple #13
0
        public string getText(LangTypeId typeId, LangItemID itemIdx)
        {
            if (!m_isLoaded)
            {
                // 多线程访问可能会有问题
                using (MLock mlock = new MLock(m_loadMutex))
                {
                    loadXml();
                }
            }

            string textStr = "";

            m_hasItem = false;

            if (null != m_nodeList)
            {
                if ((int)typeId < m_nodeList.Count)
                {
                    m_tmpEleList = (m_nodeList[(int)typeId] as SecurityElement).Children;
                    if ((int)itemIdx < m_tmpEleList.Count)
                    {
                        m_hasItem = true;
                        m_tmpEle  = m_tmpEleList[(int)itemIdx] as SecurityElement;
                        //Ctx.m_instance.m_shareData.m_retLangStr = m_tmpEle.InnerText;
                        textStr = m_tmpEle.Text;
                    }
                }
            }

            if (!m_hasItem)
            {
                //Ctx.m_instance.m_shareData.m_retLangStr = "default string";
                textStr = "default string";
            }

            return(textStr);
        }
Exemple #14
0
        public string getText(LangTypeId typeId, LangItemID itemIdx)
        {
            if (!m_isLoaded)
            {
                // 多线程访问可能会有问题
                using (MLock mlock = new MLock(m_loadMutex))
                {
                    loadXml();
                }
            }

            string textStr = "";
            m_hasItem = false;

            if(null != m_nodeList)
            {
                if ((int)typeId < m_nodeList.Count)
                {
                    m_tmpEleList = (m_nodeList[(int)typeId] as SecurityElement).Children;
                    if((int)itemIdx < m_tmpEleList.Count)
                    {
                        m_hasItem = true;
                        m_tmpEle = m_tmpEleList[(int)itemIdx] as SecurityElement;
                        //Ctx.m_instance.m_shareData.m_retLangStr = m_tmpEle.InnerText;
                        textStr = m_tmpEle.Text;
                    }
                }
            }

            if (!m_hasItem)
            {
                //Ctx.m_instance.m_shareData.m_retLangStr = "default string";
                textStr = "default string";
            }

            return textStr;
        }
Exemple #15
0
        public void sendAndRecData()
        {
            #if NET_MULTHREAD
            using (MLock mlock = new MLock(m_visitMutex))
            #endif
            {
                // 从原始缓冲区取数据,然后放到解压和解密后的消息缓冲区中
                foreach (NetTCPClient client in m_id2ClientDic.Values)
                {
                    if (!client.brecvThreadStart && client.isConnected)
                    {
                        client.brecvThreadStart = true;
                        client.Receive();
                    }

                    // 处理接收到的数据
                    //socket.dataBuffer.moveRaw2Msg();
                    // 处理发送数据
                    if (client.canSendNewData())        // 只有上一次发送的数据全部发送出去后,才能继续发送新的数据
                    {
                        client.Send();
                    }
                }
            }
        }
Exemple #16
0
        //发送回调
        private void SendCallback(System.IAsyncResult ar)
        {
#if NET_MULTHREAD
            using (MLock mlock = new MLock(m_sendMutex))
#endif
            {
                if (!checkAndUpdateConnect())
                {
                    return;
                }

                //checkThread();

                try
                {
                    int bytesSent = m_socket.EndSend(ar);
                    Ctx.m_instance.m_logSys.log(string.Format("结束发送字节数 {0} ", bytesSent));

                    if (m_clientBuffer.sendBuffer.length < m_clientBuffer.sendBuffer.position + (uint)bytesSent)
                    {
                        Ctx.m_instance.m_logSys.log(string.Format("结束发送字节数错误 {0}", bytesSent));
                        m_clientBuffer.sendBuffer.setPos(m_clientBuffer.sendBuffer.length);
                    }
                    else
                    {
                        m_clientBuffer.sendBuffer.setPos(m_clientBuffer.sendBuffer.position + (uint)bytesSent);
                    }

                    if (m_clientBuffer.sendBuffer.bytesAvailable > 0)     // 如果上一次发送的数据还没发送完成,继续发送
                    {
                        Send();                 // 继续发送数据
                    }
                }
                catch (System.Exception e)
                {
                    // 输出日志
                    Ctx.m_instance.m_logSys.error(e.Message);
                    //Disconnect(0);
                }
            }
        }
Exemple #17
0
        // 发送消息
        public void Send()
        {
#if NET_MULTHREAD
            using (MLock mlock = new MLock(m_sendMutex))
#endif
            {
                if (!checkAndUpdateConnect())
                {
                    return;
                }

                //checkThread();

                if (m_socket == null)
                {
                    return;
                }

                if (m_clientBuffer.sendBuffer.bytesAvailable == 0)     // 如果发送缓冲区没有要发送的数据
                {
                    if (m_clientBuffer.sendTmpBuffer.circularBuffer.size > 0)      // 如果发送临时缓冲区有数据要发
                    {
                        m_clientBuffer.getSocketSendData();
                    }

                    if (m_clientBuffer.sendBuffer.bytesAvailable == 0)        // 如果发送缓冲区中确实没有数据
                    {
#if NET_MULTHREAD
                        m_msgSendEndEvent.Set();        // 通知等待线程,所有数据都发送完成
#endif
                        return;
                    }
                }

                try
                {
                    Ctx.m_instance.m_logSys.log(string.Format("开始发送字节数 {0} ", m_clientBuffer.sendBuffer.bytesAvailable));

                    IAsyncResult asyncSend = m_socket.BeginSend(m_clientBuffer.sendBuffer.dynBuff.buff, (int)m_clientBuffer.sendBuffer.position, (int)m_clientBuffer.sendBuffer.bytesAvailable, 0, new System.AsyncCallback(SendCallback), 0);
                    //bool success = asyncSend.AsyncWaitHandle.WaitOne(m_sendTimeout, true);
                    //if (!success)
                    //{
                    //    Ctx.m_instance.m_logSys.asyncLog(string.Format("SendMsg Timeout {0} ", m_sendTimeout));
                    //}
                }
                catch (System.Exception e)
                {
#if NET_MULTHREAD
                    m_msgSendEndEvent.Set();        // 发生异常,通知等待线程,所有数据都发送完成,防止等待线程不能解锁
#endif
                    // 输出日志
                    Ctx.m_instance.m_logSys.error(e.Message);
                    //Disconnect(0);
                }
            }
        }
Exemple #18
0
        /**
         * @brief 关闭当前 socket
         */
        public void closeCurSocket()
        {
            if(m_curClient != null)
            {
                string ip;
                int port;

                ip = m_curClient.m_ip;
                port = m_curClient.m_port;

                string key = ip + "&" + port;

                // 关闭 socket 之前要等待所有的数据都发送完成
                //m_id2SocketDic[key].msgSendEndEvent.Reset();        // 重置信号
                //m_id2SocketDic[key].msgSendEndEvent.WaitOne();      // 阻塞等待数据全部发送完成

                if (m_id2ClientDic.ContainsKey(key))
                {
                    #if NET_MULTHREAD
                    using (MLock mlock = new MLock(m_visitMutex))
                    #endif
                    {
                        m_id2ClientDic[key].Disconnect(0);
                        m_id2ClientDic.Remove(key);
                    }
                    m_curClient = null;
                }
            }
        }