/// <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);
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Send given packet to the client
        /// </summary>
        /// <param name="packet">the packet to send</param>
        public void Send(Packet packet)
        {
            if (!IsConnectionAlive)
            {
                Task t = new Task(delegate()
                {
                    OnSent(this, SendStatus.FAIL_NOT_CONNECTED, packet);
                });
                t.Start();

                return;
            }
            if (packet.PacketByteSize <= 0)
            {
                Task t = new Task(delegate()
                {
                    OnSent(this, SendStatus.FAIL_INVALID_PACKET, packet);
                });
                t.Start();

                return;
            }

            lock (m_sendLock)
            {
                Packet            sendSizePacket = new Packet(null, 0, Preamble.SIZE_PACKET_LENGTH, false);
                PacketTransporter transport      = new PacketTransporter(PacketType.SIZE, sendSizePacket, 0, Preamble.SIZE_PACKET_LENGTH, this, packet);

                //sendSizePacket.SetPacket(BitConverter.GetBytes(packet.GetPacketByteSize()), 4);
                sendSizePacket.SetPacket(Preamble.ToPreamblePacket(packet.PacketByteSize), 0, Preamble.SIZE_PACKET_LENGTH);
                if (m_sendEvent.TryLock())
                {
                    try { m_client.Client.BeginSend(sendSizePacket.PacketRaw, 0, Preamble.SIZE_PACKET_LENGTH, SocketFlags.None, new AsyncCallback(IocpTcpSocket.onSent), transport); }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                        Disconnect();
                        return;
                    }
                }
                else
                {
                    lock (m_sendQueueLock)
                    {
                        m_sendQueue.Enqueue(transport);
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Send given packet to the server
        /// </summary>
        /// <param name="packet">packet to send</param>
        public void Send(Packet packet)
        {

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

            lock (m_sendLock)
            {
                Packet sendSizePacket = new Packet(null,0, Preamble.SIZE_PACKET_LENGTH, false);
                PacketTransporter transport = new PacketTransporter(PacketType.SIZE, sendSizePacket, 0, Preamble.SIZE_PACKET_LENGTH, this, packet);
                //sendSizePacket.SetPacket(BitConverter.GetBytes(packet.GetPacketByteSize()), ServerConf.SIZE_PACKET_LENGTH);
                sendSizePacket.SetPacket(Preamble.ToPreamblePacket(packet.PacketByteSize), 0, Preamble.SIZE_PACKET_LENGTH);
                if (m_sendEvent.TryLock())
                {
                    try { m_client.Client.BeginSend(sendSizePacket.PacketRaw, 0, Preamble.SIZE_PACKET_LENGTH, SocketFlags.None, new AsyncCallback(IocpTcpClient.onSent), transport); }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                        OnSent(this, SendStatus.FAIL_SOCKET_ERROR, packet);
                        Disconnect(); 
                        return; 
                    }
                }
                else
                {
                    lock (m_sendQueueLock)
                    {
                        m_sendQueue.Enqueue(transport);
                    }
                }
            }
            
            
        }
Esempio n. 4
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);
                    }
                }
            }
            
            
        }