public void Send(IPacket packet) { try { byte[] data = packet.GetData(); HeadStream headStream = new HeadStream(); headStream.Write(BitConverter.GetBytes(data.Length)); headStream.Close(); //Console.WriteLine("准备发送的包长度:" + data.Length); List <byte> sendData = new List <byte>(); sendData.AddRange(PREAMBLE); sendData.AddRange(data); if (IsListen && !MultiSocket) { clientSocket.Send(headStream.GetBuffer()); clientSocket.Send(sendData.ToArray()); } else { socket.Send(headStream.GetBuffer()); socket.Send(sendData.ToArray()); } } catch (Exception) { Disconnect(); } }
// 데이터를 서버로 전송(TCP) public int SendReliable <T>(IPacket <T> packet) { int sendSize = 0; if (transportTCP != null) { // 모듈에서 사용할 헤더 정보를 생성합니다. PacketHeader header = new PacketHeader(); HeaderSerializer serializer = new HeaderSerializer(); //packetid = skill, moving 등등 moving은 2 header.packetId = (int)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); string str = "Send reliable packet[" + header.packetId + "]"; sendSize = transportTCP.Send(data, data.Length); } return(sendSize); }
// 데이터를 서버로 전송(UDP) public int SendUnreliable <T>(IPacket <T> packet) { int sendSize = 0; if (transportUDP != null) { // 헤더 정보 생성 PacketHeader header = new PacketHeader(); HeaderSerializer serializer = new HeaderSerializer(); // FIX THIS : 명시적 형변환 해줌. 소스코드와 다름 header.packetId = (int)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 = transportUDP.Send(data, data.Length); } return(sendSize); }
public int Send <T>(int node, PacketId id, IPacket <T> packet) { int sendSize = 0; if (m_sessionTcp != null) { // 모듈에서 사용하는 헤더 정보 생성. PacketHeader header = new PacketHeader(); HeaderSerializer serializer = new HeaderSerializer(); header.packetId = id; 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); //string str = "Send Packet[" + id + "]"; sendSize = m_sessionTcp.Send(node, data, data.Length); } return(sendSize); }
public int SendUnreliable <T>(IPacket <T> packet) { int sendSize = 0; if (m_udp != 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_udp.Send(data, data.Length); } return(sendSize); }
public void SendReliableToAll <T>(IPacket <T> packet) { byte[] data = packet.GetData(); int sendSize = m_tcp.Send(data, data.Length); if (sendSize < 0) { // 송신 오류. } }
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 void GenerateBuffer(ref Byte[] dest, IPacket packet) { Byte[] data = packet.GetData(); int iLength = GetLength(data.Length); // 数据自动加 8 分别存储 消息长度和消息类型 dest = new Byte[iLength]; // 长度 Byte[] bLength = BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder(iLength - 4)); Array.Copy(bLength, 0, dest, 0, 4); // 类型 int iType = packet.GetPacketType(); Byte[] bType = BitConverter.GetBytes(iType); Array.Copy(bType, 0, dest, 4, 4); // 数据 Array.Copy(data, 0, dest, 8, data.Length); }
public int SendReliable <T>(int node, IPacket <T> packet) { int sendSize = 0; if (m_sessionTcp != 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); //string str = "Send reliable packet[" + header.packetId + "]"; sendSize = m_sessionTcp.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); }
// 组装这个包 public void GenerateBuffer(ref Byte[] dest, IPacket packet) { Byte[] data = packet.GetData(); int iLength = GetLength(data.Length); dest = new Byte[iLength]; // 包头 Array.Copy(PACKET_HEAD, 0, dest, 0, 2); // 长度 Byte[] bLength = BitConverter.GetBytes(iLength); Array.Copy(bLength, 0, dest, 2, 4); // 类型 int iType = packet.GetPacketType(); Byte[] bType = BitConverter.GetBytes(iType); Array.Copy(bType, 0, dest, 6, 4); // 数据 Array.Copy(data, 0, dest, 10, data.Length); }