private void btnSend_Click(object sender, EventArgs e)
 {
     string sendText = tbSend.Text.Trim();
     if (sendText.Length <= 0)
     {
         MessageBox.Show("Please type in something to send.");
     }
     byte[] bytes = BytesFromString(sendText);
     Packet packet=new Packet(bytes,bytes.Count(),false);
     m_client.Send(packet);
 }
 public void OnSent(INetworkClient client, SendStatus status, Packet sentPacket)
 {
     switch (status)
     {
         case SendStatus.SUCCESS:
             Debug.WriteLine("SEND Success");
             break;
         case SendStatus.FAIL_CONNECTION_CLOSING:
             Debug.WriteLine("SEND failed due to connection closing");
             break;
         case SendStatus.FAIL_INVALID_PACKET:
             Debug.WriteLine("SEND failed due to invalid socket");
             break;
         case SendStatus.FAIL_NOT_CONNECTED:
             Debug.WriteLine("SEND failed due to no connection");
             break;
         case SendStatus.FAIL_SOCKET_ERROR:
             Debug.WriteLine("SEND Socket Error");
             break;
     }
 }
 public void OnReceived(INetworkSocket socket, Packet receivedPacket)
 {
     string sendString = "User(" + socket.IPInfo.IPAddress + ") : " + StringFromByteArr(receivedPacket.PacketRaw);
     AddMsg(sendString);
     foreach (var socketObj in m_socketList)
     {
         if (socketObj != socket)
         {
             socketObj.Send(receivedPacket);
         }
     }
 }
 /// <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);
         }
     }
   }
 public void OnReceived(INetworkClient client, Packet receivedPacket)
 {
     string sendString = StringFromByteArr(receivedPacket.GetPacket()) + "\r\n";
     AddMsg(sendString);
 }
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="packetType">packet type</param>
 /// <param name="packet">packet</param>
 /// <param name="offset">offset</param>
 /// <param name="size">size of packet in byte</param>
 /// <param name="iocpTcpClient">client</param>
 /// <param name="dataPacket">data packet for send</param>
 public PacketTransporter(PacketType packetType,Packet packet, int offset, int size, IocpTcpClient iocpTcpClient,Packet dataPacket=null)
 {
     m_packetType = packetType;
     m_packet = packet;
     m_offset = offset;
     m_size = size;
     m_iocpTcpClient = iocpTcpClient;
     m_dataPacket = dataPacket;
 }
        /// <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);
                    }
                }
            }
            
            
        }
