Пример #1
0
 public void Close()
 {
     if (m_Socket != null && m_Socket.Connected)
     {
         try
         {
             //m_Socket.Disconnect(true)
             m_Socket.Shutdown(SocketShutdown.Both);
         }
         catch (Exception e)
         {
             TcpLogger.LogError(e.Message);
         }
         try
         {
             m_Socket.Close();
         }
         catch (Exception e)
         {
             TcpLogger.LogError(e.Message);
         }
     }
     m_Socket     = null;
     IsConnecting = false;
     m_RecvIdx    = 0;
     m_SendIdx    = 0;
     m_SendQueue  = new ConcurrentQueue <byte[]>();
     m_RecvQueue  = new ConcurrentQueue <byte[]>();
     InitAesEncryptor();
     InitAesDecryptor();
 }
Пример #2
0
        void Update(long tick)
        {
            QueueItem item  = null;
            bool      error = false;

            while (m_TcpConnectionsToUpdate.TryDequeue(out item))
            {
                if (item.AddOrRemove)
                {
                    error = false;
                    try
                    {
                        item.conn.Initialize();
                    }
                    catch (Exception e)
                    {
                        error = true;
                        item.conn.Stop();
                        TcpLogger.LogError(e.Message);
                    }
                    if (!error)
                    {
                        m_TcpConnections.Add(item.conn);
                    }
                }
                else
                {
                    m_TcpConnections.Remove(item.conn);
                }
            }
            foreach (var conn in m_TcpConnections)
            {
                error = false;
                try
                {
                    conn.Update(tick);
                }
                catch (Exception e)
                {
                    conn.Stop();
                    TcpLogger.LogError(e.Message);
                }
                if (error)
                {
                    m_TcpConnectionsToRemove.Add(conn);
                }
            }
            if (m_TcpConnectionsToRemove.Count > 0)
            {
                foreach (var conn in m_TcpConnectionsToRemove)
                {
                    m_TcpConnections.Remove(conn);
                }
                m_TcpConnectionsToRemove.Clear();
            }
        }
Пример #3
0
 void OnRecvPack(byte[] pack)
 {
     if (!IsConnected)
     {
         TcpLogger.LogError("Socket not connected, recvied pack will be drop.");
         return;
     }
     pack = TcpTools.Decode(pack, m_AesDecryptor, ref m_RecvIdx);//解压缩,解加密
     m_RecvQueue.Enqueue(pack);
 }
Пример #4
0
        internal static byte[] Decode(byte[] arr, AesDecryptor aesDecryptor, ref int recvIdx)
        {
            byte flag  = arr[0];
            bool ziped = ((flag & 0x80) == 0x80);
            bool aesed = ((flag & 0x40) == 0x40);
            bool crced = ((flag & 0x20) == 0x20);
            int  idx   = flag & 0x1F;

            if (recvIdx == idx)
            {
                recvIdx++;
                if (recvIdx > 0x1F)
                {
                    recvIdx = 0;
                }
                Byte[] bcrc = new Byte[4];
                Buffer.BlockCopy(arr, 1, bcrc, 0, 4);
                int    crc32 = BitConverter.ToInt32(bcrc, 0);
                Byte[] data  = new Byte[arr.Length - 1 - 4];
                Buffer.BlockCopy(arr, 1 + 4, data, 0, data.Length);
                int ncrc32 = 0;
                if (crced)
                {
                    ncrc32 = Crc.Crc32(data);
                }
                if (ncrc32 == crc32)
                {
                    if (aesed)
                    {
                        data = aesDecryptor.Decrypt(data);
                    }
                    if (ziped)
                    {
                        data = ZLib.UnZip(data);
                    }
                    if (data != null)
                    {
                        return(data);
                    }
                    else
                    {
                        TcpLogger.LogError("Recv Decode data null");
                    }
                }
                else
                {
                    TcpLogger.LogError("Recv error crc32 " + crc32 + "   ncrc32" + ncrc32);
                }
            }
            else
            {
                TcpLogger.LogError("Recv error idx " + idx + "   lidx" + recvIdx);
            }
            return(null);
        }
Пример #5
0
        public bool Connect(string addr, int port)
        {
            if (!IsRunning)
            {
                TcpLogger.LogError("Not running");
                return(false);
            }
            var ip = ParseIpAddressV6(addr);

            if (ip == null)
            {
                TcpLogger.LogError("Unknown addr = " + addr);
                return(false);
            }

            Close();

            TcpLogger.Log("Connect " + addr + " -> " + ip + ":" + port);

            m_Socket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            m_Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, SEND_BUFFER_LENGTH);
            m_Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, RECEIVE_BUFFER_LENGTH);

            m_Address = addr;
            m_Port    = port;

            try
            {
                //m_Socket.Blocking = false;
                IsConnecting = true;
                m_Socket.BeginConnect(new IPEndPoint(ip, port), new AsyncCallback(ConnectCallback), m_Socket);
                return(true);
            }
            catch (Exception e)
            {
                AddEvent(TcpEventType.EventConnected, SocketError.SocketError, e.Message);
                return(false);
            }
        }
Пример #6
0
 void HandleAcceptConnected(IAsyncResult ar)
 {
     if (IsRunning)
     {
         var server = ar.AsyncState as Socket;
         var client = server.EndAccept(ar);
         if (m_ClientCount >= m_MaxClient)
         {
             client.Close();
             TcpLogger.LogError("Accept more than max " + m_MaxClient);
         }
         else
         {
             lock (m_Lock)
             {
                 m_ClientCount++;
             }
             TcpConnection <T> .AcceptConnected(this, client);
         }
         server.BeginAccept(new AsyncCallback(HandleAcceptConnected), server);
     }
 }