private byte[] getBytes(RunnerNetworkPacket str) { int size = Marshal.SizeOf(str); byte[] arr = new byte[size]; IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(str, ptr, true); Marshal.Copy(ptr, arr, 0, size); Marshal.FreeHGlobal(ptr); return(arr); }
public void BeginBatch() { IsBatching = true; BatchStream = new MemoryStream(); BatchWriter = new BinaryWriter(BatchStream); var pk = new RunnerNetworkPacket(); BatchWriter.Write(RunnerNetworkPacket.PK_HEADER); BatchWriter.Write(Marshal.SizeOf(pk)); BatchSizeOff = BatchStream.Position; BatchWriter.Write(0); BatchWriter.Write((int)RunnerCommand.BatchCommand); BatchCmdOff = BatchStream.Position; BatchCmdCount = 0; BatchWriter.Write(BatchCmdCount); }
public bool SendCommand(params int[] Command) { if (State != RunnerSocketState.Connected) { return(false); } if (Command.Length == 0) { return(false); } RunnerNetworkPacket pk = new RunnerNetworkPacket(); pk.Header = RunnerNetworkPacket.PK_HEADER; pk.Size = Marshal.SizeOf(pk); pk.PacketSize = pk.Size; pk.Command = (RunnerCommand)Command[0]; if (Command.Length > 1) { pk.Arg1 = (uint)Command[1]; } if (Command.Length > 2) { pk.Arg2 = (uint)Command[2]; } if (Command.Length > 3) { pk.Arg3 = (uint)Command[3]; } if (Command.Length > 4) { pk.Arg4 = (uint)Command[4]; } if (Command.Length > 5) { pk.Arg5 = (uint)Command[5]; } if (Command.Length > 6) { pk.Arg6 = (uint)Command[6]; } if (Command.Length > 7) { pk.Arg7 = (uint)Command[7]; } byte[] data = getBytes(pk); int tries = 4; while (tries > 0) { try { Sender.Send(data, 0, pk.Size, SocketFlags.None); tries = 0; } catch { tries--; } } if (tries < 0) { return(false); } return(true); }