/// <summary>
        /// Receive callback function
        /// </summary>
        /// <param name="result">result</param>
        private static void onReceived(IAsyncResult result)
        {
            PacketTransporter transport = result.AsyncState as PacketTransporter;
            NetworkStream     socket    = transport.m_iocpTcpClient.m_clientStream;

            int readSize = 0;

            try { readSize = socket.EndRead(result); }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                transport.m_iocpTcpClient.Disconnect(); return;
            }
            if (readSize == 0)
            {
                transport.m_iocpTcpClient.Disconnect();
                return;
            }
            if (readSize < transport.m_size)
            {
                transport.m_offset = transport.m_offset + readSize;
                transport.m_size   = transport.m_size - readSize;
                try{ socket.BeginRead(transport.m_packet.GetPacket(), transport.m_offset, transport.m_size, new AsyncCallback(IocpTcpClient.onReceived), transport); }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                    transport.m_iocpTcpClient.Disconnect(); return;
                }
            }
            else
            {
                if (transport.m_packetType == PacketType.SIZE)
                {
                    int               shouldReceive = BitConverter.ToInt32(transport.m_packet.GetPacket(), 0);
                    Packet            recvPacket    = new Packet(null, shouldReceive);
                    PacketTransporter dataTransport = new PacketTransporter(PacketType.DATA, recvPacket, 0, shouldReceive, transport.m_iocpTcpClient);
                    try{ socket.BeginRead(recvPacket.GetPacket(), 0, shouldReceive, new AsyncCallback(IocpTcpClient.onReceived), dataTransport); }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                        transport.m_iocpTcpClient.Disconnect(); return;
                    }
                }
                else
                {
                    PacketTransporter sizeTransport = new PacketTransporter(PacketType.SIZE, transport.m_iocpTcpClient.m_recvSizePacket, 0, 4, transport.m_iocpTcpClient);
                    try { socket.BeginRead(sizeTransport.m_packet.GetPacket(), 0, 4, new AsyncCallback(IocpTcpClient.onReceived), sizeTransport); }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                        transport.m_iocpTcpClient.Disconnect(); return;
                    }
                    transport.m_callBackObj.OnReceived(transport.m_iocpTcpClient, transport.m_packet);
                }
            }
        }
        /// <summary>
        /// Send given packet to the server
        /// </summary>
        /// <param name="packet">packet to send</param>
        public void Send(Packet packet)
        {
            if (!IsConnectionAlive())
            {
                if (m_callBackObj != null)
                {
                    Thread t = new Thread(delegate()
                    {
                        m_callBackObj.OnSent(this, SendStatus.FAIL_NOT_CONNECTED);
                    });
                    t.Start();
                }
                return;
            }
            if (packet.GetPacketByteSize() <= 0)
            {
                if (m_callBackObj != null)
                {
                    Thread t = new Thread(delegate()
                    {
                        m_callBackObj.OnSent(this, SendStatus.FAIL_INVALID_PACKET);
                    });
                    t.Start();
                }
                return;
            }

            lock (m_sendLock)
            {
                Packet            sendSizePacket = new Packet(null, 4, false);
                PacketTransporter transport      = new PacketTransporter(PacketType.SIZE, sendSizePacket, 0, 4, this, packet);
                sendSizePacket.SetPacket(BitConverter.GetBytes(packet.GetPacketByteSize()), 4);
                if (m_sendEvent.TryLock())
                {
                    try { m_clientStream.BeginWrite(sendSizePacket.GetPacket(), 0, 4, new AsyncCallback(IocpTcpClient.onSent), transport); }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                        if (m_callBackObj != null)
                        {
                            m_callBackObj.OnSent(this, SendStatus.FAIL_SOCKET_ERROR);
                        }
                        Disconnect();
                        return;
                    }
                }
                else
                {
                    lock (m_sendQueueLock)
                    {
                        m_sendQueue.Enqueue(transport);
                    }
                }
            }
        }
        /// <summary>
        /// Start to receive packet from the server
        /// </summary>
        private void startReceive()
        {
            PacketTransporter transport = new PacketTransporter(PacketType.SIZE, m_recvSizePacket, 0, 4, this);

            try { m_clientStream.BeginRead(m_recvSizePacket.GetPacket(), 0, 4, new AsyncCallback(IocpTcpClient.onReceived), transport); }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                Disconnect(); return;
            }
        }
 public void OnReceived(INetworkClient client, Packet receivedPacket)
 {
     string sendString = StringFromByteArr(receivedPacket.GetPacket()) + "\r\n";
     AddMsg(sendString);
 }
 public void OnReceived(INetworkSocket socket, Packet receivedPacket)
 {
     string sendString = "User(" + socket.GetIPInfo().GetIPAddress() + ") : " + StringFromByteArr(receivedPacket.GetPacket());
     AddMsg(sendString);
     foreach (var socketObj in m_socketList)
     {
         if (socketObj != socket)
         {
             socketObj.Send(receivedPacket);
         }
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// Receive callback function
 /// </summary>
 /// <param name="result">result</param>
 private static void onReceived(IAsyncResult result)
 {
     PacketTransporter transport = result.AsyncState as PacketTransporter;
     NetworkStream socket = transport.m_iocpTcpClient.m_clientStream;
     
     int readSize=0;
     try { readSize = socket.EndRead(result); }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message + " >" + ex.StackTrace);
         transport.m_iocpTcpClient.Disconnect(); return;
     }
     if (readSize == 0)
     {
         transport.m_iocpTcpClient.Disconnect();
         return;
     }
     if (readSize < transport.m_size)
     {
         transport.m_offset = transport.m_offset + readSize;
         transport.m_size = transport.m_size - readSize;
         try{socket.BeginRead(transport.m_packet.GetPacket(), transport.m_offset, transport.m_size,  new AsyncCallback(IocpTcpClient.onReceived), transport);}
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message + " >" + ex.StackTrace);
             transport.m_iocpTcpClient.Disconnect(); return;
         }
     }
     else
     {
         if (transport.m_packetType == PacketType.SIZE)
         {
             int shouldReceive = BitConverter.ToInt32(transport.m_packet.GetPacket(), 0);
             Packet recvPacket = new Packet(null, shouldReceive);
             PacketTransporter dataTransport = new PacketTransporter(PacketType.DATA, recvPacket, 0, shouldReceive, transport.m_iocpTcpClient);
             try{socket.BeginRead(recvPacket.GetPacket(), 0, shouldReceive,  new AsyncCallback(IocpTcpClient.onReceived), dataTransport);}
             catch (Exception ex)
             {
                 Console.WriteLine(ex.Message + " >" + ex.StackTrace); 
                 transport.m_iocpTcpClient.Disconnect(); return;
             }
         }
         else
         {
             PacketTransporter sizeTransport = new PacketTransporter(PacketType.SIZE, transport.m_iocpTcpClient.m_recvSizePacket, 0, 4, transport.m_iocpTcpClient);
             try { socket.BeginRead(sizeTransport.m_packet.GetPacket(), 0, 4, new AsyncCallback(IocpTcpClient.onReceived), sizeTransport); }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                 transport.m_iocpTcpClient.Disconnect(); return;
             }
             transport.m_callBackObj.OnReceived(transport.m_iocpTcpClient, transport.m_packet);
         }
     }
   }
