public int SendUdp(int node, IPacket packet) { int sendSize = 0; if (sessionUdp == null) { return(sendSize); } PacketHeader header = new PacketHeader(); PacketHeaderSerializer headerSerializer = new PacketHeaderSerializer(); header.type = packet.GetPacketType(); byte[] headerData = null; if (headerSerializer.Serialize(header) == true) { headerData = headerSerializer.GetSerializedData(); } byte[] packetData = packet.GetData(); byte[] data = new byte[headerData.Length + packetData.Length]; int headerSize = Marshal.SizeOf(typeof(PacketHeader)); Buffer.BlockCopy(headerData, 0, data, 0, headerSize); Buffer.BlockCopy(packetData, 0, data, headerSize, packetData.Length); sendSize = sessionUdp.Send(node, data, data.Length); if (sendSize < 0 && nodeStateHandler != null) { NetworkState state = new NetworkState(); state.node = node; state.type = NetEventType.SendError; state.result = NetEventResult.Failure; nodeStateHandler.Invoke(ConnectionType.UDP, state); } return(sendSize); }
public int SendUnreliable <T>(int node, IPacket <T> packet) { int sendSize = 0; if (m_sessionUdp != null) { // 모듈에서 사용하는 헤더 정보를 생성. PacketHeader header = new PacketHeader(); HeaderSerializer serializer = new HeaderSerializer(); header.packetId = packet.GetPacketId(); byte[] headerData = null; if (serializer.Serialize(header) == true) { headerData = serializer.GetSerializedData(); } byte[] packetData = packet.GetData(); byte[] data = new byte[headerData.Length + packetData.Length]; int headerSize = Marshal.SizeOf(typeof(PacketHeader)); Buffer.BlockCopy(headerData, 0, data, 0, headerSize); Buffer.BlockCopy(packetData, 0, data, headerSize, packetData.Length); sendSize = m_sessionUdp.Send(node, data, data.Length); if (sendSize < 0 && m_eventQueue != null) { // 송신 오류. NetEventState state = new NetEventState(); state.node = node; state.type = NetEventType.SendError; state.result = NetEventResult.Failure; m_eventQueue.Add(state); } } return(sendSize); }