public BufferPacketData ReadData() { lock(recv_lock) { if (RecvPacketList.Count > 0) { BufferPacketData return_data = RecvPacketList[0]; RecvPacketList.RemoveAt(0); return return_data; } } return null; }
//원하는 패킷을 읽고싶을때 사용할 것, 기본적으로 모든 패킷은 이곳에 저장된다. public BufferPacketData ReadAndRemoveData(int _event) { for (int i = 0; RecvPacketList.Count > i; i++) { if (RecvPacketList[i].PacketID == _event) { BufferPacketData return_data = RecvPacketList[i]; RecvPacketList.RemoveAt(i); return return_data; } } return null; }
// 인증 패킷 구조체를 바이트 배열로 변환하는 함수 public static byte[] GetBytes_Bind(BufferPacketData packet) { byte[] btBuffer = new byte[packet.DataSize]; System.IO.MemoryStream ms = new System.IO.MemoryStream(btBuffer, true); System.IO.BinaryWriter bw = new System.IO.BinaryWriter(ms); bw.Write(packet.PacketID); bw.Write(packet.DataSize); bw.Write(packet.Index); bw.Write(packet.Sequence); bw.Write(packet.BufferData, 0, packet.BufferData.Length); bw.Close(); ms.Close(); return btBuffer; }
void SetDisconnectd() { lock (recv_lock) { BufferPacketData packet = new BufferPacketData(); packet.PacketID = PACKET_ID_DISCONNECTED; packet.DataSize = (uint)header_size; packet.Index = 0; packet.Sequence = 0; RecvPacketList.Add(packet); // 연결 끊김 이벤트를 패킷으로 전달. } lock (send_lock) { SendPacketQueue.Clear(); } }
private static BufferPacketData GetBindAck(ArraySegment<byte> array_segment) { BufferPacketData packet = new BufferPacketData(); System.IO.MemoryStream ms = new System.IO.MemoryStream(array_segment.Array, array_segment.Offset, array_segment.Count, false); System.IO.BinaryReader br = new System.IO.BinaryReader(ms); packet.PacketID = br.ReadUInt32(); packet.DataSize = br.ReadUInt32(); packet.Index = br.ReadUInt32(); packet.Sequence = br.ReadUInt16(); var buffer_size = (int)packet.DataSize - header_size; packet.BufferData = new byte[buffer_size]; packet.BufferData = br.ReadBytes(buffer_size); br.Close(); ms.Close(); return packet; }
// 패킷 보내기 public bool Send(int _event, byte[] bodyData, uint index = 0) { if(Network.IsConnected() == false) { return false; } BufferPacketData header = new BufferPacketData(); header.PacketID = (UInt32)_event; header.DataSize = (uint)(bodyData.Length + header_size); header.Index = index; header.Sequence = ++sequence; header.BufferData = new byte[bodyData.Length]; Buffer.BlockCopy(bodyData, 0, header.BufferData, 0, bodyData.Length); lock (send_lock) { SendPacketQueue.Enqueue(GetBytes_Bind(header)); } return true; }