Exemplo n.º 7
0
        /// <summary>
        /// Send given packet to the server
        /// </summary>
        /// <param name="packet">packet to send</param>
        public void Send(Packet packet)
        {

            if (!IsConnectionAlive())
            {
                if (m_callBackObj != null)
                {
                    Thread t = new Thread(delegate()
                    {
                        m_callBackObj.OnSent(this, SendStatus.FAIL_NOT_CONNECTED);
                    });
                    t.Start();
                }
                return;
            }
            if (packet.GetPacketByteSize() <= 0)
            {
                if (m_callBackObj != null)
                {
                    Thread t = new Thread(delegate()
                    {
                        m_callBackObj.OnSent(this, SendStatus.FAIL_INVALID_PACKET);
                    });
                    t.Start();
                }
                return;
            }

            lock (m_sendLock)
            {
                Packet sendSizePacket = new Packet(null, 4, false);
                PacketTransporter transport = new PacketTransporter(PacketType.SIZE, sendSizePacket, 0, 4, this, packet);
                sendSizePacket.SetPacket(BitConverter.GetBytes(packet.GetPacketByteSize()), 4);
                if (m_sendEvent.TryLock())
                {
                    try { m_clientStream.BeginWrite(sendSizePacket.GetPacket(), 0, 4, new AsyncCallback(IocpTcpClient.onSent), transport); }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                        if (m_callBackObj != null)
                            m_callBackObj.OnSent(this, SendStatus.FAIL_SOCKET_ERROR);
                        Disconnect(); 
                        return; 
                    }
                }
                else
                {
                    lock (m_sendQueueLock)
                    {
                        m_sendQueue.Enqueue(transport);
                    }
                }
            }
            
            
        }