Example #8
0
        /// <summary>
        /// Broadcast the given packet to all the client, connected
        /// </summary>
        /// <param name="sender">sender of the broadcast</param>
        /// <param name="packet">packet to broadcast</param>
        public void Broadcast(INetworkSocket sender, Packet packet)
        {
            List<INetworkSocket> list = GetSocketList();
            foreach (INetworkSocket socket in list)
            {
                if(socket!=sender)
                    socket.Send(packet);
            }

            Task t = new Task(delegate()
            {
                OnBroadcast(this,sender, packet);
            });
            t.Start();

        }
 /// <summary>
 /// Receive callback
 /// </summary>
 /// <param name="socket">client socket</param>
 /// <param name="receivedPacket">received packet</param>
 public void OnReceived(INetworkSocket socket, Packet receivedPacket)
 {
     ParallelPacket receivedParallelPacket = new ParallelPacket(receivedPacket);
     switch (receivedParallelPacket.PacketType)
     {
         case ParallelPacketType.IDENTITY_RESPONSE:
             PacketSerializer<IdentityResponse> serializer = new PacketSerializer<IdentityResponse>(receivedParallelPacket.PacketRaw,receivedParallelPacket.HeaderSize,receivedParallelPacket.DataByteSize);
             IdentityResponse response = serializer.ClonePacketObj();
             Guid guid = response.m_guid;
             int streamCount = response.m_streamCount;
             lock (m_listLock)
             {
                 if (m_socketMap.ContainsKey(guid))
                 {
                     m_socketMap[guid].AddSocket(socket);
                 }
                 else
                 {
                     if (CallBackObj == null)
                     {
                         socket.Disconnect();
                         return;
                     }
                     
                     if (Acceptor.OnAccept(this, socket.IPInfo, streamCount))
                     {
                         // Create new Parallel Socket
                         IParallelSocketCallback socketCallback = Acceptor.GetSocketCallback();
                         ParallelSocket parallelSocket = new ParallelSocket(guid,socket, this);
                         parallelSocket.CallBackObj = socketCallback;
                         parallelSocket.Start();
                         m_socketMap[guid] = parallelSocket;
                         OnParallelServerAccepted(this, parallelSocket);
                     }
                     else
                     {
                         // Rejected by server
                         socket.Disconnect();
                     }
                 }
             }
             break;
         default:
             // Invalid protocol
             socket.Disconnect();
             break;
     }
 }
 /// <summary>
 /// Broadcast the given packet to all the client, connected
 /// </summary>
 /// <param name="packet">packet to broadcast</param>
 public void BroadcastToRoom(string roomName, Packet packet)
 {
     lock (m_roomLock)
     {
         if (m_roomMap.ContainsKey(roomName))
         {
             m_roomMap[roomName].Broadcast(this, packet);
         }
     }
 }
 /// <summary>
 /// Broadcast given data to all client other than this
 /// </summary>
 /// <param name="packet">packet to broadcast</param>
 public void Broadcast(Packet packet)
 {
     ((IocpTcpServer)Server).Broadcast(this, packet);
 }
 /// <summary>
 /// Receive callback
 /// </summary>
 /// <param name="client">client</param>
 /// <param name="receivedPacket">received packet</param>
 public void OnReceived(INetworkClient client, Packet receivedPacket)
 {
     ParallelPacket receivedParallelPacket = new ParallelPacket(receivedPacket);
     switch (receivedParallelPacket.PacketType)
     {
         case ParallelPacketType.DATA:
             if (ReceiveType == ReceiveType.BURST)
             {
                 OnParallelClientReceived(this, receivedParallelPacket);
             }
             else if (m_receiveType == ReceiveType.SEQUENTIAL)
             {
                 lock (m_receiveLock)
                 {
                     m_receivedQueue.Enqueue(receivedParallelPacket);
                     while (!m_receivedQueue.IsEmpty() && m_curReceivedPacketId + 1 == m_receivedQueue.Peek().PacketID)
                     {
                         ParallelPacket curPacket = m_receivedQueue.Dequeue();
                         m_curReceivedPacketId = curPacket.PacketID;
                         if (m_curReceivedPacketId == long.MaxValue)
                         {
                             m_curReceivedPacketId = -1;
                         }
                         OnParallelClientReceived(this, curPacket);
                     }
                 }
             }
             break;
         case ParallelPacketType.IDENTITY_REQUEST:
             PacketSerializer<IdentityResponse> serializer = new PacketSerializer<IdentityResponse>(new IdentityResponse(Guid,MaxSocketCount));
             ParallelPacket sendPacket=new ParallelPacket(-1,ParallelPacketType.IDENTITY_RESPONSE,serializer.PacketRaw);
             client.Send(sendPacket.PacketRaw);
             break;
         case ParallelPacketType.READY:
             lock (m_sendLock)
             {
                 m_pendingClientSet.Add(client);
                 if (m_pendingClientSet.Count > 0 && (m_errorPacketSet.Count > 0 || m_packetQueue.Count > 0))
                     m_sendReadyEvent.SetEvent();
             }
             break;
     }
 }
        /// <summary>
        /// Broadcast the given packet to the all client, connected
        /// </summary>
        /// <param name="packet">the packet to broadcast</param>
        public void Broadcast(INetworkSocket sender, Packet packet)
        {
            List<IocpTcpSocket> socketList = GetClientSocketList();

            foreach (IocpTcpSocket socket in socketList)
            {
                if(socket!=sender)
                    socket.Send(packet);
            }
        }
        /// <summary>
        /// Receive callback
        /// </summary>
        /// <param name="client">client</param>
        /// <param name="receivedPacket">received packet</param>
        public void OnReceived(INetworkClient client, Packet receivedPacket)
        {
            ParallelPacket receivedParallelPacket = new ParallelPacket(receivedPacket);
            switch (receivedParallelPacket.GetPacketType())
            {
                case ParallelPacketType.DATA:
                    if (m_receiveType == ReceiveType.BURST)
                    {
                        if (CallBackObj != null)
                        {
//                             Task t = new Task(delegate()
//                             {
//                                 CallBackObj.OnReceived(this, receivedParallelPacket);
//                             });
//                             t.Start();
                            CallBackObj.OnReceived(this, receivedParallelPacket);
                        }
                    }
                    else if (m_receiveType == ReceiveType.SEQUENTIAL)
                    {
                        lock (m_receiveLock)
                        {
                            m_receivedQueue.Enqueue(receivedParallelPacket);
                            while (m_curReceivedPacketId == -1 || (!m_receivedQueue.IsEmpty() && m_curReceivedPacketId + 1 == m_receivedQueue.Peek().GetPacketID()))
                            {
                                ParallelPacket curPacket = m_receivedQueue.Dequeue();
                                m_curReceivedPacketId = curPacket.GetPacketID();
                                if (CallBackObj != null)
                                {
//                                     Task t = new Task(delegate()
//                                     {
//                                         CallBackObj.OnReceived(this, curPacket);
//                                     });
//                                     t.Start();
                                    m_callBackObj.OnReceived(this, curPacket);
                                }
                            }
                        }
                    }
                    break;
                case ParallelPacketType.IDENTITY_REQUEST:
                    PacketSerializer<IdentityResponse> serializer = new PacketSerializer<IdentityResponse>(new IdentityResponse(Guid));
                    ParallelPacket sendPacket=new ParallelPacket(getCurPacketSequence(),ParallelPacketType.IDENTITY_RESPONSE,serializer.GetPacketRaw());
                    client.Send(sendPacket.GetPacketRaw());
                    break;
                case ParallelPacketType.READY:
                    lock (m_sendLock)
                    {
                        m_pendingClientSet.Add(client);
                        if (m_pendingClientSet.Count > 0 && (m_errorPacketSet.Count > 0 || m_packetQueue.Count > 0))
                            m_sendReadyEvent.SetEvent();
                    }
                    break;
            }
        }
 /// <summary>
 /// Receive callback
 /// </summary>
 /// <param name="socket">client socket</param>
 /// <param name="receivedPacket">received packet</param>
 public void OnReceived(INetworkSocket socket, Packet receivedPacket)
 {
     ParallelPacket receivedParallelPacket = new ParallelPacket(receivedPacket);
     switch (receivedParallelPacket.PacketType)
     {
         case ParallelPacketType.DATA:
             if (ReceiveType == ReceiveType.BURST)
             {
                 OnParallelSocketReceived(this, receivedParallelPacket);
             }
             else if (ReceiveType == ReceiveType.SEQUENTIAL)
             {
                 lock (m_receiveLock)
                 {
                     m_receivedQueue.Enqueue(receivedParallelPacket);
                     while (!m_receivedQueue.IsEmpty() && m_curReceivedPacketId + 1 == m_receivedQueue.Peek().PacketID)
                     {
                         ParallelPacket curPacket = m_receivedQueue.Dequeue();
                         m_curReceivedPacketId = curPacket.PacketID;
                         if (m_curReceivedPacketId == long.MaxValue)
                         {
                             m_curReceivedPacketId = -1;
                         }
                         OnParallelSocketReceived(this, curPacket);
                     }
                 }
             }
             break;
         default:
             socket.Disconnect(); // Invalid protocol
             break;
     }
 }
        /// <summary>
        /// Send callback
        /// </summary>
        /// <param name="socket">client socket</param>
        /// <param name="status">stend status</param>
        /// <param name="sentPacket">sent packet</param>
        public void OnSent(INetworkSocket socket, SendStatus status, Packet sentPacket)
        {
            ParallelPacket sentParallelPacket = ParallelPacket.FromPacket(sentPacket);
            if (sentParallelPacket.PacketType == ParallelPacketType.DATA)
            {
                lock (m_sendLock)
                {
                    m_pendingPacketSet.Remove(sentParallelPacket);
                    if (status == SendStatus.SUCCESS || status == SendStatus.FAIL_INVALID_PACKET)
                    {
                        m_pendingClientSet.Add(socket);
                    }
                    if (status != SendStatus.SUCCESS)
                    {
                        m_errorPacketSet.Add(sentParallelPacket);
                    }
                    if (m_pendingClientSet.Count > 0 && (m_errorPacketSet.Count > 0 || m_packetQueue.Count > 0))
                        m_sendReadyEvent.SetEvent();
                }
                
                Task t = new Task(delegate()
                {
                    OnParallelSocketSent(this, status, sentParallelPacket);
                });
                t.Start();

            }
        }
