/// <summary> /// TCP 소켓의 BeginReceive에 등록된 콜백 메소드 /// </summary> /// <param name="ar">BeginReceive에 설정한 인스턴스가 담긴 파라미터</param> private void ReceiveCb(IAsyncResult ar) { try { var sock = (Socket)ar.AsyncState; var dataSize = sock.EndReceive(ar); if (dataSize > 0) { Console.WriteLine("\n[TCP] Recv [" + dataSize + "] : bytes."); var dataAry = new byte[dataSize]; Array.Copy(_recvBuffer, 0, dataAry, 0, dataSize); PacketMaker.GetPacket(dataAry, ref _packetQueue); Array.Clear(_recvBuffer, 0, _recvBuffer.Length); while (_packetQueue.Count > 0) { var packet = _packetQueue.Dequeue(); Console.WriteLine("Received Packet : " + packet.GetType()); ProcessPacket(packet); } sock.BeginReceive(_recvBuffer, 0, _recvBuffer.Length, SocketFlags.None, ReceiveCb, sock); } else { Console.WriteLine("[TCP] Receive no data."); } } catch (Exception e) { Console.WriteLine(e); } }
private void ReceiveCb(IAsyncResult ar) { try { //Console.WriteLine("d"); var uc = (UdpClient)ar.AsyncState; var remoteEp = new IPEndPoint(IPAddress.Any, 0); var dataAry = uc.EndReceive(ar, ref remoteEp); if (dataAry.Length > 0) { //Console.WriteLine("[UDP] Received data size : " + dataAry.Length); PacketMaker.GetPacket(dataAry, ref _packetQueue); } while (_packetQueue.Count > 0) { var packet = _packetQueue.Dequeue(); //Console.WriteLine("Received Packet : " + packet.GetType()); ProcessHandler(packet); } uc.BeginReceive(ReceiveCb, uc); } catch (Exception e) { Console.WriteLine(e); } }
private void ReceiveCb(IAsyncResult ar) { try { var sock = (Socket)ar.AsyncState; int recvSize = sock.EndReceive(ar); if (recvSize > 0) { //Debug.Log("[TCP] Received data size : " + recvSize); var dataAry = new byte[recvSize]; Array.Copy(_recvBuffer, 0, dataAry, 0, recvSize); PacketMaker.GetPacket(dataAry, ref packetQueue); Array.Clear(_recvBuffer, 0, _recvBuffer.Length); //Debug.Log("[TCP] Process was done."); } else { Debug.Log("[TCP] Receive no data."); } sock.BeginReceive(_recvBuffer, 0, _recvBuffer.Length, SocketFlags.None, ReceiveCb, sock); } catch (Exception e) { Debug.Log(e); } }
private void ReceiveCb(IAsyncResult ar) { try { Debug.Log("받는지 확인"); var uc = (UdpClient)ar.AsyncState; var remoteEp = new IPEndPoint(IPAddress.Any, 0); var dataAry = uc.EndReceive(ar, ref remoteEp); if (dataAry.Length > 0) { //Debug.Log("[UDP] Received data size : " + dataAry.Length); PacketMaker.GetPacket(dataAry, ref packetQueue); } uc.BeginReceive(ReceiveCb, uc); } catch (Exception e) { Debug.Log(e); } }