/// <summary> /// Initializes a new instance of the <see cref="Packet" /> class. /// </summary> public Packet(EServerType server_type, EPacketType type, byte value, uint length, byte[] data) { BufferChunk.SetNetworking(); Header = new PacketHeader(server_type); Header.type = (byte)type; Header.value = value; Header.packet_number = PacketNumber++; var len = PacketHeader.Length + 1; // +1 checksum Header.datalength = 1; if ((data != null) && (length > 0)) { Header.datalength = length + 1; len += Header.datalength; Data = new BufferChunk((int)len); Data.Add(Header); Data += data; //var str = Encoding.ASCII.GetString(Data.Buffer); } else { Data = new BufferChunk((int)len); Data.Add(Header); } AddChecksumm(); }
/// <summary> /// Initializes a new instance of the <see cref="Packet" /> class. /// </summary> public Packet(EServerType server_type, EPacketType type, byte cmd) { BufferChunk.SetNetworking(); Header = new PacketHeader(server_type); Header.type = (byte)type; Header.value = cmd; Header.packet_number = PacketNumber++; Header.datalength = 1; var len = PacketHeader.Length; len += Header.datalength; Data = new BufferChunk((int)len); Data.Add(Header); AddChecksumm(); }
/// <summary> /// Initializes a new instance of the <see cref="Packet" /> class. /// </summary> public Packet(EServerType server_type, EPacketType type, byte cmd, byte value = 0, byte[] data = null) { BufferChunk.SetNetworking(); Header = new PacketHeader(server_type); Header.type = (byte)type; Header.value = value; Header.packet_number = PacketNumber++; var len = PacketHeader.Length + 1; // +1 checksum Header.datalength = 1; if ((data != null) && (data.Length > 0)) { Header.datalength += (uint)data.Length; len += Header.datalength; Data = new BufferChunk((int)len); Data.Add(Header); Data += data; } else { Data = new BufferChunk((int)len); Data.Add(Header); } AddChecksumm(); #if LOCAL_DEBUG Console.WriteLine(Encoding.ASCII.GetString(Header.sign)); if (data != null) { var str = Encoding.ASCII.GetString(data); Console.WriteLine(str); } #endif }
/// <summary> /// Initializes a new instance of the <see cref="Packet"/> class. /// </summary> public Packet(EServerType server_type, EPacketType type, string data) { BufferChunk.SetNetworking(); Header = new PacketHeader(server_type); Header.type = (byte)type; Header.value = 0; Header.packet_number = PacketNumber++; data += "\0"; // quick&dirty if (!string.IsNullOrEmpty(data)) { Header.datalength = (uint)data.Length + 1; // +1 checksum var len = PacketHeader.Length; len += Header.datalength; Data = new BufferChunk((int)len); Data.Add(Header); Data += data; var str = Encoding.ASCII.GetString(Data.Buffer); } else { Header.datalength = 1; Data = new BufferChunk((int)(PacketHeader.Length + 1)); Data.Add(Header); } AddChecksumm(); #if LOCAL_DEBUG Console.WriteLine(Encoding.ASCII.GetString(Header.sign)); if (!string.IsNullOrEmpty(data)) { var str = data; Console.WriteLine(str); } #endif }
/// <summary> /// Sends the plan to server. /// </summary> /// <param name="spots">The spots.</param> /// <param name="nblocks">The nblocks.</param> /// <returns><c>true</c> if OK, <c>false</c> otherwise.</returns> /// <exception cref="SendPlanException"> /// </exception> public virtual bool Send(List <Spot> spots, uint nblocks = 10) { if (!IsConnected) { if (Globals.Debug) { Console.WriteLine(Resources.Server_is_not_connected); } return(false); } BufferChunk.SetNetworking(); var plan = new BufferChunk(); foreach (var spot in spots) { plan.Add(spot); } var len = Spot.Length * nblocks; if (Globals.Debug) { Console.WriteLine(Resources.Sending_plan_to_server + ": length = " + (plan.Length / 1000.0) + " Kb"); } try { while (plan.Length > len) { var bf = plan.NextBufferChunk((int)len); Send(bf); } try { if (plan.Length >= Spot.Length) { var bf = plan.NextBufferChunk(plan.Length); Send(bf); } } catch { throw new SendPlanException(); } } catch { if (plan.Length >= Spot.Length) // send the last portion of data { try { var bf = plan.NextBufferChunk(plan.Length); Send(bf); } catch { throw new SendPlanException(); } } } if (Globals.Debug) { Console.WriteLine(Resources.Plan_sent_to_server); } SendCommand(EPlanCommand.GETSTATE); return(true); }