Exemple #1
0
        private void ThreadSend()
        {
            byte[] sendBuffer = new byte[BufferSize];

            while (!m_Abort && IsConnected())
            {
                PacketSender packetSender;
                lock (m_QueueToSend.SyncRoot)
                {
                    while (m_QueueToSend.Count <= 0 && !m_Abort)
                    {
                        Monitor.Wait(m_QueueToSend.SyncRoot);
                    }

                    if (m_Abort)
                    {
                        break;
                    }

                    packetSender = (PacketSender)m_QueueToSend.Dequeue();
                }


                bool bSendCompress = false;
                int  packetSize    = PacketHeaderSize;
                if (m_bEnableSendCompress)
                {
                    //(1)先压缩
                    byte[] dataCompress;
                    MiniLZO.Compress(packetSender.data, out dataCompress);

                    //(2)判断长度是否比不压缩的更长
                    bSendCompress = dataCompress.Length < packetSender.data.Length;
                    if (bSendCompress)//发送压缩数据
                    {
                        //压缩的头两个字节,第16位指明是否压缩,剩下的15位是长度
                        int    compressDataHead = dataCompress.Length | CompressFlag;
                        byte[] packetSizeBytes  = BitConverter.GetBytes((ushort)compressDataHead);
                        Array.Copy(packetSizeBytes, 0, sendBuffer, 0, PacketLenSize);
                        Array.Copy(dataCompress, 0, sendBuffer, PacketLenSize, dataCompress.Length);
                        packetSize += dataCompress.Length;
                    }
                }

                //不被压缩的发原来的数据
                if (!bSendCompress)
                {
                    int    compressDataHead = packetSender.data.Length;
                    byte[] packetSizeBytes  = BitConverter.GetBytes((ushort)compressDataHead);
                    Array.Copy(packetSizeBytes, 0, sendBuffer, 0, PacketLenSize);
                    Array.Copy(packetSender.data, 0, sendBuffer, PacketLenSize, packetSender.data.Length);
                    packetSize += packetSender.data.Length;
                }


                SocketError error;
                m_Connection.Send(sendBuffer, 0, packetSize, SocketFlags.None, out error);

                if (error != SocketError.Success)
                {
                    UnityEngine.Debug.LogErrorFormat("Send message error:{0}", error.ToString());
                }
            }
        }
Exemple #2
0
        private void ThreadReceive()
        {
            var packetBuffer = new PacketBuffer();

            while (!m_Abort && IsConnected())
            {
                try
                {
                    int num = m_Connection.Receive(packetBuffer.buffer, packetBuffer.len, BufferSize - packetBuffer.len, SocketFlags.None);
                    if (num > 0)
                    {
                        packetBuffer.len += num;

                        int index = 0;
                        for (; index + PacketHeaderSize <= packetBuffer.len;)
                        {
                            UInt16 packetLen  = BitConverter.ToUInt16(packetBuffer.buffer, index);
                            bool   isCompress = (packetLen & CompressFlag) != 0;
                            packetLen &= (CompressFlag - 1);
                            if (index + packetLen + PacketLenSize > packetBuffer.len)
                            {
                                break;
                            }

                            UInt16 packetId = 0;
                            byte[] message;

                            if (isCompress)
                            {
                                uint decompressLen = 0;
                                MiniLZO.Decompress(packetBuffer.buffer, index + PacketLenSize, packetLen,
                                                   m_DecompressData, ref decompressLen);

                                //packetId = BitConverter.ToUInt16(m_DecompressData, 0);

                                message = new byte[decompressLen];
                                Array.Copy(m_DecompressData, message, message.Length);
                            }
                            else
                            {
                                //packetId = BitConverter.ToUInt16(packetBuffer.buffer, index + PacketLenSize);

                                int messageLength = packetLen;
                                message = new byte[messageLength];

                                Array.Copy(packetBuffer.buffer, index + PacketHeaderSize, message, 0, messageLength);
                            }

                            if (SocketMessageReceived != null)
                            {
                                SocketMessageReceived(this,
                                                      new SocketMessageReceived(packetId, message, (UInt16)message.Length));
                            }

                            index += packetLen + PacketLenSize;
                        }

                        int remainSize = packetBuffer.len - index;
                        if (remainSize > 0)
                        {
                            Array.Copy(packetBuffer.buffer, index, packetBuffer.swapBuffer, 0, remainSize);

                            byte[] temp = packetBuffer.buffer;
                            packetBuffer.buffer     = packetBuffer.swapBuffer;
                            packetBuffer.swapBuffer = temp;
                        }
                        packetBuffer.len = remainSize;
                    }
                    else
                    {
                        if (CloseHandler != null)
                        {
                            CloseHandler(this, new EventArgs());
                        }
                        break;
                    }
                }
                catch (SocketException e)
                {
                    m_Abort = true;
                    UnityEngine.Debug.LogErrorFormat("ThreadReceive error:{0}", e.ToString());
                    ConnectError(this, new EventArgs());
                }
                catch (OverflowException e)
                {
                    m_Abort = true;
                    UnityEngine.Debug.LogErrorFormat("ThreadReceive error:{0}", e.ToString());
                    ConnectError(this, new EventArgs());
                }
            }
        }