Example #17
0
        /// <summary>
        /// Default copy constructor
        /// </summary>
        /// <param name="b">the object to copy from</param>
        public Packet(Packet b)
        {

            lock(b.m_packetLock)
            {
                m_packet=null;
	            if(b.m_isAllocated)
	            {
		            if(b.m_packetSize>0)
		            {
			            m_packet=new byte[b.m_packetSize];
                        Array.Copy(b.m_packet, m_packet, b.m_packetSize);
		            }
		            m_packetSize=b.m_packetSize;
	            }
	            else
	            {
		            m_packet=b.m_packet;
		            m_packetSize=b.m_packetSize;
	            }
	            m_isAllocated=b.m_isAllocated;
            }
	        
        }
Example #18
0
 /// <summary>
 /// Broadcast the given packet to all the client, connected
 /// </summary>
 /// <param name="packet">packet to broadcast</param>
 public void Broadcast(Packet packet)
 {
     Broadcast(null, packet);
 }
Example #19
0
 /// <summary>
 /// Receive callback
 /// </summary>
 /// <param name="socket">client socket</param>
 /// <param name="receivedPacket">received packet</param>
 public void OnReceived(INetworkSocket socket, Packet receivedPacket)
 {
     lock (m_generalLock)
     {
         if (socket == m_socket1)
         {
             m_socket2.Send(receivedPacket);
         }
         else
         {
             m_socket1.Send(receivedPacket);
         }
     }
 }
         /// <summary>
        /// Send given data to the server
        /// </summary>
        /// <param name="data">data in byte array</param>
        /// <param name="offset">offset in bytes</param>
        /// <param name="dataSize">data size in bytes</param>
        public void Send(byte[] data, int offset, int dataSize)
        {
            Packet sendPacket=null;
            sendPacket = new Packet(data,offset, dataSize, false);
//          byte[] packet = new byte[dataSize];
//          MemoryStream stream = new MemoryStream(packet);
//          stream.Write(data, offset, dataSize);
            //          Packet sendPacket = new Packet(packet,0, packet.Count(), false);

            Send(sendPacket);

        }
        /// <summary>
        /// Receive callback
        /// </summary>
        /// <param name="socket">client socket</param>
        /// <param name="receivedPacket">received packet</param>
        public void OnReceived(INetworkSocket socket, Packet receivedPacket)
        {
            ParallelPacket receivedParallelPacket = new ParallelPacket(receivedPacket);
            switch (receivedParallelPacket.GetPacketType())
            {
                case ParallelPacketType.IDENTITY_RESPONSE:
                    PacketSerializer<IdentityResponse> serializer = new PacketSerializer<IdentityResponse>(receivedParallelPacket.GetPacketRaw(),receivedParallelPacket.GetHeaderSize(),receivedParallelPacket.GetDataByteSize());

                    Guid guid = serializer.GetPacket().m_guid;
                    lock (m_listLock)
                    {
                        if (m_socketMap.ContainsKey(guid))
                        {
                            m_socketMap[guid].AddSocket(socket);
                        }
                        else
                        {
                            IParallelSocketCallback socketCallback = CallBackObj.OnAccept(this, socket.IPInfo);
                            if (socketCallback != null)
                            {
                                // Create new Parallel Socket
                                ParallelSocket parallelSocket = new ParallelSocket(guid,socket, this);
                                parallelSocket.CallBackObj = socketCallback;
                                parallelSocket.Start();
                                m_socketMap[guid] = parallelSocket;
                            }
                            else
                            {
                                // Rejected by server
                                socket.Disconnect();
                            }
                        }
                    }
                    break;
                default:
                    // Invalid protocol
                    socket.Disconnect();
                    break;
            }
        }
 /// <summary>
 /// Receive callback function
 /// </summary>
 /// <param name="result">result</param>
 private static void onReceived(IAsyncResult result)
 {
     PacketTransporter transport = result.AsyncState as PacketTransporter;
     Socket socket = transport.m_iocpTcpClient.m_client.Client;
     
     int readSize=0;
     try { readSize = socket.EndReceive(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.BeginReceive(transport.m_packet.PacketRaw, transport.m_offset, transport.m_size, SocketFlags.None, 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.PacketRaw, 0);
             int shouldReceive = Preamble.ToShouldReceive(transport.m_packet.PacketRaw);
           
             // preamble packet is corrupted
             // try to receive another byte to check preamble
             if (shouldReceive < 0)
             {
                 int preambleOffset = Preamble.CheckPreamble(transport.m_packet.PacketRaw);
                 // set offset to length - preamble offset
                 transport.m_offset = transport.m_packet.PacketByteSize - preambleOffset;
                 // need to receive as much as preamble offset
                 transport.m_size = preambleOffset;
                 try
                 {
                     // shift to left by preamble offset
                     Buffer.BlockCopy(transport.m_packet.PacketRaw, preambleOffset, transport.m_packet.PacketRaw, 0, transport.m_packet.PacketByteSize - preambleOffset);
                     // receive rest of bytes at the end
                     socket.BeginReceive(transport.m_packet.PacketRaw, transport.m_offset, transport.m_size, SocketFlags.None, new AsyncCallback(IocpTcpClient.onReceived), transport);
                 }
                 catch (Exception ex)
                 {
                     Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                     transport.m_iocpTcpClient.Disconnect(); return;
                 }
                 return;
             }
             Packet recvPacket = new Packet(null, 0, shouldReceive);
             PacketTransporter dataTransport = new PacketTransporter(PacketType.DATA, recvPacket, 0, shouldReceive, transport.m_iocpTcpClient);
             try { socket.BeginReceive(recvPacket.PacketRaw, 0, shouldReceive, SocketFlags.None, 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, Preamble.SIZE_PACKET_LENGTH, transport.m_iocpTcpClient);
             try { socket.BeginReceive(sizeTransport.m_packet.PacketRaw, 0, Preamble.SIZE_PACKET_LENGTH, SocketFlags.None, new AsyncCallback(IocpTcpClient.onReceived), sizeTransport); }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                 transport.m_iocpTcpClient.Disconnect(); return;
             }
             transport.m_iocpTcpClient.OnReceived(transport.m_iocpTcpClient, transport.m_packet);
         }
     }
   }
 /// <summary>
 /// Send callback
 /// </summary>
 /// <param name="socket">client socket</param>
 /// <param name="status">stend status</param>
 /// <param name="sentPacket">sent packet</param>
 public void OnSent(INetworkSocket socket, SendStatus status, Packet sentPacket)
 {
 }
        /// <summary>
        /// Broadcast given data to all client other than this
        /// </summary>
        /// <param name="packet">packet to broadcast</param>
        public void Broadcast(Packet packet)
        {
            List<IocpTcpSocket> socketList = Server.GetClientSocketList();

            foreach (IocpTcpSocket socket in socketList)
            {
                if(socket!=this)
                    socket.Send(packet);
            }
        }
        /// <summary>
        /// Receive callback
        /// </summary>
        /// <param name="socket">client socket</param>
        /// <param name="receivedPacket">received packet</param>
        public void OnReceived(INetworkSocket socket, Packet receivedPacket)
        {
            ParallelPacket receivedParallelPacket = new ParallelPacket(receivedPacket);
            switch (receivedParallelPacket.GetPacketType())
            {
                case ParallelPacketType.DATA:
                    if (m_receiveType == ReceiveType.BURST)
                    {
                        if (CallBackObj != null)
                        {
//                             Task t = new Task(delegate()
//                             {
//                                 CallBackObj.OnReceived(this, receivedParallelPacket);
//                             });
//                             t.Start();
                            CallBackObj.OnReceived(this, receivedParallelPacket);
                        }
                    }
                    else if (m_receiveType == ReceiveType.SEQUENTIAL)
                    {
                        lock (m_receiveLock)
                        {
                            m_receivedQueue.Enqueue(receivedParallelPacket);
                            while (m_curReceivedPacketId == -1 || (!m_receivedQueue.IsEmpty() && m_curReceivedPacketId + 1 == m_receivedQueue.Peek().GetPacketID()))
                            {
                                ParallelPacket curPacket = m_receivedQueue.Dequeue();
                                m_curReceivedPacketId = curPacket.GetPacketID();
                                if (CallBackObj != null)
                                {
//                                     Task t = new Task(delegate()
//                                     {
//                                         CallBackObj.OnReceived(this, curPacket);
//                                     });
//                                     t.Start();
                                    m_callBackObj.OnReceived(this, curPacket);
                                }
                            }
                        }
                    }
                    break;
                default:
                    socket.Disconnect(); // Invalid protocol
                    break;
            }
        }
        /// <summary>
        /// Broadcast the given packet to the all client, connected
        /// </summary>
        /// <param name="packet">the packet to broadcast</param>
        public void Broadcast(Packet packet)
        {
            List<IocpTcpSocket> socketList = GetClientSocketList();

            foreach (IocpTcpSocket socket in socketList)
            {
                socket.Send(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);
                    }
                }
            }
            
            
        }